]> Cypherpunks repositories - gostls13.git/commitdiff
go/printer: test that formatted code is parseable
authorRobert Griesemer <gri@golang.org>
Fri, 10 Feb 2012 21:28:29 +0000 (13:28 -0800)
committerRobert Griesemer <gri@golang.org>
Fri, 10 Feb 2012 21:28:29 +0000 (13:28 -0800)
- Added test case for issue 1542.

Fixes #1542.

R=golang-dev, rsc
CC=golang-dev
https://golang.org/cl/5645080

src/pkg/go/printer/printer_test.go
src/pkg/go/printer/testdata/comments.golden
src/pkg/go/printer/testdata/comments.input

index a0578814aab9a7c404dadcb718368d60e98ef2ac..38eaf65303de630da2c0b0905141e6d3376a9180 100644 (file)
@@ -67,6 +67,13 @@ func runcheck(t *testing.T, source, golden string, mode checkMode) {
        }
        res := buf.Bytes()
 
+       // formatted source must be valid
+       if _, err := parser.ParseFile(fset, "", res, 0); err != nil {
+               t.Error(err)
+               t.Logf("\n%s", res)
+               return
+       }
+
        // update golden files if necessary
        if *update {
                if err := ioutil.WriteFile(golden, res, 0644); err != nil {
index d2ad9e3a2ff94dc4e7747ade0df954dafa2cfe81..7438a32e44c206c194f23d6b2afb00577c6ac76c 100644 (file)
@@ -404,7 +404,7 @@ func _() {
        */
 }
 
-// Some interesting interspersed comments
+// Some interesting interspersed comments.
 func _( /* this */ x /* is */ /* an */ int) {
 }
 
@@ -428,6 +428,26 @@ func _() {
        _ = []int{0, 1 /* don't introduce a newline after this comment - was issue 1365 */ }
 }
 
+// Test cases from issue 1542:
+// Comments must not be placed before commas and cause invalid programs.
+func _() {
+       var a = []int{1, 2      /*jasldf*/}
+       _ = a
+}
+
+func _() {
+       var a = []int{1, 2}/*jasldf
+        */
+
+       _ = a
+}
+
+func _() {
+       var a = []int{1, 2}// jasldf 
+
+       _ = a
+}
+
 // Comments immediately adjacent to punctuation (for which the go/printer
 // may only have estimated position information) must remain after the punctuation.
 func _() {
index 222e0a713d4621786ab449c0ecfc6084d6d84a1f..e382764081b1c9a9e006d6e93fdfcada0366d3d6 100644 (file)
@@ -410,7 +410,7 @@ func _() {
 }
 
 
-// Some interesting interspersed comments
+// Some interesting interspersed comments.
 func _(/* this */x/* is *//* an */ int) {
 }
 
@@ -432,6 +432,26 @@ func _() {
        _ = []int{0, 1 /* don't introduce a newline after this comment - was issue 1365 */}
 }
 
+// Test cases from issue 1542:
+// Comments must not be placed before commas and cause invalid programs.
+func _() {
+       var a = []int{1, 2, /*jasldf*/
+       }
+       _ = a
+}
+
+func _() {
+       var a = []int{1, 2, /*jasldf
+                                               */
+       }
+       _ = a
+}
+
+func _() {
+       var a = []int{1, 2, // jasldf 
+       }
+       _ = a
+}
 
 // Comments immediately adjacent to punctuation (for which the go/printer
 // may only have estimated position information) must remain after the punctuation.