Skip to content

Commit f6e6117

Browse files
authored
test(internal/yaml): add tests (#3105)
Add tests for functions in package yaml.
1 parent bce1fd3 commit f6e6117

File tree

2 files changed

+161
-0
lines changed

2 files changed

+161
-0
lines changed

internal/yaml/ref_test.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package yaml
16+
17+
import (
18+
"strings"
19+
"testing"
20+
21+
"gopkg.in/yaml.v3"
22+
)
23+
24+
type refConfig struct {
25+
Ref RefString `yaml:"ref"`
26+
}
27+
28+
func TestRefStringMarshal(t *testing.T) {
29+
input := &refConfig{Ref: RefString("some/path")}
30+
data, err := yaml.Marshal(input)
31+
if err != nil {
32+
t.Fatal(err)
33+
}
34+
if !strings.Contains(string(data), "!REF") {
35+
t.Errorf("Marshal() output missing !REF tag: %s", string(data))
36+
}
37+
}
38+
39+
func TestRefStringUnmarshal(t *testing.T) {
40+
data := "ref: !REF some/path\n"
41+
var got refConfig
42+
if err := yaml.Unmarshal([]byte(data), &got); err != nil {
43+
t.Fatal(err)
44+
}
45+
if got.Ref != "some/path" {
46+
t.Errorf("Unmarshal() ref = %q, want %q", got.Ref, "some/path")
47+
}
48+
}
49+
50+
func TestRefStringUnmarshalError(t *testing.T) {
51+
data := "ref: some/path\n"
52+
var got refConfig
53+
err := yaml.Unmarshal([]byte(data), &got)
54+
if err == nil {
55+
t.Error("Unmarshal() expected error for missing !REF tag")
56+
}
57+
}
58+
59+
func TestRefStringRoundTrip(t *testing.T) {
60+
input := &refConfig{Ref: RefString("some/path")}
61+
data, err := yaml.Marshal(input)
62+
if err != nil {
63+
t.Fatal(err)
64+
}
65+
var got refConfig
66+
if err := yaml.Unmarshal(data, &got); err != nil {
67+
t.Fatal(err)
68+
}
69+
if got.Ref != input.Ref {
70+
t.Errorf("round-trip ref = %q, want %q", got.Ref, input.Ref)
71+
}
72+
}

internal/yaml/yaml_test.go

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package yaml
16+
17+
import (
18+
"path/filepath"
19+
"testing"
20+
21+
"github.com/google/go-cmp/cmp"
22+
)
23+
24+
type testConfig struct {
25+
Name string `yaml:"name"`
26+
Version string `yaml:"version"`
27+
}
28+
29+
func TestUnmarshal(t *testing.T) {
30+
got, err := Unmarshal[testConfig]([]byte("name: test\nversion: v1.0.0\n"))
31+
if err != nil {
32+
t.Fatal(err)
33+
}
34+
want := &testConfig{Name: "test", Version: "v1.0.0"}
35+
if diff := cmp.Diff(want, got); diff != "" {
36+
t.Errorf("mismatch (-want +got):\n%s", diff)
37+
}
38+
}
39+
40+
func TestUnmarshalError(t *testing.T) {
41+
_, err := Unmarshal[testConfig]([]byte("name: [invalid"))
42+
if err == nil {
43+
t.Error("Unmarshal() expected error for invalid YAML")
44+
}
45+
}
46+
47+
func TestMarshal(t *testing.T) {
48+
input := &testConfig{Name: "test", Version: "v1.0.0"}
49+
data, err := Marshal(input)
50+
if err != nil {
51+
t.Fatal(err)
52+
}
53+
got, err := Unmarshal[testConfig](data)
54+
if err != nil {
55+
t.Fatal(err)
56+
}
57+
if diff := cmp.Diff(input, got); diff != "" {
58+
t.Errorf("mismatch (-want +got):\n%s", diff)
59+
}
60+
}
61+
62+
func TestReadWrite(t *testing.T) {
63+
want := &testConfig{Name: "test", Version: "v1.0.0"}
64+
path := filepath.Join(t.TempDir(), "test.yaml")
65+
if err := Write(path, want); err != nil {
66+
t.Fatal(err)
67+
}
68+
got, err := Read[testConfig](path)
69+
if err != nil {
70+
t.Fatal(err)
71+
}
72+
if diff := cmp.Diff(want, got); diff != "" {
73+
t.Errorf("mismatch (-want +got):\n%s", diff)
74+
}
75+
}
76+
77+
func TestReadError(t *testing.T) {
78+
_, err := Read[testConfig]("/nonexistent/path/file.yaml")
79+
if err == nil {
80+
t.Error("Read() expected error for nonexistent file")
81+
}
82+
}
83+
84+
func TestWriteError(t *testing.T) {
85+
err := Write("/nonexistent/path/file.yaml", &testConfig{Name: "test"})
86+
if err == nil {
87+
t.Error("Write() expected error for invalid path")
88+
}
89+
}

0 commit comments

Comments
 (0)