Skip to content

Commit

Permalink
test: add test of template functions
Browse files Browse the repository at this point in the history
  • Loading branch information
macrat committed Apr 16, 2021
1 parent d365c70 commit 06cf10b
Showing 1 changed file with 91 additions and 0 deletions.
91 changes: 91 additions & 0 deletions exporter/templates_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package exporter

import (
"reflect"
"fmt"

"testing"
)

func TestInvertIncidents(t *testing.T) {
f := templateFuncs["invert_incidents"].(func([]frozenIncident) []frozenIncident)

t.Run("no_incidents", func(t *testing.T) {
result := f([]frozenIncident{})
if len(result) != 0 {
t.Fatalf("unexpected result length: %d", len(result))
}
})

t.Run("three_incidents", func(t *testing.T) {
input := []frozenIncident{
{Message: "foo"},
{Message: "bar"},
{Message: "baz"},
}
expect := []frozenIncident{
{Message: "baz"},
{Message: "bar"},
{Message: "foo"},
}

result := f(input)
if len(result) != len(expect) {
t.Fatalf("unexpected result length: %d", len(result))
}

for i := range result {
if result[i].Message != expect[i].Message {
t.Errorf("%d: unexpected message: %#v", i, result[i].Message)
}
}
})
}

func TestBreakText(t *testing.T) {
f := templateFuncs["break_text"].(func(string, int) []string)

tests := []struct{
Input string
Width int
Output []string
}{
{"hello_world", 20, []string{"hello_world"}},
{"hello_world", 5, []string{"hello", "_worl", "d"}},
{"foobar", 3, []string{"foo", "bar"}},
}

for _, tt := range tests {
tt := tt
t.Run(fmt.Sprintf("%s_%d", tt.Input, tt.Width), func(t *testing.T) {
result := f(tt.Input, tt.Width)
if !reflect.DeepEqual(tt.Output, result) {
t.Errorf("expected %#v\n but got %#v", tt.Output, result)
}
})
}
}

func TestAlignCenter(t *testing.T) {
f := templateFuncs["align_center"].(func(string, int) string)

tests := []struct{
Input string
Width int
Output string
}{
{"foobar", 10, " foobar"},
{"foo_bar", 10, " foo_bar"},
{"foobar", 5, "foobar"},
}

for _, tt := range tests {
tt := tt
t.Run(fmt.Sprintf("%s_%d", tt.Input, tt.Width), func(t *testing.T) {
result := f(tt.Input, tt.Width)
if tt.Output != result {
t.Errorf("expected %#v\n but got %#v", tt.Output, result)
}
})
}
}

0 comments on commit 06cf10b

Please sign in to comment.