Skip to content

Commit

Permalink
feat:assert approximatively equal, à la JS number definition
Browse files Browse the repository at this point in the history
  • Loading branch information
scarisey committed Nov 4, 2024
1 parent e9368f2 commit ae57959
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ class FeaturesStepDefinitions
)
}

And("^assert\\s+(\\S+)\\s+(\\$\\S*)\\s*==\\s+(\\d+\\.\\d+)\\s+\\+\\-\\s+(\\d+\\.\\d+)\\s*$") {
And("""^assert\s+(\S+)\s+(\$\S*)\s*==\s+([+-]?(?=\.\d|\d)(?:\d+)?(?:\.?\d*)(?:[eE][+-]?\d+)?)\s+\+\-\s+([+-]?(?=\.\d|\d)(?:\d+)?(?:\.?\d*)(?:[eE][+-]?\d+)?)\s*$""") {
(alias: String, jsonExpression: String, expectedJsonNumber: String,approximationJsonNumber:String) =>
val interpolatedExpectedJson =
backgroundContext.substituteVariablesIn(expectedJsonNumber)
Expand Down
4 changes: 3 additions & 1 deletion src/test/resources/features/assertions.feature
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ Feature: assertions
And assert value2.2 $ == [3,4]
And assert value2.3 $ == "value2.3"
And assert value2.4 $.foo == 12.0038209653823934567890123456789
And assert value2.4 $.foo == 12.0038 +- 0.1
And assert value2.4 $.foo == 12.0 +- 0.1
And assert value2.4 $.foo == 12.1 +- 0.1
And assert value2.4 $.foo == 12.0038 +- 1E4
And assert aliasHeaders2.1 $ == {"foo":"bar","baz":"42"}
And assert aliasHeaders2.1 $.foo == "bar"
5 changes: 4 additions & 1 deletion src/test/scala/com/lectra/kapoeira/glue/AssertsTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ class AssertsTest extends AnyFlatSpec with Matchers with MockFactory {
val expectedConsumedRecords = List(keyValueRecord)
assertionContext.launchConsumption(expectedConsumedRecords)

Asserts.approxEqual(assertionContext, valueAlias, "$.foo", "12.0038","0.1" )
Asserts.approxEqual(assertionContext, valueAlias, "$.foo", "12.0038","0.0001" )
Asserts.approxEqual(assertionContext, valueAlias, "$.foo", "12.0038","1E4" )
Asserts.approxEqual(assertionContext, valueAlias, "$.foo", "12.0","0.1" )
Asserts.approxEqual(assertionContext, valueAlias, "$.foo", "12.1","0.1" )

}

Expand Down

0 comments on commit ae57959

Please sign in to comment.