|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "regexp" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/google/go-cmp/cmp" |
| 10 | +) |
| 11 | + |
| 12 | +func TestRender(t *testing.T) { |
| 13 | + full := &searchResultsAlert{ |
| 14 | + Title: "foo", |
| 15 | + Description: "bar", |
| 16 | + ProposedQueries: []struct { |
| 17 | + Description string |
| 18 | + Query string |
| 19 | + }{ |
| 20 | + { |
| 21 | + Description: "quux", |
| 22 | + Query: "xyz:abc", |
| 23 | + }, |
| 24 | + { |
| 25 | + Description: "baz", |
| 26 | + Query: "def:ghi", |
| 27 | + }, |
| 28 | + }, |
| 29 | + } |
| 30 | + |
| 31 | + zero := &searchResultsAlert{} |
| 32 | + |
| 33 | + t.Run("zero value", func(t *testing.T) { |
| 34 | + content, err := zero.Render() |
| 35 | + if err != nil { |
| 36 | + t.Errorf("unexpected error rendering zero alert: %v", err) |
| 37 | + } |
| 38 | + |
| 39 | + if content != "" { |
| 40 | + t.Errorf("unexpected content for zero alert: %s", content) |
| 41 | + } |
| 42 | + }) |
| 43 | + |
| 44 | + t.Run("no description", func(t *testing.T) { |
| 45 | + alert := *full |
| 46 | + alert.Description = zero.Description |
| 47 | + |
| 48 | + content, err := alert.Render() |
| 49 | + if err != nil { |
| 50 | + t.Errorf("unexpected error rendering alert without description: %v", err) |
| 51 | + } |
| 52 | + |
| 53 | + expected := subColorCodes(strings.Join([]string{ |
| 54 | + "[[search-alert-title]]❗foo[[nc]]\n", |
| 55 | + "[[search-alert-proposed-title]] Did you mean:[[nc]]\n", |
| 56 | + "[[search-alert-proposed-query]] xyz:abc[[nc]] - [[search-alert-proposed-description]]quux[[nc]]\n", |
| 57 | + "[[search-alert-proposed-query]] def:ghi[[nc]] - [[search-alert-proposed-description]]baz[[nc]]\n", |
| 58 | + }, "")) |
| 59 | + if diff := cmp.Diff(expected, content); diff != "" { |
| 60 | + t.Errorf("alert without description content incorrect: %s", diff) |
| 61 | + } |
| 62 | + }) |
| 63 | + |
| 64 | + t.Run("no proposed queries", func(t *testing.T) { |
| 65 | + alert := *full |
| 66 | + alert.ProposedQueries = zero.ProposedQueries |
| 67 | + |
| 68 | + content, err := alert.Render() |
| 69 | + if err != nil { |
| 70 | + t.Errorf("unexpected error rendering alert without queries: %v", err) |
| 71 | + } |
| 72 | + |
| 73 | + expected := subColorCodes(strings.Join([]string{ |
| 74 | + "[[search-alert-title]]❗foo[[nc]]\n", |
| 75 | + "[[search-alert-description]] bar[[nc]]\n", |
| 76 | + }, "")) |
| 77 | + if diff := cmp.Diff(expected, content); diff != "" { |
| 78 | + t.Errorf("alert without queries content incorrect: %s", diff) |
| 79 | + } |
| 80 | + }) |
| 81 | + |
| 82 | + t.Run("full", func(t *testing.T) { |
| 83 | + content, err := full.Render() |
| 84 | + if err != nil { |
| 85 | + t.Errorf("unexpected error rendering full alert: %v", err) |
| 86 | + } |
| 87 | + |
| 88 | + expected := subColorCodes(strings.Join([]string{ |
| 89 | + "[[search-alert-title]]❗foo[[nc]]\n", |
| 90 | + "[[search-alert-description]] bar[[nc]]\n", |
| 91 | + "[[search-alert-proposed-title]] Did you mean:[[nc]]\n", |
| 92 | + "[[search-alert-proposed-query]] xyz:abc[[nc]] - [[search-alert-proposed-description]]quux[[nc]]\n", |
| 93 | + "[[search-alert-proposed-query]] def:ghi[[nc]] - [[search-alert-proposed-description]]baz[[nc]]\n", |
| 94 | + }, "")) |
| 95 | + if diff := cmp.Diff(expected, content); diff != "" { |
| 96 | + t.Errorf("full alert content incorrect: %s", diff) |
| 97 | + } |
| 98 | + }) |
| 99 | +} |
| 100 | + |
| 101 | +var subColorCodesRegex = regexp.MustCompile(`\[\[[a-zA-Z0-9-]+\]\]`) |
| 102 | + |
| 103 | +// subColorCodes provides ad-hoc templating of just ANSI colour codes from our |
| 104 | +// ansiColors map, using a [[colour-code]] syntax. |
| 105 | +func subColorCodes(in string) string { |
| 106 | + // We could use text/template here, but at a certain point it feels like |
| 107 | + // we're just reinventing the template string that's a const in |
| 108 | + // search_alert.go. This allows us to express the colour codes in the |
| 109 | + // expected string literals above while hopefully maintaining some meaning |
| 110 | + // to the tests. |
| 111 | + return subColorCodesRegex.ReplaceAllStringFunc(in, func(code string) string { |
| 112 | + esc, ok := ansiColors[strings.Trim(code, "[]")] |
| 113 | + if !ok { |
| 114 | + panic(fmt.Sprintf("cannot find colour %s", code)) |
| 115 | + } |
| 116 | + return esc |
| 117 | + }) |
| 118 | +} |
0 commit comments