Skip to content

Commit

Permalink
tests for AllTokens
Browse files Browse the repository at this point in the history
  • Loading branch information
apparentlymart committed May 27, 2017
1 parent c658837 commit 33a8a4c
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions textseg/all_tokens_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package textseg

import (
"bufio"
"reflect"
"testing"
)

func TestAllTokens(t *testing.T) {
tests := []struct {
input string
want []string
}{
{
``,
[]string{},
},
{
`hello`,
[]string{
`hello`,
},
},
{
`hello world`,
[]string{
`hello`,
`world`,
},
},
{
`hello worldly world`,
[]string{
`hello`,
`worldly`,
`world`,
},
},
}

for _, test := range tests {
t.Run(test.input, func(t *testing.T) {
gotBytes, err := AllTokens([]byte(test.input), bufio.ScanWords)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}

got := make([]string, len(gotBytes))
for i, buf := range gotBytes {
got[i] = string(buf)
}

if !reflect.DeepEqual(got, test.want) {
wantBytes := make([][]byte, len(test.want))
for i, str := range test.want {
wantBytes[i] = []byte(str)
}

t.Errorf(
"wrong result\ninput: %s\ngot: %s\nwant: %s",
formatBytes([]byte(test.input)),
formatByteRanges(gotBytes),
formatByteRanges(wantBytes),
)
}
})
}
}

0 comments on commit 33a8a4c

Please sign in to comment.