From 5d58ab24be8111a3a2fd81555d28874414731557 Mon Sep 17 00:00:00 2001 From: Kent Gruber Date: Mon, 30 Apr 2018 21:07:24 -0400 Subject: [PATCH] add simple tests for core table functions add simple tests for each of table functions in core --- core/table_test.go | 82 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 core/table_test.go diff --git a/core/table_test.go b/core/table_test.go new file mode 100644 index 00000000..8b9d8fee --- /dev/null +++ b/core/table_test.go @@ -0,0 +1,82 @@ +package core + +import ( + "bytes" + "regexp" + "testing" +) + +func TestViewLen(t *testing.T) { + exp := 2 + got := viewLen("<3") + if got != exp { + t.Fatalf("expected '%d', got '%d'", exp, got) + } +} + +func TestMaxLen(t *testing.T) { + exp := 7 + got := maxLen([]string{"go", "python", "ruby", "crystal"}) + if got != exp { + t.Fatalf("expected '%d', got '%d'", exp, got) + } +} + +func TestAlignLeft(t *testing.T) { + exp := Alignment(0) + got := AlignLeft + if got != exp { + t.Fatalf("expected '%d', got '%d'", exp, got) + } +} + +func TestAlignCenter(t *testing.T) { + exp := Alignment(1) + got := AlignCenter + if got != exp { + t.Fatalf("expected '%d', got '%d'", exp, got) + } +} + +func TestAlignRight(t *testing.T) { + exp := Alignment(2) + got := AlignRight + if got != exp { + t.Fatalf("expected '%d', got '%d'", exp, got) + } +} + +func TestGetPads(t *testing.T) { + lPadExp := -9 + rPadExp := -8 + lPadGot, rPadGot := getPads("Pikachu, thunderbolt!", 3, AlignCenter) + if rPadGot != rPadExp { + t.Fatalf("expected '%d', got '%d'", rPadExp, rPadGot) + } + if lPadGot != lPadExp { + t.Fatalf("expected '%d', got '%d'", lPadExp, lPadGot) + } +} + +func TestPadded(t *testing.T) { + exp := "<3" + got := padded("<3", 1, AlignLeft) + if got != exp { + t.Fatalf("expected '%s', got '%s'", exp, got) + } +} + +func TestAsTable(t *testing.T) { + var b bytes.Buffer + + AsTable(&b, []string{"top"}, [][]string{[]string{"bottom"}}) + + // look for "+-------+" table style for whichever size + match, err := regexp.MatchString(`\++-+\+`, b.String()) + if err != nil { + t.Fatalf("unable to perform regex on table output") + } + if !match { + t.Fatalf("expected table in format not found") + } +}