Fixing tests

This commit is contained in:
Mike Farah 2023-10-17 20:12:41 +11:00
parent 2356ea0942
commit c8f35d912d
3 changed files with 61 additions and 24 deletions

View File

@ -291,26 +291,22 @@ func recursiveNodeEqual(lhs *CandidateNode, rhs *CandidateNode) bool {
return false
}
// yaml numbers can be hex encoded...
// yaml numbers can be hex and octal encoded...
func parseInt64(numberString string) (string, int64, error) {
if strings.HasPrefix(numberString, "0x") ||
strings.HasPrefix(numberString, "0X") {
num, err := strconv.ParseInt(numberString[2:], 16, 64)
return "0x%X", num, err
} else if strings.HasPrefix(numberString, "0o") {
num, err := strconv.ParseInt(numberString[2:], 8, 64)
return "0o%o", num, err
}
num, err := strconv.ParseInt(numberString, 10, 64)
return "%v", num, err
}
func parseInt(numberString string) (int, error) {
var err error
var parsed int64
if strings.HasPrefix(numberString, "0x") ||
strings.HasPrefix(numberString, "0X") {
parsed, err = strconv.ParseInt(numberString[2:], 16, 64)
} else {
parsed, err = strconv.ParseInt(numberString, 10, 64)
}
_, parsed, err := parseInt64(numberString)
if err != nil {
return 0, err

View File

@ -1,10 +1,10 @@
package yqlib
import (
"fmt"
"testing"
"github.com/mikefarah/yq/v4/test"
yaml "gopkg.in/yaml.v3"
)
func TestGetLogger(t *testing.T) {
@ -16,7 +16,7 @@ func TestGetLogger(t *testing.T) {
type parseSnippetScenario struct {
snippet string
expected *yaml.Node
expected *CandidateNode
expectedError string
}
@ -27,15 +27,15 @@ var parseSnippetScenarios = []parseSnippetScenario{
},
{
snippet: "",
expected: &yaml.Node{
Kind: yaml.ScalarNode,
expected: &CandidateNode{
Kind: ScalarNode,
Tag: "!!null",
},
},
{
snippet: "null",
expected: &yaml.Node{
Kind: yaml.ScalarNode,
expected: &CandidateNode{
Kind: ScalarNode,
Tag: "!!null",
Value: "null",
Line: 0,
@ -44,8 +44,8 @@ var parseSnippetScenarios = []parseSnippetScenario{
},
{
snippet: "3",
expected: &yaml.Node{
Kind: yaml.ScalarNode,
expected: &CandidateNode{
Kind: ScalarNode,
Tag: "!!int",
Value: "3",
Line: 0,
@ -54,8 +54,8 @@ var parseSnippetScenarios = []parseSnippetScenario{
},
{
snippet: "cat",
expected: &yaml.Node{
Kind: yaml.ScalarNode,
expected: &CandidateNode{
Kind: ScalarNode,
Tag: "!!str",
Value: "cat",
Line: 0,
@ -64,8 +64,8 @@ var parseSnippetScenarios = []parseSnippetScenario{
},
{
snippet: "3.1",
expected: &yaml.Node{
Kind: yaml.ScalarNode,
expected: &CandidateNode{
Kind: ScalarNode,
Tag: "!!float",
Value: "3.1",
Line: 0,
@ -74,8 +74,8 @@ var parseSnippetScenarios = []parseSnippetScenario{
},
{
snippet: "true",
expected: &yaml.Node{
Kind: yaml.ScalarNode,
expected: &CandidateNode{
Kind: ScalarNode,
Tag: "!!bool",
Value: "true",
Line: 0,
@ -93,7 +93,7 @@ func TestParseSnippet(t *testing.T) {
} else {
test.AssertResultComplexWithContext(t, tt.expectedError, err.Error(), tt.snippet)
}
return
continue
}
if err != nil {
t.Error(tt.snippet)
@ -102,3 +102,37 @@ func TestParseSnippet(t *testing.T) {
test.AssertResultComplexWithContext(t, tt.expected, actual, tt.snippet)
}
}
type parseInt64Scenario struct {
numberString string
expectedParsedNumber int64
}
var parseInt64Scenarios = []parseInt64Scenario{
{
numberString: "34",
expectedParsedNumber: 34,
},
{
numberString: "0x10",
expectedParsedNumber: 16,
},
{
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)
test.AssertResultComplexWithContext(t, tt.numberString, fmt.Sprintf(format, actualNumber), fmt.Sprintf("Formatting of: %v", tt.numberString))
}
}

View File

@ -19,6 +19,13 @@ var yamlFormatScenarios = []formatScenario{
// input: "~",
// expected: "~\n",
// },
{
description: "octal",
skipDoc: true,
input: "0o30",
expression: "tag",
expected: "!!int\n",
},
{
description: "basic - [null]",
skipDoc: true,