Skip to content

Commit

Permalink
pkg: update tests to use matrix
Browse files Browse the repository at this point in the history
To reduce the otherwise large number of diffs,
we filter the "(and X more errors)" strings from error
messages.

Issue #3060
Issue #2884

Signed-off-by: Marcel van Lohuizen <[email protected]>
Change-Id: I47c140f04dcbd78a2cd22fc7ca074f4e323dc843
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1194196
Unity-Result: CUE porcuepine <[email protected]>
TryBot-Result: CUEcueckoo <[email protected]>
Reviewed-by: Daniel Martí <[email protected]>
  • Loading branch information
mpvl committed May 3, 2024
1 parent 4706840 commit a983b19
Show file tree
Hide file tree
Showing 8 changed files with 997 additions and 9 deletions.
263 changes: 263 additions & 0 deletions pkg/encoding/json/testdata/gen.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,269 @@ unmarshalStream: {
nums1: '1 2'
nums2: "1 2"
}
-- out/json-v3 --
Errors:
validate.t2.result: error in call to encoding/json.Validate: invalid value 10 (out of bound <3):
./in.cue:6:10
./in.cue:9:27
json.Validate:1:6
unmarshal.trailingInvalid.#result: error in call to encoding/json.Unmarshal: json: invalid JSON:
./in.cue:39:11
unmarshal.trailingValid.#result: error in call to encoding/json.Unmarshal: json: invalid JSON:
./in.cue:39:11

Result:
import "encoding/json"

validate: {
t1: {
str: *"{\"a\":10}" | string
schema: {
b: string
}
result: true
}
t2: {
str: *"{\"a\":10}" | string
schema: {
a: <3
}
result: _|_ // validate.t2.result: error in call to encoding/json.Validate: validate.t2.schema.a: invalid value 10 (out of bound <3)
}

// Issue #2395
enforceRequired: {
str: "{}"
schema: {
x!: int
}
result: json.Validate(str, schema)
}
}
valid: {
t1: {
"1"
#result: json.Valid(string)
}
}
compact: {
t1: {
"[1, 2]"
#result: "[1,2]"
}
}
indent: {
t1: {
"{\"a\": 1, \"b\": 2}"
#initial: *"" | string
#indent: *" " | string
#result: """
{
"a": 1,
"b": 2
}
"""
}
}
unmarshal: {
t1: {
"1"
#result: 1
}
trailingValid: {
#result: _|_ // unmarshal.trailingValid.#result: error in call to encoding/json.Unmarshal: json: invalid JSON
}
trailingInvalid: {
#result: _|_ // unmarshal.trailingInvalid.#result: error in call to encoding/json.Unmarshal: json: invalid JSON
}
}
marshalStream: {
t1: {
#result: json.MarshalStream(X)
[{
a: 1
}, {
b: 2
}]
}
t2: {
#result: json.MarshalStream(X)
[{
a: 1
}, {
b: *2 | int
}]
}
t3: {
#result: json.MarshalStream(X)
[{
a: "\\ \" & < >"
}, {
b: ""
}]
}
}
marshal: {
t1: {
#x: int
a: #x
} & {
_
#result: json.Marshal(X)
}
t2: {
a: #"\ " & < >"#
} & {
_
#result: json.Marshal(X)
}
}
htmlEscape: {
t1: marshal.t2.#result & {
string
#result: json.HTMLEscape(X)
}
t2: marshalStream.t3.#result & {
string
#result: json.HTMLEscape(X)
}
}
unmarshalStream: {
t1: {
"{\"a\": 1}{\"b\": 2}"
#result: [{
a: 1
}, {
b: 2
}]
}
t2: {
'{"a": 1}{"b": 2}'
#result: [{
a: 1
}, {
b: 2
}]
}
empty1: {
''
#result: []
}
empty2: {
""
#result: []
}
nums1: {
'1 2'
#result: [1, 2]
}
nums2: {
"1 2"
#result: [1, 2]
}
}
-- diff/-out/json-v3<==>+out/json --
diff old new
--- old
+++ new
@@ -75,23 +75,15 @@
}
marshalStream: {
t1: {
- #result: """
- {"a":1}
- {"b":2}
-
- """
- [{
- a: 1
- }, {
- b: 2
- }]
- }
- t2: {
- #result: """
- {"a":1}
- {"b":2}
-
- """
+ #result: json.MarshalStream(X)
+ [{
+ a: 1
+ }, {
+ b: 2
+ }]
+ }
+ t2: {
+ #result: json.MarshalStream(X)
[{
a: 1
}, {
@@ -99,11 +91,7 @@
}]
}
t3: {
- #result: """
- {"a":"\\\\ \\" & < >"}
- {"b":""}
-
- """
+ #result: json.MarshalStream(X)
[{
a: "\\ \" & < >"
}, {
@@ -113,31 +101,27 @@
}
marshal: {
t1: {
- #x: int
- #result: json.Marshal(X)
- a: int
- }
- t2: {
- a: "\\ \" & < >"
- #result: "{\"a\":\"\\\\ \\\" & < >\"}"
+ #x: int
+ a: #x
+ } & {
+ _
+ #result: json.Marshal(X)
+ }
+ t2: {
+ a: #"\ " & < >"#
+ } & {
+ _
+ #result: json.Marshal(X)
}
}
htmlEscape: {
- t1: {
- "{\"a\":\"\\\\ \\\" & < >\"}"
- #result: "{\"a\":\"\\\\ \\\" \\u0026 \\u003c \\u003e\"}"
- }
- t2: {
- """
- {"a":"\\\\ \\" & < >"}
- {"b":""}
-
- """
- #result: """
- {"a":"\\\\ \\" \\u0026 \\u003c \\u003e"}
- {"b":""}
-
- """
+ t1: marshal.t2.#result & {
+ string
+ #result: json.HTMLEscape(X)
+ }
+ t2: marshalStream.t3.#result & {
+ string
+ #result: json.HTMLEscape(X)
}
}
unmarshalStream: {
-- diff/todo/p0 --
Builtins left as unevaluated in output.
-- out/json --
Errors:
validate.t2.result: error in call to encoding/json.Validate: invalid value 10 (out of bound <3):
Expand Down
17 changes: 12 additions & 5 deletions pkg/internal/builtintest/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,30 @@ package builtintest

import (
"fmt"
"regexp"
"testing"

"cuelang.org/go/cue/format"
"cuelang.org/go/internal/core/eval"
"cuelang.org/go/internal/core/export"
"cuelang.org/go/internal/core/runtime"
"cuelang.org/go/internal/core/validate"
"cuelang.org/go/internal/cuetdtest"
"cuelang.org/go/internal/cuetxtar"
)

func Run(name string, t *testing.T) {
test := cuetxtar.TxTarTest{
Root: "./testdata",
Name: name,
Root: "./testdata",
Name: name,
Matrix: cuetdtest.SmallMatrix,
}

r := runtime.New()
// Find common patterns that we want to ignore for testing purposes.
// TODO(evalv3): remove once new implementation is stable.
re := regexp.MustCompile(` \(and \d* more errors\)`)

test.Run(t, func(t *cuetxtar.Test) {
r := t.Runtime()
a := t.Instance()

v, errs := r.Build(nil, a)
Expand Down Expand Up @@ -68,6 +73,8 @@ func Run(name string, t *testing.T) {
t.Fatal(err)
}

fmt.Fprint(t, string(b))
s := re.ReplaceAllString(string(b), "")

fmt.Fprint(t, s)
})
}
Loading

0 comments on commit a983b19

Please sign in to comment.