github.com/tomwright/dasel@v1.27.3/storage/yaml_test.go (about) 1 package storage_test 2 3 import ( 4 "github.com/tomwright/dasel/storage" 5 "reflect" 6 "strings" 7 "testing" 8 ) 9 10 var yamlBytes = []byte(`name: Tom 11 numbers: 12 - 1 13 - 2 14 `) 15 var yamlMap = map[string]interface{}{ 16 "name": "Tom", 17 "numbers": []interface{}{ 18 1, 19 2, 20 }, 21 } 22 23 var yamlBytesMulti = []byte(`name: Tom 24 --- 25 name: Jim 26 `) 27 var yamlMapMulti = []interface{}{ 28 map[string]interface{}{ 29 "name": "Tom", 30 }, 31 map[string]interface{}{ 32 "name": "Jim", 33 }, 34 } 35 36 func TestYAMLParser_FromBytes(t *testing.T) { 37 t.Run("Valid", func(t *testing.T) { 38 got, err := (&storage.YAMLParser{}).FromBytes(yamlBytes) 39 if err != nil { 40 t.Errorf("unexpected error: %s", err) 41 return 42 } 43 exp := &storage.BasicSingleDocument{Value: yamlMap} 44 if !reflect.DeepEqual(exp, got) { 45 t.Errorf("expected %v, got %v", exp, got) 46 } 47 }) 48 t.Run("ValidMultiDocument", func(t *testing.T) { 49 got, err := (&storage.YAMLParser{}).FromBytes(yamlBytesMulti) 50 if err != nil { 51 t.Errorf("unexpected error: %s", err) 52 return 53 } 54 exp := &storage.BasicMultiDocument{Values: yamlMapMulti} 55 56 if !reflect.DeepEqual(exp, got) { 57 t.Errorf("expected %v, got %v", exp, got) 58 } 59 }) 60 t.Run("Invalid", func(t *testing.T) { 61 _, err := (&storage.YAMLParser{}).FromBytes([]byte(`{1:asd`)) 62 if err == nil || !strings.Contains(err.Error(), "could not unmarshal data") { 63 t.Errorf("unexpected error: %v", err) 64 return 65 } 66 }) 67 t.Run("Empty", func(t *testing.T) { 68 got, err := (&storage.YAMLParser{}).FromBytes([]byte(``)) 69 if err != nil { 70 t.Errorf("unexpected error: %s", err) 71 return 72 } 73 if !reflect.DeepEqual(nil, got) { 74 t.Errorf("expected %v, got %v", nil, got) 75 } 76 }) 77 } 78 79 func TestYAMLParser_ToBytes(t *testing.T) { 80 t.Run("Valid", func(t *testing.T) { 81 got, err := (&storage.YAMLParser{}).ToBytes(yamlMap) 82 if err != nil { 83 t.Errorf("unexpected error: %s", err) 84 return 85 } 86 if string(yamlBytes) != string(got) { 87 t.Errorf("expected %s, got %s", yamlBytes, got) 88 } 89 }) 90 t.Run("ValidSingle", func(t *testing.T) { 91 got, err := (&storage.YAMLParser{}).ToBytes(&storage.BasicSingleDocument{Value: yamlMap}) 92 if err != nil { 93 t.Errorf("unexpected error: %s", err) 94 return 95 } 96 if string(yamlBytes) != string(got) { 97 t.Errorf("expected %s, got %s", yamlBytes, got) 98 } 99 }) 100 t.Run("ValidSingleColourise", func(t *testing.T) { 101 got, err := (&storage.YAMLParser{}).ToBytes(&storage.BasicSingleDocument{Value: yamlMap}, storage.ColouriseOption(true)) 102 if err != nil { 103 t.Errorf("unexpected error: %s", err) 104 return 105 } 106 expBuf, _ := storage.Colourise(string(yamlBytes), "yaml") 107 exp := expBuf.Bytes() 108 if !reflect.DeepEqual(exp, got) { 109 t.Errorf("expected %v, got %v", exp, got) 110 } 111 }) 112 t.Run("ValidMulti", func(t *testing.T) { 113 got, err := (&storage.YAMLParser{}).ToBytes(&storage.BasicMultiDocument{Values: yamlMapMulti}) 114 if err != nil { 115 t.Errorf("unexpected error: %s", err) 116 return 117 } 118 if string(yamlBytesMulti) != string(got) { 119 t.Errorf("expected %s, got %s", yamlBytesMulti, got) 120 } 121 }) 122 }