1package yaml
2
3import (
4	"fmt"
5	"math"
6
7	. "github.com/onsi/ginkgo"
8	. "github.com/onsi/gomega"
9)
10
11var _ = Describe("finding paths", func() {
12
13	Describe("Find", func() {
14
15		It("fails when the path is not found", func() {
16			tree := parseYAML(`
17---
18foo: bar
19`)
20
21			_, found := Find(tree, "foo", "bar", "biscuit")
22			Expect(found).To(BeFalse())
23		})
24
25		Context("when the node tree contains maps of maps", func() {
26			tree := parseYAML(`
27---
28foo:
29  bar:
30    baz: found
31`)
32
33			It("accepts string keys to index maps", func() {
34				val, found := Find(tree, "foo", "bar", "baz")
35				Expect(found).To(BeTrue())
36				Expect(val).To(Equal(node("found")))
37			})
38		})
39
40		Context("when the node tree contains maps of lists of maps", func() {
41			tree := parseYAML(`
42---
43foo:
44  bar:
45    - buzz: wrong
46    - fizz: right
47`)
48
49			It("accepts [x] to index lists", func() {
50				val, found := Find(tree, "foo", "bar", "[1]", "fizz")
51				Expect(found).To(BeTrue())
52				Expect(val).To(Equal(node("right")))
53			})
54		})
55
56	})
57
58	Describe("FindString", func() {
59		tree := parseYAML(`
60---
61foo: a string
62bar: 42
63`)
64
65		Context("when the found node is a string", func() {
66			It("returns the string and true", func() {
67				found, ok := FindString(tree, "foo")
68				Expect(ok).To(BeTrue())
69				Expect(found).To(Equal("a string"))
70			})
71		})
72
73		Context("when the found node is NOT a string", func() {
74			It("returns false", func() {
75				_, ok := FindString(tree, "bar")
76				Expect(ok).To(BeFalse())
77			})
78		})
79
80		Context("when the node is not found", func() {
81			It("returns false", func() {
82				_, ok := FindString(tree, "baz")
83				Expect(ok).To(BeFalse())
84			})
85		})
86	})
87
88	Describe("FindInt", func() {
89
90		Context("when the found node is an int", func() {
91			intValue := 64
92			tree := parseYAML(fmt.Sprintf(`
93---
94foo: %d
95bar: a string
96`, intValue))
97
98			It("returns the value and true", func() {
99				found, ok := FindInt(tree, "foo")
100				Expect(ok).To(BeTrue())
101				Expect(found).To(BeNumerically("==", intValue))
102			})
103		})
104
105		Context("when the found node is a int64", func() {
106			int64Value := int64(math.MaxInt32) + 1
107			tree := parseYAML(fmt.Sprintf(`
108---
109foo: %d
110bar: a string
111`, int64Value))
112
113			It("returns the value and true", func() {
114				found, ok := FindInt(tree, "foo")
115				Expect(ok).To(BeTrue())
116				Expect(found).To(BeNumerically("==", int64Value))
117			})
118		})
119
120		Context("when the found node is NOT an int", func() {
121			tree := parseYAML(`
122---
123foo: bar
124`)
125			It("returns false", func() {
126				_, ok := FindInt(tree, "foo")
127				Expect(ok).To(BeFalse())
128			})
129		})
130
131		Context("when the node is not found", func() {
132			tree := parseYAML(`
133---
134foo: bar
135`)
136			It("returns false", func() {
137				_, ok := FindInt(tree, "baz")
138				Expect(ok).To(BeFalse())
139			})
140		})
141	})
142})
143