|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +func TestReadJSON(t *testing.T) { |
| 9 | + t.Run("read non-existent file", func(t *testing.T) { |
| 10 | + _, err := readJSON("non-existent.json") |
| 11 | + if err == nil { |
| 12 | + t.Fatalf("readJSON: expected error, got nil") |
| 13 | + } |
| 14 | + if !strings.HasPrefix(err.Error(), "open") { |
| 15 | + t.Fatalf("readJSON: expected error to start with 'open', got %v", err) |
| 16 | + } |
| 17 | + }) |
| 18 | + |
| 19 | + t.Run("read invalid file", func(t *testing.T) { |
| 20 | + _, err := readJSON("../../testdata/test_invalid.json") |
| 21 | + if err == nil { |
| 22 | + t.Fatalf("readJSON: expected error, got nil") |
| 23 | + } |
| 24 | + if !strings.HasPrefix(err.Error(), "unmarshal") { |
| 25 | + t.Fatalf("readJSON: expected error to start with 'unmarshal', got %v", err) |
| 26 | + } |
| 27 | + }) |
| 28 | + |
| 29 | + t.Run("read valid file", func(t *testing.T) { |
| 30 | + actual, err := readJSON("../../testdata/test_int.json") |
| 31 | + if err != nil { |
| 32 | + t.Fatalf("readJSON: %v", err) |
| 33 | + } |
| 34 | + if len(actual) == 0 { |
| 35 | + t.Fatalf("readJSON: expected non-empty map, got empty map") |
| 36 | + } |
| 37 | + if _, ok := actual["IntField"]; !ok { |
| 38 | + t.Fatalf("readJSON: expected key IntField, got %v", actual) |
| 39 | + } |
| 40 | + }) |
| 41 | +} |
0 commit comments