...

Source file src/github.com/mailru/easyjson/tests/opt_test.go

Documentation: github.com/mailru/easyjson/tests

     1  package tests
     2  
     3  import (
     4  	"math"
     5  	"reflect"
     6  	"testing"
     7  
     8  	"encoding/json"
     9  
    10  	"github.com/mailru/easyjson/opt"
    11  )
    12  
    13  // This struct type must NOT have a generated marshaler
    14  type OptsVanilla struct {
    15  	Int  opt.Int
    16  	Uint opt.Uint
    17  
    18  	Int8  opt.Int8
    19  	Int16 opt.Int16
    20  	Int32 opt.Int32
    21  	Int64 opt.Int64
    22  
    23  	Uint8  opt.Uint8
    24  	Uint16 opt.Uint16
    25  	Uint32 opt.Uint32
    26  	Uint64 opt.Uint64
    27  
    28  	Float32 opt.Float32
    29  	Float64 opt.Float64
    30  
    31  	Bool   opt.Bool
    32  	String opt.String
    33  }
    34  
    35  var optsVanillaValue = OptsVanilla{
    36  	Int:  opt.OInt(-123),
    37  	Uint: opt.OUint(123),
    38  
    39  	Int8:  opt.OInt8(math.MaxInt8),
    40  	Int16: opt.OInt16(math.MaxInt16),
    41  	Int32: opt.OInt32(math.MaxInt32),
    42  	Int64: opt.OInt64(math.MaxInt64),
    43  
    44  	Uint8:  opt.OUint8(math.MaxUint8),
    45  	Uint16: opt.OUint16(math.MaxUint16),
    46  	Uint32: opt.OUint32(math.MaxUint32),
    47  	Uint64: opt.OUint64(math.MaxUint64),
    48  
    49  	Float32: opt.OFloat32(math.MaxFloat32),
    50  	Float64: opt.OFloat64(math.MaxFloat64),
    51  
    52  	Bool:   opt.OBool(true),
    53  	String: opt.OString("foo"),
    54  }
    55  
    56  func TestOptsVanilla(t *testing.T) {
    57  	data, err := json.Marshal(optsVanillaValue)
    58  	if err != nil {
    59  		t.Errorf("Failed to marshal vanilla opts: %v", err)
    60  	}
    61  
    62  	var ov OptsVanilla
    63  	if err := json.Unmarshal(data, &ov); err != nil {
    64  		t.Errorf("Failed to unmarshal vanilla opts: %v", err)
    65  	}
    66  
    67  	if !reflect.DeepEqual(optsVanillaValue, ov) {
    68  		t.Errorf("Vanilla opts unmarshal returned invalid value %+v, want %+v", ov, optsVanillaValue)
    69  	}
    70  }
    71  

View as plain text