- All internal tests now use Assert() and Check().
- Deprecated original Assert*() helpers.
- Fixed IsNil and NotNil checker to deal with nil values in channels,
slices, etc.
diff --git a/Makefile b/Makefile
index 4be4dbd..2f61404 100644
--- a/Makefile
+++ b/Makefile
@@ -3,6 +3,6 @@
subdirs:
make -C src $(MAKECMDGOALS)
- make -C src/checkers $(MAKECMDGOALS)
+ make -C src/local $(MAKECMDGOALS)
.PHONY: subdirs
diff --git a/src/Makefile b/src/Makefile
index 6f18a9d..68b538f 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -17,11 +17,11 @@
-testpackage: _test/gocheck/checkers.a
+testpackage: _test/gocheck/local.a
-_test/gocheck/checkers.a: checkers/_test/gocheck/checkers.a
+_test/gocheck/local.a: local/_test/gocheck/local.a
@mkdir -p _test/gocheck
- cp checkers/_test/gocheck/checkers.a _test/gocheck
+ cp local/_test/gocheck/local.a _test/gocheck
-checkers/_test/gocheck/checkers.a:
- make -C checkers _test/gocheck/checkers.a
+local/_test/gocheck/local.a:
+ make -C local _test/gocheck/local.a
diff --git a/src/checkers/checkers.go b/src/checkers/checkers.go
deleted file mode 100644
index 759138d..0000000
--- a/src/checkers/checkers.go
+++ /dev/null
@@ -1,131 +0,0 @@
-package checkers
-
-import (
- "reflect"
- "regexp"
-)
-
-
-// Checkers used with the c.Assert() and c.Check() helpers must have
-// this interface.
-type Checker interface {
- Name() string
- ObtainedLabel() (varName, varLabel string)
- ExpectedLabel() (varName, varLabel string)
- NeedsExpectedValue() bool
- Check(obtained, expected interface{}) (result bool, error string)
-}
-
-
-type CheckerType struct{}
-
-
-// Trick to ensure it matchers the desired interface.
-var _ Checker = (*CheckerType)(nil)
-
-
-// The function name used to build the matcher.
-func (checker *CheckerType) Name() string {
- return "Checker"
-}
-
-// Method must return true if the given matcher needs to be informed
-// of an expected value in addition to the actual value obtained to
-// verify its expectations.
-func (checker *CheckerType) NeedsExpectedValue() bool {
- return true
-}
-
-// Label to be used for the obtained value when reporting a failure
-// in the expectations established.
-func (checker *CheckerType) ObtainedLabel() (varName, varLabel string) {
- return "obtained", "Obtained"
-}
-
-// Label to be used for the obtained value when reporting a failure
-// in the expectations established.
-func (checker *CheckerType) ExpectedLabel() (varName, varLabel string) {
- return "expected", "Expected"
-}
-
-// Method must return true if the obtained value succeeds the
-// expectations established by the given matcher. If an error is
-// returned, it means the provided parameters are somehow invalid.
-func (checker *CheckerType) Check(obtained, expected interface{}) (
- result bool, error string) {
- return false, ""
-}
-
-
-
-// -----------------------------------------------------------------------
-// Equals checker.
-
-var Equals Checker = &equalsChecker{}
-
-type equalsChecker struct {
- CheckerType
-}
-
-func (checker *equalsChecker) Name() string {
- return "Equals"
-}
-
-func (checker *equalsChecker) Check(obtained, expected interface{}) (
- result bool, error string) {
- // This will use a fast path to check for equality of normal types,
- // and then fallback to reflect.DeepEqual if things go wrong.
- defer func() {
- if recover() != nil {
- result = reflect.DeepEqual(obtained, expected)
- }
- }()
- return obtained == expected, ""
-}
-
-
-// -----------------------------------------------------------------------
-// Matches checker.
-
-var Matches Checker = &matchesChecker{}
-
-type matchesChecker struct {
- CheckerType
-}
-
-func (checker *matchesChecker) Name() string {
- return "Matches"
-}
-
-func (checker *matchesChecker) ObtainedLabel() (varName, varLabel string) {
- return "value", "Value"
-}
-
-func (checker *matchesChecker) ExpectedLabel() (varName, varLabel string) {
- return "regex", "Expected to match regex"
-}
-
-type hasString interface {
- String() string
-}
-
-func (checker *matchesChecker) Check(value, re interface{}) (bool, string) {
- reStr, ok := re.(string)
- if !ok {
- return false, "Regex must be a string"
- }
- valueStr, valueIsStr := value.(string)
- if !valueIsStr {
- if valueWithStr, valueHasStr := value.(hasString); valueHasStr {
- valueStr, valueIsStr = valueWithStr.String(), true
- }
- }
- if valueIsStr {
- matches, err := regexp.MatchString("^" + reStr + "$", valueStr)
- if err != nil {
- return false, "Can't compile regex: " + err.String()
- }
- return matches, ""
- }
- return false, "Obtained value is not a string and has no .String()"
-}
diff --git a/src/checkers/checkers_test.go b/src/checkers/checkers_test.go
deleted file mode 100644
index b5b0699..0000000
--- a/src/checkers/checkers_test.go
+++ /dev/null
@@ -1,92 +0,0 @@
-package checkers_test
-
-import (
- "testing"
- "gocheck"
- . "gocheck/checkers"
- "os"
-)
-
-
-func TestGocheck(t *testing.T) {
- gocheck.TestingT(t)
-}
-
-
-type CheckersS struct{}
-
-var _ = gocheck.Suite(&CheckersS{})
-
-
-func testInfo(c *gocheck.C, checker Checker, name,
- obtainedVarName, obtainedVarLabel,
- expectedVarName, expectedVarLabel string) {
- if checker.Name() != name {
- c.Fatalf("Got name %s, expected %s", checker.Name(), name)
- }
- varName, varLabel := checker.ObtainedLabel()
- if varName != obtainedVarName || varLabel != obtainedVarLabel {
- c.Fatalf("Got obtained label (%#v, %#v), expected (%#v, %#v)",
- varName, varLabel, obtainedVarName, obtainedVarLabel)
- }
- varName, varLabel = checker.ExpectedLabel()
- if varName != expectedVarName || varLabel != expectedVarLabel {
- c.Fatalf("Got expected label (%#v, %#v), expected (%#v, %#v)",
- varName, varLabel, expectedVarName, expectedVarLabel)
- }
-}
-
-func testCheck(c *gocheck.C, checker Checker,
- obtained, expected interface{},
- wantedResult bool, wantedError string) {
- result, error := checker.Check(obtained, expected)
- if result != wantedResult || error != wantedError {
- c.Fatalf("%s.Check(%#v, %#v) returned " +
- "(%#v, %#v) rather than (%#v, %#v)",
- checker.Name(), obtained, expected,
- result, error, wantedResult, wantedError)
- }
-}
-
-
-func (s *CheckersS) TestEquals(c *gocheck.C) {
- testInfo(c, Equals, "Equals",
- "obtained", "Obtained", "expected", "Expected")
-
- // The simplest.
- testCheck(c, Equals, 42, 42, true, "")
- testCheck(c, Equals, 42, 43, false, "")
-
- // Different native types.
- testCheck(c, Equals, int32(42), int64(42), false, "")
-
- // With nil.
- testCheck(c, Equals, 42, nil, false, "")
-
- // Arrays
- testCheck(c, Equals, []byte{1,2}, []byte{1,2}, true, "")
- testCheck(c, Equals, []byte{1,2}, []byte{1,3}, false, "")
-}
-
-func (s *CheckersS) TestMatches(c *gocheck.C) {
- testInfo(c, Matches, "Matches",
- "value", "Value", "regex", "Expected to match regex")
-
- // Simple matching
- testCheck(c, Matches, "abc", "abc", true, "")
- testCheck(c, Matches, "abc", "a.c", true, "")
-
- // Must match fully
- testCheck(c, Matches, "abc", "ab", false, "")
- testCheck(c, Matches, "abc", "bc", false, "")
-
- // String()-enabled values accepted
- testCheck(c, Matches, os.NewError("abc"), "a.c", true, "")
- testCheck(c, Matches, os.NewError("abc"), "a.d", false, "")
-
- // Some error conditions.
- testCheck(c, Matches, 1, "a.c", false,
- "Obtained value is not a string and has no .String()")
- testCheck(c, Matches, "abc", "a[c", false,
- "Can't compile regex: unmatched '['")
-}
diff --git a/src/fixture_test.go b/src/fixture_test.go
index 650be97..c606dba 100644
--- a/src/fixture_test.go
+++ b/src/fixture_test.go
@@ -5,7 +5,7 @@
import (
"gocheck"
- "regexp"
+ . "gocheck/local"
)
@@ -27,15 +27,15 @@
func (s *FixtureS) TestOrder(c *gocheck.C) {
helper := FixtureHelper{}
gocheck.Run(&helper, nil)
- c.CheckEqual(helper.calls[0], "SetUpSuite")
- c.CheckEqual(helper.calls[1], "SetUpTest")
- c.CheckEqual(helper.calls[2], "Test1")
- c.CheckEqual(helper.calls[3], "TearDownTest")
- c.CheckEqual(helper.calls[4], "SetUpTest")
- c.CheckEqual(helper.calls[5], "Test2")
- c.CheckEqual(helper.calls[6], "TearDownTest")
- c.CheckEqual(helper.calls[7], "TearDownSuite")
- c.CheckEqual(helper.n, 8)
+ c.Check(helper.calls[0], Equals, "SetUpSuite")
+ c.Check(helper.calls[1], Equals, "SetUpTest")
+ c.Check(helper.calls[2], Equals, "Test1")
+ c.Check(helper.calls[3], Equals, "TearDownTest")
+ c.Check(helper.calls[4], Equals, "SetUpTest")
+ c.Check(helper.calls[5], Equals, "Test2")
+ c.Check(helper.calls[6], Equals, "TearDownTest")
+ c.Check(helper.calls[7], Equals, "TearDownSuite")
+ c.Check(helper.n, Equals, 8)
}
@@ -46,15 +46,15 @@
helper := FixtureHelper{panicOn: "Test1"}
output := String{}
gocheck.Run(&helper, &gocheck.RunConf{Output: &output})
- c.CheckEqual(helper.calls[0], "SetUpSuite")
- c.CheckEqual(helper.calls[1], "SetUpTest")
- c.CheckEqual(helper.calls[2], "Test1")
- c.CheckEqual(helper.calls[3], "TearDownTest")
- c.CheckEqual(helper.calls[4], "SetUpTest")
- c.CheckEqual(helper.calls[5], "Test2")
- c.CheckEqual(helper.calls[6], "TearDownTest")
- c.CheckEqual(helper.calls[7], "TearDownSuite")
- c.CheckEqual(helper.n, 8)
+ c.Check(helper.calls[0], Equals, "SetUpSuite")
+ c.Check(helper.calls[1], Equals, "SetUpTest")
+ c.Check(helper.calls[2], Equals, "Test1")
+ c.Check(helper.calls[3], Equals, "TearDownTest")
+ c.Check(helper.calls[4], Equals, "SetUpTest")
+ c.Check(helper.calls[5], Equals, "Test2")
+ c.Check(helper.calls[6], Equals, "TearDownTest")
+ c.Check(helper.calls[7], Equals, "TearDownSuite")
+ c.Check(helper.n, Equals, 8)
expected := "^\n-+\n" +
"PANIC: gocheck_test\\.go:[0-9]+: FixtureHelper.Test1\n\n" +
@@ -66,23 +66,18 @@
".*gocheck_test.go:[0-9]+\n" +
" in FixtureHelper.Test1\n$"
- matched, err := regexp.MatchString(expected, output.value)
- if err != nil {
- c.Error("Bad expression:", expected)
- } else if !matched {
- c.Error("Panic not logged properly:\n", output.value)
- }
+ c.Check(output.value, Matches, expected)
}
func (s *FixtureS) TestPanicOnSetUpTest(c *gocheck.C) {
helper := FixtureHelper{panicOn: "SetUpTest"}
output := String{}
gocheck.Run(&helper, &gocheck.RunConf{Output: &output})
- c.CheckEqual(helper.calls[0], "SetUpSuite")
- c.CheckEqual(helper.calls[1], "SetUpTest")
- c.CheckEqual(helper.calls[2], "TearDownTest")
- c.CheckEqual(helper.calls[3], "TearDownSuite")
- c.CheckEqual(helper.n, 4)
+ c.Check(helper.calls[0], Equals, "SetUpSuite")
+ c.Check(helper.calls[1], Equals, "SetUpTest")
+ c.Check(helper.calls[2], Equals, "TearDownTest")
+ c.Check(helper.calls[3], Equals, "TearDownSuite")
+ c.Check(helper.n, Equals, 4)
expected := "^\n-+\n" +
"PANIC: gocheck_test\\.go:[0-9]+: " +
@@ -100,24 +95,19 @@
"\\.\\.\\. Panic: Fixture has panicked " +
"\\(see related PANIC\\)\n$"
- matched, err := regexp.MatchString(expected, output.value)
- if err != nil {
- c.Error("Bad expression:", expected)
- } else if !matched {
- c.Error("Panic not logged properly:\n", output.value)
- }
+ c.Check(output.value, Matches, expected)
}
func (s *FixtureS) TestPanicOnTearDownTest(c *gocheck.C) {
helper := FixtureHelper{panicOn: "TearDownTest"}
output := String{}
gocheck.Run(&helper, &gocheck.RunConf{Output: &output})
- c.CheckEqual(helper.calls[0], "SetUpSuite")
- c.CheckEqual(helper.calls[1], "SetUpTest")
- c.CheckEqual(helper.calls[2], "Test1")
- c.CheckEqual(helper.calls[3], "TearDownTest")
- c.CheckEqual(helper.calls[4], "TearDownSuite")
- c.CheckEqual(helper.n, 5)
+ c.Check(helper.calls[0], Equals, "SetUpSuite")
+ c.Check(helper.calls[1], Equals, "SetUpTest")
+ c.Check(helper.calls[2], Equals, "Test1")
+ c.Check(helper.calls[3], Equals, "TearDownTest")
+ c.Check(helper.calls[4], Equals, "TearDownSuite")
+ c.Check(helper.n, Equals, 5)
expected := "^\n-+\n" +
"PANIC: gocheck_test\\.go:[0-9]+: " +
@@ -135,21 +125,16 @@
"\\.\\.\\. Panic: Fixture has panicked " +
"\\(see related PANIC\\)\n$"
- matched, err := regexp.MatchString(expected, output.value)
- if err != nil {
- c.Error("Bad expression:", expected)
- } else if !matched {
- c.Error("Panic not logged properly:\n", output.value)
- }
+ c.Check(output.value, Matches, expected)
}
func (s *FixtureS) TestPanicOnSetUpSuite(c *gocheck.C) {
helper := FixtureHelper{panicOn: "SetUpSuite"}
output := String{}
gocheck.Run(&helper, &gocheck.RunConf{Output: &output})
- c.CheckEqual(helper.calls[0], "SetUpSuite")
- c.CheckEqual(helper.calls[1], "TearDownSuite")
- c.CheckEqual(helper.n, 2)
+ c.Check(helper.calls[0], Equals, "SetUpSuite")
+ c.Check(helper.calls[1], Equals, "TearDownSuite")
+ c.Check(helper.n, Equals, 2)
expected := "^\n-+\n" +
"PANIC: gocheck_test\\.go:[0-9]+: " +
@@ -162,29 +147,22 @@
".*gocheck_test.go:[0-9]+\n" +
" in FixtureHelper.SetUpSuite\n$"
- // XXX Changing the expression above to not match breaks Go. WTF?
-
- matched, err := regexp.MatchString(expected, output.value)
- if err != nil {
- c.Error("Bad expression:", expected)
- } else if !matched {
- c.Error("Panic not logged properly:\n", output.value)
- }
+ c.Check(output.value, Matches, expected)
}
func (s *FixtureS) TestPanicOnTearDownSuite(c *gocheck.C) {
helper := FixtureHelper{panicOn: "TearDownSuite"}
output := String{}
gocheck.Run(&helper, &gocheck.RunConf{Output: &output})
- c.CheckEqual(helper.calls[0], "SetUpSuite")
- c.CheckEqual(helper.calls[1], "SetUpTest")
- c.CheckEqual(helper.calls[2], "Test1")
- c.CheckEqual(helper.calls[3], "TearDownTest")
- c.CheckEqual(helper.calls[4], "SetUpTest")
- c.CheckEqual(helper.calls[5], "Test2")
- c.CheckEqual(helper.calls[6], "TearDownTest")
- c.CheckEqual(helper.calls[7], "TearDownSuite")
- c.CheckEqual(helper.n, 8)
+ c.Check(helper.calls[0], Equals, "SetUpSuite")
+ c.Check(helper.calls[1], Equals, "SetUpTest")
+ c.Check(helper.calls[2], Equals, "Test1")
+ c.Check(helper.calls[3], Equals, "TearDownTest")
+ c.Check(helper.calls[4], Equals, "SetUpTest")
+ c.Check(helper.calls[5], Equals, "Test2")
+ c.Check(helper.calls[6], Equals, "TearDownTest")
+ c.Check(helper.calls[7], Equals, "TearDownSuite")
+ c.Check(helper.n, Equals, 8)
expected := "^\n-+\n" +
"PANIC: gocheck_test\\.go:[0-9]+: " +
@@ -197,12 +175,7 @@
".*gocheck_test.go:[0-9]+\n" +
" in FixtureHelper.TearDownSuite\n$"
- matched, err := regexp.MatchString(expected, output.value)
- if err != nil {
- c.Error("Bad expression:", expected)
- } else if !matched {
- c.Error("Panic not logged properly:\n", output.value)
- }
+ c.Check(output.value, Matches, expected)
}
@@ -213,14 +186,14 @@
helper := WrongTestArgHelper{}
output := String{}
gocheck.Run(&helper, &gocheck.RunConf{Output: &output})
- c.CheckEqual(helper.calls[0], "SetUpSuite")
- c.CheckEqual(helper.calls[1], "SetUpTest")
- c.CheckEqual(helper.calls[2], "TearDownTest")
- c.CheckEqual(helper.calls[3], "SetUpTest")
- c.CheckEqual(helper.calls[4], "Test2")
- c.CheckEqual(helper.calls[5], "TearDownTest")
- c.CheckEqual(helper.calls[6], "TearDownSuite")
- c.CheckEqual(helper.n, 7)
+ c.Check(helper.calls[0], Equals, "SetUpSuite")
+ c.Check(helper.calls[1], Equals, "SetUpTest")
+ c.Check(helper.calls[2], Equals, "TearDownTest")
+ c.Check(helper.calls[3], Equals, "SetUpTest")
+ c.Check(helper.calls[4], Equals, "Test2")
+ c.Check(helper.calls[5], Equals, "TearDownTest")
+ c.Check(helper.calls[6], Equals, "TearDownSuite")
+ c.Check(helper.n, Equals, 7)
expected := "^\n-+\n" +
"PANIC: fixture_test\\.go:[0-9]+: " +
@@ -228,19 +201,14 @@
"\\.\\.\\. Panic: WrongTestArgHelper\\.Test1 argument " +
"should be \\*gocheck\\.C\n"
- matched, err := regexp.MatchString(expected, output.value)
- if err != nil {
- c.Error("Bad expression: ", expected)
- } else if !matched {
- c.Error("Panic not logged properly:\n", output.value)
- }
+ c.Check(output.value, Matches, expected)
}
func (s *FixtureS) TestPanicOnWrongSetUpTestArg(c *gocheck.C) {
helper := WrongSetUpTestArgHelper{}
output := String{}
gocheck.Run(&helper, &gocheck.RunConf{Output: &output})
- c.CheckEqual(helper.n, 0)
+ c.Check(helper.n, Equals, 0)
expected :=
"^\n-+\n" +
@@ -249,19 +217,14 @@
"\\.\\.\\. Panic: WrongSetUpTestArgHelper\\.SetUpTest argument " +
"should be \\*gocheck\\.C\n"
- matched, err := regexp.MatchString(expected, output.value)
- if err != nil {
- c.Error("Bad expression: ", expected)
- } else if !matched {
- c.Error("Panic not logged properly:\n", output.value)
- }
+ c.Check(output.value, Matches, expected)
}
func (s *FixtureS) TestPanicOnWrongSetUpSuiteArg(c *gocheck.C) {
helper := WrongSetUpSuiteArgHelper{}
output := String{}
gocheck.Run(&helper, &gocheck.RunConf{Output: &output})
- c.CheckEqual(helper.n, 0)
+ c.Check(helper.n, Equals, 0)
expected :=
"^\n-+\n" +
@@ -270,12 +233,7 @@
"\\.\\.\\. Panic: WrongSetUpSuiteArgHelper\\.SetUpSuite argument " +
"should be \\*gocheck\\.C\n"
- matched, err := regexp.MatchString(expected, output.value)
- if err != nil {
- c.Error("Bad expression: ", expected)
- } else if !matched {
- c.Error("Panic not logged properly:\n", output.value)
- }
+ c.Check(output.value, Matches, expected)
}
@@ -286,14 +244,14 @@
helper := WrongTestArgCountHelper{}
output := String{}
gocheck.Run(&helper, &gocheck.RunConf{Output: &output})
- c.CheckEqual(helper.calls[0], "SetUpSuite")
- c.CheckEqual(helper.calls[1], "SetUpTest")
- c.CheckEqual(helper.calls[2], "TearDownTest")
- c.CheckEqual(helper.calls[3], "SetUpTest")
- c.CheckEqual(helper.calls[4], "Test2")
- c.CheckEqual(helper.calls[5], "TearDownTest")
- c.CheckEqual(helper.calls[6], "TearDownSuite")
- c.CheckEqual(helper.n, 7)
+ c.Check(helper.calls[0], Equals, "SetUpSuite")
+ c.Check(helper.calls[1], Equals, "SetUpTest")
+ c.Check(helper.calls[2], Equals, "TearDownTest")
+ c.Check(helper.calls[3], Equals, "SetUpTest")
+ c.Check(helper.calls[4], Equals, "Test2")
+ c.Check(helper.calls[5], Equals, "TearDownTest")
+ c.Check(helper.calls[6], Equals, "TearDownSuite")
+ c.Check(helper.n, Equals, 7)
expected := "^\n-+\n" +
"PANIC: fixture_test\\.go:[0-9]+: " +
@@ -301,19 +259,14 @@
"\\.\\.\\. Panic: WrongTestArgCountHelper\\.Test1 argument " +
"should be \\*gocheck\\.C\n"
- matched, err := regexp.MatchString(expected, output.value)
- if err != nil {
- c.Error("Bad expression: ", expected)
- } else if !matched {
- c.Error("Panic not logged properly:\n", output.value)
- }
+ c.Check(output.value, Matches, expected)
}
func (s *FixtureS) TestPanicOnWrongSetUpTestArgCount(c *gocheck.C) {
helper := WrongSetUpTestArgCountHelper{}
output := String{}
gocheck.Run(&helper, &gocheck.RunConf{Output: &output})
- c.CheckEqual(helper.n, 0)
+ c.Check(helper.n, Equals, 0)
expected :=
"^\n-+\n" +
@@ -322,19 +275,14 @@
"\\.\\.\\. Panic: WrongSetUpTestArgCountHelper\\.SetUpTest argument " +
"should be \\*gocheck\\.C\n"
- matched, err := regexp.MatchString(expected, output.value)
- if err != nil {
- c.Error("Bad expression: ", expected)
- } else if !matched {
- c.Error("Panic not logged properly:\n", output.value)
- }
+ c.Check(output.value, Matches, expected)
}
func (s *FixtureS) TestPanicOnWrongSetUpSuiteArgCount(c *gocheck.C) {
helper := WrongSetUpSuiteArgCountHelper{}
output := String{}
gocheck.Run(&helper, &gocheck.RunConf{Output: &output})
- c.CheckEqual(helper.n, 0)
+ c.Check(helper.n, Equals, 0)
expected :=
"^\n-+\n" +
@@ -343,12 +291,7 @@
"\\.\\.\\. Panic: WrongSetUpSuiteArgCountHelper" +
"\\.SetUpSuite argument should be \\*gocheck\\.C\n"
- matched, err := regexp.MatchString(expected, output.value)
- if err != nil {
- c.Error("Bad expression: ", expected)
- } else if !matched {
- c.Error("Panic not logged properly:\n", output.value)
- }
+ c.Check(output.value, Matches, expected)
}
@@ -362,8 +305,6 @@
func (s *WrongTestArgHelper) Test1(t int) {
}
-// ----
-
type WrongSetUpTestArgHelper struct {
FixtureHelper
}
@@ -419,7 +360,7 @@
helper := NoTestsHelper{}
output := String{}
gocheck.Run(&helper, &gocheck.RunConf{Output: &output})
- c.CheckEqual(helper.hasRun, false)
+ c.Check(helper.hasRun, Equals, false)
}
@@ -434,9 +375,9 @@
func (s *FixtureCheckHelper) SetUpSuite(c *gocheck.C) {
switch s.fail {
case "SetUpSuiteAssert":
- c.AssertEqual(false, true)
+ c.Assert(false, Equals, true)
case "SetUpSuiteCheck":
- c.CheckEqual(false, true)
+ c.Check(false, Equals, true)
}
s.completed = true
}
@@ -444,9 +385,9 @@
func (s *FixtureCheckHelper) SetUpTest(c *gocheck.C) {
switch s.fail {
case "SetUpTestAssert":
- c.AssertEqual(false, true)
+ c.Assert(false, Equals, true)
case "SetUpTestCheck":
- c.CheckEqual(false, true)
+ c.Check(false, Equals, true)
}
s.completed = true
}
@@ -459,28 +400,28 @@
helper := FixtureCheckHelper{fail: "SetUpSuiteCheck"}
output := String{}
gocheck.Run(&helper, &gocheck.RunConf{Output: &output})
- c.AssertMatch(output.value,
- "\n---+\n" +
- "FAIL: fixture_test\\.go:[0-9]+: " +
- "FixtureCheckHelper\\.SetUpSuite\n\n" +
- "fixture_test\\.go:[0-9]+:\n" +
- "\\.+ CheckEqual\\(obtained, expected\\):\n" +
- "\\.+ Obtained \\(bool\\): false\n" +
- "\\.+ Expected \\(bool\\): true\n\n")
- c.AssertEqual(helper.completed, true)
+ c.Assert(output.value, Matches,
+ "\n---+\n" +
+ "FAIL: fixture_test\\.go:[0-9]+: " +
+ "FixtureCheckHelper\\.SetUpSuite\n\n" +
+ "fixture_test\\.go:[0-9]+:\n" +
+ "\\.+ Check\\(obtained, Equals, expected\\):\n" +
+ "\\.+ Obtained \\(bool\\): false\n" +
+ "\\.+ Expected \\(bool\\): true\n\n")
+ c.Assert(helper.completed, Equals, true)
}
func (s *FixtureS) TestSetUpSuiteAssert(c *gocheck.C) {
helper := FixtureCheckHelper{fail: "SetUpSuiteAssert"}
output := String{}
gocheck.Run(&helper, &gocheck.RunConf{Output: &output})
- c.AssertMatch(output.value,
- "\n---+\n" +
- "FAIL: fixture_test\\.go:[0-9]+: " +
- "FixtureCheckHelper\\.SetUpSuite\n\n" +
- "fixture_test\\.go:[0-9]+:\n" +
- "\\.+ AssertEqual\\(obtained, expected\\):\n" +
- "\\.+ Obtained \\(bool\\): false\n" +
- "\\.+ Expected \\(bool\\): true\n\n")
- c.AssertEqual(helper.completed, false)
+ c.Assert(output.value, Matches,
+ "\n---+\n" +
+ "FAIL: fixture_test\\.go:[0-9]+: " +
+ "FixtureCheckHelper\\.SetUpSuite\n\n" +
+ "fixture_test\\.go:[0-9]+:\n" +
+ "\\.+ Assert\\(obtained, Equals, expected\\):\n" +
+ "\\.+ Obtained \\(bool\\): false\n" +
+ "\\.+ Expected \\(bool\\): true\n\n")
+ c.Assert(helper.completed, Equals, false)
}
diff --git a/src/foundation_test.go b/src/foundation_test.go
index 529c3c9..bb7a460 100644
--- a/src/foundation_test.go
+++ b/src/foundation_test.go
@@ -8,6 +8,7 @@
import (
"gocheck"
+ gocheck_local "gocheck/local"
"strings"
"regexp"
"fmt"
@@ -60,7 +61,9 @@
c.Error("FailNow() didn't fail the test")
} else {
c.Succeed()
- c.CheckEqual(c.GetTestLog(), "")
+ if c.GetTestLog() != "" {
+ c.Error("Something got logged:\n" + c.GetTestLog())
+ }
}
})()
@@ -73,7 +76,9 @@
if c.Failed() {
c.Error("SucceedNow() didn't succeed the test")
}
- c.CheckEqual(c.GetTestLog(), "")
+ if c.GetTestLog() != "" {
+ c.Error("Something got logged:\n" + c.GetTestLog())
+ }
})()
c.Fail()
@@ -104,9 +109,11 @@
c.Error("Fatal() didn't fail the test")
} else {
c.Succeed()
- c.CheckEqual(c.GetTestLog(),
- fmt.Sprintf("foundation_test.go:%d:\n" +
- "... Error: Die now!\n", line))
+ expected := fmt.Sprintf("foundation_test.go:%d:\n" +
+ "... Error: Die now!\n", line)
+ if c.GetTestLog() != expected {
+ c.Error("Incorrect log:\n" + c.GetTestLog())
+ }
}
})()
@@ -122,9 +129,11 @@
c.Error("Fatalf() didn't fail the test")
} else {
c.Succeed()
- c.CheckEqual(c.GetTestLog(),
- fmt.Sprintf("foundation_test.go:%d:\n" +
- "... Error: Die now!\n", line))
+ expected := fmt.Sprintf("foundation_test.go:%d:\n" +
+ "... Error: Die now!\n", line)
+ if c.GetTestLog() != expected {
+ c.Error("Incorrect log:\n" + c.GetTestLog())
+ }
}
})()
@@ -137,14 +146,14 @@
func (s *FoundationS) TestCallerLoggingInsideTest(c *gocheck.C) {
log := fmt.Sprintf(
"foundation_test.go:%d:\n" +
- "\\.\\.\\. CheckEqual\\(obtained, expected\\):\n" +
+ "\\.\\.\\. Check\\(obtained, Equals, expected\\):\n" +
"\\.\\.\\. Obtained \\(int\\): 10\n" +
"\\.\\.\\. Expected \\(int\\): 20\n\n",
getMyLine()+1)
- result := c.CheckEqual(10, 20)
+ result := c.Check(10, gocheck_local.Equals, 20)
checkState(c, result,
&expectedState{
- name: "CheckEqual(10, 20)",
+ name: "Check(10, Equals, 20)",
result: false,
failed: true,
log: log,
@@ -156,13 +165,13 @@
testLine := getMyLine()-1
log := fmt.Sprintf(
"foundation_test.go:%d > gocheck_test.go:%d:\n" +
- "\\.\\.\\. CheckEqual\\(obtained, expected\\):\n" +
+ "\\.\\.\\. Check\\(obtained, Equals, expected\\):\n" +
"\\.\\.\\. Obtained \\(int\\): 10\n" +
"\\.\\.\\. Expected \\(int\\): 20\n\n",
testLine, line)
checkState(c, result,
&expectedState{
- name: "CheckEqual(10, 20)",
+ name: "Check(10, Equals, 20)",
result: false,
failed: true,
log: log,
@@ -228,7 +237,8 @@
func (s *EmbeddedS) TestMethod(c *gocheck.C) {
// http://code.google.com/p/go/issues/detail?id=906
- c.CheckEqual(s.called, false,
- "The bug described in issue 906 is affecting the runner")
+ c.Check(s.called, gocheck_local.Equals, false,
+ gocheck_local.Bug("The bug described in issue 906 is " +
+ "affecting the runner"))
s.called = true
}
diff --git a/src/gocheck.go b/src/gocheck.go
index 0df23d0..60bc1bc 100644
--- a/src/gocheck.go
+++ b/src/gocheck.go
@@ -267,7 +267,7 @@
Skipped int
Panicked int
FixturePanicked int
- Missed int // Not even tried to run, related to a panic in fixture.
+ Missed int // Not even tried to run, related to a panic in the fixture.
RunError os.Error // Houston, we've got a problem.
}
diff --git a/src/gocheck_test.go b/src/gocheck_test.go
index 59d811f..6d1317f 100644
--- a/src/gocheck_test.go
+++ b/src/gocheck_test.go
@@ -15,6 +15,7 @@
import (
"gocheck"
+ gocheck_local "gocheck/local"
"testing"
"runtime"
"regexp"
@@ -80,7 +81,7 @@
func checkEqualWrapper(c *gocheck.C,
expected interface{},
obtained interface{}) (result bool, line int) {
- return c.CheckEqual(expected, obtained), getMyLine()
+ return c.Check(expected, gocheck_local.Equals, obtained), getMyLine()
}
diff --git a/src/helpers.go b/src/helpers.go
index d4a6711..d86c638 100644
--- a/src/helpers.go
+++ b/src/helpers.go
@@ -3,6 +3,7 @@
import (
"reflect"
+ "strings"
"regexp"
"fmt"
"os"
@@ -164,8 +165,11 @@
}
+var usedDeprecatedChecks = false
+
func (c *C) internalCheckEqual(a interface{}, b interface{}, equal bool,
summary string, issue ...interface{}) bool {
+ usedDeprecatedChecks = true
typeA := reflect.Typeof(a)
typeB := reflect.Typeof(b)
if (typeA == typeB && checkEqual(a, b)) != equal {
@@ -230,6 +234,7 @@
func (c *C) internalCheckMatch(value interface{}, expression string,
equal bool, summary string,
issue ...interface{}) bool {
+ usedDeprecatedChecks = true
valueStr, valueIsStr := value.(string)
if !valueIsStr {
if valueWithStr, valueHasStr := value.(hasString); valueHasStr {
@@ -265,19 +270,23 @@
// -----------------------------------------------------------------------
// Generic checks and assertions based on checkers.
-// We have a custom copy of the following interface to avoid importing
+// We have a custom copy of the following interfaces to avoid importing
// the checkers subpackage into gocheck itself. Make sure this is in sync!
// Checkers used with the c.Assert() and c.Check() helpers must have
-// this interface. See the gocheck/checkers package for more details.
+// this interface. See the gocheck/local package for more details.
type Checker interface {
Name() string
- ObtainedLabel() (varName, varLabel string)
- ExpectedLabel() (varName, varLabel string)
+ VarNames() (obtained, expected string)
NeedsExpectedValue() bool
Check(obtained, expected interface{}) (result bool, error string)
}
+type bugInfo interface {
+ GetBugInfo() string
+}
+
+
// Verify if the first value matches with the expected value. What
// matching means is defined by the provided checker. In case they do not
@@ -308,50 +317,71 @@
func (c *C) internalCheck(funcName string,
obtained interface{}, checker Checker,
args ...interface{}) bool {
- var expected interface{}
- needsExpected := checker.NeedsExpectedValue()
- if needsExpected {
- if len(args) == 0 {
- obtainedVarName, _ := checker.ObtainedLabel()
- expectedVarName, _ := checker.ExpectedLabel()
- c.logCaller(2, fmt.Sprintf("%s(%s, %s, >%s<):",
- funcName, obtainedVarName,
- checker.Name(), expectedVarName))
- c.logString(fmt.Sprint("Missing ", expectedVarName,
- " value for the ", checker.Name(),
- " checker"))
- c.logNewLine()
- c.Fail()
- return false
- }
- expected = args[0]
- args = args[1:]
+ if checker == nil {
+ c.logCaller(2, fmt.Sprintf("%s(obtained, nil!?, ...):", funcName))
+ c.logString("Oops.. you've provided a nil checker!")
+ goto fail
}
+
+ // If the last argument is a bug info, extract it out.
+ var bug bugInfo
+ if len(args) > 0 {
+ if gotBug, hasBug := args[len(args)-1].(bugInfo); hasBug {
+ bug = gotBug
+ args = args[:len(args)-1]
+ }
+ }
+
+ // Ensure we got the needed number of arguments in expected. Note that
+ // this logic is a bit more complex than it ought to be, mainly because
+ // it's leaving the door open to multiple expected values.
+ var expectedWanted int
+ var expected interface{}
+ if checker.NeedsExpectedValue() {
+ expectedWanted = 1
+ }
+ if len(args) == expectedWanted {
+ if expectedWanted > 0 {
+ expected = args[0]
+ }
+ } else {
+ obtainedName, expectedName := checker.VarNames()
+ c.logCaller(2, fmt.Sprintf("%s(%s, %s, >%s<):", funcName, obtainedName,
+ checker.Name(), expectedName))
+ c.logString(fmt.Sprintf("Wrong number of %s args for %s: " +
+ "want %d, got %d", expectedName, checker.Name(),
+ expectedWanted, len(args)))
+ goto fail
+ }
+
+ // Do the actual check.
result, error := checker.Check(obtained, expected)
if !result || error != "" {
- obtainedVarName, obtainedVarLabel := checker.ObtainedLabel()
- expectedVarName, expectedVarLabel := checker.ExpectedLabel()
+ obtainedName, expectedName := checker.VarNames()
var summary string
- if needsExpected {
- summary = fmt.Sprintf("%s(%s, %s, %s):", funcName, obtainedVarName,
- checker.Name(), expectedVarName)
+ if expectedWanted > 0 {
+ summary = fmt.Sprintf("%s(%s, %s, %s):", funcName, obtainedName,
+ checker.Name(), expectedName)
} else {
- summary = fmt.Sprintf("%s(%s, %s):", funcName, obtainedVarName,
+ summary = fmt.Sprintf("%s(%s, %s):", funcName, obtainedName,
checker.Name())
}
c.logCaller(2, summary)
- c.logValue(obtainedVarLabel, obtained)
- if needsExpected {
- c.logValue(expectedVarLabel, expected)
+ c.logValue(strings.Title(obtainedName), obtained)
+ if expectedWanted > 0 {
+ c.logValue(strings.Title(expectedName), expected)
}
if error != "" {
c.logString(error)
- } else if len(args) != 0 {
- c.logString(fmt.Sprint(args...))
+ } else if bug != nil {
+ c.logString(bug.GetBugInfo())
}
- c.logNewLine()
- c.Fail()
- return false
+ goto fail
}
return true
+
+fail:
+ c.logNewLine()
+ c.Fail()
+ return false
}
diff --git a/src/helpers_test.go b/src/helpers_test.go
index 1dfa48e..f280b04 100644
--- a/src/helpers_test.go
+++ b/src/helpers_test.go
@@ -5,6 +5,7 @@
import (
"gocheck"
+ gocheck_local "gocheck/local"
"os"
)
@@ -19,7 +20,7 @@
// -----------------------------------------------------------------------
-// Fake checker to verify the behavior of Assert() and Check().
+// Fake checker and bug info to verify the behavior of Assert() and Check().
type MyChecker struct {
checkError string
@@ -31,12 +32,8 @@
return "MyChecker"
}
-func (checker *MyChecker) ObtainedLabel() (varName, varLabel string) {
- return "myobtained", "MyObtained"
-}
-
-func (checker *MyChecker) ExpectedLabel() (varName, varLabel string) {
- return "myexpected", "MyExpected"
+func (checker *MyChecker) VarNames() (obtained, expected string) {
+ return "myobtained", "myexpected"
}
func (checker *MyChecker) NeedsExpectedValue() bool {
@@ -50,6 +47,33 @@
}
+type myBugInfo struct {
+ info string
+}
+
+func (bug *myBugInfo) GetBugInfo() string {
+ return bug.info
+}
+
+func myBug(info string) *myBugInfo {
+ return &myBugInfo{info}
+}
+
+
+// -----------------------------------------------------------------------
+// Ensure the internal interface matches the one in the subpackage.
+
+// The Checker interface is internal inside the main gocheck package, to
+// avoid importing the subpackage and thus having to know its location.
+// Here we ensure that the interface in the subpackage actually matches,
+// and thus that the whole thing works end to end.
+func (s *HelpersS) TestCheckerInterface(c *gocheck.C) {
+ testHelperSuccess(c, "Check(1, Equals, 1)", true, func() interface{} {
+ return c.Check(1, gocheck_local.Equals, 1)
+ })
+}
+
+
// -----------------------------------------------------------------------
// Tests for Check(), mostly the same as for Assert() following these.
@@ -79,24 +103,24 @@
checker := &MyChecker{failCheck: true}
log := "helpers_test\\.go:[0-9]+ > helpers_test\\.go:[0-9]+:\n" +
"\\.+ Check\\(myobtained, MyChecker, myexpected\\):\n" +
- "\\.+ MyObtained \\(int\\): 1\n" +
- "\\.+ MyExpected \\(int\\): 2\n\n"
+ "\\.+ Myobtained \\(int\\): 1\n" +
+ "\\.+ Myexpected \\(int\\): 2\n\n"
testHelperFailure(c, "Check(1, checker, 2)", false, false, log,
func() interface{} {
return c.Check(1, checker, 2)
})
}
-func (s *HelpersS) TestCheckFailWithExpectedAndMessage(c *gocheck.C) {
+func (s *HelpersS) TestCheckFailWithExpectedAndBugInfo(c *gocheck.C) {
checker := &MyChecker{failCheck: true}
log := "helpers_test\\.go:[0-9]+ > helpers_test\\.go:[0-9]+:\n" +
"\\.+ Check\\(myobtained, MyChecker, myexpected\\):\n" +
- "\\.+ MyObtained \\(int\\): 1\n" +
- "\\.+ MyExpected \\(int\\): 2\n" +
+ "\\.+ Myobtained \\(int\\): 1\n" +
+ "\\.+ Myexpected \\(int\\): 2\n" +
"\\.+ Hello world!\n\n"
testHelperFailure(c, "Check(1, checker, 2, msg)", false, false, log,
func() interface{} {
- return c.Check(1, checker, 2, "Hello", " world!")
+ return c.Check(1, checker, 2, myBug("Hello world!"))
})
}
@@ -104,7 +128,7 @@
checker := &MyChecker{failCheck: true, noExpectedValue: true}
log := "helpers_test\\.go:[0-9]+ > helpers_test\\.go:[0-9]+:\n" +
"\\.+ Check\\(myobtained, MyChecker\\):\n" +
- "\\.+ MyObtained \\(int\\): 1\n\n"
+ "\\.+ Myobtained \\(int\\): 1\n\n"
testHelperFailure(c, "Check(1, checker)", false, false, log,
func() interface{} {
return c.Check(1, checker)
@@ -115,11 +139,11 @@
checker := &MyChecker{failCheck: true, noExpectedValue: true}
log := "helpers_test\\.go:[0-9]+ > helpers_test\\.go:[0-9]+:\n" +
"\\.+ Check\\(myobtained, MyChecker\\):\n" +
- "\\.+ MyObtained \\(int\\): 1\n" +
+ "\\.+ Myobtained \\(int\\): 1\n" +
"\\.+ Hello world!\n\n"
testHelperFailure(c, "Check(1, checker, msg)", false, false, log,
func() interface{} {
- return c.Check(1, checker, "Hello", " world!")
+ return c.Check(1, checker, myBug("Hello world!"))
})
}
@@ -127,19 +151,32 @@
checker := &MyChecker{failCheck: true}
log := "helpers_test\\.go:[0-9]+ > helpers_test\\.go:[0-9]+:\n" +
"\\.+ Check\\(myobtained, MyChecker, >myexpected<\\):\n" +
- "\\.+ Missing myexpected value for the MyChecker checker\n\n"
+ "\\.+ Wrong number of myexpected args for MyChecker: " +
+ "want 1, got 0\n\n"
testHelperFailure(c, "Check(1, checker, !?)", false, false, log,
func() interface{} {
return c.Check(1, checker)
})
}
+func (s *HelpersS) TestCheckWithTooManyExpected(c *gocheck.C) {
+ checker := &MyChecker{noExpectedValue: true}
+ log := "helpers_test\\.go:[0-9]+ > helpers_test\\.go:[0-9]+:\n" +
+ "\\.+ Check\\(myobtained, MyChecker, >myexpected<\\):\n" +
+ "\\.+ Wrong number of myexpected args for MyChecker: " +
+ "want 0, got 1\n\n"
+ testHelperFailure(c, "Check(1, checker, !?)", false, false, log,
+ func() interface{} {
+ return c.Check(1, checker, 1)
+ })
+}
+
func (s *HelpersS) TestCheckWithError(c *gocheck.C) {
checker := &MyChecker{checkError: "Some not so cool data provided!"}
log := "helpers_test\\.go:[0-9]+ > helpers_test\\.go:[0-9]+:\n" +
"\\.+ Check\\(myobtained, MyChecker, myexpected\\):\n" +
- "\\.+ MyObtained \\(int\\): 1\n" +
- "\\.+ MyExpected \\(int\\): 2\n" +
+ "\\.+ Myobtained \\(int\\): 1\n" +
+ "\\.+ Myexpected \\(int\\): 2\n" +
"\\.+ Some not so cool data provided!\n\n"
testHelperFailure(c, "Check(1, checker, 2)", false, false, log,
func() interface{} {
@@ -147,6 +184,17 @@
})
}
+func (s *HelpersS) TestCheckWithNilChecker(c *gocheck.C) {
+ log := "helpers_test\\.go:[0-9]+ > helpers_test\\.go:[0-9]+:\n" +
+ "\\.+ Check\\(obtained, nil!\\?, \\.\\.\\.\\):\n" +
+ "\\.+ Oops\\.\\. you've provided a nil checker!\n\n"
+ testHelperFailure(c, "Check(obtained, nil)", false, false, log,
+ func() interface{} {
+ return c.Check(1, nil)
+ })
+}
+
+
// -----------------------------------------------------------------------
// Tests for Assert(), mostly the same as for Check() above.
@@ -178,8 +226,8 @@
checker := &MyChecker{failCheck: true}
log := "helpers_test\\.go:[0-9]+ > helpers_test\\.go:[0-9]+:\n" +
"\\.+ Assert\\(myobtained, MyChecker, myexpected\\):\n" +
- "\\.+ MyObtained \\(int\\): 1\n" +
- "\\.+ MyExpected \\(int\\): 2\n\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)
@@ -191,12 +239,12 @@
checker := &MyChecker{failCheck: true}
log := "helpers_test\\.go:[0-9]+ > helpers_test\\.go:[0-9]+:\n" +
"\\.+ Assert\\(myobtained, MyChecker, myexpected\\):\n" +
- "\\.+ MyObtained \\(int\\): 1\n" +
- "\\.+ MyExpected \\(int\\): 2\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, "Hello", " world!")
+ c.Assert(1, checker, 2, myBug("Hello world!"))
return nil
})
}
@@ -205,7 +253,7 @@
checker := &MyChecker{failCheck: true, noExpectedValue: true}
log := "helpers_test\\.go:[0-9]+ > helpers_test\\.go:[0-9]+:\n" +
"\\.+ Assert\\(myobtained, MyChecker\\):\n" +
- "\\.+ MyObtained \\(int\\): 1\n\n"
+ "\\.+ Myobtained \\(int\\): 1\n\n"
testHelperFailure(c, "Assert(1, checker)", nil, true, log,
func() interface{} {
c.Assert(1, checker)
@@ -217,11 +265,11 @@
checker := &MyChecker{failCheck: true, noExpectedValue: true}
log := "helpers_test\\.go:[0-9]+ > helpers_test\\.go:[0-9]+:\n" +
"\\.+ Assert\\(myobtained, MyChecker\\):\n" +
- "\\.+ MyObtained \\(int\\): 1\n" +
+ "\\.+ Myobtained \\(int\\): 1\n" +
"\\.+ Hello world!\n\n"
testHelperFailure(c, "Assert(1, checker, msg)", nil, true, log,
func() interface{} {
- c.Assert(1, checker, "Hello", " world!")
+ c.Assert(1, checker, myBug("Hello world!"))
return nil
})
}
@@ -230,7 +278,8 @@
checker := &MyChecker{failCheck: true}
log := "helpers_test\\.go:[0-9]+ > helpers_test\\.go:[0-9]+:\n" +
"\\.+ Assert\\(myobtained, MyChecker, >myexpected<\\):\n" +
- "\\.+ Missing myexpected value for the MyChecker checker\n\n"
+ "\\.+ Wrong number of myexpected args for MyChecker: " +
+ "want 1, got 0\n\n"
testHelperFailure(c, "Assert(1, checker, !?)", nil, true, log,
func() interface{} {
c.Assert(1, checker)
@@ -242,8 +291,8 @@
checker := &MyChecker{checkError: "Some not so cool data provided!"}
log := "helpers_test\\.go:[0-9]+ > helpers_test\\.go:[0-9]+:\n" +
"\\.+ Assert\\(myobtained, MyChecker, myexpected\\):\n" +
- "\\.+ MyObtained \\(int\\): 1\n" +
- "\\.+ MyExpected \\(int\\): 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{} {
@@ -252,6 +301,16 @@
})
}
+func (s *HelpersS) TestAssertWithNilChecker(c *gocheck.C) {
+ log := "helpers_test\\.go:[0-9]+ > helpers_test\\.go:[0-9]+:\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
+ })
+}
// -----------------------------------------------------------------------
@@ -261,8 +320,8 @@
checker := &MyChecker{failCheck: true}
log := "helpers_test.go:[0-9]+ > helpers_test.go:[0-9]+:\n" +
"\\.+ Check\\(myobtained, MyChecker, myexpected\\):\n" +
- "\\.+ MyObtained \\(\\[\\]uint8\\): \\[\\]byte{0x1, 0x2}\n" +
- "\\.+ MyExpected \\(\\[\\]uint8\\): \\[\\]byte{0x1, 0x3}\n\n"
+ "\\.+ Myobtained \\(\\[\\]uint8\\): \\[\\]byte{0x1, 0x2}\n" +
+ "\\.+ Myexpected \\(\\[\\]uint8\\): \\[\\]byte{0x1, 0x3}\n\n"
testHelperFailure(c, "Check([]byte{1}, chk, []byte{3})", false, false, log,
func() interface{} {
return c.Check([]byte{1,2}, checker, []byte{1,3})
@@ -306,7 +365,7 @@
})
}
-func (s *HelpersS) TestCheckEqualWithMessage(c *gocheck.C) {
+func (s *HelpersS) TestCheckEqualWithBugInfo(c *gocheck.C) {
log := "helpers_test.go:[0-9]+ > helpers_test.go:[0-9]+:\n" +
"\\.+ CheckEqual\\(obtained, expected\\):\n" +
"\\.+ Obtained \\(int\\): 10\n" +
@@ -340,7 +399,7 @@
})
}
-func (s *HelpersS) TestCheckNotEqualWithMessage(c *gocheck.C) {
+func (s *HelpersS) TestCheckNotEqualWithBugInfo(c *gocheck.C) {
log := "helpers_test.go:[0-9]+ > helpers_test.go:[0-9]+:\n" +
"\\.+ CheckNotEqual\\(obtained, unexpected\\):\n" +
"\\.+ Both \\(int\\): 10\n" +
@@ -602,14 +661,15 @@
helper := MkDirHelper{}
output := String{}
gocheck.Run(&helper, &gocheck.RunConf{Output: &output})
- c.AssertEqual(output.value, "")
- c.CheckEqual(helper.isDir1, true)
- c.CheckEqual(helper.isDir2, true)
- c.CheckEqual(helper.isDir3, true)
- c.CheckEqual(helper.isDir4, true)
- c.CheckNotEqual(helper.path1, helper.path2)
- c.CheckEqual(isDir(helper.path1), false)
- c.CheckEqual(isDir(helper.path2), false)
+ c.Assert(output.value, gocheck_local.Equals, "")
+ c.Check(helper.isDir1, gocheck_local.Equals, true)
+ c.Check(helper.isDir2, gocheck_local.Equals, true)
+ c.Check(helper.isDir3, gocheck_local.Equals, true)
+ c.Check(helper.isDir4, gocheck_local.Equals, true)
+ c.Check(helper.path1, gocheck_local.Not(gocheck_local.Equals),
+ helper.path2)
+ c.Check(isDir(helper.path1), gocheck_local.Equals, false)
+ c.Check(isDir(helper.path2), gocheck_local.Equals, false)
}
func isDir(path string) bool {
diff --git a/src/checkers/Makefile b/src/local/Makefile
similarity index 82%
rename from src/checkers/Makefile
rename to src/local/Makefile
index f122c44..6ce5370 100644
--- a/src/checkers/Makefile
+++ b/src/local/Makefile
@@ -1,15 +1,15 @@
include $(GOROOT)/src/Make.inc
-TARG=gocheck/checkers
+TARG=gocheck/local
GOFMT=gofmt -spaces=true -tabindent=false -tabwidth=4
GOFILES=\
- checkers.go\
+ local.go\
include $(GOROOT)/src/Make.pkg
format:
- ${GOFMT} -w checkers.go
+ ${GOFMT} -w local.go
testpackage: _test/gocheck.a
diff --git a/src/local/local.go b/src/local/local.go
new file mode 100644
index 0000000..8a28827
--- /dev/null
+++ b/src/local/local.go
@@ -0,0 +1,260 @@
+package checkers
+
+import (
+ "reflect"
+ "unsafe"
+ "regexp"
+ "fmt"
+)
+
+
+// -----------------------------------------------------------------------
+// BugInfo and Bug() helper, to attach extra information to checks.
+
+type bugInfo struct {
+ format string
+ args []interface{}
+}
+
+// Function to attach some information to an Assert() or Check() call.
+// This should be used as, for instance:
+//
+// Assert(a, Equals, 8192, Bug("Buffer size is incorrect, bug #123"))
+//
+// If the matching fails, the provided arguments will be passed to
+// fmt.Sprintf(), and will be presented next to the logged failure. Note
+// that it must be the last argument provided.
+func Bug(format string, args ...interface{}) BugInfo {
+ return &bugInfo{format, args}
+}
+
+// Interface which must be supported for attaching extra information to
+// checks. See the Bug() function.
+type BugInfo interface {
+ GetBugInfo() string
+}
+
+func (bug *bugInfo) GetBugInfo() string {
+ return fmt.Sprintf(bug.format, bug.args...)
+}
+
+
+// -----------------------------------------------------------------------
+// A useful Checker template.
+
+// Checkers used with the c.Assert() and c.Check() verification methods
+// must have this interface. See the CheckerType type for an understanding
+// of how the individual methods must work.
+type Checker interface {
+ Name() string
+ VarNames() (obtained, expected string)
+ NeedsExpectedValue() bool
+ Check(obtained, expected interface{}) (result bool, error string)
+}
+
+// Sample checker type with some sane defaults.
+type CheckerType struct{}
+
+// Trick to ensure it matchers the desired interface.
+var _ Checker = (*CheckerType)(nil)
+
+
+// The function name used to build the matcher. E.g. "IsNil".
+func (checker *CheckerType) Name() string {
+ return "Checker"
+}
+
+// Method must return true if the given matcher needs to be informed
+// of an expected value in addition to the actual value obtained to
+// verify its expectations. E.g. false for IsNil.
+func (checker *CheckerType) NeedsExpectedValue() bool {
+ return true
+}
+
+// Variable names to be used for the obtained and expected values when
+// reporting a failure in the expectations established. E.g.
+// "obtained" and "expected".
+func (checker *CheckerType) VarNames() (obtained, expected string) {
+ return "obtained", "expected"
+}
+
+// Method must return true if the obtained value succeeds the
+// expectations established by the given matcher. If an error is
+// returned, it means the provided parameters are somehow invalid.
+func (checker *CheckerType) Check(obtained, expected interface{}) (
+ result bool, error string) {
+ return false, ""
+}
+
+
+// -----------------------------------------------------------------------
+// Not() checker logic inverter.
+
+// Invert the logic of the provided checker. The resulting checker will
+// succeed where the original one failed, and vice versa. E.g.
+// Assert(a, Not(Equals), b)
+func Not(checker Checker) Checker {
+ return ¬Checker{checker}
+}
+
+type notChecker struct {
+ sub Checker
+}
+
+func (checker *notChecker) Name() string {
+ return "Not(" + checker.sub.Name() + ")"
+}
+
+func (checker *notChecker) NeedsExpectedValue() bool {
+ return checker.sub.NeedsExpectedValue()
+}
+
+func (checker *notChecker) VarNames() (obtained, expected string) {
+ obtained, expected = checker.sub.VarNames()
+ return
+}
+
+func (checker *notChecker) Check(obtained, expected interface{}) (
+ result bool, error string) {
+ result, error = checker.sub.Check(obtained, expected)
+ result = !result // So much for so little. :-)
+ return
+}
+
+
+// -----------------------------------------------------------------------
+// IsNil checker.
+
+// Check if the obtained value is nil. E.g. Assert(err, IsNil).
+var IsNil Checker = &isNilChecker{}
+
+type isNilChecker struct{
+ CheckerType
+}
+
+func (checker *isNilChecker) Name() string {
+ return "IsNil"
+}
+
+func (checker *isNilChecker) NeedsExpectedValue() bool {
+ return false
+}
+
+func (checker *isNilChecker) VarNames() (obtained, expected string) {
+ return "value", ""
+}
+
+func (checker *isNilChecker) Check(obtained, expected interface{}) (
+ result bool, error string) {
+ return isNil(obtained), ""
+}
+
+
+func isNil(obtained interface{}) (result bool) {
+ if obtained == nil {
+ result = true
+ } else {
+ value := reflect.NewValue(obtained)
+ result = *(*uintptr)(unsafe.Pointer(value.Addr())) == 0
+ }
+ return
+}
+
+
+// -----------------------------------------------------------------------
+// NotNil checker. Alias for Not(IsNil), since it's so common.
+
+// Check if the obtained value is not nil. E.g. Assert(iface, NotNil).
+// This is an Alias for Not(IsNil), since it's a fairly common check.
+var NotNil Checker = ¬NilChecker{}
+
+type notNilChecker struct{
+ isNilChecker
+}
+
+func (checker *notNilChecker) Name() string {
+ return "NotNil"
+}
+
+func (checker *notNilChecker) Check(obtained, expected interface{}) (
+ result bool, error string) {
+ return !isNil(obtained), ""
+}
+
+
+// -----------------------------------------------------------------------
+// Equals checker.
+
+// Check that the obtained value is equal to the expected value. The
+// check will work correctly even when facing arrays, interfaces, and
+// values of different types (which always fails the test). E.g.
+// Assert(value, Equals, 42).
+var Equals Checker = &equalsChecker{}
+
+type equalsChecker struct {
+ CheckerType
+}
+
+func (checker *equalsChecker) Name() string {
+ return "Equals"
+}
+
+func (checker *equalsChecker) Check(obtained, expected interface{}) (
+ result bool, error string) {
+ // This will use a fast path to check for equality of normal types,
+ // and then fallback to reflect.DeepEqual if things go wrong.
+ defer func() {
+ if recover() != nil {
+ result = reflect.DeepEqual(obtained, expected)
+ }
+ }()
+ return obtained == expected, ""
+}
+
+
+// -----------------------------------------------------------------------
+// Matches checker.
+
+// Check that the string provided as the obtained value (or the result of
+// its .String() method, in case the value is not a string) matches the
+// regular expression provided. Note that, given the interface os.Error
+// commonly used for errors, this checker will correctly verify its
+// string representation. E.g. Assert(err, Matches, "perm.*denied")
+var Matches Checker = &matchesChecker{}
+
+type matchesChecker struct {
+ CheckerType
+}
+
+func (checker *matchesChecker) Name() string {
+ return "Matches"
+}
+
+func (checker *matchesChecker) VarNames() (obtained, expected string) {
+ return "value", "regex"
+}
+
+type hasString interface {
+ String() string
+}
+
+func (checker *matchesChecker) Check(value, re interface{}) (bool, string) {
+ reStr, ok := re.(string)
+ if !ok {
+ return false, "Regex must be a string"
+ }
+ valueStr, valueIsStr := value.(string)
+ if !valueIsStr {
+ if valueWithStr, valueHasStr := value.(hasString); valueHasStr {
+ valueStr, valueIsStr = valueWithStr.String(), true
+ }
+ }
+ if valueIsStr {
+ matches, err := regexp.MatchString("^" + reStr + "$", valueStr)
+ if err != nil {
+ return false, "Can't compile regex: " + err.String()
+ }
+ return matches, ""
+ }
+ return false, "Obtained value is not a string and has no .String()"
+}
diff --git a/src/local/local_test.go b/src/local/local_test.go
new file mode 100644
index 0000000..90a38a8
--- /dev/null
+++ b/src/local/local_test.go
@@ -0,0 +1,131 @@
+package checkers_test
+
+import (
+ "testing"
+ "gocheck"
+ . "gocheck/local"
+ "os"
+)
+
+
+func TestGocheck(t *testing.T) {
+ gocheck.TestingT(t)
+}
+
+
+type CheckersS struct{}
+
+var _ = gocheck.Suite(&CheckersS{})
+
+
+func testInfo(c *gocheck.C, checker Checker,
+ name, obtainedVarName, expectedVarName string) {
+ if checker.Name() != name {
+ c.Fatalf("Got name %s, expected %s", checker.Name(), name)
+ }
+ obtainedName, expectedName := checker.VarNames()
+ if obtainedName != obtainedVarName {
+ c.Fatalf("Got obtained label %#v, expected %#v",
+ obtainedName, obtainedVarName)
+ }
+ if expectedName != expectedVarName {
+ c.Fatalf("Got expected label %#v, expected %#v",
+ expectedName, expectedVarName)
+ }
+}
+
+func testCheck(c *gocheck.C, checker Checker,
+ obtained, expected interface{},
+ wantedResult bool, wantedError string) {
+ result, error := checker.Check(obtained, expected)
+ if result != wantedResult || error != wantedError {
+ c.Fatalf("%s.Check(%#v, %#v) returned " +
+ "(%#v, %#v) rather than (%#v, %#v)",
+ checker.Name(), obtained, expected,
+ result, error, wantedResult, wantedError)
+ }
+}
+
+func (s *CheckersS) TestBug(c *gocheck.C) {
+ bug := Bug("a %d bc", 42)
+ info := bug.GetBugInfo()
+ if info != "a 42 bc" {
+ c.Fatalf("Bug() returned %#v", info)
+ }
+}
+
+func (s *CheckersS) TestIsNil(c *gocheck.C) {
+ testInfo(c, IsNil, "IsNil", "value", "")
+
+ testCheck(c, IsNil, nil, nil, true, "")
+ testCheck(c, IsNil, "a", nil, false, "")
+
+ testCheck(c, IsNil, (chan int)(nil), nil, true, "")
+ testCheck(c, IsNil, make(chan int), nil, false, "")
+ testCheck(c, IsNil, (os.Error)(nil), nil, true, "")
+ testCheck(c, IsNil, os.NewError(""), nil, false, "")
+ testCheck(c, IsNil, ([]int)(nil), nil, true, "")
+ testCheck(c, IsNil, make([]int, 1), nil, false, "")
+}
+
+func (s *CheckersS) TestNotNil(c *gocheck.C) {
+ testInfo(c, NotNil, "NotNil", "value", "")
+
+ testCheck(c, NotNil, nil, nil, false, "")
+ testCheck(c, NotNil, "a", nil, true, "")
+
+ testCheck(c, NotNil, (chan int)(nil), nil, false, "")
+ testCheck(c, NotNil, make(chan int), nil, true, "")
+ testCheck(c, NotNil, (os.Error)(nil), nil, false, "")
+ testCheck(c, NotNil, os.NewError(""), nil, true, "")
+ testCheck(c, NotNil, ([]int)(nil), nil, false, "")
+ testCheck(c, NotNil, make([]int, 1), nil, true, "")
+}
+
+func (s *CheckersS) TestNot(c *gocheck.C) {
+ testInfo(c, Not(IsNil), "Not(IsNil)", "value", "")
+
+ testCheck(c, Not(IsNil), nil, nil, false, "")
+ testCheck(c, Not(IsNil), "a", nil, true, "")
+}
+
+
+func (s *CheckersS) TestEquals(c *gocheck.C) {
+ testInfo(c, Equals, "Equals", "obtained", "expected")
+
+ // The simplest.
+ testCheck(c, Equals, 42, 42, true, "")
+ testCheck(c, Equals, 42, 43, false, "")
+
+ // Different native types.
+ testCheck(c, Equals, int32(42), int64(42), false, "")
+
+ // With nil.
+ testCheck(c, Equals, 42, nil, false, "")
+
+ // Arrays
+ testCheck(c, Equals, []byte{1,2}, []byte{1,2}, true, "")
+ testCheck(c, Equals, []byte{1,2}, []byte{1,3}, false, "")
+}
+
+func (s *CheckersS) TestMatches(c *gocheck.C) {
+ testInfo(c, Matches, "Matches", "value", "regex")
+
+ // Simple matching
+ testCheck(c, Matches, "abc", "abc", true, "")
+ testCheck(c, Matches, "abc", "a.c", true, "")
+
+ // Must match fully
+ testCheck(c, Matches, "abc", "ab", false, "")
+ testCheck(c, Matches, "abc", "bc", false, "")
+
+ // String()-enabled values accepted
+ testCheck(c, Matches, os.NewError("abc"), "a.c", true, "")
+ testCheck(c, Matches, os.NewError("abc"), "a.d", false, "")
+
+ // Some error conditions.
+ testCheck(c, Matches, 1, "a.c", false,
+ "Obtained value is not a string and has no .String()")
+ testCheck(c, Matches, "abc", "a[c", false,
+ "Can't compile regex: unmatched '['")
+}
diff --git a/src/run.go b/src/run.go
index 947d599..1ce4fcf 100644
--- a/src/run.go
+++ b/src/run.go
@@ -50,6 +50,14 @@
func TestingT(testingT *testing.T) {
result := RunAll(&RunConf{Filter: *filterFlag})
println(result.String())
+ if usedDeprecatedChecks {
+ println("WARNING: The Check/AssertEqual() and Check/AssertMatch() " +
+ "family of functions\n is deprecated, and will be " +
+ "removed in the future. Assert() and\n Check() " +
+ "are both more comfortable and more powerful. See the\n" +
+ " documentation at http://labix.org/gocheck for " +
+ "more details.\n")
+ }
if !result.Passed() {
testingT.Fail()
}
diff --git a/src/run_test.go b/src/run_test.go
index ee8419e..ebe7aba 100644
--- a/src/run_test.go
+++ b/src/run_test.go
@@ -4,6 +4,7 @@
import (
"gocheck"
+ . "gocheck/local"
"os"
)
@@ -23,76 +24,76 @@
func (s *RunS) TestSuccess(c *gocheck.C) {
output := String{}
result := gocheck.Run(&SuccessHelper{}, &gocheck.RunConf{Output: &output})
- c.CheckEqual(result.Succeeded, 1)
- c.CheckEqual(result.Failed, 0)
- c.CheckEqual(result.Skipped, 0)
- c.CheckEqual(result.Panicked, 0)
- c.CheckEqual(result.FixturePanicked, 0)
- c.CheckEqual(result.Missed, 0)
- c.CheckEqual(result.RunError, nil)
+ c.Check(result.Succeeded, Equals, 1)
+ c.Check(result.Failed, Equals, 0)
+ c.Check(result.Skipped, Equals, 0)
+ c.Check(result.Panicked, Equals, 0)
+ c.Check(result.FixturePanicked, Equals, 0)
+ c.Check(result.Missed, Equals, 0)
+ c.Check(result.RunError, IsNil)
}
func (s *RunS) TestFailure(c *gocheck.C) {
output := String{}
result := gocheck.Run(&FailHelper{}, &gocheck.RunConf{Output: &output})
- c.CheckEqual(result.Succeeded, 0)
- c.CheckEqual(result.Failed, 1)
- c.CheckEqual(result.Skipped, 0)
- c.CheckEqual(result.Panicked, 0)
- c.CheckEqual(result.FixturePanicked, 0)
- c.CheckEqual(result.Missed, 0)
- c.CheckEqual(result.RunError, nil)
+ c.Check(result.Succeeded, Equals, 0)
+ c.Check(result.Failed, Equals, 1)
+ c.Check(result.Skipped, Equals, 0)
+ c.Check(result.Panicked, Equals, 0)
+ c.Check(result.FixturePanicked, Equals, 0)
+ c.Check(result.Missed, Equals, 0)
+ c.Check(result.RunError, IsNil)
}
func (s *RunS) TestFixture(c *gocheck.C) {
output := String{}
result := gocheck.Run(&FixtureHelper{}, &gocheck.RunConf{Output: &output})
- c.CheckEqual(result.Succeeded, 2)
- c.CheckEqual(result.Failed, 0)
- c.CheckEqual(result.Skipped, 0)
- c.CheckEqual(result.Panicked, 0)
- c.CheckEqual(result.FixturePanicked, 0)
- c.CheckEqual(result.Missed, 0)
- c.CheckEqual(result.RunError, nil)
+ c.Check(result.Succeeded, Equals, 2)
+ c.Check(result.Failed, Equals, 0)
+ c.Check(result.Skipped, Equals, 0)
+ c.Check(result.Panicked, Equals, 0)
+ c.Check(result.FixturePanicked, Equals, 0)
+ c.Check(result.Missed, Equals, 0)
+ c.Check(result.RunError, IsNil)
}
func (s *RunS) TestPanicOnTest(c *gocheck.C) {
output := String{}
helper := &FixtureHelper{panicOn:"Test1"}
result := gocheck.Run(helper, &gocheck.RunConf{Output: &output})
- c.CheckEqual(result.Succeeded, 1)
- c.CheckEqual(result.Failed, 0)
- c.CheckEqual(result.Skipped, 0)
- c.CheckEqual(result.Panicked, 1)
- c.CheckEqual(result.FixturePanicked, 0)
- c.CheckEqual(result.Missed, 0)
- c.CheckEqual(result.RunError, nil)
+ c.Check(result.Succeeded, Equals, 1)
+ c.Check(result.Failed, Equals, 0)
+ c.Check(result.Skipped, Equals, 0)
+ c.Check(result.Panicked, Equals, 1)
+ c.Check(result.FixturePanicked, Equals, 0)
+ c.Check(result.Missed, Equals, 0)
+ c.Check(result.RunError, IsNil)
}
func (s *RunS) TestPanicOnSetUpTest(c *gocheck.C) {
output := String{}
helper := &FixtureHelper{panicOn:"SetUpTest"}
result := gocheck.Run(helper, &gocheck.RunConf{Output: &output})
- c.CheckEqual(result.Succeeded, 0)
- c.CheckEqual(result.Failed, 0)
- c.CheckEqual(result.Skipped, 0)
- c.CheckEqual(result.Panicked, 0)
- c.CheckEqual(result.FixturePanicked, 1)
- c.CheckEqual(result.Missed, 2)
- c.CheckEqual(result.RunError, nil)
+ c.Check(result.Succeeded, Equals, 0)
+ c.Check(result.Failed, Equals, 0)
+ c.Check(result.Skipped, Equals, 0)
+ c.Check(result.Panicked, Equals, 0)
+ c.Check(result.FixturePanicked, Equals, 1)
+ c.Check(result.Missed, Equals, 2)
+ c.Check(result.RunError, IsNil)
}
func (s *RunS) TestPanicOnSetUpSuite(c *gocheck.C) {
output := String{}
helper := &FixtureHelper{panicOn:"SetUpSuite"}
result := gocheck.Run(helper, &gocheck.RunConf{Output: &output})
- c.CheckEqual(result.Succeeded, 0)
- c.CheckEqual(result.Failed, 0)
- c.CheckEqual(result.Skipped, 0)
- c.CheckEqual(result.Panicked, 0)
- c.CheckEqual(result.FixturePanicked, 1)
- c.CheckEqual(result.Missed, 2)
- c.CheckEqual(result.RunError, nil)
+ c.Check(result.Succeeded, Equals, 0)
+ c.Check(result.Failed, Equals, 0)
+ c.Check(result.Skipped, Equals, 0)
+ c.Check(result.Panicked, Equals, 0)
+ c.Check(result.FixturePanicked, Equals, 1)
+ c.Check(result.Missed, Equals, 2)
+ c.Check(result.RunError, IsNil)
}
@@ -104,13 +105,13 @@
FixturePanicked:5, Missed:6}
result.Add(&gocheck.Result{Succeeded:10, Skipped:20, Failed:30,
Panicked:40, FixturePanicked:50, Missed:60})
- c.CheckEqual(result.Succeeded, 11)
- c.CheckEqual(result.Skipped, 22)
- c.CheckEqual(result.Failed, 33)
- c.CheckEqual(result.Panicked, 44)
- c.CheckEqual(result.FixturePanicked, 55)
- c.CheckEqual(result.Missed, 66)
- c.CheckEqual(result.RunError, nil)
+ c.Check(result.Succeeded, Equals, 11)
+ c.Check(result.Skipped, Equals, 22)
+ c.Check(result.Failed, Equals, 33)
+ c.Check(result.Panicked, Equals, 44)
+ c.Check(result.FixturePanicked, Equals, 55)
+ c.Check(result.Missed, Equals, 66)
+ c.Check(result.RunError, IsNil)
}
@@ -118,14 +119,14 @@
// Check the Passed() method.
func (s *RunS) TestPassed(c *gocheck.C) {
- c.AssertEqual((&gocheck.Result{}).Passed(), true)
- c.AssertEqual((&gocheck.Result{Succeeded:1}).Passed(), true)
- c.AssertEqual((&gocheck.Result{Skipped:1}).Passed(), true)
- c.AssertEqual((&gocheck.Result{Failed:1}).Passed(), false)
- c.AssertEqual((&gocheck.Result{Panicked:1}).Passed(), false)
- c.AssertEqual((&gocheck.Result{FixturePanicked:1}).Passed(), false)
- c.AssertEqual((&gocheck.Result{Missed:1}).Passed(), false)
- c.AssertEqual((&gocheck.Result{RunError:os.NewError("!")}).Passed(), false)
+ c.Assert((&gocheck.Result{}).Passed(), Equals, true)
+ c.Assert((&gocheck.Result{Succeeded:1}).Passed(), Equals, true)
+ c.Assert((&gocheck.Result{Skipped:1}).Passed(), Equals, true)
+ c.Assert((&gocheck.Result{Failed:1}).Passed(), Equals, false)
+ c.Assert((&gocheck.Result{Panicked:1}).Passed(), Equals, false)
+ c.Assert((&gocheck.Result{FixturePanicked:1}).Passed(), Equals, false)
+ c.Assert((&gocheck.Result{Missed:1}).Passed(), Equals, false)
+ c.Assert((&gocheck.Result{RunError:os.NewError("!")}).Passed(), Equals, false)
}
// -----------------------------------------------------------------------
@@ -133,45 +134,46 @@
func (s *RunS) TestPrintSuccess(c *gocheck.C) {
result := &gocheck.Result{Succeeded:5}
- c.CheckEqual(result.String(), "OK: 5 passed")
+ c.Check(result.String(), Equals, "OK: 5 passed")
}
func (s *RunS) TestPrintFailure(c *gocheck.C) {
result := &gocheck.Result{Failed:5}
- c.CheckEqual(result.String(), "OOPS: 0 passed, 5 FAILED")
+ c.Check(result.String(), Equals, "OOPS: 0 passed, 5 FAILED")
}
func (s *RunS) TestPrintSkipped(c *gocheck.C) {
result := &gocheck.Result{Skipped:5}
- c.CheckEqual(result.String(), "OK: 0 passed, 5 skipped")
+ c.Check(result.String(), Equals, "OK: 0 passed, 5 skipped")
}
func (s *RunS) TestPrintPanicked(c *gocheck.C) {
result := &gocheck.Result{Panicked:5}
- c.CheckEqual(result.String(), "OOPS: 0 passed, 5 PANICKED")
+ c.Check(result.String(), Equals, "OOPS: 0 passed, 5 PANICKED")
}
func (s *RunS) TestPrintFixturePanicked(c *gocheck.C) {
result := &gocheck.Result{FixturePanicked:5}
- c.CheckEqual(result.String(), "OOPS: 0 passed, 5 FIXTURE PANICKED")
+ c.Check(result.String(), Equals, "OOPS: 0 passed, 5 FIXTURE PANICKED")
}
func (s *RunS) TestPrintMissed(c *gocheck.C) {
result := &gocheck.Result{Missed:5}
- c.CheckEqual(result.String(), "OOPS: 0 passed, 5 MISSED")
+ c.Check(result.String(), Equals, "OOPS: 0 passed, 5 MISSED")
}
func (s *RunS) TestPrintAll(c *gocheck.C) {
result := &gocheck.Result{Succeeded:1, Skipped:2, Panicked:3,
FixturePanicked:4, Missed:5}
- c.CheckEqual(result.String(), "OOPS: 1 passed, 2 skipped, 3 PANICKED, " +
- "4 FIXTURE PANICKED, 5 MISSED")
+ c.Check(result.String(), Equals,
+ "OOPS: 1 passed, 2 skipped, 3 PANICKED, " +
+ "4 FIXTURE PANICKED, 5 MISSED")
}
func (s *RunS) TestPrintRunError(c *gocheck.C) {
result := &gocheck.Result{Succeeded:1, Failed:1,
RunError: os.NewError("Kaboom!")}
- c.CheckEqual(result.String(), "ERROR: Kaboom!")
+ c.Check(result.String(), Equals, "ERROR: Kaboom!")
}
@@ -183,12 +185,12 @@
output := String{}
runConf := gocheck.RunConf{Output: &output, Filter: "Test[91]"}
gocheck.Run(&helper, &runConf)
- c.CheckEqual(helper.calls[0], "SetUpSuite")
- c.CheckEqual(helper.calls[1], "SetUpTest")
- c.CheckEqual(helper.calls[2], "Test1")
- c.CheckEqual(helper.calls[3], "TearDownTest")
- c.CheckEqual(helper.calls[4], "TearDownSuite")
- c.CheckEqual(helper.n, 5)
+ c.Check(helper.calls[0], Equals, "SetUpSuite")
+ c.Check(helper.calls[1], Equals, "SetUpTest")
+ c.Check(helper.calls[2], Equals, "Test1")
+ c.Check(helper.calls[3], Equals, "TearDownTest")
+ c.Check(helper.calls[4], Equals, "TearDownSuite")
+ c.Check(helper.n, Equals, 5)
}
func (s *RunS) TestFilterTestNameWithAll(c *gocheck.C) {
@@ -196,15 +198,15 @@
output := String{}
runConf := gocheck.RunConf{Output: &output, Filter: ".*"}
gocheck.Run(&helper, &runConf)
- c.CheckEqual(helper.calls[0], "SetUpSuite")
- c.CheckEqual(helper.calls[1], "SetUpTest")
- c.CheckEqual(helper.calls[2], "Test1")
- c.CheckEqual(helper.calls[3], "TearDownTest")
- c.CheckEqual(helper.calls[4], "SetUpTest")
- c.CheckEqual(helper.calls[5], "Test2")
- c.CheckEqual(helper.calls[6], "TearDownTest")
- c.CheckEqual(helper.calls[7], "TearDownSuite")
- c.CheckEqual(helper.n, 8)
+ c.Check(helper.calls[0], Equals, "SetUpSuite")
+ c.Check(helper.calls[1], Equals, "SetUpTest")
+ c.Check(helper.calls[2], Equals, "Test1")
+ c.Check(helper.calls[3], Equals, "TearDownTest")
+ c.Check(helper.calls[4], Equals, "SetUpTest")
+ c.Check(helper.calls[5], Equals, "Test2")
+ c.Check(helper.calls[6], Equals, "TearDownTest")
+ c.Check(helper.calls[7], Equals, "TearDownSuite")
+ c.Check(helper.n, Equals, 8)
}
func (s *RunS) TestFilterSuiteName(c *gocheck.C) {
@@ -212,15 +214,15 @@
output := String{}
runConf := gocheck.RunConf{Output: &output, Filter: "FixtureHelper"}
gocheck.Run(&helper, &runConf)
- c.CheckEqual(helper.calls[0], "SetUpSuite")
- c.CheckEqual(helper.calls[1], "SetUpTest")
- c.CheckEqual(helper.calls[2], "Test1")
- c.CheckEqual(helper.calls[3], "TearDownTest")
- c.CheckEqual(helper.calls[4], "SetUpTest")
- c.CheckEqual(helper.calls[5], "Test2")
- c.CheckEqual(helper.calls[6], "TearDownTest")
- c.CheckEqual(helper.calls[7], "TearDownSuite")
- c.CheckEqual(helper.n, 8)
+ c.Check(helper.calls[0], Equals, "SetUpSuite")
+ c.Check(helper.calls[1], Equals, "SetUpTest")
+ c.Check(helper.calls[2], Equals, "Test1")
+ c.Check(helper.calls[3], Equals, "TearDownTest")
+ c.Check(helper.calls[4], Equals, "SetUpTest")
+ c.Check(helper.calls[5], Equals, "Test2")
+ c.Check(helper.calls[6], Equals, "TearDownTest")
+ c.Check(helper.calls[7], Equals, "TearDownSuite")
+ c.Check(helper.n, Equals, 8)
}
func (s *RunS) TestFilterSuiteNameAndTestName(c *gocheck.C) {
@@ -228,12 +230,12 @@
output := String{}
runConf := gocheck.RunConf{Output: &output, Filter: "FixtureHelper\\.Test2"}
gocheck.Run(&helper, &runConf)
- c.CheckEqual(helper.calls[0], "SetUpSuite")
- c.CheckEqual(helper.calls[1], "SetUpTest")
- c.CheckEqual(helper.calls[2], "Test2")
- c.CheckEqual(helper.calls[3], "TearDownTest")
- c.CheckEqual(helper.calls[4], "TearDownSuite")
- c.CheckEqual(helper.n, 5)
+ c.Check(helper.calls[0], Equals, "SetUpSuite")
+ c.Check(helper.calls[1], Equals, "SetUpTest")
+ c.Check(helper.calls[2], Equals, "Test2")
+ c.Check(helper.calls[3], Equals, "TearDownTest")
+ c.Check(helper.calls[4], Equals, "TearDownSuite")
+ c.Check(helper.n, Equals, 5)
}
func (s *RunS) TestFilterAllOut(c *gocheck.C) {
@@ -241,7 +243,7 @@
output := String{}
runConf := gocheck.RunConf{Output: &output, Filter: "NotFound"}
gocheck.Run(&helper, &runConf)
- c.CheckEqual(helper.n, 0)
+ c.Check(helper.n, Equals, 0)
}
func (s *RunS) TestRequirePartialMatch(c *gocheck.C) {
@@ -249,7 +251,7 @@
output := String{}
runConf := gocheck.RunConf{Output: &output, Filter: "est"}
gocheck.Run(&helper, &runConf)
- c.CheckEqual(helper.n, 8)
+ c.Check(helper.n, Equals, 8)
}
@@ -258,7 +260,7 @@
output := String{}
runConf := gocheck.RunConf{Output: &output, Filter: "]["}
result := gocheck.Run(&helper, &runConf)
- c.CheckEqual(result.String(),
- "ERROR: Bad filter expression: unmatched ']'")
- c.CheckEqual(helper.n, 0)
+ c.Check(result.String(), Equals,
+ "ERROR: Bad filter expression: unmatched ']'")
+ c.Check(helper.n, Equals, 0)
}