Debian Codegraph @

pkg/internal/third_party/go-json-experiment/json/fold_test.go

raw
1 // Copyright 2020 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package json
6
7 import (
8 "fmt"
9 "reflect"
10 "testing"
11 "unicode"
12 )
13
14 var equalFoldTestdata = []struct {
15 in1, in2 string
16 want bool
17 }{
18 {"", "", true},
19 {"abc", "abc", true},
20 {"ABcd", "ABcd", true},
21 {"123abc", "123ABC", true},
22 {"_1_2_-_3__--a-_-b-c-", "123ABC", true},
23 {"αβδ", "ΑΒΔ", true},
24 {"abc", "xyz", false},
25 {"abc", "XYZ", false},
26 {"abcdefghijk", "abcdefghijX", false},
27 {"abcdefghijk", "abcdefghij\u212A", true},
28 {"abcdefghijK", "abcdefghij\u212A", true},
29 {"abcdefghijkz", "abcdefghij\u212Ay", false},
30 {"abcdefghijKz", "abcdefghij\u212Ay", false},
31 {"1", "2", false},
32 {"utf-8", "US-ASCII", false},
33 {"hello, world!", "hello, world!", true},
34 {"hello, world!", "Hello, World!", true},
35 {"hello, world!", "HELLO, WORLD!", true},
36 {"hello, world!", "jello, world!", false},
37 {"γειά, κόσμε!", "γειά, κόσμε!", true},
38 {"γειά, κόσμε!", "Γειά, Κόσμε!", true},
39 {"γειά, κόσμε!", "ΓΕΙΆ, ΚΌΣΜΕ!", true},
40 {"γειά, κόσμε!", "ΛΕΙΆ, ΚΌΣΜΕ!", false},
41 {"AESKey", "aesKey", true},
42 {"γειά, κόσμε!", "Γ\xce_\xb5ιά, Κόσμε!", false},
43 {"aeskey", "AESKEY", true},
44 {"AESKEY", "aes_key", true},
45 {"aes_key", "AES_KEY", true},
46 {"AES_KEY", "aes-key", true},
47 {"aes-key", "AES-KEY", true},
48 {"AES-KEY", "aesKey", true},
49 {"aesKey", "AesKey", true},
50 {"AesKey", "AESKey", true},
51 {"AESKey", "aeskey", true},
52 {"DESKey", "aeskey", false},
53 {"AES Key", "aeskey", false},
54 {"aes﹏key", "aeskey", false}, // Unicode underscore not handled
55 {"aes〰key", "aeskey", false}, // Unicode dash not handled
56 }
57
58 func TestEqualFold(t *testing.T) {
59 for _, tt := range equalFoldTestdata {
60 got := equalFold([]byte(tt.in1), []byte(tt.in2))
61 if got != tt.want {
62 t.Errorf("equalFold(%q, %q) = %v, want %v", tt.in1, tt.in2, got, tt.want)
63 }
64 }
65 }
66
67 func equalFold(x, y []byte) bool {
68 return string(foldName(x)) == string(foldName(y))
69 }
70
71 func TestFoldRune(t *testing.T) {
72 if testing.Short() {
73 t.Skip()
74 }
75
76 var foldSet []rune
77 for r := range rune(unicode.MaxRune + 1) {
78 // Derive all runes that are all part of the same fold set.
79 foldSet = foldSet[:0]
80 for r0 := r; r != r0 || len(foldSet) == 0; r = unicode.SimpleFold(r) {
81 foldSet = append(foldSet, r)
82 }
83
84 // Normalized form of each rune in a foldset must be the same and
85 // also be within the set itself.
86 var withinSet bool
87 rr0 := foldRune(foldSet[0])
88 for _, r := range foldSet {
89 withinSet = withinSet || rr0 == r
90 rr := foldRune(r)
91 if rr0 != rr {
92 t.Errorf("foldRune(%q) = %q, want %q", r, rr, rr0)
93 }
94 }
95 if !withinSet {
96 t.Errorf("foldRune(%q) = %q not in fold set %q", foldSet[0], rr0, string(foldSet))
97 }
98 }
99 }
100
101 // TestBenchmarkUnmarshalUnknown unmarshals an unknown field into a struct with
102 // varying number of fields. Since the unknown field does not directly match
103 // any known field by name, it must fall back on case-insensitive matching.
104 func TestBenchmarkUnmarshalUnknown(t *testing.T) {
105 in := []byte(`{"NameUnknown":null}`)
106 for _, n := range []int{1, 2, 5, 10, 20, 50, 100} {
107 unmarshal := Unmarshal
108
109 var fields []reflect.StructField
110 for i := range n {
111 fields = append(fields, reflect.StructField{
112 Name: fmt.Sprintf("Name%d", i),
113 Type: T[int](),
114 Tag: `json:",case:ignore"`,
115 })
116 }
117 out := reflect.New(reflect.StructOf(fields)).Interface()
118
119 t.Run(fmt.Sprintf("N%d", n), func(t *testing.T) {
120 if err := unmarshal(in, out); err != nil {
121 t.Fatalf("Unmarshal error: %v", err)
122 }
123 })
124 }
125 }