1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
|
package pinyin_search
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestGeneralize(t *testing.T) {
ret := GeneralizeQuery("Firefox - Browser")
assert.Equal(t, "firefoxbrowser", ret)
}
func Test_strSliceUniq(t *testing.T) {
ret := strSliceUniq(nil)
assert.Nil(t, ret)
ret = strSliceUniq([]string{"a"})
assert.Equal(t, []string{"a"}, ret)
ret = strSliceUniq([]string{"a", "a"})
assert.Equal(t, []string{"a"}, ret)
ret = strSliceUniq([]string{"a", "b"})
assert.Equal(t, []string{"a", "b"}, ret)
ret = strSliceUniq([]string{"b", "c", "a", "b", "c"})
assert.Equal(t, []string{"a", "b", "c"}, ret)
}
func Test_getPyList(t *testing.T) {
ret := getPyList("zhong")
assert.Equal(t, []string{"zhong", "zhon", "zho", "zh", "z"}, ret)
ret = getPyList("z")
assert.Equal(t, []string{"z"}, ret)
ret = getPyList("")
assert.Nil(t, ret)
}
func Test_matchAux(t *testing.T) {
isMatch, end, n := matchAux("dong", "dong")
assert.True(t, isMatch)
assert.True(t, end)
_ = n
isMatch, end, _ = matchAux("dong", "don")
assert.True(t, isMatch)
assert.True(t, end)
isMatch, end, n = matchAux("dong", "dongsh")
assert.True(t, isMatch)
assert.False(t, end)
assert.Equal(t, 4, n)
isMatch, _, _ = matchAux("dong", "sh")
assert.False(t, isMatch)
}
func TestSplit(t *testing.T) {
ret := Split("")
assert.Nil(t, ret)
ret = Split("中文汉字")
assert.Equal(t, Blocks{
zhBlock{zh: '中', pys: []string{"zhong"}},
zhBlock{zh: '文', pys: []string{"wen"}},
zhBlock{zh: '汉', pys: []string{"han"}},
zhBlock{zh: '字', pys: []string{"zi"}}},
ret)
ret = Split("的不")
assert.Equal(t, Blocks{
zhBlock{zh: '的', pys: []string{"de", "di"}},
zhBlock{zh: '不', pys: []string{"bu", "fou", "fu"}},
}, ret)
ret = Split(" english word web-browser ")
assert.Equal(t, Blocks{
commonBlock("english"),
commonBlock("word"),
commonBlock("web"),
commonBlock("browser"),
}, ret)
ret = Split(" hello-world! 中文 english word ")
assert.Equal(t, Blocks{
commonBlock("hello"),
commonBlock("world"),
zhBlock{zh: '中', pys: []string{"zhong"}},
zhBlock{zh: '文', pys: []string{"wen"}},
commonBlock("english"),
commonBlock("word"),
}, ret)
}
func Test_matchBegin(t *testing.T) {
blocks := Split("钟南")
ret := matchBegin(blocks, "zhongnan")
assert.True(t, ret)
ret = matchBegin(blocks, "zn")
assert.True(t, ret)
ret = matchBegin(blocks, "z")
assert.True(t, ret)
ret = matchBegin(blocks, "n")
assert.False(t, ret)
ret = matchBegin(blocks, "zhongna")
assert.True(t, ret)
ret = matchBegin(blocks, "zhona")
assert.True(t, ret)
ret = matchBegin(blocks, "zhna")
assert.True(t, ret)
ret = matchBegin(blocks, "钟nan")
assert.True(t, ret)
blocks = Split("open web browser")
ret = matchBegin(blocks, "open")
assert.True(t, ret)
ret = matchBegin(blocks, "op")
assert.True(t, ret)
ret = matchBegin(blocks, "openwebbrowser")
assert.True(t, ret)
ret = matchBegin(blocks, "openweb")
assert.True(t, ret)
ret = matchBegin(blocks, "opweb")
assert.False(t, ret)
}
func TestMatch(t *testing.T) {
blocks := Split("李钟南")
ret := blocks.Match("zhongnan")
assert.True(t, ret)
blocks = Split("启动firefox")
ret = blocks.Match("dofirefox")
assert.True(t, ret)
ret = blocks.Match("firefox")
assert.True(t, ret)
ret = blocks.Match("fir")
assert.True(t, ret)
ret = blocks.Match("firg")
assert.False(t, ret)
}
|