From dbae7f1342ebee4a55deb66354fe55f60122b0d9 Mon Sep 17 00:00:00 2001 From: Marcin Niemira Date: Sun, 22 Sep 2019 21:15:00 +1000 Subject: [PATCH] Add tests for style.go --- pkg/minikube/out/style_test.go | 171 +++++++++++++++++++++++++++++++++ 1 file changed, 171 insertions(+) create mode 100644 pkg/minikube/out/style_test.go diff --git a/pkg/minikube/out/style_test.go b/pkg/minikube/out/style_test.go new file mode 100644 index 0000000000..e78dcec01f --- /dev/null +++ b/pkg/minikube/out/style_test.go @@ -0,0 +1,171 @@ +package out + +import ( + "fmt" + "strings" + "testing" +) + +func TestApplyPrefix(t *testing.T) { + + var tests = []struct { + prefix, format, expected, description string + }{ + { + prefix: "bar", + format: "foo", + expected: "barfoo", + description: "bar prefix", + }, + { + prefix: "", + format: "foo", + expected: "foo", + description: "empty prefix", + }, + } + for _, test := range tests { + t.Run(test.description, func(t *testing.T) { + got := applyPrefix(test.prefix, test.format) + if got != test.expected { + t.Errorf("Expected %v but got %v", test.expected, got) + } + }) + } +} + +func TestLowPrefix(t *testing.T) { + + var tests = []struct { + expected string + description string + style style + }{ + { + expected: lowBullet, + description: "empty prefix", + }, + { + expected: "bar", + style: style{LowPrefix: "bar"}, + description: "lowPrefix", + }, + { + expected: lowBullet, + style: style{Prefix: "foo"}, + description: "prefix without spaces", + }, + { + expected: lowIndent, + style: style{Prefix: " foo"}, + description: "prefix with spaces", + }, + } + for _, test := range tests { + t.Run(test.description, func(t *testing.T) { + got := lowPrefix(test.style) + if got != test.expected { + t.Errorf("Expected %v but got %v", test.expected, got) + } + }) + } +} + +func TestApplyStyle(t *testing.T) { + + var tests = []struct { + expected string + description string + styleEnum StyleEnum + format string + useColor bool + }{ + { + expected: fmt.Sprintf("%sbar", lowBullet), + description: "format bar, empty style, color off", + styleEnum: Empty, + useColor: false, + format: "bar", + }, + { + expected: "bar", + description: "not existing style", + styleEnum: 9999, + useColor: false, + format: "bar", + }, + { + expected: fmt.Sprintf("%sfoo", styles[Ready].Prefix), + description: "format foo, ready style, color on", + styleEnum: Ready, + useColor: true, + format: "foo", + }, + } + for _, test := range tests { + t.Run(test.description, func(t *testing.T) { + rawGot := applyStyle(test.styleEnum, test.useColor, test.format) + got := strings.TrimSpace(rawGot) + if got != test.expected { + t.Errorf("Expected '%v' but got '%v'", test.expected, got) + } + }) + } +} + +func TestApplyTemplateFormating(t *testing.T) { + + var tests = []struct { + expected string + description string + styleEnum StyleEnum + format string + useColor bool + a []V + }{ + { + expected: fmt.Sprintf("%sbar", lowBullet), + description: "format bar, empty style, color off", + styleEnum: Empty, + useColor: false, + format: "bar", + }, + { + expected: "bar", + description: "not existing style", + styleEnum: 9999, + useColor: false, + format: "bar", + }, + { + expected: fmt.Sprintf("%sfoo", styles[Ready].Prefix), + description: "format foo, ready style, color on, a nil", + styleEnum: Ready, + useColor: true, + format: "foo", + }, + { + expected: fmt.Sprintf("%sfoo", styles[Ready].Prefix), + description: "format foo, ready style, color on", + styleEnum: Ready, + useColor: true, + format: "foo", + }, + { + expected: fmt.Sprintf("%s{{ a }}", styles[Ready].Prefix), + description: "bad format", + styleEnum: Ready, + useColor: true, + format: "{{ a }}", + }, + } + for _, test := range tests { + t.Run(test.description, func(t *testing.T) { + rawGot := applyTemplateFormatting(test.styleEnum, test.useColor, test.format, test.a...) + got := strings.TrimSpace(rawGot) + if got != test.expected { + t.Errorf("Expected '%v' but got '%v'", test.expected, got) + } + }) + } +}