yq/pkg/yqlib/lib_test.go

163 lines
3.1 KiB
Go
Raw Normal View History

package yqlib
2022-10-30 05:29:42 +00:00
import (
"fmt"
2022-10-30 05:29:42 +00:00
"testing"
"github.com/mikefarah/yq/v4/test"
)
func TestGetLogger(t *testing.T) {
l := GetLogger()
if l != log {
t.Fatal("GetLogger should return the yq logger instance, not a copy")
}
}
2022-10-30 05:29:42 +00:00
type parseSnippetScenario struct {
2022-11-09 10:46:00 +00:00
snippet string
expected *CandidateNode
2022-11-09 10:46:00 +00:00
expectedError string
2022-10-30 05:29:42 +00:00
}
var parseSnippetScenarios = []parseSnippetScenario{
2022-11-09 10:46:00 +00:00
{
snippet: ":",
expectedError: "yaml: did not find expected key",
},
2022-10-30 05:29:42 +00:00
{
snippet: "",
expected: &CandidateNode{
Kind: ScalarNode,
2022-10-30 05:29:42 +00:00
Tag: "!!null",
},
},
2022-10-31 22:15:59 +00:00
{
snippet: "null",
expected: &CandidateNode{
Kind: ScalarNode,
2022-10-31 22:15:59 +00:00
Tag: "!!null",
Value: "null",
2022-11-15 00:35:31 +00:00
Line: 0,
Column: 0,
2022-10-31 22:15:59 +00:00
},
},
2022-10-30 05:29:42 +00:00
{
snippet: "3",
expected: &CandidateNode{
Kind: ScalarNode,
2022-10-30 05:29:42 +00:00
Tag: "!!int",
Value: "3",
2022-11-15 00:35:31 +00:00
Line: 0,
Column: 0,
2022-10-30 05:29:42 +00:00
},
},
{
snippet: "cat",
expected: &CandidateNode{
Kind: ScalarNode,
2022-10-30 05:29:42 +00:00
Tag: "!!str",
Value: "cat",
2022-11-15 00:35:31 +00:00
Line: 0,
Column: 0,
2022-10-30 05:29:42 +00:00
},
},
{
snippet: "# things",
expected: &CandidateNode{
Kind: ScalarNode,
Tag: "!!null",
LineComment: "# things",
Line: 0,
Column: 0,
},
},
2022-10-30 05:29:42 +00:00
{
snippet: "3.1",
expected: &CandidateNode{
Kind: ScalarNode,
2022-10-30 05:29:42 +00:00
Tag: "!!float",
Value: "3.1",
2022-11-15 00:35:31 +00:00
Line: 0,
Column: 0,
2022-10-30 05:29:42 +00:00
},
},
{
snippet: "true",
expected: &CandidateNode{
Kind: ScalarNode,
2022-10-30 05:29:42 +00:00
Tag: "!!bool",
Value: "true",
2022-11-15 00:35:31 +00:00
Line: 0,
Column: 0,
2022-10-30 05:29:42 +00:00
},
},
}
func TestParseSnippet(t *testing.T) {
for _, tt := range parseSnippetScenarios {
actual, err := parseSnippet(tt.snippet)
2022-11-09 10:46:00 +00:00
if tt.expectedError != "" {
if err == nil {
t.Errorf("Expected error '%v' but it worked!", tt.expectedError)
} else {
test.AssertResultComplexWithContext(t, tt.expectedError, err.Error(), tt.snippet)
}
continue
2022-11-09 10:46:00 +00:00
}
2022-10-30 05:29:42 +00:00
if err != nil {
t.Error(tt.snippet)
t.Error(err)
}
test.AssertResultComplexWithContext(t, tt.expected, actual, tt.snippet)
}
}
type parseInt64Scenario struct {
numberString string
expectedParsedNumber int64
2024-06-16 01:05:27 +00:00
expectedFormatString string
}
var parseInt64Scenarios = []parseInt64Scenario{
{
numberString: "34",
expectedParsedNumber: 34,
},
2024-06-16 01:05:27 +00:00
{
numberString: "10_000",
expectedParsedNumber: 10000,
expectedFormatString: "10000",
},
{
numberString: "0x10",
expectedParsedNumber: 16,
},
2024-06-16 01:05:27 +00:00
{
numberString: "0x10_000",
expectedParsedNumber: 65536,
expectedFormatString: "0x10000",
},
{
numberString: "0o10",
expectedParsedNumber: 8,
},
}
func TestParseInt64(t *testing.T) {
for _, tt := range parseInt64Scenarios {
format, actualNumber, err := parseInt64(tt.numberString)
if err != nil {
t.Error(tt.numberString)
t.Error(err)
}
test.AssertResultComplexWithContext(t, tt.expectedParsedNumber, actualNumber, tt.numberString)
2024-06-16 01:05:27 +00:00
if tt.expectedFormatString == "" {
tt.expectedFormatString = tt.numberString
}
2024-06-16 01:05:27 +00:00
test.AssertResultComplexWithContext(t, tt.expectedFormatString, fmt.Sprintf(format, actualNumber), fmt.Sprintf("Formatting of: %v", tt.numberString))
}
}