| 1 | package schema |
| 2 | |
| 3 | import ( |
| 4 | "reflect" |
| 5 | "testing" |
| 6 | ) |
| 7 | |
| 8 | func TestParseTagAndOptions(t *testing.T) { |
| 9 | alias, opts := parseTag("name,omitempty,default:foo") |
| 10 | if alias != "name" { |
| 11 | t.Fatalf("expected alias name, got %s", alias) |
| 12 | } |
| 13 | if !opts.Contains("omitempty") { |
| 14 | t.Fatalf("expected omitempty option") |
| 15 | } |
| 16 | if val := opts.getDefaultOptionValue(); val != "foo" { |
| 17 | t.Fatalf("expected default foo, got %s", val) |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | func TestFieldAlias(t *testing.T) { |
| 22 | type S struct { |
| 23 | Field string `json:"custom,omitempty"` |
| 24 | } |
| 25 | f, ok := reflect.TypeOf(S{}).FieldByName("Field") |
| 26 | if !ok { |
| 27 | t.Fatal("field not found") |
| 28 | } |
| 29 | alias, opts := fieldAlias(f, "json") |
| 30 | if alias != "custom" { |
| 31 | t.Fatalf("expected alias custom, got %s", alias) |
| 32 | } |
| 33 | if !opts.Contains("omitempty") { |
| 34 | t.Fatalf("expected omitempty option") |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | func TestTagOptionsContains(t *testing.T) { |
| 39 | opts := tagOptions("a,b,default:val") |
| 40 | if !opts.Contains("a") || opts.Contains("c") { |
| 41 | t.Fatalf("contains failed") |
| 42 | } |
| 43 | if val := opts.getDefaultOptionValue(); val != "val" { |
| 44 | t.Fatalf("expected default val, got %s", val) |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | func BenchmarkParseTag(b *testing.B) { |
| 49 | for b.Loop() { |
| 50 | parseTag("field,omitempty,default:value") |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | func BenchmarkIsZero(b *testing.B) { |
| 55 | type S struct{ A int } |
| 56 | v := reflect.ValueOf(S{}) |
| 57 | for b.Loop() { |
| 58 | isZero(v) |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | type customZero struct{ A int } |
| 63 | |
| 64 | func (c customZero) IsZero() bool { return c.A == 0 } |
| 65 | |
| 66 | func TestIsZeroCases(t *testing.T) { |
| 67 | var sl []int |
| 68 | if !isZero(reflect.ValueOf(sl)) { |
| 69 | t.Errorf("nil slice should be zero") |
| 70 | } |
| 71 | sl = []int{} |
| 72 | if !isZero(reflect.ValueOf(sl)) { |
| 73 | t.Errorf("empty slice should be zero") |
| 74 | } |
| 75 | sl = []int{1} |
| 76 | if isZero(reflect.ValueOf(sl)) { |
| 77 | t.Errorf("non-empty slice considered zero") |
| 78 | } |
| 79 | |
| 80 | arr := [2]int{} |
| 81 | if !isZero(reflect.ValueOf(arr)) { |
| 82 | t.Errorf("zero array should be zero") |
| 83 | } |
| 84 | arr = [2]int{0, 1} |
| 85 | if isZero(reflect.ValueOf(arr)) { |
| 86 | t.Errorf("non-zero array considered zero") |
| 87 | } |
| 88 | |
| 89 | type S struct { |
| 90 | A int |
| 91 | B string |
| 92 | } |
| 93 | if !isZero(reflect.ValueOf(S{})) { |
| 94 | t.Errorf("zero struct should be zero") |
| 95 | } |
| 96 | if isZero(reflect.ValueOf(S{A: 1})) { |
| 97 | t.Errorf("non-zero struct considered zero") |
| 98 | } |
| 99 | |
| 100 | if !isZero(reflect.ValueOf(customZero{})) { |
| 101 | t.Errorf("IsZero method not used for zero value") |
| 102 | } |
| 103 | if isZero(reflect.ValueOf(customZero{A: 1})) { |
| 104 | t.Errorf("IsZero method not used for non-zero value") |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | func TestIsZeroFuncAndMap(t *testing.T) { |
| 109 | // nil func should be zero |
| 110 | var nilFn func() |
| 111 | if !isZero(reflect.ValueOf(nilFn)) { |
| 112 | t.Errorf("nil func should be zero") |
| 113 | } |
| 114 | // non-nil func should not be zero |
| 115 | nonNilFn := func() {} |
| 116 | if isZero(reflect.ValueOf(nonNilFn)) { |
| 117 | t.Errorf("non-nil func should not be zero") |
| 118 | } |
| 119 | |
| 120 | var m map[string]int |
| 121 | if !isZero(reflect.ValueOf(m)) { |
| 122 | t.Errorf("nil map should be zero") |
| 123 | } |
| 124 | m = map[string]int{} |
| 125 | if !isZero(reflect.ValueOf(m)) { |
| 126 | t.Errorf("empty map should be zero") |
| 127 | } |
| 128 | m["a"] = 1 |
| 129 | if isZero(reflect.ValueOf(m)) { |
| 130 | t.Errorf("non-empty map considered zero") |
| 131 | } |
| 132 | } |