github.com/elliott5/community@v0.14.1-0.20160709191136-823126fb026a/wordsmith/utility/words_test.go (about) 1 // Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved. 2 // 3 // This software (Documize Community Edition) is licensed under 4 // GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html 5 // 6 // You can operate outside the AGPL restrictions by purchasing 7 // Documize Enterprise Edition and obtaining a commercial license 8 // by contacting <sales@documize.com>. 9 // 10 // https://documize.com 11 12 package utility 13 14 import ( 15 "sort" 16 "strings" 17 "testing" 18 ) 19 20 func TestWords(t *testing.T) { 21 ws(t, " the quick brown fox jumps over the lazy dog [ ] [" +string(rune(0x200B)), 0, true, 22 "the quick brown fox jumps over the lazy dog [ [", 1) 23 ws(t, "the quick brown [ dog jumps over the lazy ] fox", 0, false, 24 "the quick brown [ fox .", 0) 25 ws(t, "the quick brown;fox;", 0, false, 26 "the quick brown ; fox ; .", 0) 27 ws(t, "the ] quick brown fox ", 1, true, 28 "quick brown fox", 0) 29 } 30 31 func ws(t *testing.T, in string, bktIn int, isTest bool, out string, bktOut int) { 32 wds := strings.Split(out, " ") 33 gotX, bo, e := Words(HTML(in), bktIn, isTest) 34 if e != nil { 35 t.Fatal(e) 36 } 37 if bo != bktOut { 38 t.Errorf("wrong bracket count returned: input `%s` bktIn %d bktOut %d\n", in, bktIn, bktOut) 39 } 40 got := make([]string, 0, len(gotX)) 41 for _, v := range gotX { // remove empty entries 42 if v != "" { 43 got = append(got, v) 44 } 45 } 46 if len(got) != len(wds) { 47 t.Errorf("wrong number of words found: input `%s` got %d %v expected %d %v`\n", in, len(got), got, len(wds), wds) 48 } else { 49 sort.Strings(wds) 50 sort.Strings(got) 51 for i := range wds { 52 if wds[i] != got[i] { 53 t.Errorf("wrong word[%d]: input `%s` got %v expected %v\n", i, in, got, wds) 54 } 55 } 56 } 57 }