databricks-cli/libs/dyn/yamlloader/yaml_anchor_test.go

118 lines
3.8 KiB
Go
Raw Permalink Normal View History

2023-10-20 12:56:59 +00:00
package yamlloader_test
import (
"testing"
"github.com/databricks/cli/libs/dyn"
2023-10-20 12:56:59 +00:00
"github.com/stretchr/testify/assert"
)
func TestYAMLAnchor01(t *testing.T) {
file := "testdata/anchor_01.yml"
self := loadYAML(t, file)
assert.NotEqual(t, dyn.NilValue, self)
2023-10-20 12:56:59 +00:00
assert.True(t, self.Get("defaults").IsAnchor())
assert.False(t, self.Get("shirt1").IsAnchor())
assert.False(t, self.Get("shirt2").IsAnchor())
pattern := self.Get("shirt1").Get("pattern")
assert.Equal(t, "striped", pattern.AsAny())
assert.Equal(t, dyn.Location{File: file, Line: 8, Column: 12}, pattern.Location())
2023-10-20 12:56:59 +00:00
}
func TestYAMLAnchor02(t *testing.T) {
file := "testdata/anchor_02.yml"
self := loadYAML(t, file)
assert.NotEqual(t, dyn.NilValue, self)
2023-10-20 12:56:59 +00:00
color := self.Get("shirt").Get("color")
assert.Equal(t, "red", color.AsAny())
assert.Equal(t, dyn.Location{File: file, Line: 4, Column: 10}, color.Location())
2023-10-20 12:56:59 +00:00
primary := self.Get("shirt").Get("primary")
assert.Equal(t, "cotton", primary.AsAny())
assert.Equal(t, dyn.Location{File: file, Line: 8, Column: 12}, primary.Location())
2023-10-20 12:56:59 +00:00
pattern := self.Get("shirt").Get("pattern")
assert.Equal(t, "striped", pattern.AsAny())
assert.Equal(t, dyn.Location{File: file, Line: 13, Column: 12}, pattern.Location())
2023-10-20 12:56:59 +00:00
}
func TestYAMLAnchor03(t *testing.T) {
file := "testdata/anchor_03.yml"
self := loadYAML(t, file)
assert.NotEqual(t, dyn.NilValue, self)
2023-10-20 12:56:59 +00:00
// Assert the override took place.
blue := self.Get("shirt").Get("color")
assert.Equal(t, "blue", blue.AsAny())
assert.Equal(t, file, blue.Location().File)
assert.Equal(t, 10, blue.Location().Line)
assert.Equal(t, 10, blue.Location().Column)
}
func TestYAMLAnchor04(t *testing.T) {
file := "testdata/anchor_04.yml"
self := loadYAML(t, file)
assert.NotEqual(t, dyn.NilValue, self)
2023-10-20 12:56:59 +00:00
p1 := self.Get("person1").Get("address").Get("city")
assert.Equal(t, "San Francisco", p1.AsAny())
assert.Equal(t, dyn.Location{File: file, Line: 4, Column: 9}, p1.Location())
2023-10-20 12:56:59 +00:00
p2 := self.Get("person2").Get("address").Get("city")
assert.Equal(t, "Los Angeles", p2.AsAny())
assert.Equal(t, dyn.Location{File: file, Line: 16, Column: 11}, p2.Location())
2023-10-20 12:56:59 +00:00
}
func TestYAMLAnchor05(t *testing.T) {
file := "testdata/anchor_05.yml"
self := loadYAML(t, file)
assert.NotEqual(t, dyn.NilValue, self)
2023-10-20 12:56:59 +00:00
features := self.Get("phone1").Get("features")
assert.Equal(t, "wifi", features.Index(0).AsAny())
assert.Equal(t, dyn.Location{File: file, Line: 4, Column: 5}, features.Index(0).Location())
2023-10-20 12:56:59 +00:00
assert.Equal(t, "bluetooth", features.Index(1).AsAny())
assert.Equal(t, dyn.Location{File: file, Line: 5, Column: 5}, features.Index(1).Location())
2023-10-20 12:56:59 +00:00
}
func TestYAMLAnchor06(t *testing.T) {
file := "testdata/anchor_06.yml"
self := loadYAML(t, file)
assert.NotEqual(t, dyn.NilValue, self)
2023-10-20 12:56:59 +00:00
greeting := self.Get("greeting1")
assert.Equal(t, "Hello, World!", greeting.AsAny())
assert.Equal(t, dyn.Location{File: file, Line: 2, Column: 16}, greeting.Location())
2023-10-20 12:56:59 +00:00
}
func TestYAMLAnchor07(t *testing.T) {
file := "testdata/anchor_07.yml"
self := loadYAML(t, file)
assert.NotEqual(t, dyn.NilValue, self)
2023-10-20 12:56:59 +00:00
name := self.Get("person1").Get("name")
assert.Equal(t, "Alice", name.AsAny())
assert.Equal(t, dyn.Location{File: file, Line: 5, Column: 9}, name.Location())
2023-10-20 12:56:59 +00:00
age := self.Get("person1").Get("age")
assert.Equal(t, 25, age.AsAny())
assert.Equal(t, dyn.Location{File: file, Line: 2, Column: 13}, age.Location())
2023-10-20 12:56:59 +00:00
}
func TestYAMLAnchor08(t *testing.T) {
file := "testdata/anchor_08.yml"
self := loadYAML(t, file)
assert.NotEqual(t, dyn.NilValue, self)
2023-10-20 12:56:59 +00:00
username := self.Get("user1").Get("username")
assert.Equal(t, "user1", username.AsAny())
assert.Equal(t, dyn.Location{File: file, Line: 5, Column: 13}, username.Location())
2023-10-20 12:56:59 +00:00
active := self.Get("user1").Get("active")
assert.Equal(t, true, active.AsAny())
assert.Equal(t, dyn.Location{File: file, Line: 2, Column: 11}, active.Location())
2023-10-20 12:56:59 +00:00
}