Add test cases (#26)
* Add tests for the Exists function * Add tests for the StripHTMLTags function
This commit is contained in:
43
file/file_test.go
Normal file
43
file/file_test.go
Normal file
@@ -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)
|
||||
}
|
||||
}
|
26
utils/html_test.go
Normal file
26
utils/html_test.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package utils
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestStripHTMLTags(t *testing.T) {
|
||||
testCases := []struct {
|
||||
htmlInput string
|
||||
expect string
|
||||
}{
|
||||
{htmlInput: "<html><body<h1>Hello World!</h1></body></html>",
|
||||
expect: "Hello World!"},
|
||||
{htmlInput: "<h1>Hello World!</h1>",
|
||||
expect: "Hello World!"},
|
||||
{htmlInput: "<h1 style='color: #5e9ca0';>Hello World!</h1>",
|
||||
expect: "Hello World!"},
|
||||
{htmlInput: "<td><img style='margin: 1px 15px;' src='images/smiley.png' alt='laughing' width='40' height='16' /><strong>Hello World!</strong></td>", 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)
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user