Skip to content

Commit

Permalink
Add test matcher support for FHIRPath expressions that evaluate to in…
Browse files Browse the repository at this point in the history
…teger.

This will provide better error messages and also prevent the test from crashing when the result is not an integer.

PiperOrigin-RevId: 309808639
  • Loading branch information
aaronnash authored and nickgeorge committed May 7, 2020
1 parent 50477e4 commit 650e571
Showing 1 changed file with 23 additions and 10 deletions.
33 changes: 23 additions & 10 deletions cc/google/fhir/fhir_path/fhir_path_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,23 @@ MATCHER(EvalsToTrue, "") {
return result.ValueOrDie();
}

// Matcher for StatusOr<EvaluationResult> that checks to see that the evaluation
// succeeded and evaluated to an integer equal to the provided value.
MATCHER_P(EvalsToInteger, expected, "") {
if (!arg.ok()) {
return false;
}

StatusOr<int> result = arg.ValueOrDie().GetInteger();
if (!result.ok()) {
*result_listener << "did not resolve to a integer: "
<< result.status().message();
return false;
}

return result.ValueOrDie() == expected;
}

MATCHER_P(EvalsToStringThatMatches, string_matcher, "") {
if (!arg.ok()) {
*result_listener << "evaluation error: " << arg.status().message();
Expand Down Expand Up @@ -749,8 +766,7 @@ FHIR_VERSION_TEST(FhirPathTest, TestFunctionAsPrimitives, {
EXPECT_THAT(Evaluate("true.as(Decimal)"), EvalsToEmpty());
EXPECT_THAT(Evaluate("true.as(Integer)"), EvalsToEmpty());

EXPECT_EQ(Evaluate("1.as(Integer)").ValueOrDie().GetInteger().ValueOrDie(),
1);
EXPECT_THAT(Evaluate("1.as(Integer)"), EvalsToInteger(1));
EXPECT_THAT(Evaluate("1.as(Decimal)"), EvalsToEmpty());
EXPECT_THAT(Evaluate("1.as(Boolean)"), EvalsToEmpty());

Expand Down Expand Up @@ -780,7 +796,7 @@ FHIR_VERSION_TEST(FhirPathTest, TestOperatorAsPrimitives, {
EXPECT_THAT(Evaluate("true as Decimal"), EvalsToEmpty());
EXPECT_THAT(Evaluate("true as Integer"), EvalsToEmpty());

EXPECT_EQ(Evaluate("1 as Integer").ValueOrDie().GetInteger().ValueOrDie(), 1);
EXPECT_THAT(Evaluate("1 as Integer"), EvalsToInteger(1));
EXPECT_THAT(Evaluate("1 as Decimal"), EvalsToEmpty());
EXPECT_THAT(Evaluate("1 as Boolean"), EvalsToEmpty());

Expand Down Expand Up @@ -1169,17 +1185,14 @@ FHIR_VERSION_TEST(FhirPathTest, TestSelectValidatesArguments, {

FHIR_VERSION_TEST(FhirPathTest, TestIif, {
// 2 parameter invocations
EXPECT_EQ(Evaluate("iif(true, 1)").ValueOrDie().GetInteger().ValueOrDie(), 1);
EXPECT_THAT(Evaluate("iif(true, 1)"), EvalsToInteger(1));
EXPECT_THAT(Evaluate("iif(false, 1)"), EvalsToEmpty());
EXPECT_THAT(Evaluate("iif({}, 1)"), EvalsToEmpty());

// 3 parameters invocations
EXPECT_EQ(Evaluate("iif(true, 1, 2)").ValueOrDie().GetInteger().ValueOrDie(),
1);
EXPECT_EQ(Evaluate("iif(false, 1, 2)").ValueOrDie().GetInteger().ValueOrDie(),
2);
EXPECT_EQ(Evaluate("iif({}, 1, 2)").ValueOrDie().GetInteger().ValueOrDie(),
2);
EXPECT_THAT(Evaluate("iif(true, 1, 2)"), EvalsToInteger(1));
EXPECT_THAT(Evaluate("iif(false, 1, 2)"), EvalsToInteger(2));
EXPECT_THAT(Evaluate("iif({}, 1, 2)"), EvalsToInteger(2));

EXPECT_THAT(Evaluate("{}.iif(true, false)"), EvalsToEmpty());
EXPECT_THAT(Evaluate("(1 | 2).iif(true, false)"),
Expand Down

0 comments on commit 650e571

Please sign in to comment.