Skip to content

Commit

Permalink
cqframework#648: added tests for validating type inference through co…
Browse files Browse the repository at this point in the history
…nditional (cqframework#1252)

* chore/added tests for validating type inference through conditional

* chore: removed unused imports from SemanticTests

* chore: removed added whitespace

* chore: updated the comparison using lists

* chore: changed list type to set for choice types return comparison

---------

Co-authored-by: JP <jonathan.i.percival@gmail.com>
  • Loading branch information
iasmile and JPercival authored Oct 20, 2023
1 parent cc89145 commit fb97381
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 4 deletions.
5 changes: 3 additions & 2 deletions Src/java-quickstart/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#Wed Oct 18 04:59:14 NDT 2023
distributionBase=GRADLE_USER_HOME
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.2-bin.zip
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
5 changes: 3 additions & 2 deletions Src/java/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"java.configuration.updateBuildConfiguration": "automatic",
"java.compile.nullAnalysis.mode": "automatic",
"java.jdt.ls.vmargs": "-noverify -Xmx4G -XX:+UseG1GC -XX:+UseStringDeduplication",
"java.jdt.ls.vmargs": "-noverify -Xmx8G -XX:+UseG1GC -XX:+UseStringDeduplication",
"cSpell.words": [
"bools",
"datumedge",
Expand All @@ -11,5 +11,6 @@
"qicore",
"testng",
"trackback"
]
],
"java.debug.settings.onBuildFailureProceed": true
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import org.testng.annotations.Test;

import java.io.IOException;
import java.util.Set;
import java.util.HashSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -652,6 +654,64 @@ public void testNonExistentFileName() {
assertThrows(IOException.class, () -> TestUtils.runSemanticTest("ThisFileDoesNotExist.cql", 0));
}

@Test
public void testCaseConditionalReturnTypes() throws IOException {
CqlTranslator translator = runSemanticTest("Issue648.cql", 0);
org.hl7.elm.r1.Library library = translator.toELM();
Map<String, ExpressionDef> defs = new HashMap<>();

if (library.getStatements() != null) {
for (ExpressionDef def : library.getStatements().getDef()) {
defs.put(def.getName(), def);
}
}

ExpressionDef caseDef = defs.get("Cases");

assertThat(caseDef.getResultType(), instanceOf(ChoiceType.class));

ChoiceType choiceType = (ChoiceType)caseDef.getResultType();

Set<String> expectedChoiceTypes = new HashSet<>();
expectedChoiceTypes.add("System.String");
expectedChoiceTypes.add("System.Boolean");
expectedChoiceTypes.add("System.Integer");

Set<String> actualChoiceTypes = new HashSet<>();
for (DataType dt : choiceType.getTypes()) {
actualChoiceTypes.add(((NamedType)dt).getName());
}
assertTrue("Expected types are String, Boolean, and Integer: ", actualChoiceTypes.equals(expectedChoiceTypes));
}

@Test
public void testIfConditionalReturnTypes() throws IOException {
CqlTranslator translator = runSemanticTest("Issue648.cql", 0);
org.hl7.elm.r1.Library library = translator.toELM();
Map<String, ExpressionDef> defs = new HashMap<>();

if (library.getStatements() != null) {
for (ExpressionDef def : library.getStatements().getDef()) {
defs.put(def.getName(), def);
}
}

ExpressionDef ifDef = defs.get("If");
assertThat(ifDef.getResultType(), instanceOf(ChoiceType.class));

ChoiceType choiceType = (ChoiceType)ifDef.getResultType();

Set<String> expectedChoiceTypes = new HashSet<>();
expectedChoiceTypes.add("System.String");
expectedChoiceTypes.add("System.Boolean");

Set<String> actualChoiceTypes = new HashSet<>();
for (DataType dt : choiceType.getTypes()) {
actualChoiceTypes.add(((NamedType)dt).getName());
}
assertTrue("Expected return types are String and Boolean: ", actualChoiceTypes.equals(expectedChoiceTypes));
}

private CqlTranslator runSemanticTest(String testFileName) throws IOException {
return runSemanticTest(testFileName, 0);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
library Issue648

/*
https://github.com/cqframework/clinical_quality_language/issues/648
Type inference through conditionals (case-when, if-else, etc)
*/

define "Cases":
case
when true then 'true'
when false then false
when true then 5
else null
end

define "If":
if true then 'true'
else if false then false
else null

0 comments on commit fb97381

Please sign in to comment.