Skip to content

Commit 4a107ee

Browse files
committed
Data driven tests for internal/libyaml
Refactored from @colinjlacy's #184 Use super clean, simple, consistent and documented foo_test.yaml files for test data, and minimal foo_test.go files for the code running the tests.
1 parent 5ab2424 commit 4a107ee

24 files changed

+3637
-74
lines changed

.github/workflows/check-commit-message.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
if: github.event.pull_request.user.login != 'dependabot[bot]'
1414
steps:
1515
- name: Checkout code
16-
uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0
16+
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
1717
with:
1818
fetch-depth: 0 # Fetch all history to ensure all SHAs are available
1919
persist-credentials: false

.github/workflows/codeql.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,20 @@ jobs:
2828
build-mode: autobuild
2929
steps:
3030
- name: Checkout repository
31-
uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0
31+
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
3232
with:
3333
persist-credentials: false
3434

3535
# Initializes the CodeQL tools for scanning.
3636
- name: Initialize CodeQL
3737
# yamllint disable-line rule:line-length
38-
uses: github/codeql-action/init@fe4161a26a8629af62121b670040955b330f9af2 # v4.31.6
38+
uses: github/codeql-action/init@0499de31b99561a6d14a36a5f662c2a54f91beee # v4.31.2
3939
with:
4040
languages: ${{ matrix.language }}
4141
build-mode: ${{ matrix.build-mode }}
4242

4343
- name: Perform CodeQL Analysis
4444
# yamllint disable-line rule:line-length
45-
uses: github/codeql-action/analyze@fe4161a26a8629af62121b670040955b330f9af2 # v4.31.6
45+
uses: github/codeql-action/analyze@0499de31b99561a6d14a36a5f662c2a54f91beee # v4.31.2
4646
with:
4747
category: /language:${{matrix.language}}

.github/workflows/files.yaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
runs-on: ubuntu-latest
1313
steps:
1414
- name: checkout-action
15-
uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0
15+
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
1616
with:
1717
persist-credentials: false
1818

@@ -24,7 +24,7 @@ jobs:
2424
runs-on: ubuntu-latest
2525
steps:
2626
- name: checkout-action
27-
uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0
27+
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
2828
with:
2929
persist-credentials: false
3030

@@ -36,7 +36,7 @@ jobs:
3636
runs-on: ubuntu-latest
3737
steps:
3838
- name: checkout-action
39-
uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0
39+
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
4040
with:
4141
persist-credentials: false
4242

@@ -49,9 +49,9 @@ jobs:
4949
runs-on: ubuntu-latest
5050
steps:
5151
- name: checkout-action
52-
uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0
52+
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
5353
with:
5454
persist-credentials: false
5555

5656
- name: typos-action
57-
uses: crate-ci/typos@2d0ce569feab1f8752f1dde43cc2f2aa53236e06 # v1.40.0
57+
uses: crate-ci/typos@07d900b8fa1097806b8adb6391b0d3e0ac2fdea7 # v1.39.0

.github/workflows/go.yaml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,19 @@ jobs:
1212
lint:
1313
runs-on: ubuntu-latest
1414
steps:
15-
- uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0
15+
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
1616
with:
1717
persist-credentials: false
1818
- name: Set up Go
19-
uses: actions/setup-go@4dc6199c7b1a012772edbd06daecab0f50c9053c # v6.1.0
19+
uses: actions/setup-go@44694675825211faa026b3c33043df3e48a5fa00 # v6.0.0
2020
with:
2121
go-version: stable
2222
- name: Install golangci-lint
2323
run: |
2424
make golangci-lint-install
2525
echo "${{ github.workspace }}/.cache/local/bin" >> "$GITHUB_PATH"
2626
# yamllint disable-line rule:line-length
27-
- uses: golangci/golangci-lint-action@e7fa5ac41e1cf5b7d48e45e42232ce7ada589601 # v9.1.0
27+
- uses: golangci/golangci-lint-action@4afd733a84b1f43292c63897423277bb7f4313a9 # v8.0.0
2828
with:
2929
install-mode: none
3030
go-versions:
@@ -33,9 +33,9 @@ jobs:
3333
outputs:
3434
matrix: ${{ steps.versions.outputs.matrix }}
3535
steps:
36-
- uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0
36+
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
3737
# yamllint disable-line rule:line-length
38-
- uses: arnested/go-version-action@978371bd4d8cad0f54106c8be4273e2fcdd74f57 # v1.1.23
38+
- uses: arnested/go-version-action@1f4135a30a2498f919e428c81c342f9334b3a93d # v1.1.22
3939
id: versions
4040
build:
4141
runs-on: ubuntu-latest
@@ -45,11 +45,11 @@ jobs:
4545
matrix:
4646
go-versions: ${{ fromJSON(needs.go-versions.outputs.matrix) }}
4747
steps:
48-
- uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0
48+
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
4949
with:
5050
persist-credentials: false
5151
- name: Set up Go
52-
uses: actions/setup-go@4dc6199c7b1a012772edbd06daecab0f50c9053c # v6.1.0
52+
uses: actions/setup-go@44694675825211faa026b3c33043df3e48a5fa00 # v6.0.0
5353
with:
5454
go-version: ${{ matrix.go-versions }}
5555
- name: Run yaml-test-suit tests

GNUmakefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ count ?= 1
6666

6767
# Test rules:
6868
test: $(GO-DEPS)
69-
go test$(if $v, -v) -vet=off .
69+
go test$(if $v, -v) -vet=off --cover . ./internal/...
7070

7171
test-data: $(YTS-DIR)
7272

