Skip to content

Commit

Permalink
all: add tests to cover HTML escaping via encoding/json
Browse files Browse the repository at this point in the history
Note how the HTML escaping always happens, even when cmd/cue's --escape
flag or the json.HTMLEscape standard library APIs are not used.
This is the bug that the next CL is going to fix, updating the tests.

For #1243.

Signed-off-by: Daniel Martí <mvdan@mvdan.cc>
Change-Id: Iff6cf70e2ce7b5a6f9d7cbf14f3edf390b222e6f
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/552319
Unity-Result: CUEcueckoo <cueckoo@cuelang.org>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Reviewed-by: Roger Peppe <rogpeppe@gmail.com>
  • Loading branch information
mvdan committed Apr 6, 2023
1 parent 1932295 commit 1aefd09
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
26 changes: 26 additions & 0 deletions cmd/cue/cmd/testdata/script/export_escape.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Verify that export with and without --escape works as expected.

exec cue export --out json file.cue
cmp stdout stdout.golden

exec cue export --out json --escape file.cue
cmp stdout stdout-escape.golden

-- file.cue --
package hello

simple: "hello"
specialJSON: #"\ ""#
specialHTML: "& < >"
-- stdout.golden --
{
"simple": "hello",
"specialJSON": "\\ \"",
"specialHTML": "\u0026 \u003c \u003e"
}
-- stdout-escape.golden --
{
"simple": "hello",
"specialJSON": "\\ \"",
"specialHTML": "\u0026 \u003c \u003e"
}
16 changes: 16 additions & 0 deletions pkg/encoding/json/testdata/gen.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ t8: {
y: json.Marshal({a: x})
}
t9: json.MarshalStream([{a: 1}, {b: int | *2}])
t10: json.Marshal({a: #"\ " & < >"#})
t11: json.HTMLEscape(t10)
t12: json.MarshalStream([{a: #"\ " & < >"#}, {b: ""}])
t13: json.HTMLEscape(t12)

unmarshalStream: {
t1: json.UnmarshalStream(#"{"a": 1}{"b": 2}"#)
Expand Down Expand Up @@ -60,6 +64,18 @@ t9: """
{"a":1}
{"b":2}

"""
t10: "{\"a\":\"\\\\ \\\" \\u0026 \\u003c \\u003e\"}"
t11: "{\"a\":\"\\\\ \\\" \\u0026 \\u003c \\u003e\"}"
t12: """
{"a":"\\\\ \\" \\u0026 \\u003c \\u003e"}
{"b":""}

"""
t13: """
{"a":"\\\\ \\" \\u0026 \\u003c \\u003e"}
{"b":""}

"""
unmarshalStream: {
t1: [{
Expand Down

0 comments on commit 1aefd09

Please sign in to comment.