|
1 | 1 | package snapshot |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "encoding/json" |
4 | 5 | "testing" |
5 | 6 | "time" |
6 | 7 |
|
7 | 8 | "github.com/stretchr/testify/assert" |
| 9 | + "github.com/stretchr/testify/require" |
8 | 10 | ) |
9 | 11 |
|
10 | 12 | // TestSnapshot_Creation tests basic snapshot creation and field initialization. |
@@ -37,6 +39,51 @@ func TestPackageSnapshot_Empty(t *testing.T) { |
37 | 39 | assert.Empty(t, ps.Taps) |
38 | 40 | } |
39 | 41 |
|
| 42 | +func TestPackageSnapshot_UnmarshalJSON(t *testing.T) { |
| 43 | + tests := []struct { |
| 44 | + name string |
| 45 | + input string |
| 46 | + expected PackageSnapshot |
| 47 | + wantErr bool |
| 48 | + }{ |
| 49 | + { |
| 50 | + name: "object format", |
| 51 | + input: `{"formulae":["git","go"],"casks":["docker"],"taps":["homebrew/core"],"npm":["typescript"]}`, |
| 52 | + expected: PackageSnapshot{ |
| 53 | + Formulae: []string{"git", "go"}, |
| 54 | + Casks: []string{"docker"}, |
| 55 | + Taps: []string{"homebrew/core"}, |
| 56 | + Npm: []string{"typescript"}, |
| 57 | + }, |
| 58 | + }, |
| 59 | + { |
| 60 | + name: "flat array treated as formulae", |
| 61 | + input: `["git","curl","jq"]`, |
| 62 | + expected: PackageSnapshot{ |
| 63 | + Formulae: []string{"git", "curl", "jq"}, |
| 64 | + }, |
| 65 | + }, |
| 66 | + { |
| 67 | + name: "invalid type", |
| 68 | + input: `123`, |
| 69 | + wantErr: true, |
| 70 | + }, |
| 71 | + } |
| 72 | + |
| 73 | + for _, tt := range tests { |
| 74 | + t.Run(tt.name, func(t *testing.T) { |
| 75 | + var ps PackageSnapshot |
| 76 | + err := json.Unmarshal([]byte(tt.input), &ps) |
| 77 | + if tt.wantErr { |
| 78 | + require.Error(t, err) |
| 79 | + return |
| 80 | + } |
| 81 | + require.NoError(t, err) |
| 82 | + assert.Equal(t, tt.expected, ps) |
| 83 | + }) |
| 84 | + } |
| 85 | +} |
| 86 | + |
40 | 87 | // TestMacOSPref_Creation tests MacOS preference creation. |
41 | 88 | func TestMacOSPref_Creation(t *testing.T) { |
42 | 89 | pref := MacOSPref{ |
|
0 commit comments