diff --git a/wordfrequency/wordfrequency_test.go b/wordfrequency/wordfrequency_test.go new file mode 100644 index 0000000..6a948bc --- /dev/null +++ b/wordfrequency/wordfrequency_test.go @@ -0,0 +1,31 @@ +package wordfrequency + +import ( + "bytes" + "fmt" + "reflect" + "testing" +) + +func TestGetWordFrequency(t *testing.T) { + input := bytes.NewBufferString( + "hello WORLD Hello world World hello cats HELLO cats dogs") + + /* NOTE: DeepEqual nuance: Can't compare [4]Word with + * []Word, because Array != Slice. + * See https://play.golang.org/p/aDPIwjDq5bJ + * Also, order of expect slice is important. Keep as is. + */ + expect := []Word{Word{"hello", 4}, Word{"world", 3}, + Word{"cats", 2}, Word{"dogs", 1}} + + actual, err := GetWordFrequency(input, SORT_DESC, 3) + + if err != nil { + fmt.Println(err) + } + + if !reflect.DeepEqual(expect, actual.Words) { + t.Errorf("Expected %v; got %v\n", expect, actual.Words) + } +}