Skip to content

Commit

Permalink
Add identifierString tests to MoreTypesTest
Browse files Browse the repository at this point in the history
  • Loading branch information
LZRS committed Aug 31, 2023
1 parent 9a043a9 commit c4d7e8f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,19 @@ fun Type.displayString(context: Context): String =
fun Type.getValueAsString(context: Context): String =
getValueString(this) ?: context.getString(R.string.not_answered)

/*
/**
* Returns the unique identifier of a [Type]. Used to differentiate between item answer options that
* may have similar display strings
*/
fun Type.identifierString(context: Context): String =
id
?: (this as? Coding)?.let {
arrayOf("${it.system.orEmpty()}${it.version.orEmpty()}", it.code.orEmpty())
.joinToString(if (it.hasSystem() && it.hasCode()) "|" else "")
?: when (this) {
is Coding ->
arrayOf("${this.system.orEmpty()}${this.version.orEmpty()}", this.code.orEmpty())
.joinToString(if (this.hasSystem() && this.hasCode()) "|" else "")
is Reference -> this.reference ?: displayString(context)
else -> displayString(context)
}
?: (this as? Reference)?.reference ?: displayString(context)

private fun getDisplayString(type: Type, context: Context): String? =
when (type) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022 Google LLC
* Copyright 2022-2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,7 +16,9 @@

package com.google.android.fhir.datacapture.extensions

import android.app.Application
import android.os.Build
import androidx.test.core.app.ApplicationProvider
import ca.uhn.fhir.model.api.TemporalPrecisionEnum
import com.google.common.truth.Truth.assertThat
import java.time.Instant
Expand All @@ -41,6 +43,7 @@ import org.hl7.fhir.r4.model.MarkdownType
import org.hl7.fhir.r4.model.OidType
import org.hl7.fhir.r4.model.PositiveIntType
import org.hl7.fhir.r4.model.Quantity
import org.hl7.fhir.r4.model.Reference
import org.hl7.fhir.r4.model.StringType
import org.hl7.fhir.r4.model.TimeType
import org.hl7.fhir.r4.model.Type
Expand All @@ -57,6 +60,8 @@ import org.robolectric.annotation.Config
@Config(sdk = [Build.VERSION_CODES.P])
class MoreTypesTest {

private val context = ApplicationProvider.getApplicationContext<Application>()

@Test
fun instant_shouldReturnExpectedStringValue() {
val value =
Expand Down Expand Up @@ -199,6 +204,24 @@ class MoreTypesTest {
assertThat(code.equalsDeep(CodeType("fakeCode"))).isTrue()
}

@Test
fun `Coding should return identifierString containing system, version and code`() {
val coding = Coding("fakeSystem", "fakeCode", "fakeDisplay").apply { version = "2.0" }
assertThat(coding.identifierString(context)).isEqualTo("fakeSystem2.0|fakeCode")
}

@Test
fun `Coding without system should return identifierString containing code`() {
val coding = Coding().apply { code = "fakeCode" }
assertThat(coding.identifierString(context)).isEqualTo("fakeCode")
}

@Test
fun `Reference should return reference`() {
val reference = Reference().apply { reference = "fakeReference" }
assertThat(reference.identifierString(context)).isEqualTo("fakeReference")
}

@Test
fun `should return calculated value for cqf expression`() {
val today = LocalDate.now().toString()
Expand Down

0 comments on commit c4d7e8f

Please sign in to comment.