decode.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,7 @@ func (p *parser) alias() *Node {
216216
n := p.node(AliasNode, "", "", string(p.event.Anchor))
217217
n.Alias = p.anchors[n.Value]
218218
if n.Alias == nil {
219-
msg := fmt.Sprintf("unknown anchor '%s' referenced", n.Value)
220-
fail(&ParserError{msg, n.Line, n.Column})
219+
failf("unknown anchor '%s' referenced", n.Value)
221220
}
222221
p.expect(libyaml.ALIAS_EVENT)
223222
return n

decode_test.go

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -927,15 +927,6 @@ var unmarshalTests = []struct {
927927
"true #" + strings.Repeat(" ", 512*3),
928928
"true",
929929
},
930-
// C1 Control characters
931-
{
932-
"\u0080",
933-
"\u0080",
934-
},
935-
{
936-
"\u009F",
937-
"\u009F",
938-
},
939930

940931
// CRLF
941932
{
@@ -1134,13 +1125,13 @@ var unmarshalErrorTests = []struct {
11341125
{"v: [A,", "yaml: line 1: did not find expected node content"},
11351126
{"v:\n- [A,", "yaml: line 2: did not find expected node content"},
11361127
{"a:\n- b: *,", "yaml: line 2: did not find expected alphabetic or numeric character"},
1137-
{"a: *b\n", "yaml: line 1: unknown anchor 'b' referenced"},
1128+
{"a: *b\n", "yaml: unknown anchor 'b' referenced"},
11381129
{"a: &a\n b: *a\n", "yaml: anchor 'a' value contains itself"},
11391130
{"value: -", "yaml: block sequence entries are not allowed in this context"},
11401131
{"a: !!binary ==", "yaml: !!binary value contains invalid base64 data"},
11411132
{"{[.]}", `yaml: cannot use '\[\]interface \{\}\{"\."\}' as a map key; try decoding into yaml.Node`},
11421133
{"{{.}}", `yaml: cannot use 'map\[string]interface \{\}\{".":interface \{\}\(nil\)\}' as a map key; try decoding into yaml.Node`},
1143-
{"b: *a\na: &a {c: 1}", `yaml: line 1: unknown anchor 'a' referenced`},
1134+
{"b: *a\na: &a {c: 1}", `yaml: unknown anchor 'a' referenced`},
11441135
{"%TAG !%79! tag:yaml.org,2002:\n---\nv: !%79!int '1'", "yaml: did not find expected whitespace"},
11451136
{"a:\n 1:\nb\n 2:", ".*could not find expected ':'"},
11461137
{"a: 1\nb: 2\nc 2\nd: 3\n", "^yaml: line 3: could not find expected ':'$"},
@@ -2132,31 +2123,6 @@ a:
21322123
}
21332124
}
21342125

2135-
func TestParserErrorUnknownAnchorPosition(t *testing.T) {
2136-
tests := []struct {
2137-
data string
2138-
line int
2139-
column int
2140-
}{
2141-
{"*x", 1, 1},
2142-
{"a: *x", 1, 4},
2143-
{"a:\n b: *x", 2, 6},
2144-
}
2145-
2146-
for _, test := range tests {
2147-
var n yaml.Node
2148-
err := yaml.Unmarshal([]byte(test.data), &n)
2149-
asErr := new(yaml.ParserError)
2150-
assert.ErrorAs(t, err, &asErr)
2151-
expected := &yaml.ParserError{
2152-
Message: "unknown anchor 'x' referenced",
2153-
Line: test.line,
2154-
Column: test.column,
2155-
}
2156-
assert.DeepEqual(t, expected, asErr)
2157-
}
2158-
}
2159-
21602126
//var data []byte
21612127
//func init() {
21622128
// var err error

encode_test.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ var marshalTests = []struct {
3636
value any
3737
data string
3838
}{
39+
// Basic types.
3940
{
4041
nil,
4142
"null\n",
@@ -136,6 +137,18 @@ var marshalTests = []struct {
136137
map[string]any{"v": ""},
137138
"v: \"\"\n",
138139
},
140+
141+
// Issue 65: Validate handling newlines at the start.
142+
{
143+
map[string]any{"v": "\nhi"},
144+
"v: |-\n\n hi\n",
145+
},
146+
{
147+
map[string][]any{"v": {map[string]any{"v1": "\nhi"}}},
148+
"v:\n - v1: |-\n\n hi\n",
149+
},
150+
151+
// Validate multiline strings.
139152
{
140153
map[string][]string{"v": {"A", "B"}},
141154
"v:\n - A\n - B\n",
@@ -942,6 +955,18 @@ a:
942955
- c: 1
943956
d: 2
944957
`),
958+
959+
normal(`
960+
v:
961+
- v1: |-
962+
963+
hi
964+
`): compact(`
965+
v:
966+
- v1: |-
967+
968+
hi
969+
`),
945970
}
946971

947972
func TestEncoderCompactIndents(t *testing.T) {
@@ -978,7 +1003,7 @@ func TestNewLinePreserved(t *testing.T) {
9781003
data, err = yaml.Marshal(obj)
9791004
assert.NoError(t, err)
9801005
// the newline at the start of the file should be preserved
981-
assert.Equal(t, "_: |4\n\n a:\n b:\n c: d\n", string(data))
1006+
assert.Equal(t, "_: |\n\n a:\n b:\n c: d\n", string(data))
9821007
}
9831008

9841009
func TestScalarStyleRules(t *testing.T) {

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
module go.yaml.in/yaml/v4
22

33
go 1.18
4+
5+
require go.yaml.in/yaml/v3 v3.0.4

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
go.yaml.in/yaml/v3 v3.0.4 h1:tfq32ie2Jv2UxXFdLJdh3jXuOzWiL1fo0bu/FbuKpbc=
2+
go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg=
3+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
4+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

0 commit comments

Comments
 (0)