|
| 1 | +package parser |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/ncw/gpython/py" |
| 8 | +) |
| 9 | + |
| 10 | +func TestDecodeEscape(t *testing.T) { |
| 11 | + for _, test := range []struct { |
| 12 | + in string |
| 13 | + want string |
| 14 | + errString string |
| 15 | + byteMode bool |
| 16 | + }{ |
| 17 | + // Stringmode tests |
| 18 | + {``, ``, "", false}, |
| 19 | + {`Potato`, `Potato`, "", false}, |
| 20 | + {`Potato\`, ``, `Trailing \ in string`, false}, |
| 21 | + {`\Potato`, `\Potato`, "", false}, |
| 22 | + {`n\\`, `n\`, "", false}, |
| 23 | + {`\'x`, `'x`, "", false}, |
| 24 | + {`\"`, `"`, "", false}, |
| 25 | + {"\\\n", ``, "", false}, |
| 26 | + {`\b`, "\010", "", false}, |
| 27 | + {`\f`, "\014", "", false}, |
| 28 | + {`\t`, "\011", "", false}, |
| 29 | + {`\n`, "\012", "", false}, |
| 30 | + {`\r`, "\015", "", false}, |
| 31 | + {`\v`, "\013", "", false}, |
| 32 | + {`\a`, "\007", "", false}, |
| 33 | + {`\1`, "\001", "", false}, |
| 34 | + {`\12`, "\012", "", false}, |
| 35 | + {`\123`, "\123", "", false}, |
| 36 | + {`\777`, "\u01ff", "", false}, |
| 37 | + {`\1\12\123\1234`, "\001\012\123\123" + "4", "", false}, |
| 38 | + {`a\1a\12a\123a`, "a\001a\012a\123a", "", false}, |
| 39 | + {`\x`, "", `truncated \x escape at position 0`, false}, |
| 40 | + {`\x1`, "", `truncated \x escape at position 0`, false}, |
| 41 | + {`\x11`, "\x11", "", false}, |
| 42 | + {`\xzz`, "", `invalid \x escape at position 0`, false}, |
| 43 | + {`{\x11}`, "{\x11}", "", false}, |
| 44 | + {`\x01\x8a\xff`, "\x01\u008a\u00ff", "", false}, |
| 45 | + {`\x01\x8A\xFF`, "\x01\u008a\u00ff", "", false}, |
| 46 | + {`\u`, "", `truncated \u escape at position 0`, false}, |
| 47 | + {`\u1`, "", `truncated \u escape at position 0`, false}, |
| 48 | + {`\u12`, "", `truncated \u escape at position 0`, false}, |
| 49 | + {`z\u134`, "", `truncated \u escape at position 1`, false}, |
| 50 | + {`\u1234`, "\u1234", "", false}, |
| 51 | + {`z\uzzzz`, "", `invalid \u escape at position 1`, false}, |
| 52 | + {`{\u1234}`, "{\u1234}", "", false}, |
| 53 | + {`\U00000001\U0000018a\U000012ff`, "\U00000001\U0000018a\U000012ff", "", false}, |
| 54 | + {`\U00000001\U0000018A\U000012FF`, "\U00000001\U0000018a\U000012ff", "", false}, |
| 55 | + {`\U0000`, "", `truncated \U escape at position 0`, false}, |
| 56 | + {`\U00001`, "", `truncated \U escape at position 0`, false}, |
| 57 | + {`\U000012`, "", `truncated \U escape at position 0`, false}, |
| 58 | + {`z\U0000134`, "", `truncated \U escape at position 1`, false}, |
| 59 | + {`\U00001234`, "\U00001234", "", false}, |
| 60 | + {`z\Uzzzzzzzz`, "", `invalid \U escape at position 1`, false}, |
| 61 | + {`{\U00001234}`, "{\U00001234}", "", false}, |
| 62 | + {`\U00000001\U0000018a\U000012ff`, "\U00000001\U0000018a\U000012ff", "", false}, |
| 63 | + {`\U00000001\U0000018A\U000012FF`, "\U00000001\U0000018a\U000012ff", "", false}, |
| 64 | + {`\N{potato}`, `\N{potato}`, "", false}, |
| 65 | + |
| 66 | + // Bytemode tests |
| 67 | + {``, ``, "", true}, |
| 68 | + {`Potato`, `Potato`, "", true}, |
| 69 | + {`Potato\`, ``, `Trailing \ in string`, true}, |
| 70 | + {`\Potato`, `\Potato`, "", true}, |
| 71 | + {`n\\`, `n\`, "", true}, |
| 72 | + {`\'x`, `'x`, "", true}, |
| 73 | + {`\"`, `"`, "", true}, |
| 74 | + {"\\\n", ``, "", true}, |
| 75 | + {`\b`, "\010", "", true}, |
| 76 | + {`\f`, "\014", "", true}, |
| 77 | + {`\t`, "\011", "", true}, |
| 78 | + {`\n`, "\012", "", true}, |
| 79 | + {`\r`, "\015", "", true}, |
| 80 | + {`\v`, "\013", "", true}, |
| 81 | + {`\a`, "\007", "", true}, |
| 82 | + {`\1`, "\001", "", true}, |
| 83 | + {`\12`, "\012", "", true}, |
| 84 | + {`\123`, "\123", "", true}, |
| 85 | + {`\777`, "\xff", "", true}, |
| 86 | + {`\1\12\123\1234`, "\001\012\123\123" + "4", "", true}, |
| 87 | + {`a\1a\12a\123a`, "a\001a\012a\123a", "", true}, |
| 88 | + {`\x`, "", `truncated \x escape at position 0`, true}, |
| 89 | + {`\x1`, "", `truncated \x escape at position 0`, true}, |
| 90 | + {`\x11`, "\x11", "", true}, |
| 91 | + {`\xzz`, "", `invalid \x escape at position 0`, true}, |
| 92 | + {`{\x11}`, "{\x11}", "", true}, |
| 93 | + {`\x01\x8a\xff`, "\x01\x8a\xff", "", true}, |
| 94 | + {`\x01\x8A\xFF`, "\x01\x8a\xff", "", true}, |
| 95 | + {`\u`, `\u`, "", true}, |
| 96 | + {`\u1`, `\u1`, "", true}, |
| 97 | + {`\u12`, `\u12`, "", true}, |
| 98 | + {`z\u134`, `z\u134`, "", true}, |
| 99 | + {`\u1234`, `\u1234`, "", true}, |
| 100 | + {`z\uzzzz`, `z\uzzzz`, "", true}, |
| 101 | + {`{\u1234}`, `{\u1234}`, "", true}, |
| 102 | + {`\U00000001\U0000018a\U000012ff`, `\U00000001\U0000018a\U000012ff`, "", true}, |
| 103 | + {`\U00000001\U0000018A\U000012FF`, `\U00000001\U0000018A\U000012FF`, "", true}, |
| 104 | + {`\U0000`, `\U0000`, "", true}, |
| 105 | + {`\U00001`, `\U00001`, "", true}, |
| 106 | + {`\U000012`, `\U000012`, "", true}, |
| 107 | + {`z\U0000134`, `z\U0000134`, "", true}, |
| 108 | + {`\U00001234`, `\U00001234`, "", true}, |
| 109 | + {`z\Uzzzzzzzz`, `z\Uzzzzzzzz`, "", true}, |
| 110 | + {`{\U00001234}`, `{\U00001234}`, "", true}, |
| 111 | + {`\U00000001\U0000018a\U000012ff`, `\U00000001\U0000018a\U000012ff`, "", true}, |
| 112 | + {`\U00000001\U0000018A\U000012FF`, `\U00000001\U0000018A\U000012FF`, "", true}, |
| 113 | + {`\N{potato}`, `\N{potato}`, "", true}, |
| 114 | + } { |
| 115 | + in := bytes.NewBufferString(test.in) |
| 116 | + out, err := DecodeEscape(in, test.byteMode) |
| 117 | + if err != nil { |
| 118 | + if test.errString == "" { |
| 119 | + t.Errorf("%q: not expecting error but got: %v", test.in, err) |
| 120 | + } else { |
| 121 | + exc := err.(*py.Exception) |
| 122 | + args := exc.Args.(py.Tuple) |
| 123 | + if string(args[0].(py.String)) != test.errString { |
| 124 | + t.Errorf("%q: want error %q but got %q", test.in, test.errString, args[0]) |
| 125 | + } |
| 126 | + } |
| 127 | + continue |
| 128 | + } |
| 129 | + if test.errString != "" { |
| 130 | + t.Errorf("%q: expecting error but didn't get one", test.in) |
| 131 | + continue |
| 132 | + } |
| 133 | + got := out.String() |
| 134 | + if test.want != got { |
| 135 | + t.Errorf("%q: want %q but got %q", test.in, test.want, got) |
| 136 | + } |
| 137 | + } |
| 138 | +} |
0 commit comments