decred.org/dcrdex@v1.0.5/dex/marshal_test.go (about) 1 // This code is available on the terms of the project LICENSE.md file, 2 // also available online at https://blueoakcouncil.org/license/1.0.0. 3 4 package dex 5 6 import "testing" 7 8 func TestBytes_UnmarshalJSON(t *testing.T) { 9 tests := []struct { 10 name string 11 encHex string 12 wantErr bool 13 }{ 14 { 15 name: "ok", 16 encHex: `"0f0e"`, 17 wantErr: false, 18 }, 19 { 20 name: "odd, 1", 21 encHex: `"f"`, 22 wantErr: true, 23 }, 24 { 25 name: "odd, 3", 26 encHex: `"fff"`, 27 wantErr: true, 28 }, 29 { 30 name: "bad hex", 31 encHex: `"adsf"`, // s not valid hex 32 wantErr: true, 33 }, 34 { 35 name: "too short", 36 encHex: `2`, 37 wantErr: true, 38 }, 39 { 40 name: "ok empty", 41 encHex: `""`, 42 wantErr: false, 43 }, 44 { 45 name: "not quoted (also invalid hex to demo error printing)", 46 encHex: `abc 47 abc`, 48 wantErr: true, 49 }, 50 } 51 for _, tt := range tests { 52 t.Run(tt.name, func(t *testing.T) { 53 b := new(Bytes) 54 err := b.UnmarshalJSON([]byte(tt.encHex)) 55 if (err != nil) != tt.wantErr { 56 t.Errorf("Bytes.UnmarshalJSON() error = %v, wantErr %v", err, tt.wantErr) 57 } 58 }) 59 } 60 }