github.com/DoNewsCode/core@v0.12.3/codec/yaml/yaml_test.go (about) 1 package yaml 2 3 import ( 4 "math" 5 "reflect" 6 "testing" 7 ) 8 9 func TestCodec_Unmarshal(t *testing.T) { 10 tests := []struct { 11 data string 12 value interface{} 13 }{ 14 { 15 "", 16 (*struct{})(nil), 17 }, 18 { 19 "{}", &struct{}{}, 20 }, 21 { 22 "v: hi", 23 map[string]string{"v": "hi"}, 24 }, 25 { 26 "v: hi", map[string]interface{}{"v": "hi"}, 27 }, 28 { 29 "v: true", 30 map[string]string{"v": "true"}, 31 }, 32 { 33 "v: true", 34 map[string]interface{}{"v": true}, 35 }, 36 { 37 "v: 10", 38 map[string]interface{}{"v": 10}, 39 }, 40 { 41 "v: 0b10", 42 map[string]interface{}{"v": 2}, 43 }, 44 { 45 "v: 0xA", 46 map[string]interface{}{"v": 10}, 47 }, 48 { 49 "v: 4294967296", 50 map[string]int64{"v": 4294967296}, 51 }, 52 { 53 "v: 0.1", 54 map[string]interface{}{"v": 0.1}, 55 }, 56 { 57 "v: .1", 58 map[string]interface{}{"v": 0.1}, 59 }, 60 { 61 "v: .Inf", 62 map[string]interface{}{"v": math.Inf(+1)}, 63 }, 64 { 65 "v: -.Inf", 66 map[string]interface{}{"v": math.Inf(-1)}, 67 }, 68 { 69 "v: -10", 70 map[string]interface{}{"v": -10}, 71 }, 72 { 73 "v: -.1", 74 map[string]interface{}{"v": -0.1}, 75 }, 76 } 77 for _, tt := range tests { 78 v := reflect.ValueOf(tt.value).Type() 79 value := reflect.New(v) 80 err := (Codec{}).Unmarshal([]byte(tt.data), value.Interface()) 81 if err != nil { 82 t.Fatalf("(codec{}).Unmarshal should not return err") 83 } 84 } 85 spec := struct { 86 A string 87 B map[string]interface{} 88 }{A: "a"} 89 err := (Codec{}).Unmarshal([]byte("v: hi"), &spec.B) 90 if err != nil { 91 t.Fatalf("(codec{}).Unmarshal should not return err") 92 } 93 } 94 95 func TestCodec_Marshal(t *testing.T) { 96 value := map[string]string{"v": "hi"} 97 got, err := (Codec{}).Marshal(value) 98 if err != nil { 99 t.Fatalf("should not return err") 100 } 101 if string(got) != "v: hi\n" { 102 t.Fatalf("want \"v: hi\n\" return \"%s\"", string(got)) 103 } 104 }