|
| 1 | +package types_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "slices" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/xtls/xray-core/infra/conf/cfgcommon/types" |
| 9 | +) |
| 10 | + |
| 11 | +type TestGroup[T any] struct { |
| 12 | + name string |
| 13 | + input string |
| 14 | + expected []T |
| 15 | +} |
| 16 | + |
| 17 | +// intentionally to be so chaos |
| 18 | +var rawJson = `{ |
| 19 | + "field": |
| 20 | + ["value1", |
| 21 | + "value2", "value3" |
| 22 | + ] |
| 23 | +}` |
| 24 | + |
| 25 | +func TestListableUnmarshal(t *testing.T) { |
| 26 | + type TestStruct struct { |
| 27 | + Field types.Listable[string] `json:"field"` |
| 28 | + } |
| 29 | + |
| 30 | + tests := []TestGroup[string]{ |
| 31 | + { |
| 32 | + name: "SingleString", |
| 33 | + input: `{"field": "hello"}`, |
| 34 | + expected: []string{"hello"}, |
| 35 | + }, |
| 36 | + { |
| 37 | + name: "ArrayString", |
| 38 | + input: `{"field": ["value1", "value2", "value3"]}`, |
| 39 | + expected: []string{"value1", "value2", "value3"}, |
| 40 | + }, |
| 41 | + { |
| 42 | + name: "ComplexArray", |
| 43 | + input: rawJson, |
| 44 | + expected: []string{"value1", "value2", "value3"}, |
| 45 | + }, |
| 46 | + { |
| 47 | + name: "SingleStringWithSpace", |
| 48 | + input: `{"field": "hello" }`, |
| 49 | + expected: []string{"hello"}, |
| 50 | + }, |
| 51 | + { |
| 52 | + name: "ArrayWithSpace", |
| 53 | + input: `{"field": [ "a", "b" ] }`, |
| 54 | + expected: []string{"a", "b"}, |
| 55 | + }, |
| 56 | + { |
| 57 | + name: "Null", |
| 58 | + input: `{"field": null}`, |
| 59 | + expected: nil, |
| 60 | + }, |
| 61 | + { |
| 62 | + name: "Missing (default)", |
| 63 | + input: `{}`, |
| 64 | + expected: nil, |
| 65 | + }, |
| 66 | + } |
| 67 | + |
| 68 | + for _, tt := range tests { |
| 69 | + t.Run(tt.name, func(t *testing.T) { |
| 70 | + var ts TestStruct |
| 71 | + err := json.Unmarshal([]byte(tt.input), &ts) |
| 72 | + if err != nil { |
| 73 | + t.Fatalf("Unmarshal failed: %v", err) |
| 74 | + } |
| 75 | + if !slices.Equal([]string(ts.Field), tt.expected) { |
| 76 | + t.Errorf("Expected %v, got %v", tt.expected, ts.Field) |
| 77 | + } |
| 78 | + }) |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +func TestListableInt(t *testing.T) { |
| 83 | + tests := []TestGroup[int]{ |
| 84 | + { |
| 85 | + name: "SingleInt", |
| 86 | + input: `123`, |
| 87 | + expected: []int{123}, |
| 88 | + }, |
| 89 | + { |
| 90 | + name: "ArrayInt", |
| 91 | + input: `[1, 2]`, |
| 92 | + expected: []int{1, 2}, |
| 93 | + }, |
| 94 | + { |
| 95 | + name: "Null", |
| 96 | + input: `null`, |
| 97 | + expected: nil, |
| 98 | + }, |
| 99 | + } |
| 100 | + |
| 101 | + for _, tt := range tests { |
| 102 | + t.Run(tt.name, func(t *testing.T) { |
| 103 | + var l types.Listable[int] |
| 104 | + err := json.Unmarshal([]byte(tt.input), &l) |
| 105 | + if err != nil { |
| 106 | + t.Fatalf("Unmarshal failed: %v", err) |
| 107 | + } |
| 108 | + if !slices.Equal([]int(l), tt.expected) { |
| 109 | + t.Errorf("Expected %v, got %v", tt.expected, l) |
| 110 | + } |
| 111 | + }) |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +func TestListableSimpleString(t *testing.T) { |
| 116 | + type TestStruct struct { |
| 117 | + Field types.ListableSimpleString `json:"field"` |
| 118 | + } |
| 119 | + |
| 120 | + tests := []TestGroup[string]{ |
| 121 | + { |
| 122 | + name: "SingleString", |
| 123 | + input: `{"field": "singleValue"}`, |
| 124 | + expected: []string{"singleValue"}, |
| 125 | + }, |
| 126 | + { |
| 127 | + name: "ArrayString", |
| 128 | + input: `{"field": ["value1", "value2", "value3"]}`, |
| 129 | + expected: []string{"value1", "value2", "value3"}, |
| 130 | + }, |
| 131 | + { |
| 132 | + name: "WaveSplit", |
| 133 | + input: `{"field": "value1~value2~value3"}`, |
| 134 | + expected: []string{"value1", "value2", "value3"}, |
| 135 | + }, |
| 136 | + } |
| 137 | + for _, tt := range tests { |
| 138 | + t.Run(tt.name, func(t *testing.T) { |
| 139 | + var ts TestStruct |
| 140 | + err := json.Unmarshal([]byte(tt.input), &ts) |
| 141 | + if err != nil { |
| 142 | + t.Fatalf("Unmarshal failed: %v", err) |
| 143 | + } |
| 144 | + if !slices.Equal([]string(ts.Field), tt.expected) { |
| 145 | + t.Errorf("Expected %v, got %v", tt.expected, ts.Field) |
| 146 | + } |
| 147 | + }) |
| 148 | + } |
| 149 | +} |
0 commit comments