-
Notifications
You must be signed in to change notification settings - Fork 637
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Compare JSON literals in Strict mode #2464
Conversation
7a57b5b
to
6d6da0b
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is awesome work!
I think if you activate strict mode, then "100.0" does not equal "1.0E2". |
...kotest-assertions-json/src/jvmTest/kotlin/com/sksamuel/kotest/tests/json/JsonLiteralsTest.kt
Outdated
Show resolved
Hide resolved
Let me know when this is ready again for a review. |
I think we should draw a line between literals ( When working with literals, IMO we should treat every number that is same number but different format (not type) as the same. But it's only my opinion 😄 I can revert to exact literal comparison, e.g. value must be identical ( I added test showing that we don't do type-conversion for quoted numbers in strict mode. |
As discussed on chat, I would update compare mode to use this: enum class CompareMode {
/**
* Types and formats must be identical.
*
* For example, "true" and true will not match because one is a string, and the other is a boolean.
* Similarly, 2.99E9 and 299000000 would not match because despite both being doubles, and the same value,
* they are not using the same representation.
*/
Exact,
/**
* Types must be identical and compare by value.
*
* For example, "true" and true will not match because one is a string, and the other is a boolean.
* But 2.99E9 and 299000000 are considered equal as they are both doubles, and both represent the same number.
* "100" and 100 would not match as they are different types.
*/
Strict,
/**
* Compare by value, coercing if possible.
*
* For example, "true" and true will match because the string value can be coerced into a valid boolean.
* Similarly, "100" and 100 will match as the former can be coerced into an int.
*/
Lenient,
} |
4e4cf49
to
dd6d9b0
Compare
213f521
to
fb4bd44
Compare
...tions/kotest-assertions-json/src/jsTest/kotlin/io.kotest.assertions.json/JsonLiteralsTest.kt
Show resolved
Hide resolved
Please do.
…On Sat, 11 Sep 2021, 06:09 Emil Kantis, ***@***.***> wrote:
***@***.**** commented on this pull request.
------------------------------
In
kotest-assertions/kotest-assertions-json/src/jsTest/kotlin/io.kotest.assertions.json/JsonLiteralsTest.kt
<#2464 (comment)>:
> + The top level expected 1e1 but was 10.0
+
+ expected:
+ 10.0
+
+ actual:
+ 10.0
This looks/feels a bit odd.
What happens is the json document is parsed to JsonElement and then
encoded back to string again. (JsonMatchers.kt:73-77). I think it would
be worthwhile to roll our own prettification of JSON-document that prevents
parsing of the values.. Shall I file an issue for it?
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#2464 (review)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAFVSGV7RLEWQ5Q6YXJW5TTUBM2HTANCNFSM5DRBOEDQ>
.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>.
|
Let me know when you want a re-review. |
01ab977
to
fb4bd44
Compare
5d1b587
to
b896059
Compare
@sksamuel I think this is ready for final review. I haven't come up with any better names for |
Nice job :) |
Aim is to fix #2458
It got a bit more complicated than simply checking exact equal string representations in Strict mode. Given that
1.0E2
is the same number as100.0
,100
or+1E+2
I think they should be considered equal. What do you think?The JSON standard seems to not make any distinction between integer and floating point numbers.
TODO:
Changelog?Side-note: Do you have a good way of writing tests that targets both JVM and JS platforms? I didn't like copying the
JsonLiteralTest
class to both targets. I tried adding a test factory tocommonTest
and including that from both platforms, but then it was hard to run a specific test within the spec.