Skip to content

Commit

Permalink
Add JSON language hints for IntelliJ String syntax rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
leomillon committed Oct 28, 2019
1 parent 0ee11d4 commit 9b8b466
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,28 @@ class JsonCompareAssertTest {

@Test
void should_match_with_default_comparator() {
assertThatJson("{\"field_name\": \"hello world!\"}")
.isValidAgainst("{\"field_name\": \"{#contains:llo wor#}\"}");
assertThatJson(
// language=json
"{\"field_name\": \"hello world!\"}"
)
.isValidAgainst(
// language=json
"{\"field_name\": \"{#contains:llo wor#}\"}"
);
}

@Test
void should_not_match_with_default_comparator() {

assertThatThrownBy(() ->
assertThatJson("{\"field_name\": \"hello_world!\"}")
.isValidAgainst("{\"field_name\": \"{#contains:llo wor#}\"}")
assertThatJson(
// language=json
"{\"field_name\": \"hello_world!\"}"
)
.isValidAgainst(
// language=json
"{\"field_name\": \"{#contains:llo wor#}\"}"
)
)
.isInstanceOf(AssertionError.class)
.hasMessage(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,9 @@ class JsonComparatorTest {

assertThat {
compare(
// language=json
"""{"field_name": "3 Feb 2011"}""",
// language=json
"""{"field_name": "{#date_time_format:d MMM uuu;some_TAG#}"}"""
)
}.isFailure().all {
Expand All @@ -161,7 +163,9 @@ class JsonComparatorTest {

assertThat {
compare(
// language=json
"""{"field_name": "2011-12-03T10:15:30Z"}""",
// language=json
"""{"field_name": "{#date_time_format:some_unknown_pattern#}"}"""
)
}.isFailure().all {
Expand All @@ -175,7 +179,9 @@ class JsonComparatorTest {

tableOf("actual", "expected", "error")
.row(
// language=json
"""{"field_name": "hello_world!"}""",
// language=json
"""{"field_name": "{#contains:llo wor#}"}""",
"""
field_name: Value should contain 'llo wor'
Expand All @@ -185,7 +191,9 @@ class JsonComparatorTest {
""".trimIndent()
)
.row(
// language=json
"""{"field_name": "hello_world!"}""",
// language=json
"""{"field_name": "{#starts_with:llo_wor#}"}""",
"""
field_name: Value should start with 'llo_wor'
Expand All @@ -195,7 +203,9 @@ class JsonComparatorTest {
""".trimIndent()
)
.row(
// language=json
"""{"field_name": "hello_world!"}""",
// language=json
"""{"field_name": "{#ends_with:llo_wor#}"}""",
"""
field_name: Value should end with 'llo_wor'
Expand All @@ -205,7 +215,9 @@ class JsonComparatorTest {
""".trimIndent()
)
.row(
// language=json
"""{"field_name": "hello_world!"}""",
// language=json
"""{"field_name": "{#regex:.*llo ?w.r.*#}"}""",
"""
field_name: Value does not match pattern /.*llo ?w.r.*/
Expand All @@ -215,7 +227,9 @@ class JsonComparatorTest {
""".trimIndent()
)
.row(
// language=json
"""{"field_name": "some value"}""",
// language=json
"""{"field_name": "{#uuid#}"}""",
"""
field_name: Value is not a valid UUID
Expand All @@ -225,7 +239,9 @@ class JsonComparatorTest {
""".trimIndent()
)
.row(
// language=json
"""{"field_name": null}""",
// language=json
"""{"field_name": "{#not_null#}"}""",
"""
field_name: Value should not be null
Expand All @@ -235,7 +251,9 @@ class JsonComparatorTest {
""".trimIndent()
)
.row(
// language=json
"""{"field_name": ""}""",
// language=json
"""{"field_name": "{#not_empty#}"}""",
"""
field_name: Value should not be empty
Expand All @@ -245,7 +263,9 @@ class JsonComparatorTest {
""".trimIndent()
)
.row(
// language=json
"""{"field_name": "some value"}""",
// language=json
"""{"field_name": "{#url#}"}""",
"""
field_name: Value is not a valid URL
Expand All @@ -255,7 +275,9 @@ class JsonComparatorTest {
""".trimIndent()
)
.row(
// language=json
"""{"field_name": "some value/?param"}""",
// language=json
"""{"field_name": "{#url_ending:?param#}"}""",
"""
field_name: Value is not a valid URL
Expand All @@ -265,7 +287,9 @@ class JsonComparatorTest {
""".trimIndent()
)
.row(
// language=json
"""{"field_name": "http://some.url:9999/path?param"}""",
// language=json
"""{"field_name": "{#url_ending:/path?param2#}"}""",
"""
field_name: Value should end with '/path?param2'
Expand All @@ -275,7 +299,9 @@ class JsonComparatorTest {
""".trimIndent()
)
.row(
// language=json
"""{"field_name": "some value/?param"}""",
// language=json
"""{"field_name": "{#url_regex:^.+some\\.url.+/path\\?param$#}"}""",
"""
field_name: Value is not a valid URL
Expand All @@ -285,7 +311,9 @@ class JsonComparatorTest {
""".trimIndent()
)
.row(
// language=json
"""{"field_name": "http://some_url:9999/path?param"}""",
// language=json
"""{"field_name": "{#url_regex:^.+some\\.url.+/path\\?param$#}"}""",
"""
field_name: Value does not match pattern /^.+some\.url.+/path\?param$/
Expand All @@ -295,7 +323,9 @@ class JsonComparatorTest {
""".trimIndent()
)
.row(
// language=json
"""{"field_name": "some value"}""",
// language=json
"""{"field_name": "{#templated_url#}"}""",
"""
field_name: Value is not a valid templated URL
Expand All @@ -305,7 +335,9 @@ class JsonComparatorTest {
""".trimIndent()
)
.row(
// language=json
"""{"field_name": "some value {?param}"}""",
// language=json
"""{"field_name": "{#templated_url_ending:{?param}#}"}""",
"""
field_name: Value is not a valid templated URL
Expand All @@ -315,7 +347,9 @@ class JsonComparatorTest {
""".trimIndent()
)
.row(
// language=json
"""{"field_name": "http://some.url:9999/path{?param}"}""",
// language=json
"""{"field_name": "{#templated_url_ending:/path{?param2}#}"}""",
"""
field_name: Value should end with '/path{?param2}'
Expand All @@ -325,7 +359,9 @@ class JsonComparatorTest {
""".trimIndent()
)
.row(
// language=json
"""{"field_name": "some value/{?param}"}""",
// language=json
"""{"field_name": "{#templated_url_regex:^.+some\\.url.+\/path\\{\\?param\\}$#}"}""",
"""
field_name: Value is not a valid templated URL
Expand All @@ -335,7 +371,9 @@ class JsonComparatorTest {
""".trimIndent()
)
.row(
// language=json
"""{"field_name": "http://some_url:9999/path{?param}"}""",
// language=json
"""{"field_name": "{#templated_url_regex:^.+some\\.url.+\/path\\{\\?param\\}$#}"}""",
"""
field_name: Value does not match pattern /^.+some\.url.+/path\{\?param\}$/
Expand All @@ -345,7 +383,9 @@ class JsonComparatorTest {
""".trimIndent()
)
.row(
// language=json
"""{"field_name": "some value"}""",
// language=json
"""{"field_name": "{#boolean_type#}"}""",
"""
field_name: Invalid value type
Expand All @@ -355,7 +395,9 @@ class JsonComparatorTest {
""".trimIndent()
)
.row(
// language=json
"""{"field_name": true}""",
// language=json
"""{"field_name": "{#string_type#}"}""",
"""
field_name: Invalid value type
Expand All @@ -365,7 +407,9 @@ class JsonComparatorTest {
""".trimIndent()
)
.row(
// language=json
"""{"field_name": "some value"}""",
// language=json
"""{"field_name": "{#number_type#}"}""",
"""
field_name: Invalid value type
Expand All @@ -375,7 +419,9 @@ class JsonComparatorTest {
""".trimIndent()
)
.row(
// language=json
"""{"field_name": "some value"}""",
// language=json
"""{"field_name": "{#array_type#}"}""",
"""
field_name: Invalid value type
Expand All @@ -385,7 +431,9 @@ class JsonComparatorTest {
""".trimIndent()
)
.row(
// language=json
"""{"field_name": "some value"}""",
// language=json
"""{"field_name": "{#object_type#}"}""",
"""
field_name: Invalid value type
Expand All @@ -395,7 +443,9 @@ class JsonComparatorTest {
""".trimIndent()
)
.row(
// language=json
"""{"field_name": "some value"}""",
// language=json
"""{"field_name": "{#date_time_format:iso_instant#}"}""",
"""
field_name: Invalid date time format
Expand Down Expand Up @@ -441,6 +491,7 @@ class JsonComparatorTest {
fun `should throw an error if element count does not match between the two arrays`() {

compare(
// language=json
"""
{
"some_array": [
Expand All @@ -450,6 +501,7 @@ class JsonComparatorTest {
]
}
""".trimIndent(),
// language=json
"""
{
"some_array": [
Expand All @@ -469,6 +521,7 @@ class JsonComparatorTest {
fun `should throw a detailed error if some elements did not match`() {

compare(
// language=json
"""
{
"some_array": [
Expand All @@ -480,6 +533,7 @@ class JsonComparatorTest {
]
}
""".trimIndent(),
// language=json
"""
{
"some_array": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@ class JsonCompareMatcherTest {
void should_match_with_default_comparator(){

assertThat(
// language=json
"{\"field_name\": \"hello world!\"}",
jsonMatcher("{\"field_name\": \"{#contains:llo wor#}\"}")
jsonMatcher(
// language=json
"{\"field_name\": \"{#contains:llo wor#}\"}"
)
);
}

Expand All @@ -37,8 +41,12 @@ void should_not_match_with_default_comparator(){
AssertionError error = Assertions.assertThrows(
AssertionError.class,
() -> assertThat(
// language=json
"{\"field_name\": \"hello_world!\"}",
jsonMatcher("{\"field_name\": \"{#contains:llo wor#}\"}")
jsonMatcher(
// language=json
"{\"field_name\": \"{#contains:llo wor#}\"}"
)
)
);

Expand Down Expand Up @@ -66,16 +74,21 @@ void should_match_with_custom_validator(){

JsonCompareMatcher customJsonMatcher = JsonMatcherBuilder.create()
.validators(customValidator())
.build("{\"field_name\": \"{#custom_notempty#}\"}");
.build(
// language=json
"{\"field_name\": \"{#custom_notempty#}\"}"
);

assertThat(
// language=json
"{\"field_name\": \"hello world!\"}",
customJsonMatcher
);

AssertionError error = Assertions.assertThrows(
AssertionError.class,
() -> assertThat(
// language=json
"{\"field_name\": \"\"}",
customJsonMatcher
)
Expand All @@ -96,16 +109,21 @@ void should_match_with_default_and_custom_validator(){

JsonCompareMatcher customJsonMatcher = JsonMatcherBuilder.create()
.validators(defaultAndCustomValidators())
.build("{\"field_name\": \"{#custom_notempty#}\"}");
.build(
// language=json
"{\"field_name\": \"{#custom_notempty#}\"}"
);

assertThat(
// language=json
"{\"field_name\": \"hello world!\"}",
customJsonMatcher
);

AssertionError error = Assertions.assertThrows(
AssertionError.class,
() -> assertThat(
// language=json
"{\"field_name\": \"\"}",
customJsonMatcher
)
Expand Down

0 comments on commit 9b8b466

Please sign in to comment.