mirror of
https://github.com/mikefarah/yq.git
synced 2024-11-12 13:48:06 +00:00
76 lines
1.7 KiB
Go
76 lines
1.7 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"runtime"
|
|
"testing"
|
|
)
|
|
|
|
var parseValueTests = []struct {
|
|
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"},
|
|
}
|
|
|
|
func TestParseValue(t *testing.T) {
|
|
for _, tt := range parseValueTests {
|
|
assertResultWithContext(t, tt.expectedResult, parseValue(tt.argument), tt.testDescription)
|
|
}
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
func TestNewYamlArray(t *testing.T) {
|
|
result, _ := newYaml([]string{"[0].cat", "meow"})
|
|
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")
|
|
}
|
|
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`
|
|
}
|
|
assertResult(t, expectedOutput, err.Error())
|
|
}
|