aboutsummaryrefslogtreecommitdiff
path: root/src/go/printer/printer_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'src/go/printer/printer_test.go')
-rw-r--r--src/go/printer/printer_test.go28
1 files changed, 28 insertions, 0 deletions
diff --git a/src/go/printer/printer_test.go b/src/go/printer/printer_test.go
index cb62b3e4f3..3a8ce60431 100644
--- a/src/go/printer/printer_test.go
+++ b/src/go/printer/printer_test.go
@@ -797,3 +797,31 @@ func f() {
t.Fatalf("got %q, want %q", got, want)
}
}
+
+func TestSourcePosNewline(t *testing.T) {
+ // We don't provide a syntax for escaping or unescaping characters in line
+ // directives (see https://go.dev/issue/24183#issuecomment-372449628).
+ // As a result, we cannot write a line directive with the correct path for a
+ // filename containing newlines. We should return an error rather than
+ // silently dropping or mangling it.
+
+ fname := "foo\nbar/bar.go"
+ src := `package bar`
+ fset := token.NewFileSet()
+ f, err := parser.ParseFile(fset, fname, src, parser.ParseComments|parser.AllErrors|parser.SkipObjectResolution)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ cfg := &Config{
+ Mode: SourcePos, // emit line comments
+ Tabwidth: 8,
+ }
+ var buf bytes.Buffer
+ if err := cfg.Fprint(&buf, fset, f); err == nil {
+ t.Errorf("Fprint did not error for source file path containing newline")
+ }
+ if buf.Len() != 0 {
+ t.Errorf("unexpected Fprint output:\n%s", buf.Bytes())
+ }
+}