|
|
@@ -7,8 +7,11 @@ import (
|
|
|
"bytes"
|
|
|
"encoding/hex"
|
|
|
"encoding/json"
|
|
|
+ "math"
|
|
|
"os"
|
|
|
+ "regexp"
|
|
|
"strconv"
|
|
|
+ "strings"
|
|
|
"testing"
|
|
|
)
|
|
|
|
|
|
@@ -86,15 +89,24 @@ func TestCborIndefiniteLength(t *testing.T) {
|
|
|
}
|
|
|
|
|
|
type testCborGolden struct {
|
|
|
- Base64 string `json:"cbor"`
|
|
|
- Hex string `json:"hex"`
|
|
|
- Roundtrip bool `json:"roundtrip"`
|
|
|
- Decoded interface{} `json:"decoded"`
|
|
|
+ Base64 string `json:"cbor"`
|
|
|
+ Hex string `json:"hex"`
|
|
|
+ Roundtrip bool `json:"roundtrip"`
|
|
|
+ Decoded interface{} `json:"decoded"`
|
|
|
+ Diagnostic string `json:"diagnostic"`
|
|
|
+ Skip bool `json:"skip"`
|
|
|
}
|
|
|
|
|
|
-// __TestCborGoldens is disabled because it includes numbers outside the range of int64/uint64
|
|
|
+// Only run this within ext dep tests.
|
|
|
+// It skips some tests and requires private setup of deepEqual.
|
|
|
+// Some tests are skipped because they include numbers outside the range of int64/uint64
|
|
|
// and it doesn't support diagnostic checking.
|
|
|
-func __TestCborGoldens(t *testing.T) {
|
|
|
+func doTestCborGoldens(t *testing.T) {
|
|
|
+ oldMapType := testCborH.MapType
|
|
|
+ defer func() {
|
|
|
+ testCborH.MapType = oldMapType
|
|
|
+ }()
|
|
|
+ testCborH.MapType = testMapStrIntfTyp
|
|
|
// decode test-cbor-goldens.json into a list of []*testCborGolden
|
|
|
// for each one,
|
|
|
// - decode hex into []byte bs
|
|
|
@@ -114,7 +126,35 @@ func __TestCborGoldens(t *testing.T) {
|
|
|
logT(t, "error json decoding test-cbor-goldens.json: %v", err)
|
|
|
failT(t)
|
|
|
}
|
|
|
+
|
|
|
+ tagregex := regexp.MustCompile(`[\d]+\(.+?\)`)
|
|
|
+ hexregex := regexp.MustCompile(`h'([0-9a-fA-F]*)'`)
|
|
|
for i, g := range gs {
|
|
|
+ // fmt.Printf("%v, skip: %v, isTag: %v, %s\n", i, g.Skip, tagregex.MatchString(g.Diagnostic), g.Diagnostic)
|
|
|
+ // skip tags or simple or those with prefix, as we can't verify them.
|
|
|
+ if g.Skip || strings.HasPrefix(g.Diagnostic, "simple(") || tagregex.MatchString(g.Diagnostic) {
|
|
|
+ // fmt.Printf("%v: skipped\n", i)
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ // println(i, "g.Diagnostic", g.Diagnostic)
|
|
|
+ if hexregex.MatchString(g.Diagnostic) {
|
|
|
+ // println(i, "g.Diagnostic matched hex")
|
|
|
+ if s2 := g.Diagnostic[2 : len(g.Diagnostic)-1]; s2 == "" {
|
|
|
+ g.Decoded = []byte{}
|
|
|
+ } else if bs2, err2 := hex.DecodeString(s2); err2 == nil {
|
|
|
+ g.Decoded = bs2
|
|
|
+ }
|
|
|
+ // fmt.Printf("%v: hex: %v\n", i, g.Decoded)
|
|
|
+ }
|
|
|
+ // if g.Diagnostic != "" {
|
|
|
+ // if strings.HasPrefix(g.Diagnostic, "simple(") {
|
|
|
+ // continue
|
|
|
+ // }
|
|
|
+ // // if regex matches num(stuff), then it is a tag and ignore it ...
|
|
|
+ // if tagregex.MatchString(g.Diagnostic) {
|
|
|
+ // continue
|
|
|
+ // }
|
|
|
+ // }
|
|
|
bs, err := hex.DecodeString(g.Hex)
|
|
|
if err != nil {
|
|
|
logT(t, "[%v] error hex decoding %s [%v]: %v", i, g.Hex, err)
|
|
|
@@ -168,11 +208,48 @@ func __TestCborGoldens(t *testing.T) {
|
|
|
continue
|
|
|
}
|
|
|
}
|
|
|
- if err = deepEqual(g.Decoded, v); err != nil {
|
|
|
- logT(t, "[%v] deepEqual error: %v", i, err)
|
|
|
- logT(t, " ....... GOLDEN: (%T) %#v", g.Decoded, g.Decoded)
|
|
|
- logT(t, " ....... DECODED: (%T) %#v", v, v)
|
|
|
- failT(t)
|
|
|
+ // check the diagnostics to compare
|
|
|
+ switch g.Diagnostic {
|
|
|
+ case "Infinity":
|
|
|
+ b := math.IsInf(v.(float64), 1)
|
|
|
+ testCborError(t, i, math.Inf(1), v, nil, &b)
|
|
|
+ case "-Infinity":
|
|
|
+ b := math.IsInf(v.(float64), -1)
|
|
|
+ testCborError(t, i, math.Inf(-1), v, nil, &b)
|
|
|
+ case "NaN":
|
|
|
+ // println(i, "checking NaN")
|
|
|
+ b := math.IsNaN(v.(float64))
|
|
|
+ testCborError(t, i, math.NaN(), v, nil, &b)
|
|
|
+ case "undefined":
|
|
|
+ b := v == nil
|
|
|
+ testCborError(t, i, nil, v, nil, &b)
|
|
|
+ default:
|
|
|
+ testCborError(t, i, g.Decoded, v, deepEqual(g.Decoded, v), nil)
|
|
|
+ // if false { // if match simple(number)
|
|
|
+ // } else if false { // if match h'XYZ'
|
|
|
+ // } else { // check deepequal
|
|
|
+ // testCborError(t, i, g.Decoded, v, deepEqual(g.Decoded, v), nil)
|
|
|
+ // }
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+func testCborError(t *testing.T, i int, v0, v1 interface{}, err error, equal *bool) {
|
|
|
+ if err == nil && equal == nil {
|
|
|
+ // fmt.Printf("%v testCborError passed (err and equal nil)\n", i)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if err != nil {
|
|
|
+ logT(t, "[%v] deepEqual error: %v", i, err)
|
|
|
+ logT(t, " ....... GOLDEN: (%T) %#v", v0, v0)
|
|
|
+ logT(t, " ....... DECODED: (%T) %#v", v1, v1)
|
|
|
+ failT(t)
|
|
|
+ }
|
|
|
+ if equal != nil && !*equal {
|
|
|
+ logT(t, "[%v] values not equal", i)
|
|
|
+ logT(t, " ....... GOLDEN: (%T) %#v", v0, v0)
|
|
|
+ logT(t, " ....... DECODED: (%T) %#v", v1, v1)
|
|
|
+ failT(t)
|
|
|
+ }
|
|
|
+ // fmt.Printf("%v testCborError passed (checks passed)\n", i)
|
|
|
+}
|