Beispiel #1
0
func TestJoin(t *testing.T) {
	testing2.
		Expect("abc,abc,abc").Arg("abc", ",", 3).
		Expect("abc,abc").Arg("abc", ",", 2).
		Expect("abc").Arg("abc", ",", 1).
		Expect("").Arg("abc", ",", 0).
		Run(t, RepeatJoin)

	testing2.
		Expect("").Arg([]int{}, ",").
		Expect("1,2,3,4").Arg([]int{1, 2, 3, 4}, ",").
		Run(t, JoinInt)

	testing2.
		Expect("").Arg([]uint{}, ",").
		Expect("1,2,3,4").Arg([]uint{1, 2, 3, 4}, ",").
		Run(t, JoinUint)

	testing2.
		Expect("").Arg([]string{}, "=?", ", ").
		Expect("a=?").Arg([]string{"a"}, "=?", ", ").
		Expect("a=?, b=?, c=?").Arg([]string{"a", "b", "c"}, "=?", ", ").
		Run(t, SuffixJoin)

}
Beispiel #2
0
func TestCols(t *testing.T) {
	var cols Cols = &cols{cols: []string{"id", "age", "name"}}

	testing2.Eq(t, 3, cols.Length())
	testing2.
		Expect("?,?,?").Arg(cols.OnlyParam()).
		Expect("id,age,name").Arg(cols.String()).
		Expect("id=?,age=?,name=?").Arg(cols.Paramed()).
		Run(t, strings2.RemoveSpace)

	cols = singleCol("id")
	testing2.Eq(t, 1, cols.Length())
	testing2.
		Expect("?").Arg(cols.OnlyParam()).
		Expect("id").Arg(cols.String()).
		Expect("id=?").Arg(cols.Paramed()).
		Run(t, strings2.RemoveSpace)

	cols = _emptyCols
	testing2.Eq(t, 0, cols.Length())
	testing2.
		Expect("").Arg(cols.OnlyParam()).
		Expect("").Arg(cols.String()).
		Expect("").Arg(cols.Paramed()).
		Run(t, strings2.RemoveSpace)
}
Beispiel #3
0
func testPager(p *Pager, t *testing.T) {
	testing2.
		Expect(p.BeginIndex).Arg("abcde").
		Expect(p.BeginIndex).Arg("").
		Expect(p.BeginIndex).Arg("-1").
		Expect(p.BeginIndex).Arg("0").
		Expect(p.BeginIndex).Arg("1").
		Expect(p.PageSize).Arg("2").
		Expect(p.PageSize*2).Arg("3").
		Run(t, p.BeginByString)

	testing2.
		Expect(p.PageSize).Arg("abcde").
		Expect(p.PageSize).Arg("").
		Expect(p.PageSize).Arg("-1").
		Expect(p.PageSize).Arg("0").
		Expect(p.PageSize).Arg("1").
		Expect(p.PageSize*2).Arg("2").
		Expect(p.PageSize*3).Arg("3").
		Run(t, p.EndByString)

	testing2.
		Expect(p.PageSize).Arg(-1).
		Expect(p.PageSize).Arg(0).
		Expect(p.PageSize).Arg(1).
		Expect(p.PageSize*2).Arg(2).
		Expect(p.PageSize*3).Arg(3).
		Run(t, p.End)
}
Beispiel #4
0
func TestAbridgeString(t *testing.T) {
	testing2.
		Expect("ABC").Arg("AaaBbbCcc").
		Expect("ABC").Arg("AaaBbbCcc").
		Expect("").Arg("").
		Run(t, ToAbridge)

	testing2.
		Expect("abc").Arg("AaaBbbCcc").
		Expect("abc").Arg("AaaBbbCcc").
		Expect("").Arg("").
		Run(t, ToLowerAbridge)
}
Beispiel #5
0
func TestNumMatched(t *testing.T) {
	testing2.
		Expect(0).Arg(IsNotEmpty, "", "", "", "").
		Expect(1).Arg(IsNotEmpty, "1", "", "", "").
		Expect(2).Arg(IsNotEmpty, "1", "2", "", "").
		Run(t, NumMatched)

	testing2.
		Expect(4).Arg(IsEmpty, "", "", "", "").
		Expect(3).Arg(IsEmpty, "1", "", "", "").
		Expect(2).Arg(IsEmpty, "1", "2", "", "").
		Run(t, NumMatched)
}
Beispiel #6
0
func TestMidSep(t *testing.T) {
	testing2.
		Expect(-1).Arg("123", byte('1')).
		Expect(-1).Arg("123", byte('3')).
		Expect(-1).Arg("123", byte('4')).
		Expect(1).Arg("123", byte('2')).
		Run(t, MidIndex)

	testing2.
		Expect("", "").Arg("123", byte('1')).
		Expect("", "").Arg("123", byte('3')).
		Expect("", "").Arg("123", byte('4')).
		Expect("1", "3").Arg("123", byte('2')).
		Run(t, Separate)
}
Beispiel #7
0
func TestSplitAtN(t *testing.T) {
	testing2.
		Expect(3).Arg("123123123", "12", 2).
		Expect(6).Arg("123123123", "12", 3).
		Expect(-1).Arg("123123123", "12", 4).
		Run(t, IndexN)
}
Beispiel #8
0
func TestRemoveSpace(t *testing.T) {
	testing2.
		Expect("abcdefg").Arg(`a b
    	c d 	e
    	 	f g`).
		Run(t, RemoveSpace)
}
Beispiel #9
0
func TestTrimQuote(t *testing.T) {
	testing2.
		Expect("aaa", true).Arg("\"aaa\"").
		Expect("aaa", true).Arg("'aaa'").
		Expect("aaa", true).Arg("`aaa`").
		Expect("", testing2.NonNil).Arg("`aaa").
		Run(t, TrimQuote)
}
Beispiel #10
0
func TestIndexNonSpace(t *testing.T) {
	testing2.
		Expect(1).Arg(" 1             ").
		Expect(-1).Arg("             ").
		Expect(0).Arg("a   ").
		Run(t, IndexNonSpace).
		Run(t, LastIndexNonSpace)
}
Beispiel #11
0
func TestCompare(t *testing.T) {
	testing2.
		Expect(1).Arg("123", "012").
		Expect(-1).Arg("123", "212").
		Expect(0).Arg("123", "123").
		Expect(-1).Arg("123", "1234").
		Expect(1).Arg("123", "12").
		Run(t, Compare)
}
Beispiel #12
0
func TestSnakeCase(t *testing.T) {
	testing2.
		Expect("_xy_xy").Arg("_xy_xy").
		Expect("_xy_xy").Arg("_xy_xy").
		Expect("_xy_xy").Arg("_xyXy").
		Expect("_xy xy").Arg("_Xy Xy").
		Expect("_xy_xy").Arg("_Xy_Xy").
		Run(t, ToSnake)
}
Beispiel #13
0
func TestUser(t *testing.T) {
	tt := testing2.Wrap(t)

	u1 := User{
		Name: "User1",
		Age:  20,
	}
	tt.Nil(u1.Add()) // add user1

	u2 := u1 // duplicate user name
	tt.Eq(ErrDuplicateUserName, u2.Add())
	u2.Name = "User2"
	tt.Nil(u2.Add()) // add user2

	f1 := Follow{
		UserId:       u1.Id,
		FollowUserId: 1024000, // user id doesn't exists
	}
	tt.Eq(ErrNoUser, f1.Add())
	f1.FollowUserId = u2.Id
	tt.Nil(f1.Add()) // user1 follow user2

	f2 := Follow{
		UserId:       u2.Id,
		FollowUserId: u1.Id,
	}

	tt.
		Nil(f2.Add()). // user2 follow user1
		Eq(ErrFollowed, f2.Add())

	tt.
		Nil(u1.ById()). // query latest user1 info
		Eq(1, u1.Followers).
		Eq(1, u1.Followings).
		Nil(u2.ById()). // query latest user2 info
		Eq(1, u2.Followers).
		Eq(1, u2.Followings)

	tt.
		Nil(f1.Delete()). // delete follow relationships
		Nil(f2.Delete())

	tt.
		Nil(u1.ById()). // query latest user1 info
		Eq(0, u1.Followings).
		Eq(0, u1.Followings).
		Nil(u2.ById()). // query latest user2 info
		Eq(0, u2.Followings).
		Eq(0, u2.Followings)

	testing2.
		Expect(nil).Arg(u1.Id). // delete user1, user2
		Expect(nil).Arg(u2.Id).
		Run(t, DeleteUserById)
}
Beispiel #14
0
func TestParseHuman(t *testing.T) {
	testing2.
		Expect(time.Hour, nil).Arg("1H").
		Expect(time.Minute, nil).Arg("1M").
		Expect(time.Second, nil).Arg("1S").
		Expect(time.Millisecond, nil).Arg("1m").
		Expect(time.Microsecond, nil).Arg("1u").
		Expect(time.Nanosecond, nil).Arg("1n").
		Expect(time.Duration(0), testing2.NonNil).Arg("1z").
		Run(t, ParseHuman)
}
Beispiel #15
0
func TestIn(t *testing.T) {
	tt := testing2.Wrap(t)

	tt.Eq(1, StringIn("b", []string{"a", "b", "c"}))
	tt.Eq(0, StringIn("a", []string{"a", "b", "c"}))
	tt.Eq(-1, StringIn("d", []string{"a", "b", "c"}))

	tt.Eq(0, CharIn('a', "abc"))
	tt.Eq(1, CharIn('b', "abc"))
	tt.Eq(2, CharIn('d', "abd"))
	tt.Eq(-1, CharIn('e', "abcd"))

	testing2.
		Expect(uint(0)).Arg(-1, uint(1<<0|1<<2)).
		Expect(uint(0)).Arg(8, uint(1<<0|1<<2)).
		Expect(uint(0)).Arg(1, uint(1<<0|1<<2)).
		Expect(uint(1<<1)).Arg(1, uint(1<<1|1<<2|1<<3)).
		Expect(uint(1<<2)).Arg(2, uint(1<<2|1<<2|1<<3)).
		Run(t, BitIn)

	testing2.
		Expect(uint(0)).Arg(-1, uint(1<<0|1<<2)).
		Expect(uint(0)).Arg(2, uint(1<<0|1<<2)).
		Expect(uint(1<<9)).Arg(9, uint(1<<0|1<<2)).
		Expect(uint(1<<5)).Arg(5, uint(1<<1|1<<2|1<<3)).
		Expect(uint(1<<8)).Arg(8, uint(1<<2|1<<2|1<<3)).
		Run(t, BitNotIn)

	tt.Eq(2, ByteIn('A', 'B', 'C', 'A'))
	tt.Eq(0, ByteIn('A', 'A', 'B', 'C', 'A'))
	tt.Eq(-1, ByteIn('A', 'B', 'C'))

	tt.Eq(2, SortedNumberIn('C', 'A', 'B', 'C'))
	tt.Eq(0, SortedNumberIn('A', 'A', 'B', 'C', 'D'))
	tt.Eq(-1, SortedNumberIn('A', 'B', 'C'))

	tt.Eq(3, RuneIn('界', '你', '好', '世', '界'))
	tt.Eq(0, RuneIn('你', '你', '好', '世', '界'))
	tt.Eq(-1, RuneIn('是', '你', '好', '世', '界'))

}
Beispiel #16
0
func TestCamelString(t *testing.T) {
	testing2.
		Expect("XyXy").Arg("xy_xy").
		Expect("Xy__Xy").Arg("xy__Xy").
		Expect("Xy Xy").Arg("xy Xy").
		Expect("XY Xy").Arg("x_y Xy").
		Expect("X_Y XY").Arg("x__y XY").
		Expect("XY XY").Arg("x_y xY").
		Expect("XY XY").Arg("x_y _x_y").
		Expect("  XY").Arg("  x_y").
		Run(t, ToCamel)
}
Beispiel #17
0
func TestTrim(t *testing.T) {
	tt := testing2.Wrap(t)

	tt.
		Eq("0123", TrimAfter("01234567", "45")).
		Eq("4567", TrimBefore("01234567", "23"))

	left, right := ("["), ("]")
	testing2.
		Expect("123", true).Arg("[123]", left, right, true).
		Expect(testing2.NoCheck, false).Arg("[123", left, right, true).
		Expect("123", true).Arg("[123", left, right, false).
		Expect("123", true).Arg("123", left, right, true).
		Expect("", true).Arg("", left, right, true).
		Run(t, TrimWrap)

	testing2.
		Expect([]string{"1", "2", "3"}).Arg(" 1 , 2  , 3   ", ",").
		Run(t, SplitAndTrim)

	tt.Eq("abcde", TrimAndToLower("  ABCDE "))
	tt.Eq("ABCDE", TrimAndToUpper("  abcde "))
}
Beispiel #18
0
func TestUnitSize(t *testing.T) {
	testing2.
		Expect(uint(1)).Arg(uint(0)).
		Expect(uint(1)).Arg(uint(1)).
		Expect(uint(1)).Arg(uint(2)).
		Expect(uint(2)).Arg(uint(3)).
		Expect(uint(2)).Arg(uint(4)).
		Expect(uint(3)).Arg(uint(5)).
		Expect(uint(3)).Arg(uint(6)).
		Expect(uint(3)).Arg(uint(7)).
		Expect(uint(3)).Arg(uint(8)).
		Expect(uint(4)).Arg(uint(9)).
		Run(t, UnitSize)
}
Beispiel #19
0
func TestTrim(t *testing.T) {
	tt := testing2.Wrap(t)

	tt.
		Eq("0123", TrimAfter("01234567", "45")).
		Eq("4567", TrimBefore("01234567", "23"))

	left, right := ("["), ("]")
	testing2.
		Expect("123", true).Arg("[123]", left, right, true).
		Expect(testing2.NoCheck, false).Arg("[123", left, right, true).
		Expect("123", true).Arg("[123", left, right, false).
		Expect("123", true).Arg("123", left, right, true).
		Run(t, TrimWrap)
}
Beispiel #20
0
func TestMergeSpace(t *testing.T) {
	testing2.
		Expect("a b c dd").Arg("   a    b   c  dd   ", true).
		Expect(" a b c dd ").Arg("   a    b   c  dd   ", false).
		Run(t, MergeSpace)
}
Beispiel #21
0
func TestRepeatJoin(t *testing.T) {
	testing2.
		Expect("abc=?,abc=?,abc").Arg("abc", "=?,", 3).
		Run(t, RepeatJoin)
}
Beispiel #22
0
func TestLastIndexByte(t *testing.T) {
	testing2.
		Expect(0).Arg("123", byte('1')).
		Expect(-1).Arg("123", byte('4')).
		Run(t, LastIndexByte)
}