Skip to content

Commit 7ed004f

Browse files
authored
docs: Document '%' in expectedJSON case (#28)
1 parent ff60c95 commit 7ed004f

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,24 @@ Create a new `*jsonassert.Asserter` in your test and use this to make assertions
1818
func TestWhatever(t *testing.T) {
1919
ja := jsonassert.New(t)
2020
// find some sort of payload
21+
name := "River Tam"
22+
age := 16
2123
ja.Assertf(payload, `
2224
{
2325
"name": "%s",
2426
"age": %d,
27+
"averageTestScore": "%s",
2528
"skills": [
2629
{ "name": "martial arts", "level": 99 },
2730
{ "name": "intelligence", "level": 100 },
2831
{ "name": "mental fortitude", "level": 4 }
2932
]
30-
}`, "River Tam", 16)
33+
}`, name, age, "99%")
3134
}
3235
```
3336

3437
You may pass in `fmt.Sprintf` arguments after the expected JSON structure.
38+
This feature may be useful for the case when you already have variables in your test with the expected data or when your expected JSON contains a `%` character which could be misinterpreted as a format directive.
3539

3640
`ja.Assertf()` supports assertions against **strings only**.
3741

examples_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,13 @@ func ExampleNew() {
2525

2626
func ExampleAsserter_Assertf_formatArguments() {
2727
ja := jsonassert.New(t)
28-
ja.Assertf(`{"hello":"世界"}`, `
29-
{
30-
"hello": "%s"
31-
}`, "world")
28+
expTestScore := "28%"
29+
ja.Assertf(
30+
`{ "name": "Jayne Cobb", "age": 36, "averageTestScore": "88%" }`,
31+
`{ "name": "Jayne Cobb", "age": 36, "averageTestScore": "%s" }`, expTestScore,
32+
)
3233
//output:
33-
//expected string at '$.hello' to be 'world' but was '世界'
34+
//expected string at '$.averageTestScore' to be '28%' but was '88%'
3435
}
3536

3637
func ExampleAsserter_Assertf_presenceOnly() {

exports.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,12 @@ along with the "world" format argument. For example:
9191
9292
ja.Assertf(`{"hello": "world"}`, `{"hello":"%s"}`, "world")
9393
94+
You may also use format arguments in the case when your expected JSON contains
95+
a percent character, which would otherwise be interpreted as a
96+
format-directive.
97+
98+
ja.Assertf(`{"averageTestScore": "99%"}`, `{"averageTestScore":"%s"}`, "99%")
99+
94100
Additionally, you may wish to make assertions against the *presence* of a
95101
value, but not against its value. For example:
96102

0 commit comments

Comments
 (0)