(t *testing.T)
| 129 | } |
| 130 | |
| 131 | func TestFields(t *testing.T) { |
| 132 | tests := []struct { |
| 133 | t FieldType |
| 134 | i int64 |
| 135 | s string |
| 136 | iface interface{} |
| 137 | want interface{} |
| 138 | }{ |
| 139 | {t: ArrayMarshalerType, iface: users(2), want: []interface{}{"user", "user"}}, |
| 140 | {t: ObjectMarshalerType, iface: users(2), want: map[string]interface{}{"users": 2}}, |
| 141 | {t: BoolType, i: 0, want: false}, |
| 142 | {t: ByteStringType, iface: []byte("foo"), want: "foo"}, |
| 143 | {t: Complex128Type, iface: 1 + 2i, want: 1 + 2i}, |
| 144 | {t: Complex64Type, iface: complex64(1 + 2i), want: complex64(1 + 2i)}, |
| 145 | {t: DurationType, i: 1000, want: time.Microsecond}, |
| 146 | {t: Float64Type, i: int64(math.Float64bits(3.14)), want: 3.14}, |
| 147 | {t: Float32Type, i: int64(math.Float32bits(3.14)), want: float32(3.14)}, |
| 148 | {t: Int64Type, i: 42, want: int64(42)}, |
| 149 | {t: Int32Type, i: 42, want: int32(42)}, |
| 150 | {t: Int16Type, i: 42, want: int16(42)}, |
| 151 | {t: Int8Type, i: 42, want: int8(42)}, |
| 152 | {t: StringType, s: "foo", want: "foo"}, |
| 153 | {t: TimeType, i: 1000, iface: time.UTC, want: time.Unix(0, 1000).In(time.UTC)}, |
| 154 | {t: TimeType, i: 1000, want: time.Unix(0, 1000)}, |
| 155 | {t: Uint64Type, i: 42, want: uint64(42)}, |
| 156 | {t: Uint32Type, i: 42, want: uint32(42)}, |
| 157 | {t: Uint16Type, i: 42, want: uint16(42)}, |
| 158 | {t: Uint8Type, i: 42, want: uint8(42)}, |
| 159 | {t: UintptrType, i: 42, want: uintptr(42)}, |
| 160 | {t: ReflectType, iface: users(2), want: users(2)}, |
| 161 | {t: ReflectType, iface: nil, want: nil}, |
| 162 | {t: NamespaceType, want: map[string]interface{}{}}, |
| 163 | {t: StringerType, iface: users(2), want: "2 users"}, |
| 164 | {t: StringerType, iface: &obj{}, want: "obj"}, |
| 165 | {t: StringerType, iface: (*obj)(nil), want: "nil obj"}, |
| 166 | {t: SkipType, want: interface{}(nil)}, |
| 167 | {t: StringerType, iface: (*url.URL)(nil), want: "<nil>"}, |
| 168 | {t: StringerType, iface: (*users)(nil), want: "<nil>"}, |
| 169 | {t: ErrorType, iface: (*errObj)(nil), want: "<nil>"}, |
| 170 | } |
| 171 | |
| 172 | for _, tt := range tests { |
| 173 | enc := NewMapObjectEncoder() |
| 174 | f := Field{Key: "k", Type: tt.t, Integer: tt.i, Interface: tt.iface, String: tt.s} |
| 175 | f.AddTo(enc) |
| 176 | assert.Equal(t, tt.want, enc.Fields["k"], "Unexpected output from field %+v.", f) |
| 177 | |
| 178 | delete(enc.Fields, "k") |
| 179 | assert.Equal(t, 0, len(enc.Fields), "Unexpected extra fields present.") |
| 180 | |
| 181 | assert.True(t, f.Equals(f), "Field does not equal itself") |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | func TestInlineMarshaler(t *testing.T) { |
| 186 | enc := NewMapObjectEncoder() |
nothing calls this directly
no test coverage detected