Print leading comments for a given line as well.
diff --git a/helpers_test.go b/helpers_test.go
index 3175dd1..e492a4e 100644
--- a/helpers_test.go
+++ b/helpers_test.go
@@ -103,7 +103,7 @@
})
}
-func (s *HelpersS) TestCheckFailWithExpectedAndBugInfo(c *gocheck.C) {
+func (s *HelpersS) TestCheckFailWithExpectedAndComment(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, myComment\\(\"Hello world!\"\\)\\)\n" +
@@ -116,6 +116,20 @@
})
}
+func (s *HelpersS) TestCheckFailWithExpectedAndStaticComment(c *gocheck.C) {
+ checker := &MyChecker{result: false}
+ log := "(?s)helpers_test\\.go:[0-9]+:.*\nhelpers_test\\.go:[0-9]+:\n" +
+ " // Nice leading comment\\.\n" +
+ " return c\\.Check\\(1, checker, 2\\) // Hello there\n" +
+ "\\.+ myobtained int = 1\n" +
+ "\\.+ myexpected int = 2\n\n"
+ testHelperFailure(c, "Check(1, checker, 2, msg)", false, false, log,
+ func() interface{} {
+ // Nice leading comment.
+ return c.Check(1, checker, 2) // Hello there
+ })
+}
+
func (s *HelpersS) TestCheckFailWithoutExpected(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" +
diff --git a/printer.go b/printer.go
index 995b422..103ab14 100644
--- a/printer.go
+++ b/printer.go
@@ -60,23 +60,34 @@
func (lp *linePrinter) emit() bool {
if lp.stmt != nil {
lp.trim(lp.stmt)
- lp.config.Fprint(&lp.output, lp.fset, lp.commentedNode(lp.stmt))
+ lp.printWithComments(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
+func (lp *linePrinter) printWithComments(n ast.Node) {
+ nfirst := lp.fset.Position(n.Pos()).Line
+ nlast := 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 {
+ cfirst := lp.fset.Position(g.Pos()).Line
+ clast := lp.fset.Position(g.End()).Line
+ if clast == nfirst-1 && lp.fset.Position(n.Pos()).Column == lp.fset.Position(g.Pos()).Column {
+ for _, c := range g.List {
+ lp.output.WriteString(c.Text)
+ lp.output.WriteByte('\n')
+ }
+ }
+ if cfirst >= nfirst && cfirst <= nlast && n.End() <= g.List[0].Slash {
+ // The printer will not include the comment if it starts past
+ // the node itself. Trick it into printing by overlapping the
+ // slash with the end of the statement.
g.List[0].Slash = n.End() - 1
}
}
- return &printer.CommentedNode{n, lp.fnode.Comments}
+ node := &printer.CommentedNode{n, lp.fnode.Comments}
+ lp.config.Fprint(&lp.output, lp.fset, node)
}
func (lp *linePrinter) Visit(n ast.Node) (w ast.Visitor) {
diff --git a/printer_test.go b/printer_test.go
index 48266dc..547a92d 100644
--- a/printer_test.go
+++ b/printer_test.go
@@ -45,6 +45,9 @@
println(24, func() {
println(25)
})
+ // Leading comment
+ // with multiple lines.
+ println(29) // Comment29
}
var printLineTests = []struct {
@@ -70,6 +73,7 @@
{24, "println(24, func() {\n println(25)\n})"},
{25, "println(25)"},
{26, "println(24, func() {\n println(25)\n})"},
+ {29, "// Leading comment\n// with multiple lines.\nprintln(29) // Comment29"},
}
func (s *PrinterS) TestPrintLine(c *C) {