|
| 1 | +package homer |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | + |
| 8 | + yaml "gopkg.in/yaml.v2" |
| 9 | +) |
| 10 | + |
| 11 | +func TestFooterFalseHandling(t *testing.T) { |
| 12 | + t.Run("YAML unmarshal footer: false", func(t *testing.T) { |
| 13 | + yamlData := ` |
| 14 | +title: "Test Dashboard" |
| 15 | +footer: false |
| 16 | +` |
| 17 | + var config HomerConfig |
| 18 | + err := yaml.Unmarshal([]byte(yamlData), &config) |
| 19 | + if err != nil { |
| 20 | + t.Fatalf("Failed to unmarshal YAML: %v", err) |
| 21 | + } |
| 22 | + |
| 23 | + if config.Footer != FooterHidden { |
| 24 | + t.Errorf("Expected Footer to be %q, got %q", FooterHidden, config.Footer) |
| 25 | + } |
| 26 | + }) |
| 27 | + |
| 28 | + t.Run("YAML unmarshal footer: string", func(t *testing.T) { |
| 29 | + yamlData := ` |
| 30 | +title: "Test Dashboard" |
| 31 | +footer: "<p>Custom Footer</p>" |
| 32 | +` |
| 33 | + var config HomerConfig |
| 34 | + err := yaml.Unmarshal([]byte(yamlData), &config) |
| 35 | + if err != nil { |
| 36 | + t.Fatalf("Failed to unmarshal YAML: %v", err) |
| 37 | + } |
| 38 | + |
| 39 | + expected := "<p>Custom Footer</p>" |
| 40 | + if config.Footer != expected { |
| 41 | + t.Errorf("Expected Footer to be %q, got %q", expected, config.Footer) |
| 42 | + } |
| 43 | + }) |
| 44 | + |
| 45 | + t.Run("JSON unmarshal footer: false", func(t *testing.T) { |
| 46 | + jsonData := `{ |
| 47 | + "title": "Test Dashboard", |
| 48 | + "footer": false |
| 49 | + }` |
| 50 | + var config HomerConfig |
| 51 | + err := json.Unmarshal([]byte(jsonData), &config) |
| 52 | + if err != nil { |
| 53 | + t.Fatalf("Failed to unmarshal JSON: %v", err) |
| 54 | + } |
| 55 | + |
| 56 | + if config.Footer != FooterHidden { |
| 57 | + t.Errorf("Expected Footer to be %q, got %q", FooterHidden, config.Footer) |
| 58 | + } |
| 59 | + }) |
| 60 | + |
| 61 | + t.Run("JSON unmarshal footer: string", func(t *testing.T) { |
| 62 | + jsonData := `{ |
| 63 | + "title": "Test Dashboard", |
| 64 | + "footer": "<p>Custom Footer</p>" |
| 65 | + }` |
| 66 | + var config HomerConfig |
| 67 | + err := json.Unmarshal([]byte(jsonData), &config) |
| 68 | + if err != nil { |
| 69 | + t.Fatalf("Failed to unmarshal JSON: %v", err) |
| 70 | + } |
| 71 | + |
| 72 | + expected := "<p>Custom Footer</p>" |
| 73 | + if config.Footer != expected { |
| 74 | + t.Errorf("Expected Footer to be %q, got %q", expected, config.Footer) |
| 75 | + } |
| 76 | + }) |
| 77 | + |
| 78 | + t.Run("YAML marshal footer: false", func(t *testing.T) { |
| 79 | + config := &HomerConfig{ |
| 80 | + Title: "Test Dashboard", |
| 81 | + Footer: FooterHidden, |
| 82 | + } |
| 83 | + |
| 84 | + yamlBytes, err := marshalHomerConfigToYAML(config) |
| 85 | + if err != nil { |
| 86 | + t.Fatalf("Failed to generate Homer YAML: %v", err) |
| 87 | + } |
| 88 | + |
| 89 | + yamlStr := string(yamlBytes) |
| 90 | + if !strings.Contains(yamlStr, "footer: false") { |
| 91 | + t.Errorf("Expected YAML to contain 'footer: false', got:\n%s", yamlStr) |
| 92 | + } |
| 93 | + }) |
| 94 | + |
| 95 | + t.Run("YAML marshal footer: string", func(t *testing.T) { |
| 96 | + config := &HomerConfig{ |
| 97 | + Title: "Test Dashboard", |
| 98 | + Footer: "<p>Custom Footer</p>", |
| 99 | + } |
| 100 | + |
| 101 | + yamlBytes, err := marshalHomerConfigToYAML(config) |
| 102 | + if err != nil { |
| 103 | + t.Fatalf("Failed to generate Homer YAML: %v", err) |
| 104 | + } |
| 105 | + |
| 106 | + yamlStr := string(yamlBytes) |
| 107 | + if !strings.Contains(yamlStr, "footer: <p>Custom Footer</p>") { |
| 108 | + t.Errorf("Expected YAML to contain custom footer string, got:\n%s", yamlStr) |
| 109 | + } |
| 110 | + }) |
| 111 | + |
| 112 | + t.Run("YAML marshal footer: empty (omitted)", func(t *testing.T) { |
| 113 | + config := &HomerConfig{ |
| 114 | + Title: "Test Dashboard", |
| 115 | + Footer: "", |
| 116 | + } |
| 117 | + |
| 118 | + yamlBytes, err := marshalHomerConfigToYAML(config) |
| 119 | + if err != nil { |
| 120 | + t.Fatalf("Failed to generate Homer YAML: %v", err) |
| 121 | + } |
| 122 | + |
| 123 | + yamlStr := string(yamlBytes) |
| 124 | + if strings.Contains(yamlStr, "footer:") { |
| 125 | + t.Errorf("Expected YAML to not contain footer field when empty, got:\n%s", yamlStr) |
| 126 | + } |
| 127 | + }) |
| 128 | +} |
0 commit comments