summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/mailru/easyjson/tests/opt_test.go
blob: bdd32aa4a6c8e508876dae67624a213a9bc7d632 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package tests

import (
	"math"
	"reflect"
	"testing"

	"encoding/json"

	"github.com/mailru/easyjson/opt"
)

// This struct type must NOT have a generated marshaler
type OptsVanilla struct {
	Int  opt.Int
	Uint opt.Uint

	Int8  opt.Int8
	Int16 opt.Int16
	Int32 opt.Int32
	Int64 opt.Int64

	Uint8  opt.Uint8
	Uint16 opt.Uint16
	Uint32 opt.Uint32
	Uint64 opt.Uint64

	Float32 opt.Float32
	Float64 opt.Float64

	Bool   opt.Bool
	String opt.String
}

var optsVanillaValue = OptsVanilla{
	Int:  opt.OInt(-123),
	Uint: opt.OUint(123),

	Int8:  opt.OInt8(math.MaxInt8),
	Int16: opt.OInt16(math.MaxInt16),
	Int32: opt.OInt32(math.MaxInt32),
	Int64: opt.OInt64(math.MaxInt64),

	Uint8:  opt.OUint8(math.MaxUint8),
	Uint16: opt.OUint16(math.MaxUint16),
	Uint32: opt.OUint32(math.MaxUint32),
	Uint64: opt.OUint64(math.MaxUint64),

	Float32: opt.OFloat32(math.MaxFloat32),
	Float64: opt.OFloat64(math.MaxFloat64),

	Bool:   opt.OBool(true),
	String: opt.OString("foo"),
}

func TestOptsVanilla(t *testing.T) {
	data, err := json.Marshal(optsVanillaValue)
	if err != nil {
		t.Errorf("Failed to marshal vanilla opts: %v", err)
	}

	var ov OptsVanilla
	if err := json.Unmarshal(data, &ov); err != nil {
		t.Errorf("Failed to unmarshal vanilla opts: %v", err)
	}

	if !reflect.DeepEqual(optsVanillaValue, ov) {
		t.Errorf("Vanilla opts unmarshal returned invalid value %+v, want %+v", ov, optsVanillaValue)
	}
}