From 45320c826581dc45add21a1f0713ea5a48220c6d Mon Sep 17 00:00:00 2001 From: Shay Elmualem Date: Wed, 7 Oct 2020 16:13:57 +0300 Subject: [PATCH] Add test cases (#26) * Add tests for the Exists function * Add tests for the StripHTMLTags function --- file/file_test.go | 43 +++++++++++++++++++++++++++++++++++++++++++ utils/html_test.go | 26 ++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 file/file_test.go create mode 100644 utils/html_test.go diff --git a/file/file_test.go b/file/file_test.go new file mode 100644 index 0000000..b81e279 --- /dev/null +++ b/file/file_test.go @@ -0,0 +1,43 @@ +package file + +import ( + "fmt" + "testing" + "time" + + "github.com/spf13/afero" +) + +var FakeFs = afero.NewOsFs() + +func TestExists(t *testing.T) { + t.Run("expects file to exist", func(t *testing.T) { + input, _ := afero.TempFile(FakeFs, ".", "") + defer removeFile(input.Name(), t) + + got := Exists(input.Name()) + expect := true + + if got != expect { + t.Errorf("Expected file %s to exist.", input.Name()) + } + }) + + t.Run("expects file to not exist", func(t *testing.T) { + input := fmt.Sprintf("test_file_%d", time.Now().UnixNano()) + + got := Exists(input) + expect := false + + if got != expect { + t.Errorf("Expected file %s to not exist", input) + } + }) +} + +func removeFile(name string, t *testing.T) { + err := FakeFs.Remove(name) + if err != nil { + t.Errorf("Did not cleanly delete file %s", name) + } +} diff --git a/utils/html_test.go b/utils/html_test.go new file mode 100644 index 0000000..220717c --- /dev/null +++ b/utils/html_test.go @@ -0,0 +1,26 @@ +package utils + +import "testing" + +func TestStripHTMLTags(t *testing.T) { + testCases := []struct { + htmlInput string + expect string + }{ + {htmlInput: "Hello World!", + expect: "Hello World!"}, + {htmlInput: "

Hello World!

", + expect: "Hello World!"}, + {htmlInput: "

Hello World!

", + expect: "Hello World!"}, + {htmlInput: "laughingHello World!", expect: "Hello World!"}, + } + for _, tc := range testCases { + got := StripHTMLTags(tc.htmlInput) + expect := tc.expect + + if got != expect { + t.Errorf("Expected %s, got %s", expect, got) + } + } +}