2016-08-14 19:56:55 +00:00
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"reflect"
|
|
|
|
|
"testing"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestIsRoot(t *testing.T) {
|
|
|
|
|
if !isRoot("/") {
|
|
|
|
|
t.Errorf(`"/" is root`)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
paths := []string{
|
|
|
|
|
"",
|
|
|
|
|
"~",
|
|
|
|
|
"foo",
|
|
|
|
|
"foo/bar",
|
|
|
|
|
"foo/bar",
|
|
|
|
|
"/home",
|
|
|
|
|
"/home/user",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, p := range paths {
|
|
|
|
|
if isRoot(p) {
|
|
|
|
|
t.Errorf("'%s' is not root", p)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-20 20:45:06 +00:00
|
|
|
|
func TestRuneWidth(t *testing.T) {
|
2016-12-18 15:01:45 +00:00
|
|
|
|
tests := []struct {
|
|
|
|
|
r rune
|
|
|
|
|
exp int
|
2016-10-20 20:45:06 +00:00
|
|
|
|
}{
|
|
|
|
|
{' ', 1},
|
|
|
|
|
{'a', 1},
|
|
|
|
|
{'ı', 1},
|
|
|
|
|
{'ş', 1},
|
|
|
|
|
{'世', 2},
|
|
|
|
|
{'界', 2},
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-18 15:01:45 +00:00
|
|
|
|
for _, test := range tests {
|
|
|
|
|
if got := runeWidth(test.r); got != test.exp {
|
|
|
|
|
t.Errorf("at input '%c' expected '%d' but got '%d'", test.r, test.exp, got)
|
2016-10-20 20:45:06 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestRuneSliceWidth(t *testing.T) {
|
2016-12-18 15:01:45 +00:00
|
|
|
|
tests := []struct {
|
|
|
|
|
rs []rune
|
|
|
|
|
exp int
|
2016-10-20 20:45:06 +00:00
|
|
|
|
}{
|
|
|
|
|
{[]rune{'a', 'b'}, 2},
|
|
|
|
|
{[]rune{'ı', 'ş'}, 2},
|
|
|
|
|
{[]rune{'世', '界'}, 4},
|
|
|
|
|
{[]rune{'世', 'a', '界', 'ı'}, 6},
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-18 15:01:45 +00:00
|
|
|
|
for _, test := range tests {
|
|
|
|
|
if got := runeSliceWidth(test.rs); got != test.exp {
|
|
|
|
|
t.Errorf("at input '%v' expected '%d' but got '%d'", test.rs, test.exp, got)
|
2016-10-20 20:45:06 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestRuneSliceWidthRange(t *testing.T) {
|
2016-12-18 15:01:45 +00:00
|
|
|
|
tests := []struct {
|
|
|
|
|
rs []rune
|
|
|
|
|
beg int
|
|
|
|
|
end int
|
|
|
|
|
exp []rune
|
2016-10-20 20:45:06 +00:00
|
|
|
|
}{
|
|
|
|
|
{[]rune{'a', 'b', 'c', 'd'}, 1, 3, []rune{'b', 'c'}},
|
|
|
|
|
{[]rune{'a', 'ı', 'b', 'ş'}, 1, 3, []rune{'ı', 'b'}},
|
|
|
|
|
{[]rune{'世', '界', '世', '界'}, 2, 6, []rune{'界', '世'}},
|
|
|
|
|
{[]rune{'世', '界', '世', '界'}, 3, 6, []rune{'世'}},
|
|
|
|
|
{[]rune{'世', '界', '世', '界'}, 2, 5, []rune{'界'}},
|
|
|
|
|
{[]rune{'世', '界', '世', '界'}, 3, 5, []rune{}},
|
|
|
|
|
{[]rune{'世', 'a', '界', 'ı'}, 2, 5, []rune{'a', '界'}},
|
|
|
|
|
{[]rune{'世', 'a', '界', 'ı'}, 2, 4, []rune{'a'}},
|
|
|
|
|
{[]rune{'世', 'a', '界', 'ı'}, 3, 5, []rune{'界'}},
|
|
|
|
|
{[]rune{'世', 'a', '界', 'ı'}, 3, 4, []rune{}},
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-18 15:01:45 +00:00
|
|
|
|
for _, test := range tests {
|
|
|
|
|
if got := runeSliceWidthRange(test.rs, test.beg, test.end); !reflect.DeepEqual(got, test.exp) {
|
|
|
|
|
t.Errorf("at input '%v' expected '%v' but got '%v'", test.rs, test.rs, got)
|
2016-10-20 20:45:06 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-02 21:28:25 +00:00
|
|
|
|
func TestEscape(t *testing.T) {
|
2016-12-18 15:01:45 +00:00
|
|
|
|
tests := []struct {
|
|
|
|
|
s string
|
|
|
|
|
exp string
|
2016-12-02 21:28:25 +00:00
|
|
|
|
}{
|
|
|
|
|
{"", ""},
|
|
|
|
|
{"foo", "foo"},
|
|
|
|
|
{"foo bar", `foo\ bar`},
|
|
|
|
|
{"foo bar", `foo\ \ bar`},
|
|
|
|
|
{`foo\ bar`, `foo\ bar`},
|
|
|
|
|
{`foo\bar`, `foo\bar`},
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-18 15:01:45 +00:00
|
|
|
|
for _, test := range tests {
|
|
|
|
|
if got := escape(test.s); !reflect.DeepEqual(got, test.exp) {
|
|
|
|
|
t.Errorf("at input '%v' expected '%v' but got '%v'", test.s, test.exp, got)
|
2016-12-02 21:28:25 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestUnescape(t *testing.T) {
|
2016-12-18 15:01:45 +00:00
|
|
|
|
tests := []struct {
|
|
|
|
|
s string
|
|
|
|
|
exp string
|
2016-12-02 21:28:25 +00:00
|
|
|
|
}{
|
|
|
|
|
{"", ""},
|
|
|
|
|
{"foo", "foo"},
|
|
|
|
|
{`foo\bar`, `foo\bar`},
|
|
|
|
|
{`foo\ bar`, "foo bar"},
|
|
|
|
|
{`foo\ \ bar`, "foo bar"},
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-18 15:01:45 +00:00
|
|
|
|
for _, test := range tests {
|
|
|
|
|
if got := unescape(test.s); !reflect.DeepEqual(got, test.exp) {
|
|
|
|
|
t.Errorf("at input '%v' expected '%v' but got '%v'", test.s, test.exp, got)
|
2016-12-02 21:28:25 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestTokenize(t *testing.T) {
|
2016-12-18 15:01:45 +00:00
|
|
|
|
tests := []struct {
|
|
|
|
|
s string
|
|
|
|
|
exp []string
|
2016-12-02 21:28:25 +00:00
|
|
|
|
}{
|
|
|
|
|
{"", []string{""}},
|
|
|
|
|
{"foo", []string{"foo"}},
|
|
|
|
|
{"foo bar", []string{"foo", "bar"}},
|
|
|
|
|
{`:rename foo\ bar`, []string{":rename", `foo\ bar`}},
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-18 15:01:45 +00:00
|
|
|
|
for _, test := range tests {
|
|
|
|
|
if got := tokenize(test.s); !reflect.DeepEqual(got, test.exp) {
|
|
|
|
|
t.Errorf("at input '%v' expected '%v' but got '%v'", test.s, test.exp, got)
|
2016-12-02 21:28:25 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-06 14:06:25 +00:00
|
|
|
|
func TestSplitWord(t *testing.T) {
|
2016-12-18 15:01:45 +00:00
|
|
|
|
tests := []struct {
|
2016-11-06 14:06:25 +00:00
|
|
|
|
s string
|
|
|
|
|
word string
|
|
|
|
|
rest string
|
|
|
|
|
}{
|
|
|
|
|
{"", "", ""},
|
|
|
|
|
{"foo", "foo", ""},
|
|
|
|
|
{" foo", "foo", ""},
|
|
|
|
|
{"foo ", "foo", ""},
|
|
|
|
|
{" foo ", "foo", ""},
|
|
|
|
|
{"foo bar baz", "foo", "bar baz"},
|
|
|
|
|
{" foo bar baz", "foo", "bar baz"},
|
|
|
|
|
{"foo bar baz", "foo", "bar baz"},
|
|
|
|
|
{" foo bar baz", "foo", "bar baz"},
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-18 15:01:45 +00:00
|
|
|
|
for _, test := range tests {
|
|
|
|
|
if w, r := splitWord(test.s); w != test.word || r != test.rest {
|
|
|
|
|
t.Errorf("at input '%s' expected '%s' and '%s' but got '%s' and '%s'", test.s, test.word, test.rest, w, r)
|
2016-11-06 14:06:25 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-14 19:56:55 +00:00
|
|
|
|
func TestHumanize(t *testing.T) {
|
2016-12-18 15:01:45 +00:00
|
|
|
|
tests := []struct {
|
|
|
|
|
i int64
|
|
|
|
|
exp string
|
2016-08-14 19:56:55 +00:00
|
|
|
|
}{
|
|
|
|
|
{0, "0"},
|
|
|
|
|
{9, "9"},
|
|
|
|
|
{99, "99"},
|
|
|
|
|
{999, "999"},
|
|
|
|
|
{1000, "1.0K"},
|
|
|
|
|
{1023, "1.0K"},
|
|
|
|
|
{1025, "1.0K"},
|
|
|
|
|
{1049, "1.0K"},
|
|
|
|
|
{1050, "1.0K"},
|
|
|
|
|
{1099, "1.0K"},
|
|
|
|
|
{9999, "9.9K"},
|
|
|
|
|
{10000, "10K"},
|
|
|
|
|
{10100, "10K"},
|
|
|
|
|
{10500, "10K"},
|
|
|
|
|
{1000000, "1.0M"},
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-18 15:01:45 +00:00
|
|
|
|
for _, test := range tests {
|
|
|
|
|
if got := humanize(test.i); got != test.exp {
|
|
|
|
|
t.Errorf("at input '%d' expected '%s' but got '%s'", test.i, test.exp, got)
|
2016-08-14 19:56:55 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-17 20:38:34 +00:00
|
|
|
|
func TestNaturalLess(t *testing.T) {
|
|
|
|
|
tests := []struct {
|
2016-12-18 15:01:45 +00:00
|
|
|
|
s1 string
|
|
|
|
|
s2 string
|
|
|
|
|
exp bool
|
2016-08-14 19:56:55 +00:00
|
|
|
|
}{
|
2016-12-17 20:38:34 +00:00
|
|
|
|
{"foo", "bar", false},
|
|
|
|
|
{"bar", "baz", true},
|
|
|
|
|
{"foo", "123", false},
|
|
|
|
|
{"foo1", "foobar", true},
|
|
|
|
|
{"foo1", "foo10", true},
|
|
|
|
|
{"foo2", "foo10", true},
|
|
|
|
|
{"foo1", "foo10bar", true},
|
|
|
|
|
{"foo2", "foo10bar", true},
|
|
|
|
|
{"foo1bar", "foo10bar", true},
|
|
|
|
|
{"foo2bar", "foo10bar", true},
|
|
|
|
|
{"foo1bar", "foo10", true},
|
|
|
|
|
{"foo2bar", "foo10", true},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, test := range tests {
|
2016-12-18 15:01:45 +00:00
|
|
|
|
if got := naturalLess(test.s1, test.s2); got != test.exp {
|
|
|
|
|
t.Errorf("at input '%s' and '%s' expected '%t' but got '%t'", test.s1, test.s2, test.exp, got)
|
2016-08-14 19:56:55 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|