Source file src/runtime/print_quoted_test.go

     1  // Copyright 2025 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package runtime_test
     6  
     7  import (
     8  	"runtime"
     9  	"testing"
    10  )
    11  
    12  func TestPrintQuoted(t *testing.T) {
    13  	for _, tbl := range []struct {
    14  		in, expected string
    15  	}{
    16  		{in: "baz", expected: `"baz"`},
    17  		{in: "foobar", expected: `"foobar"`},
    18  		// make sure newlines get escaped
    19  		{in: "baz\n", expected: `"baz\n"`},
    20  		// make sure null and escape bytes are properly escaped
    21  		{in: "b\033it", expected: `"b\x1bit"`},
    22  		{in: "b\000ar", expected: `"b\x00ar"`},
    23  		// verify that simple 16-bit unicode runes are escaped with \u, including a greek upper-case sigma and an arbitrary unicode character.
    24  		{in: "\u1234Σ", expected: `"\u1234\u03a3"`},
    25  		// verify that 32-bit unicode runes are escaped with \U along with tabs
    26  		{in: "fizz\tle", expected: `"fizz\tle"`},
    27  		{in: "\U00045678boop", expected: `"\U00045678boop"`},
    28  		// verify carriage returns and backslashes get escaped along with our nulls, newlines and a 32-bit unicode character
    29  		{in: "fiz\\zl\re", expected: `"fiz\\zl\re"`},
    30  	} {
    31  		t.Run(tbl.in, func(t *testing.T) {
    32  			out := runtime.DumpPrintQuoted(tbl.in)
    33  			if out != tbl.expected {
    34  				t.Errorf("unexpected output for print(escaped(%q));\n got: %s\nwant: %s", tbl.in, out, tbl.expected)
    35  			}
    36  		})
    37  	}
    38  }
    39  

View as plain text