From c8f35d912dd2830b85d35fc5dbbab3dad516587a Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Tue, 17 Oct 2023 20:12:41 +1100 Subject: [PATCH] Fixing tests --- pkg/yqlib/lib.go | 14 ++++----- pkg/yqlib/lib_test.go | 64 ++++++++++++++++++++++++++++++++---------- pkg/yqlib/yaml_test.go | 7 +++++ 3 files changed, 61 insertions(+), 24 deletions(-) diff --git a/pkg/yqlib/lib.go b/pkg/yqlib/lib.go index 1def16e0..78bd31ba 100644 --- a/pkg/yqlib/lib.go +++ b/pkg/yqlib/lib.go @@ -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 diff --git a/pkg/yqlib/lib_test.go b/pkg/yqlib/lib_test.go index c3e01009..ecd541a7 100644 --- a/pkg/yqlib/lib_test.go +++ b/pkg/yqlib/lib_test.go @@ -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)) + } +} diff --git a/pkg/yqlib/yaml_test.go b/pkg/yqlib/yaml_test.go index 6305220b..a68218d3 100644 --- a/pkg/yqlib/yaml_test.go +++ b/pkg/yqlib/yaml_test.go @@ -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,