|
| 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