| 21 | ) |
| 22 | |
| 23 | func TestParseStringConstant(t *testing.T) { |
| 24 | tests := []struct { |
| 25 | name string |
| 26 | str string |
| 27 | want ast.Term |
| 28 | }{ |
| 29 | { |
| 30 | name: "string with newline", |
| 31 | str: `"hello\ |
| 32 | |
| 33 | world"`, |
| 34 | want: ast.String("hello\n\nworld"), |
| 35 | }, |
| 36 | { |
| 37 | name: "string constant, double quote", |
| 38 | str: `"hello"`, |
| 39 | want: ast.String("hello"), |
| 40 | }, |
| 41 | { |
| 42 | name: "bytestring constant, double quote", |
| 43 | str: `b"hello"`, |
| 44 | want: ast.Bytes([]byte("hello")), |
| 45 | }, |
| 46 | { |
| 47 | name: "string constant, single quote", |
| 48 | str: `'hello'`, |
| 49 | want: ast.String("hello"), |
| 50 | }, |
| 51 | { |
| 52 | name: "string constant, single quote containing double quote", |
| 53 | str: `'he"llo'`, |
| 54 | want: ast.String(`he"llo`), |
| 55 | }, |
| 56 | { |
| 57 | name: "string constant escaped", |
| 58 | str: `"he\\ll\"o"`, |
| 59 | want: ast.String(`he\ll"o`), |
| 60 | }, |
| 61 | { |
| 62 | name: "string byte and unicode escape", |
| 63 | str: `"\x68\u{0065}ll\"o"`, |
| 64 | want: ast.String(`hell"o`), |
| 65 | }, |
| 66 | { |
| 67 | name: "byte string byte escape", |
| 68 | str: `b"\x80\x81\x82\n"`, |
| 69 | want: ast.Bytes([]byte{0x80, 0x81, 0x82, 0x0A}), |
| 70 | }, |
| 71 | { |
| 72 | name: "byte string byte escape", |
| 73 | str: `b"\x80\x81\x82😤"`, |
| 74 | want: ast.Bytes([]byte{0x80, 0x81, 0x82, 0xf0, 0x9f, 0x98, 0xa4}), |
| 75 | }, |
| 76 | { |
| 77 | name: "string emoji", |
| 78 | str: `"\u{01f624}"`, |
| 79 | want: ast.String("😤"), |
| 80 | }, |