Beispiel #1
0
func (s *CheckersS) TestPanics(c *check.C) {
	testInfo(c, check.Panics, "Panics", []string{"function", "expected"})

	// Some errors.
	testCheck(c, check.Panics, false, "Function has not panicked", func() bool { return false }, "BOOM")
	testCheck(c, check.Panics, false, "Function must take zero arguments", 1, "BOOM")

	// Plain strings.
	testCheck(c, check.Panics, true, "", func() { panic("BOOM") }, "BOOM")
	testCheck(c, check.Panics, false, "", func() { panic("KABOOM") }, "BOOM")
	testCheck(c, check.Panics, true, "", func() bool { panic("BOOM") }, "BOOM")

	// Error values.
	testCheck(c, check.Panics, true, "", func() { panic(errors.New("BOOM")) }, errors.New("BOOM"))
	testCheck(c, check.Panics, false, "", func() { panic(errors.New("KABOOM")) }, errors.New("BOOM"))

	type deep struct{ i int }
	// Deep value
	testCheck(c, check.Panics, true, "", func() { panic(&deep{99}) }, &deep{99})

	// Verify params/names mutation
	params, names := testCheck(c, check.Panics, false, "", func() { panic(errors.New("KABOOM")) }, errors.New("BOOM"))
	c.Assert(params[0], check.ErrorMatches, "KABOOM")
	c.Assert(names[0], check.Equals, "panic")

	// Verify a nil panic
	testCheck(c, check.Panics, true, "", func() { panic(nil) }, nil)
	testCheck(c, check.Panics, false, "", func() { panic(nil) }, "NOPE")
}
Beispiel #2
0
func testToFloat64(c *check.C, val interface{}, expect float64) {
	b, err := ToFloat64(val)
	c.Assert(err, check.IsNil)
	diff := math.Abs(b - expect)
	Epsilon := float64(0.00000001)
	c.Assert(Epsilon, check.Greater, diff)
}
Beispiel #3
0
func (s *testFieldTypeSuite) TestDefaultTypeForValue(c *check.C) {
	nullType := DefaultTypeForValue(nil)
	di := &DataItem{Type: nullType}
	cases := []struct {
		value interface{}
		tp    byte
	}{
		{nil, mysql.TypeNull},
		{1, mysql.TypeLonglong},
		{uint64(1), mysql.TypeLonglong},
		{"abc", mysql.TypeVarString},
		{1.1, mysql.TypeNewDecimal},
		{[]byte("abc"), mysql.TypeBlob},
		{mysql.Bit{}, mysql.TypeBit},
		{mysql.Hex{}, mysql.TypeVarchar},
		{mysql.Time{Type: mysql.TypeDatetime}, mysql.TypeDatetime},
		{mysql.Duration{}, mysql.TypeDuration},
		{mysql.Decimal{}, mysql.TypeNewDecimal},
		{mysql.Enum{}, mysql.TypeEnum},
		{mysql.Set{}, mysql.TypeSet},
		{di, mysql.TypeNull},
	}
	for _, ca := range cases {
		ft := DefaultTypeForValue(ca.value)
		c.Assert(ft.Tp, check.Equals, ca.tp, check.Commentf("%v %v", ft, ca))
	}
}
Beispiel #4
0
func (s *FoundationS) TestExpectFailureSucceed(c *check.C) {
	helper := ExpectFailureSucceedHelper{}
	output := String{}
	result := check.Run(&helper, &check.RunConf{Output: &output})

	c.Assert(output.value, check.Equals, "")
	c.Assert(result.ExpectedFailures, check.Equals, 1)
}
Beispiel #5
0
func (s *BootstrapS) TestMinLogger(c *check.C) {
	var logger minLogger
	logger = log.New(os.Stderr, "", 0)
	logger = c
	logger.Output(0, "Hello there")
	expected := `\[LOG\] [0-9]+:[0-9][0-9]\.[0-9][0-9][0-9] +Hello there\n`
	output := c.GetTestLog()
	c.Assert(output, check.Matches, expected)
}
Beispiel #6
0
func (s *HelpersS) TestAssertSucceedWithoutExpected(c *check.C) {
	checker := &MyChecker{result: true, info: &check.CheckerInfo{Params: []string{"myvalue"}}}
	testHelperSuccess(c, "Assert(1, checker)", nil, func() interface{} {
		c.Assert(1, checker)
		return nil
	})
	if !reflect.DeepEqual(checker.params, []interface{}{1}) {
		c.Fatalf("Bad params for check: %#v", checker.params)
	}
}
Beispiel #7
0
func accept(c *check.C, tp byte, value interface{}, unsigned bool, expected string) {
	ft := NewFieldType(tp)
	if unsigned {
		ft.Flag |= mysql.UnsignedFlag
	}
	//	casted, err := col.CastValue(nil, value)
	casted, err := Convert(value, ft)
	c.Assert(err, check.IsNil, check.Commentf("%v", ft))
	c.Assert(fmt.Sprintf("%v", casted), check.Equals, expected)
}
Beispiel #8
0
func (s *HelpersS) TestAssertSucceedWithExpected(c *check.C) {
	checker := &MyChecker{result: true}
	testHelperSuccess(c, "Assert(1, checker, 2)", nil, func() interface{} {
		c.Assert(1, checker, 2)
		return nil
	})
	if !reflect.DeepEqual(checker.params, []interface{}{1, 2}) {
		c.Fatalf("Bad params for check: %#v", checker.params)
	}
}
Beispiel #9
0
func (s *HelpersS) TestAssertFailWithoutExpected(c *check.C) {
	checker := &MyChecker{result: false, info: &check.CheckerInfo{Params: []string{"myvalue"}}}
	log := "(?s)helpers_test\\.go:[0-9]+:.*\nhelpers_test\\.go:[0-9]+:\n" +
		"    c\\.Assert\\(1, checker\\)\n" +
		"\\.+ myvalue int = 1\n\n"
	testHelperFailure(c, "Assert(1, checker)", nil, true, log,
		func() interface{} {
			c.Assert(1, checker)
			return nil
		})
}
Beispiel #10
0
func (s *HelpersS) TestAssertWithNilChecker(c *check.C) {
	log := "(?s)helpers_test\\.go:[0-9]+:.*\nhelpers_test\\.go:[0-9]+:\n" +
		"    c\\.Assert\\(1, nil\\)\n" +
		"\\.+ Assert\\(obtained, nil!\\?, \\.\\.\\.\\):\n" +
		"\\.+ Oops\\.\\. you've provided a nil checker!\n\n"
	testHelperFailure(c, "Assert(obtained, nil)", nil, true, log,
		func() interface{} {
			c.Assert(1, nil)
			return nil
		})
}
Beispiel #11
0
func (s *HelpersS) TestAssertFailWithExpected(c *check.C) {
	checker := &MyChecker{result: false}
	log := "(?s)helpers_test\\.go:[0-9]+:.*\nhelpers_test\\.go:[0-9]+:\n" +
		"    c\\.Assert\\(1, checker, 2\\)\n" +
		"\\.+ myobtained int = 1\n" +
		"\\.+ myexpected int = 2\n\n"
	testHelperFailure(c, "Assert(1, checker, 2)", nil, true, log,
		func() interface{} {
			c.Assert(1, checker, 2)
			return nil
		})
}
Beispiel #12
0
func (s *HelpersS) TestAssertFailWithExpectedAndMessage(c *check.C) {
	checker := &MyChecker{result: false}
	log := "(?s)helpers_test\\.go:[0-9]+:.*\nhelpers_test\\.go:[0-9]+:\n" +
		"    c\\.Assert\\(1, checker, 2, myComment\\(\"Hello world!\"\\)\\)\n" +
		"\\.+ myobtained int = 1\n" +
		"\\.+ myexpected int = 2\n" +
		"\\.+ Hello world!\n\n"
	testHelperFailure(c, "Assert(1, checker, 2, msg)", nil, true, log,
		func() interface{} {
			c.Assert(1, checker, 2, myComment("Hello world!"))
			return nil
		})
}
Beispiel #13
0
func (s *HelpersS) TestAssertWithError(c *check.C) {
	checker := &MyChecker{result: false, error: "Some not so cool data provided!"}
	log := "(?s)helpers_test\\.go:[0-9]+:.*\nhelpers_test\\.go:[0-9]+:\n" +
		"    c\\.Assert\\(1, checker, 2\\)\n" +
		"\\.+ myobtained int = 1\n" +
		"\\.+ myexpected int = 2\n" +
		"\\.+ Some not so cool data provided!\n\n"
	testHelperFailure(c, "Assert(1, checker, 2)", nil, true, log,
		func() interface{} {
			c.Assert(1, checker, 2)
			return nil
		})
}
Beispiel #14
0
func (s *CheckersS) TestErrorMatches(c *check.C) {
	testInfo(c, check.ErrorMatches, "ErrorMatches", []string{"value", "regex"})

	testCheck(c, check.ErrorMatches, false, "Error value is nil", nil, "some error")
	testCheck(c, check.ErrorMatches, false, "Value is not an error", 1, "some error")
	testCheck(c, check.ErrorMatches, true, "", errors.New("some error"), "some error")
	testCheck(c, check.ErrorMatches, true, "", errors.New("some error"), "so.*or")

	// Verify params mutation
	params, names := testCheck(c, check.ErrorMatches, false, "", errors.New("some error"), "other error")
	c.Assert(params[0], check.Equals, "some error")
	c.Assert(names[0], check.Equals, "error")
}
Beispiel #15
0
func (s *HelpersS) TestAssertWithMissingExpected(c *check.C) {
	checker := &MyChecker{result: true}
	log := "(?s)helpers_test\\.go:[0-9]+:.*\nhelpers_test\\.go:[0-9]+:\n" +
		"    c\\.Assert\\(1, checker\\)\n" +
		"\\.+ Assert\\(myobtained, MyChecker, myexpected\\):\n" +
		"\\.+ Wrong number of parameters for MyChecker: " +
		"want 3, got 2\n\n"
	testHelperFailure(c, "Assert(1, checker, !?)", nil, true, log,
		func() interface{} {
			c.Assert(1, checker)
			return nil
		})
}
Beispiel #16
0
func (s *testTypeConvertSuite) TestConvertToBool(c *check.C) {
	testToBool(c, int(0), 0)
	testToBool(c, int64(0), 0)
	testToBool(c, uint64(0), 0)
	testToBool(c, float32(0), 0)
	testToBool(c, float64(0), 0)
	testToBool(c, "", 0)
	testToBool(c, "0", 0)
	testToBool(c, []byte{}, 0)
	testToBool(c, []byte("0"), 0)
	testToBool(c, mysql.Hex{Value: 0}, 0)
	testToBool(c, mysql.Bit{Value: 0, Width: 8}, 0)
	testToBool(c, mysql.Enum{Name: "a", Value: 1}, 1)
	testToBool(c, mysql.Set{Name: "a", Value: 1}, 1)

	t, err := mysql.ParseTime("2011-11-10 11:11:11.999999", mysql.TypeTimestamp, 6)
	c.Assert(err, check.IsNil)
	testToBool(c, t, 1)

	td, err := mysql.ParseDuration("11:11:11.999999", 6)
	c.Assert(err, check.IsNil)
	testToBool(c, td, 1)

	ft := NewFieldType(mysql.TypeNewDecimal)
	ft.Decimal = 5
	v, err := Convert(3.1415926, ft)
	c.Assert(err, check.IsNil)
	testToBool(c, v, 1)

	_, err = ToBool(&invalidMockType{})
	c.Assert(err, check.NotNil)
}
Beispiel #17
0
func (s *testTypeEtcSuite) TestIsOrderedType(c *check.C) {
	r := IsOrderedType(1)
	c.Assert(r, check.IsTrue)
	r = IsOrderedType(-1)
	c.Assert(r, check.IsTrue)
	r = IsOrderedType(uint(1))
	c.Assert(r, check.IsTrue)

	r = IsOrderedType(mysql.Duration{Duration: time.Duration(0), Fsp: 0})
	c.Assert(r, check.IsTrue)

	r = IsOrderedType([]byte{1})
	c.Assert(r, check.IsTrue)
}
Beispiel #18
0
func (s *testTypeConvertSuite) TestConvertToFloat64(c *check.C) {
	testToFloat64(c, "0", float64(0))
	testToFloat64(c, int(0), float64(0))
	testToFloat64(c, int64(0), float64(0))
	testToFloat64(c, uint64(0), float64(0))
	// TODO: check this
	//testToFloat64(c, float32(3.1), float64(3.1))
	testToFloat64(c, float64(3.1), float64(3.1))
	testToFloat64(c, mysql.Hex{Value: 100}, float64(100))
	testToFloat64(c, mysql.Bit{Value: 100, Width: 8}, float64(100))
	testToFloat64(c, mysql.Enum{Name: "a", Value: 1}, float64(1))
	testToFloat64(c, mysql.Set{Name: "a", Value: 1}, float64(1))

	t, err := mysql.ParseTime("2011-11-10 11:11:11.999999", mysql.TypeTimestamp, 6)
	c.Assert(err, check.IsNil)
	testToFloat64(c, t, float64(20111110111111.999999))

	td, err := mysql.ParseDuration("11:11:11.999999", 6)
	c.Assert(err, check.IsNil)
	testToFloat64(c, td, float64(111111.999999))

	ft := NewFieldType(mysql.TypeNewDecimal)
	ft.Decimal = 5
	v, err := Convert(3.1415926, ft)
	c.Assert(err, check.IsNil)
	testToFloat64(c, v, float64(3.14159))

	_, err = ToFloat64(&invalidMockType{})
	c.Assert(err, check.NotNil)
}
Beispiel #19
0
func (s *HelpersS) TestMkDir(c *check.C) {
	helper := MkDirHelper{}
	output := String{}
	check.Run(&helper, &check.RunConf{Output: &output})
	c.Assert(output.value, check.Equals, "")
	c.Check(helper.isDir1, check.Equals, true)
	c.Check(helper.isDir2, check.Equals, true)
	c.Check(helper.isDir3, check.Equals, true)
	c.Check(helper.isDir4, check.Equals, true)
	c.Check(helper.path1, check.Not(check.Equals),
		helper.path2)
	c.Check(isDir(helper.path1), check.Equals, false)
	c.Check(isDir(helper.path2), check.Equals, false)
}
Beispiel #20
0
func (s *testTypeConvertSuite) TestConvertToString(c *check.C) {
	testToString(c, "0", "0")
	testToString(c, true, "1")
	testToString(c, "false", "false")
	testToString(c, int(0), "0")
	testToString(c, int64(0), "0")
	testToString(c, uint64(0), "0")
	testToString(c, float32(1.6), "1.6")
	testToString(c, float64(-0.6), "-0.6")
	testToString(c, []byte{1}, "\x01")
	testToString(c, mysql.Hex{Value: 0x4D7953514C}, "MySQL")
	testToString(c, mysql.Bit{Value: 0x41, Width: 8}, "A")
	testToString(c, mysql.Enum{Name: "a", Value: 1}, "a")
	testToString(c, mysql.Set{Name: "a", Value: 1}, "a")

	t, err := mysql.ParseTime("2011-11-10 11:11:11.999999", mysql.TypeTimestamp, 6)
	c.Assert(err, check.IsNil)
	testToString(c, t, "2011-11-10 11:11:11.999999")

	td, err := mysql.ParseDuration("11:11:11.999999", 6)
	c.Assert(err, check.IsNil)
	testToString(c, td, "11:11:11.999999")

	ft := NewFieldType(mysql.TypeNewDecimal)
	ft.Decimal = 5
	v, err := Convert(3.1415926, ft)
	c.Assert(err, check.IsNil)
	testToString(c, v, "3.14159")

	_, err = ToString(&invalidMockType{})
	c.Assert(err, check.NotNil)
}
Beispiel #21
0
func (s *testCompareSuite) TestCompareDatum(c *check.C) {
	cmpTbl := []struct {
		lhs Datum
		rhs Datum
		ret int // 0, 1, -1
	}{
		{MaxValueDatum(), NewDatum("00:00:00"), 1},
		{MinNotNullDatum(), NewDatum("00:00:00"), -1},
		{Datum{}, NewDatum("00:00:00"), -1},
		{Datum{}, Datum{}, 0},
		{MinNotNullDatum(), MinNotNullDatum(), 0},
		{MaxValueDatum(), MaxValueDatum(), 0},
		{Datum{}, MinNotNullDatum(), -1},
		{MinNotNullDatum(), MaxValueDatum(), -1},
	}
	for i, t := range cmpTbl {
		comment := check.Commentf("%d %v %v", i, t.lhs, t.rhs)
		ret, err := t.lhs.CompareDatum(t.rhs)
		c.Assert(err, check.IsNil)
		c.Assert(ret, check.Equals, t.ret, comment)

		ret, err = t.rhs.CompareDatum(t.lhs)
		c.Assert(err, check.IsNil)
		c.Assert(ret, check.Equals, -t.ret, comment)
	}
}
Beispiel #22
0
func (s *testTypeEtcSuite) TestTruncate(c *check.C) {
	tbl := []struct {
		Input   float64
		Decimal int
		Expect  float64
	}{
		{100.114, 2, 100.11},
		{100.115, 2, 100.11},
		{100.1156, 2, 100.12},
	}

	for _, t := range tbl {
		f := truncateFloat(t.Input, t.Decimal)
		c.Assert(f, check.Equals, t.Expect)
	}
}
Beispiel #23
0
func (s *testTypeEtcSuite) TestMaxFloat(c *check.C) {
	tbl := []struct {
		Flen    int
		Decimal int
		Expect  float64
	}{
		{3, 2, 9.99},
		{5, 2, 999.99},
		{10, 1, 999999999.9},
		{5, 5, 0.99999},
	}

	for _, t := range tbl {
		f := getMaxFloat(t.Flen, t.Decimal)
		c.Assert(f, check.Equals, t.Expect)
	}
}
Beispiel #24
0
func (t *mysqlTestSuite) TestMysqlGTIDContain(c *check.C) {
	g1, err := ParseMysqlGTIDSet("3E11FA47-71CA-11E1-9E33-C80AA9429562:23")
	c.Assert(err, check.IsNil)

	g2, err := ParseMysqlGTIDSet("3E11FA47-71CA-11E1-9E33-C80AA9429562:21-57")
	c.Assert(err, check.IsNil)

	c.Assert(g2.Contain(g1), check.Equals, true)
	c.Assert(g1.Contain(g2), check.Equals, false)
}
Beispiel #25
0
func (s *FoundationS) TestExpectFailureSucceedVerbose(c *check.C) {
	helper := ExpectFailureSucceedHelper{}
	output := String{}
	result := check.Run(&helper, &check.RunConf{Output: &output, Verbose: true})

	expected := "" +
		"FAIL EXPECTED: foundation_test\\.go:[0-9]+:" +
		" ExpectFailureSucceedHelper\\.TestSucceed \\(It booms!\\)\t *[.0-9]+s\n"

	matched, err := regexp.MatchString(expected, output.value)
	if err != nil {
		c.Error("Bad expression: ", expected)
	} else if !matched {
		c.Error("ExpectFailure() didn't log properly:\n", output.value)
	}

	c.Assert(result.ExpectedFailures, check.Equals, 1)
}
Beispiel #26
0
func (s *testTypeEtcSuite) TestRoundFloat(c *check.C) {
	tbl := []struct {
		Input  float64
		Expect float64
	}{
		{2.5, 2},
		{1.5, 2},
		{0.5, 0},
		{0, 0},
		{-0.5, 0},
		{-2.5, -2},
		{-1.5, -2},
	}

	for _, t := range tbl {
		f := RoundFloat(t.Input)
		c.Assert(f, check.Equals, t.Expect)
	}
}
Beispiel #27
0
func (s *testFieldTypeSuite) TestDataItem(c *check.C) {
	ft := NewFieldType(mysql.TypeBlob)
	d := &DataItem{
		Type: ft,
	}
	c.Assert(RawData(d), check.IsNil)
	c.Assert(IsNil(d), check.IsTrue)

	d.Data = "string"
	c.Assert(RawData(d), check.Equals, "string")
	c.Assert(IsNil(d), check.IsFalse)
}
func (s *testDistinctSuite) TestDistinct(c *check.C) {
	dc := CreateDistinctChecker()
	cases := []struct {
		vals   []interface{}
		expect bool
	}{
		{[]interface{}{1, 1}, true},
		{[]interface{}{1, 1}, false},
		{[]interface{}{1, 2}, true},
		{[]interface{}{1, 2}, false},
		{[]interface{}{1, nil}, true},
		{[]interface{}{1, nil}, false},
	}
	for _, t := range cases {
		d, err := dc.Check(t.vals)
		c.Assert(err, check.IsNil)
		c.Assert(d, check.Equals, t.expect)
	}
}
Beispiel #29
0
func (s *FoundationS) TestExpectFailureFail(c *check.C) {
	helper := ExpectFailureFailHelper{}
	output := String{}
	result := check.Run(&helper, &check.RunConf{Output: &output})

	expected := "" +
		"^\n-+\n" +
		"FAIL: foundation_test\\.go:[0-9]+:" +
		" ExpectFailureFailHelper\\.TestFail\n\n" +
		"\\.\\.\\. Error: Test succeeded, but was expected to fail\n" +
		"\\.\\.\\. Reason: Bug #XYZ\n$"

	matched, err := regexp.MatchString(expected, output.value)
	if err != nil {
		c.Error("Bad expression: ", expected)
	} else if !matched {
		c.Error("ExpectFailure() didn't log properly:\n", output.value)
	}

	c.Assert(result.ExpectedFailures, check.Equals, 0)
}
Beispiel #30
0
func (s *testTypeConvertSuite) TestFieldTypeToStr(c *check.C) {
	v := TypeToStr(mysql.TypeDecimal, "not binary")
	c.Assert(v, check.Equals, type2Str[mysql.TypeDecimal])
	v = TypeToStr(mysql.TypeBlob, charset.CharsetBin)
	c.Assert(v, check.Equals, "blob")
	v = TypeToStr(mysql.TypeString, charset.CharsetBin)
	c.Assert(v, check.Equals, "binary")
}