2015-10-03 07:25:13 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-02-26 22:01:52 +00:00
|
|
|
"fmt"
|
2019-01-03 13:11:47 +00:00
|
|
|
"runtime"
|
2015-10-03 07:25:13 +00:00
|
|
|
"testing"
|
2019-11-23 03:52:29 +00:00
|
|
|
"github.com/mikefarah/yq/test"
|
|
|
|
"github.com/mikefarah/yq/pkg/yqlib"
|
2015-10-03 07:25:13 +00:00
|
|
|
)
|
|
|
|
|
2015-10-07 23:31:31 +00:00
|
|
|
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"},
|
2017-06-06 00:19:32 +00:00
|
|
|
{"", "", "empty string"},
|
2015-10-03 07:25:13 +00:00
|
|
|
}
|
|
|
|
|
2015-10-07 23:31:31 +00:00
|
|
|
func TestParseValue(t *testing.T) {
|
|
|
|
for _, tt := range parseValueTests {
|
2019-11-23 03:52:29 +00:00
|
|
|
test.AssertResultWithContext(t, tt.expectedResult, parseValue(tt.argument), tt.testDescription)
|
2015-10-03 07:25:13 +00:00
|
|
|
}
|
|
|
|
}
|
2015-10-09 10:24:42 +00:00
|
|
|
|
2018-03-27 05:22:24 +00:00
|
|
|
func TestMultilineString(t *testing.T) {
|
|
|
|
testString := `
|
|
|
|
abcd
|
|
|
|
efg`
|
2019-11-23 03:52:29 +00:00
|
|
|
formattedResult, _ := yqlib.YamlToString(testString, false)
|
|
|
|
test.AssertResult(t, testString, formattedResult)
|
2018-03-27 05:22:24 +00:00
|
|
|
}
|
|
|
|
|
2017-04-12 11:10:00 +00:00
|
|
|
func TestNewYaml(t *testing.T) {
|
2017-09-22 19:58:12 +00:00
|
|
|
result, _ := newYaml([]string{"b.c", "3"})
|
2017-04-12 11:10:00 +00:00
|
|
|
formattedResult := fmt.Sprintf("%v", result)
|
2019-11-23 03:52:29 +00:00
|
|
|
test.AssertResult(t,
|
2017-04-12 11:10:00 +00:00
|
|
|
"[{b [{c 3}]}]",
|
|
|
|
formattedResult)
|
|
|
|
}
|
|
|
|
|
2017-08-08 07:04:30 +00:00
|
|
|
func TestNewYamlArray(t *testing.T) {
|
2017-09-22 19:58:12 +00:00
|
|
|
result, _ := newYaml([]string{"[0].cat", "meow"})
|
2017-08-08 07:04:30 +00:00
|
|
|
formattedResult := fmt.Sprintf("%v", result)
|
2019-11-23 03:52:29 +00:00
|
|
|
test.AssertResult(t,
|
2017-08-08 07:04:30 +00:00
|
|
|
"[[{cat meow}]]",
|
|
|
|
formattedResult)
|
|
|
|
}
|
|
|
|
|
2017-04-12 11:10:00 +00:00
|
|
|
func TestNewYaml_WithScript(t *testing.T) {
|
2017-09-20 23:40:33 +00:00
|
|
|
writeScript = "examples/instruction_sample.yaml"
|
2017-04-14 02:56:50 +00:00
|
|
|
expectedResult := `b:
|
|
|
|
c: cat
|
|
|
|
e:
|
|
|
|
- name: Mike Farah`
|
2017-09-22 19:58:12 +00:00
|
|
|
result, _ := newYaml([]string{""})
|
2019-11-23 03:52:29 +00:00
|
|
|
actualResult, _ := yqlib.YamlToString(result, true)
|
|
|
|
test.AssertResult(t, expectedResult, actualResult)
|
2017-04-12 11:10:00 +00:00
|
|
|
}
|
2017-09-22 19:58:12 +00:00
|
|
|
|
|
|
|
func TestNewYaml_WithUnknownScript(t *testing.T) {
|
|
|
|
writeScript = "fake-unknown"
|
|
|
|
_, err := newYaml([]string{""})
|
|
|
|
if err == nil {
|
|
|
|
t.Error("Expected error due to unknown file")
|
|
|
|
}
|
2019-01-03 13:11:47 +00:00
|
|
|
var expectedOutput string
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
expectedOutput = `open fake-unknown: The system cannot find the file specified.`
|
|
|
|
} else {
|
|
|
|
expectedOutput = `open fake-unknown: no such file or directory`
|
|
|
|
}
|
2019-11-23 03:52:29 +00:00
|
|
|
test.AssertResult(t, expectedOutput, err.Error())
|
2017-09-22 19:58:12 +00:00
|
|
|
}
|