- Renamed Bug to Commentf.
- Print end-of-line comments next to the checks.
diff --git a/checkers.go b/checkers.go
index 70d5b68..8232384 100644
--- a/checkers.go
+++ b/checkers.go
@@ -7,41 +7,45 @@
)
// -----------------------------------------------------------------------
-// BugInfo and Bug() helper, to attach extra information to checks.
+// CommentInterface and Commentf helper, to attach extra information to checks.
-type bugInfo struct {
+type comment struct {
format string
args []interface{}
}
-// Bug enables attaching some information to Assert() or Check() calls.
+// Commentf returns an infomational value to use with Assert or Check calls.
// If the checker test fails, the provided arguments will be passed to
-// fmt.Sprintf(), and will be presented next to the logged failure.
+// fmt.Sprintf, and will be presented next to the logged failure.
//
// For example:
//
-// c.Assert(l, Equals, 8192, Bug("Buffer size is incorrect, bug #123"))
-// c.Assert(v, Equals, 42, Bug("Iteration #%d", i))
+// c.Assert(v, Equals, 42, Commentf("Iteration #%d failed.", i))
//
-func Bug(format string, args ...interface{}) BugInfo {
- return &bugInfo{format, args}
+// Note that if the comment is constant, a better option is to
+// simply use a normal comment next to the line:
+//
+// c.Assert(l, Equals, 8192) // Ensure buffer size is correct (bug #123)
+//
+func Commentf(format string, args ...interface{}) CommentInterface {
+ return &comment{format, args}
}
-// BugInfo is the interface which must be supported for attaching extra
-// information to checks. See the Bug() function for details.
-type BugInfo interface {
- GetBugInfo() string
+// CommentInterface must be implemented by types that attach extra
+// information to failed checks. See the Commentf function for details.
+type CommentInterface interface {
+ CheckCommentString() string
}
-func (bug *bugInfo) GetBugInfo() string {
- return fmt.Sprintf(bug.format, bug.args...)
+func (c *comment) CheckCommentString() string {
+ return fmt.Sprintf(c.format, c.args...)
}
// -----------------------------------------------------------------------
// The Checker interface.
// The Checker interface must be provided by checkers used with
-// the c.Assert() and c.Check() verification methods.
+// the Assert and Check verification methods.
type Checker interface {
Info() *CheckerInfo
Check(params []interface{}, names []string) (result bool, error string)
@@ -58,9 +62,9 @@
}
// -----------------------------------------------------------------------
-// Not() checker logic inverter.
+// Not checker logic inverter.
-// The Not() checker inverts the logic of the provided checker. The
+// The Not checker inverts the logic of the provided checker. The
// resulting checker will succeed where the original one failed, and
// vice-versa.
//
diff --git a/checkers_test.go b/checkers_test.go
index c1bf711..d87ace4 100644
--- a/checkers_test.go
+++ b/checkers_test.go
@@ -35,11 +35,11 @@
return params, names
}
-func (s *CheckersS) TestBug(c *gocheck.C) {
- bug := gocheck.Bug("a %d bc", 42)
- info := bug.GetBugInfo()
- if info != "a 42 bc" {
- c.Fatalf("Bug() returned %#v", info)
+func (s *CheckersS) TestComment(c *gocheck.C) {
+ bug := gocheck.Commentf("a %d bc", 42)
+ comment := bug.CheckCommentString()
+ if comment != "a 42 bc" {
+ c.Fatalf("Commentf returned %#v", comment)
}
}
diff --git a/foundation_test.go b/foundation_test.go
index b69eb5e..cc309ac 100644
--- a/foundation_test.go
+++ b/foundation_test.go
@@ -335,8 +335,6 @@
func (s *EmbeddedS) TestMethod(c *gocheck.C) {
// http://code.google.com/p/go/issues/detail?id=906
- c.Check(s.called, gocheck.Equals, false,
- gocheck.Bug("The bug described in issue 906 is "+
- "affecting the runner"))
+ c.Check(s.called, gocheck.Equals, false) // Go issue 906 is affecting the runner?
s.called = true
}
diff --git a/helpers.go b/helpers.go
index 1e71e69..fe4ca51 100644
--- a/helpers.go
+++ b/helpers.go
@@ -176,10 +176,10 @@
}
// If the last argument is a bug info, extract it out.
- var bug BugInfo
+ var comment CommentInterface
if len(args) > 0 {
- if gotBug, hasBug := args[len(args)-1].(BugInfo); hasBug {
- bug = gotBug
+ if c, ok := args[len(args)-1].(CommentInterface); ok {
+ comment = c
args = args[:len(args)-1]
}
}
@@ -207,8 +207,8 @@
for i := 0; i != len(params); i++ {
c.logValue(names[i], params[i])
}
- if bug != nil {
- c.logString(bug.GetBugInfo())
+ if comment != nil {
+ c.logString(comment.CheckCommentString())
}
if error != "" {
c.logString(error)
diff --git a/helpers_test.go b/helpers_test.go
index ed58ca8..3175dd1 100644
--- a/helpers_test.go
+++ b/helpers_test.go
@@ -49,16 +49,14 @@
return checker.result, checker.error
}
-type myBugInfo struct {
- info string
+type myCommentType string
+
+func (c myCommentType) CheckCommentString() string {
+ return string(c)
}
-func (bug *myBugInfo) GetBugInfo() string {
- return bug.info
-}
-
-func myBug(info string) *myBugInfo {
- return &myBugInfo{info}
+func myComment(s string) myCommentType {
+ return myCommentType(s)
}
// -----------------------------------------------------------------------
@@ -108,13 +106,13 @@
func (s *HelpersS) TestCheckFailWithExpectedAndBugInfo(c *gocheck.C) {
checker := &MyChecker{result: false}
log := "(?s)helpers_test\\.go:[0-9]+:.*\nhelpers_test\\.go:[0-9]+:\n" +
- " return c\\.Check\\(1, checker, 2, myBug\\(\"Hello world!\"\\)\\)\n" +
+ " return c\\.Check\\(1, checker, 2, myComment\\(\"Hello world!\"\\)\\)\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, myBug("Hello world!"))
+ return c.Check(1, checker, 2, myComment("Hello world!"))
})
}
@@ -132,12 +130,12 @@
func (s *HelpersS) TestCheckFailWithoutExpectedAndMessage(c *gocheck.C) {
checker := &MyChecker{result: false, info: &gocheck.CheckerInfo{Params: []string{"myvalue"}}}
log := "(?s)helpers_test\\.go:[0-9]+:.*\nhelpers_test\\.go:[0-9]+:\n" +
- " return c\\.Check\\(1, checker, myBug\\(\"Hello world!\"\\)\\)\n" +
+ " return c\\.Check\\(1, checker, myComment\\(\"Hello world!\"\\)\\)\n" +
"\\.+ myvalue int = 1\n" +
"\\.+ Hello world!\n\n"
testHelperFailure(c, "Check(1, checker, msg)", false, false, log,
func() interface{} {
- return c.Check(1, checker, myBug("Hello world!"))
+ return c.Check(1, checker, myComment("Hello world!"))
})
}
@@ -244,13 +242,13 @@
func (s *HelpersS) TestAssertFailWithExpectedAndMessage(c *gocheck.C) {
checker := &MyChecker{result: false}
log := "(?s)helpers_test\\.go:[0-9]+:.*\nhelpers_test\\.go:[0-9]+:\n" +
- " c\\.Assert\\(1, checker, 2, myBug\\(\"Hello world!\"\\)\\)\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, myBug("Hello world!"))
+ c.Assert(1, checker, 2, myComment("Hello world!"))
return nil
})
}
@@ -270,12 +268,12 @@
func (s *HelpersS) TestAssertFailWithoutExpectedAndMessage(c *gocheck.C) {
checker := &MyChecker{result: false, info: &gocheck.CheckerInfo{Params: []string{"myvalue"}}}
log := "(?s)helpers_test\\.go:[0-9]+:.*\nhelpers_test\\.go:[0-9]+:\n" +
- " c\\.Assert\\(1, checker, myBug\\(\"Hello world!\"\\)\\)\n" +
+ " c\\.Assert\\(1, checker, myComment\\(\"Hello world!\"\\)\\)\n" +
"\\.+ myvalue int = 1\n" +
"\\.+ Hello world!\n\n"
testHelperFailure(c, "Assert(1, checker, msg)", nil, true, log,
func() interface{} {
- c.Assert(1, checker, myBug("Hello world!"))
+ c.Assert(1, checker, myComment("Hello world!"))
return nil
})
}
diff --git a/printer.go b/printer.go
index e5cb386..995b422 100644
--- a/printer.go
+++ b/printer.go
@@ -4,8 +4,8 @@
"bytes"
"go/ast"
"go/parser"
- "go/token"
"go/printer"
+ "go/token"
"os"
)
@@ -32,19 +32,26 @@
if err != nil {
return "", err
}
- fnode, err := parser.ParseFile(fset, filename, file, 0)
+ fnode, err := parser.ParseFile(fset, filename, file, parser.ParseComments)
if err != nil {
return "", err
}
config := &printer.Config{Mode: printer.UseSpaces, Tabwidth: 4}
- lp := &linePrinter{fset: fset, line: line, config: config}
+ lp := &linePrinter{fset: fset, fnode: fnode, line: line, config: config}
ast.Walk(lp, fnode)
- return lp.output.String(), nil
+ result := lp.output.Bytes()
+ // Comments leave \n at the end.
+ n := len(result)
+ for n > 0 && result[n-1] == '\n' {
+ n--
+ }
+ return string(result[:n]), nil
}
type linePrinter struct {
config *printer.Config
fset *token.FileSet
+ fnode *ast.File
line int
output bytes.Buffer
stmt ast.Stmt
@@ -53,13 +60,25 @@
func (lp *linePrinter) emit() bool {
if lp.stmt != nil {
lp.trim(lp.stmt)
- lp.config.Fprint(&lp.output, lp.fset, lp.stmt)
+ lp.config.Fprint(&lp.output, lp.fset, lp.commentedNode(lp.stmt))
lp.stmt = nil
return true
}
return false
}
+func (lp *linePrinter) commentedNode(n ast.Node) *printer.CommentedNode {
+ first := lp.fset.Position(n.Pos()).Line
+ last := lp.fset.Position(n.End()).Line
+ for _, g := range lp.fnode.Comments {
+ line := lp.fset.Position(g.Pos()).Line
+ if line >= first && line <= last && n.End() <= g.List[0].Slash {
+ g.List[0].Slash = n.End() - 1
+ }
+ }
+ return &printer.CommentedNode{n, lp.fnode.Comments}
+}
+
func (lp *linePrinter) Visit(n ast.Node) (w ast.Visitor) {
if n == nil {
if lp.output.Len() == 0 {
diff --git a/printer_test.go b/printer_test.go
index abd55ea..48266dc 100644
--- a/printer_test.go
+++ b/printer_test.go
@@ -19,15 +19,15 @@
}
func printTestFunc() {
- println(1)
- if 2 == 2 {
- println(3)
+ println(1) // Comment1
+ if 2 == 2 { // Comment2
+ println(3) // Comment3
}
switch 5 {
- case 6: println(6)
+ case 6: println(6) // Comment6
println(7)
}
- switch interface{}(9).(type) {
+ switch interface{}(9).(type) {// Comment9
case int: println(10)
println(11)
}
@@ -51,13 +51,13 @@
line int
output string
}{
- {1, "println(1)"},
- {2, "if 2 == 2 {\n ...\n}"},
- {3, "println(3)"},
+ {1, "println(1) // Comment1"},
+ {2, "if 2 == 2 { // Comment2\n ...\n}"},
+ {3, "println(3) // Comment3"},
{5, "switch 5 {\n...\n}"},
- {6, "case 6:\n println(6)\n ..."},
+ {6, "case 6:\n println(6) // Comment6\n ..."},
{7, "println(7)"},
- {9, "switch interface{}(9).(type) {\n...\n}"},
+ {9, "switch interface{}(9).(type) { // Comment9\n...\n}"},
{10, "case int:\n println(10)\n ..."},
{14, "case <-(chan bool)(nil):\n println(14)\n ..."},
{15, "println(15)"},