test(encoding): fix failing tests due to the yaml library update

Signed-off-by: Mark Sagi-Kazar <mark.sagikazar@gmail.com>
This commit is contained in:
Mark Sagi-Kazar 2022-01-01 17:02:57 +01:00 committed by Márk Sági-Kazár
parent f8a13cf704
commit 98c10c3c31
6 changed files with 343 additions and 136 deletions

View file

@ -5,93 +5,6 @@ import (
"testing" "testing"
) )
// original form of the data
const original = `# key-value pair
key: value
list:
- item1
- item2
- item3
map:
key: value
# nested
# map
nested_map:
map:
key: value
list:
- item1
- item2
- item3
`
// encoded form of the data
const encoded = `key: value
list:
- item1
- item2
- item3
map:
key: value
nested_map:
map:
key: value
list:
- item1
- item2
- item3
`
// decoded form of the data
//
// in case of YAML it's slightly different from Viper's internal representation
// (eg. map is decoded into a map with interface key)
var decoded = map[string]interface{}{
"key": "value",
"list": []interface{}{
"item1",
"item2",
"item3",
},
"map": map[interface{}]interface{}{
"key": "value",
},
"nested_map": map[interface{}]interface{}{
"map": map[interface{}]interface{}{
"key": "value",
"list": []interface{}{
"item1",
"item2",
"item3",
},
},
},
}
// Viper's internal representation
var data = map[string]interface{}{
"key": "value",
"list": []interface{}{
"item1",
"item2",
"item3",
},
"map": map[string]interface{}{
"key": "value",
},
"nested_map": map[string]interface{}{
"map": map[string]interface{}{
"key": "value",
"list": []interface{}{
"item1",
"item2",
"item3",
},
},
},
}
func TestCodec_Encode(t *testing.T) { func TestCodec_Encode(t *testing.T) {
codec := Codec{} codec := Codec{}

View file

@ -0,0 +1,91 @@
//go:build !viper_yaml3
// +build !viper_yaml3
package yaml
// original form of the data
const original = `# key-value pair
key: value
list:
- item1
- item2
- item3
map:
key: value
# nested
# map
nested_map:
map:
key: value
list:
- item1
- item2
- item3
`
// encoded form of the data
const encoded = `key: value
list:
- item1
- item2
- item3
map:
key: value
nested_map:
map:
key: value
list:
- item1
- item2
- item3
`
// decoded form of the data
//
// in case of YAML it's slightly different from Viper's internal representation
// (eg. map is decoded into a map with interface key)
var decoded = map[string]interface{}{
"key": "value",
"list": []interface{}{
"item1",
"item2",
"item3",
},
"map": map[interface{}]interface{}{
"key": "value",
},
"nested_map": map[interface{}]interface{}{
"map": map[interface{}]interface{}{
"key": "value",
"list": []interface{}{
"item1",
"item2",
"item3",
},
},
},
}
// Viper's internal representation
var data = map[string]interface{}{
"key": "value",
"list": []interface{}{
"item1",
"item2",
"item3",
},
"map": map[string]interface{}{
"key": "value",
},
"nested_map": map[string]interface{}{
"map": map[string]interface{}{
"key": "value",
"list": []interface{}{
"item1",
"item2",
"item3",
},
},
},
}

View file

@ -0,0 +1,91 @@
//go:build viper_yaml3
// +build viper_yaml3
package yaml
// original form of the data
const original = `# key-value pair
key: value
list:
- item1
- item2
- item3
map:
key: value
# nested
# map
nested_map:
map:
key: value
list:
- item1
- item2
- item3
`
// encoded form of the data
const encoded = `key: value
list:
- item1
- item2
- item3
map:
key: value
nested_map:
map:
key: value
list:
- item1
- item2
- item3
`
// decoded form of the data
//
// in case of YAML it's slightly different from Viper's internal representation
// (eg. map is decoded into a map with interface key)
var decoded = map[string]interface{}{
"key": "value",
"list": []interface{}{
"item1",
"item2",
"item3",
},
"map": map[string]interface{}{
"key": "value",
},
"nested_map": map[string]interface{}{
"map": map[string]interface{}{
"key": "value",
"list": []interface{}{
"item1",
"item2",
"item3",
},
},
},
}
// Viper's internal representation
var data = map[string]interface{}{
"key": "value",
"list": []interface{}{
"item1",
"item2",
"item3",
},
"map": map[string]interface{}{
"key": "value",
},
"nested_map": map[string]interface{}{
"map": map[string]interface{}{
"key": "value",
"list": []interface{}{
"item1",
"item2",
"item3",
},
},
},
}

View file

@ -33,21 +33,21 @@ import (
"github.com/spf13/viper/internal/testutil" "github.com/spf13/viper/internal/testutil"
) )
var yamlExample = []byte(`Hacker: true // var yamlExample = []byte(`Hacker: true
name: steve // name: steve
hobbies: // hobbies:
- skateboarding // - skateboarding
- snowboarding // - snowboarding
- go // - go
clothing: // clothing:
jacket: leather // jacket: leather
trousers: denim // trousers: denim
pants: // pants:
size: large // size: large
age: 35 // age: 35
eyes : brown // eyes : brown
beard: true // beard: true
`) // `)
var yamlExampleWithExtras = []byte(`Existing: true var yamlExampleWithExtras = []byte(`Existing: true
Bogus: true Bogus: true
@ -1558,21 +1558,21 @@ p_ppu = 0.55
p_batters.batter.type = Regular p_batters.batter.type = Regular
`) `)
var yamlWriteExpected = []byte(`age: 35 // var yamlWriteExpected = []byte(`age: 35
beard: true // beard: true
clothing: // clothing:
jacket: leather // jacket: leather
pants: // pants:
size: large // size: large
trousers: denim // trousers: denim
eyes: brown // eyes: brown
hacker: true // hacker: true
hobbies: // hobbies:
- skateboarding // - skateboarding
- snowboarding // - snowboarding
- go // - go
name: steve // name: steve
`) // `)
func TestWriteConfig(t *testing.T) { func TestWriteConfig(t *testing.T) {
fs := afero.NewMemMapFs() fs := afero.NewMemMapFs()
@ -2410,25 +2410,25 @@ func TestUnmarshal_DotSeparatorBackwardCompatibility(t *testing.T) {
assert.Equal(t, "cobra_flag", config.Foo.Bar) assert.Equal(t, "cobra_flag", config.Foo.Bar)
} }
var yamlExampleWithDot = []byte(`Hacker: true // var yamlExampleWithDot = []byte(`Hacker: true
name: steve // name: steve
hobbies: // hobbies:
- skateboarding // - skateboarding
- snowboarding // - snowboarding
- go // - go
clothing: // clothing:
jacket: leather // jacket: leather
trousers: denim // trousers: denim
pants: // pants:
size: large // size: large
age: 35 // age: 35
eyes : brown // eyes : brown
beard: true // beard: true
emails: // emails:
steve@hacker.com: // steve@hacker.com:
created: 01/02/03 // created: 01/02/03
active: true // active: true
`) // `)
func TestKeyDelimiter(t *testing.T) { func TestKeyDelimiter(t *testing.T) {
v := NewWithOptions(KeyDelimiter("::")) v := NewWithOptions(KeyDelimiter("::"))

56
viper_yaml2_test.go Normal file
View file

@ -0,0 +1,56 @@
//go:build !viper_yaml3
// +build !viper_yaml3
package viper
var yamlExample = []byte(`Hacker: true
name: steve
hobbies:
- skateboarding
- snowboarding
- go
clothing:
jacket: leather
trousers: denim
pants:
size: large
age: 35
eyes : brown
beard: true
`)
var yamlWriteExpected = []byte(`age: 35
beard: true
clothing:
jacket: leather
pants:
size: large
trousers: denim
eyes: brown
hacker: true
hobbies:
- skateboarding
- snowboarding
- go
name: steve
`)
var yamlExampleWithDot = []byte(`Hacker: true
name: steve
hobbies:
- skateboarding
- snowboarding
- go
clothing:
jacket: leather
trousers: denim
pants:
size: large
age: 35
eyes : brown
beard: true
emails:
steve@hacker.com:
created: 01/02/03
active: true
`)

56
viper_yaml3_test.go Normal file
View file

@ -0,0 +1,56 @@
//go:build viper_yaml3
// +build viper_yaml3
package viper
var yamlExample = []byte(`Hacker: true
name: steve
hobbies:
- skateboarding
- snowboarding
- go
clothing:
jacket: leather
trousers: denim
pants:
size: large
age: 35
eyes : brown
beard: true
`)
var yamlWriteExpected = []byte(`age: 35
beard: true
clothing:
jacket: leather
pants:
size: large
trousers: denim
eyes: brown
hacker: true
hobbies:
- skateboarding
- snowboarding
- go
name: steve
`)
var yamlExampleWithDot = []byte(`Hacker: true
name: steve
hobbies:
- skateboarding
- snowboarding
- go
clothing:
jacket: leather
trousers: denim
pants:
size: large
age: 35
eyes : brown
beard: true
emails:
steve@hacker.com:
created: 01/02/03
active: true
`)