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 return false
} }
// yaml numbers can be hex encoded... // yaml numbers can be hex and octal encoded...
func parseInt64(numberString string) (string, int64, error) { func parseInt64(numberString string) (string, int64, error) {
if strings.HasPrefix(numberString, "0x") || if strings.HasPrefix(numberString, "0x") ||
strings.HasPrefix(numberString, "0X") { strings.HasPrefix(numberString, "0X") {
num, err := strconv.ParseInt(numberString[2:], 16, 64) num, err := strconv.ParseInt(numberString[2:], 16, 64)
return "0x%X", num, err 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) num, err := strconv.ParseInt(numberString, 10, 64)
return "%v", num, err return "%v", num, err
} }
func parseInt(numberString string) (int, error) { func parseInt(numberString string) (int, error) {
var err error _, parsed, err := parseInt64(numberString)
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)
}
if err != nil { if err != nil {
return 0, err return 0, err

View File

@ -1,10 +1,10 @@
package yqlib package yqlib
import ( import (
"fmt"
"testing" "testing"
"github.com/mikefarah/yq/v4/test" "github.com/mikefarah/yq/v4/test"
yaml "gopkg.in/yaml.v3"
) )
func TestGetLogger(t *testing.T) { func TestGetLogger(t *testing.T) {
@ -16,7 +16,7 @@ func TestGetLogger(t *testing.T) {
type parseSnippetScenario struct { type parseSnippetScenario struct {
snippet string snippet string
expected *yaml.Node expected *CandidateNode
expectedError string expectedError string
} }
@ -27,15 +27,15 @@ var parseSnippetScenarios = []parseSnippetScenario{
}, },
{ {
snippet: "", snippet: "",
expected: &yaml.Node{ expected: &CandidateNode{
Kind: yaml.ScalarNode, Kind: ScalarNode,
Tag: "!!null", Tag: "!!null",
}, },
}, },
{ {
snippet: "null", snippet: "null",
expected: &yaml.Node{ expected: &CandidateNode{
Kind: yaml.ScalarNode, Kind: ScalarNode,
Tag: "!!null", Tag: "!!null",
Value: "null", Value: "null",
Line: 0, Line: 0,
@ -44,8 +44,8 @@ var parseSnippetScenarios = []parseSnippetScenario{
}, },
{ {
snippet: "3", snippet: "3",
expected: &yaml.Node{ expected: &CandidateNode{
Kind: yaml.ScalarNode, Kind: ScalarNode,
Tag: "!!int", Tag: "!!int",
Value: "3", Value: "3",
Line: 0, Line: 0,
@ -54,8 +54,8 @@ var parseSnippetScenarios = []parseSnippetScenario{
}, },
{ {
snippet: "cat", snippet: "cat",
expected: &yaml.Node{ expected: &CandidateNode{
Kind: yaml.ScalarNode, Kind: ScalarNode,
Tag: "!!str", Tag: "!!str",
Value: "cat", Value: "cat",
Line: 0, Line: 0,
@ -64,8 +64,8 @@ var parseSnippetScenarios = []parseSnippetScenario{
}, },
{ {
snippet: "3.1", snippet: "3.1",
expected: &yaml.Node{ expected: &CandidateNode{
Kind: yaml.ScalarNode, Kind: ScalarNode,
Tag: "!!float", Tag: "!!float",
Value: "3.1", Value: "3.1",
Line: 0, Line: 0,
@ -74,8 +74,8 @@ var parseSnippetScenarios = []parseSnippetScenario{
}, },
{ {
snippet: "true", snippet: "true",
expected: &yaml.Node{ expected: &CandidateNode{
Kind: yaml.ScalarNode, Kind: ScalarNode,
Tag: "!!bool", Tag: "!!bool",
Value: "true", Value: "true",
Line: 0, Line: 0,
@ -93,7 +93,7 @@ func TestParseSnippet(t *testing.T) {
} else { } else {
test.AssertResultComplexWithContext(t, tt.expectedError, err.Error(), tt.snippet) test.AssertResultComplexWithContext(t, tt.expectedError, err.Error(), tt.snippet)
} }
return continue
} }
if err != nil { if err != nil {
t.Error(tt.snippet) t.Error(tt.snippet)
@ -102,3 +102,37 @@ func TestParseSnippet(t *testing.T) {
test.AssertResultComplexWithContext(t, tt.expected, actual, tt.snippet) 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: "~", // input: "~",
// expected: "~\n", // expected: "~\n",
// }, // },
{
description: "octal",
skipDoc: true,
input: "0o30",
expression: "tag",
expected: "!!int\n",
},
{ {
description: "basic - [null]", description: "basic - [null]",
skipDoc: true, skipDoc: true,