yq/yq_test.go

101 lines
2.3 KiB
Go
Raw Normal View History

2015-10-03 07:25:13 +00:00
package main
import (
2017-02-26 22:01:52 +00:00
"fmt"
2015-10-03 07:25:13 +00:00
"testing"
)
var parseValueTests = []struct {
2015-10-03 07:25:13 +00:00
argument string
expectedResult interface{}
testDescription string
}{
{"true", true, "boolean"},
{"\"true\"", "true", "boolean as string"},
{"3.4", 3.4, "number"},
{"\"3.4\"", "3.4", "number as string"},
{"", "", "empty string"},
2015-10-03 07:25:13 +00:00
}
func TestParseValue(t *testing.T) {
for _, tt := range parseValueTests {
assertResultWithContext(t, tt.expectedResult, parseValue(tt.argument), tt.testDescription)
2015-10-03 07:25:13 +00:00
}
}
func TestRead(t *testing.T) {
result, _ := read([]string{"examples/sample.yaml", "b.c"})
assertResult(t, 2, result)
}
2018-06-12 05:33:59 +00:00
func TestReadMulti(t *testing.T) {
docIndex = 1
result, _ := read([]string{"examples/multiple_docs.yaml", "another.document"})
assertResult(t, "here", result)
docIndex = 0
}
2017-08-03 07:30:07 +00:00
func TestReadArray(t *testing.T) {
result, _ := read([]string{"examples/sample_array.yaml", "[1]"})
2017-08-03 07:30:07 +00:00
assertResult(t, 2, result)
}
func TestReadString(t *testing.T) {
result, _ := read([]string{"examples/sample_text.yaml"})
2017-08-03 07:30:07 +00:00
assertResult(t, "hi", result)
}
2017-02-26 22:01:52 +00:00
func TestOrder(t *testing.T) {
result, _ := read([]string{"examples/order.yaml"})
formattedResult, _ := yamlToString(result)
2017-02-26 22:01:52 +00:00
assertResult(t,
`version: 3
application: MyApp`,
formattedResult)
}
func TestMultilineString(t *testing.T) {
testString := `
abcd
efg`
formattedResult, _ := yamlToString(testString)
assertResult(t, testString, formattedResult)
}
func TestNewYaml(t *testing.T) {
result, _ := newYaml([]string{"b.c", "3"})
formattedResult := fmt.Sprintf("%v", result)
assertResult(t,
"[{b [{c 3}]}]",
formattedResult)
}
2017-08-08 07:04:30 +00:00
func TestNewYamlArray(t *testing.T) {
result, _ := newYaml([]string{"[0].cat", "meow"})
2017-08-08 07:04:30 +00:00
formattedResult := fmt.Sprintf("%v", result)
assertResult(t,
"[[{cat meow}]]",
formattedResult)
}
func TestNewYaml_WithScript(t *testing.T) {
writeScript = "examples/instruction_sample.yaml"
expectedResult := `b:
c: cat
e:
- name: Mike Farah`
result, _ := newYaml([]string{""})
actualResult, _ := yamlToString(result)
assertResult(t, expectedResult, actualResult)
}
func TestNewYaml_WithUnknownScript(t *testing.T) {
writeScript = "fake-unknown"
_, err := newYaml([]string{""})
if err == nil {
t.Error("Expected error due to unknown file")
}
expectedOutput := `open fake-unknown: no such file or directory`
assertResult(t, expectedOutput, err.Error())
}