1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
|
# frozen_string_literal: true
require "yarp_test_helper"
return if YARP::BACKEND == :FFI
module UnescapeTest
class UnescapeNoneTest < Test::Unit::TestCase
def test_backslash
assert_unescape_none("\\")
end
def test_single_quote
assert_unescape_none("'")
end
private
def assert_unescape_none(source)
assert_equal(source, YARP.const_get(:Debug).unescape_none(source))
end
end
class UnescapeMinimalTest < Test::Unit::TestCase
def test_backslash
assert_unescape_minimal("\\", "\\\\")
end
def test_single_quote
assert_unescape_minimal("'", "\\'")
end
def test_single_char
assert_unescape_minimal("\\a", "\\a")
end
private
def assert_unescape_minimal(expected, source)
assert_equal(expected, YARP.const_get(:Debug).unescape_minimal(source))
end
end
class UnescapeAllTest < Test::Unit::TestCase
def test_backslash
assert_unescape_all("\\", "\\\\")
end
def test_single_quote
assert_unescape_all("'", "\\'")
end
def test_single_char
assert_unescape_all("\a", "\\a")
assert_unescape_all("\b", "\\b")
assert_unescape_all("\e", "\\e")
assert_unescape_all("\f", "\\f")
assert_unescape_all("\n", "\\n")
assert_unescape_all("\r", "\\r")
assert_unescape_all("\s", "\\s")
assert_unescape_all("\t", "\\t")
assert_unescape_all("\v", "\\v")
end
def test_octal
assert_unescape_all("\a", "\\7")
assert_unescape_all("#", "\\43")
assert_unescape_all("a", "\\141")
end
def test_hexadecimal
assert_unescape_all("\a", "\\x7")
assert_unescape_all("#", "\\x23")
assert_unescape_all("a", "\\x61")
end
def test_deletes
assert_unescape_all("\x7f", "\\c?")
assert_unescape_all("\x7f", "\\C-?")
end
def test_unicode_codepoint
assert_unescape_all("a", "\\u0061")
assert_unescape_all("Ā", "\\u0100", "UTF-8")
assert_unescape_all("က", "\\u1000", "UTF-8")
assert_unescape_all("တ", "\\u1010", "UTF-8")
assert_nil(unescape_all("\\uxxxx"))
end
def test_unicode_codepoints
assert_unescape_all("a", "\\u{61}")
assert_unescape_all("Ā", "\\u{0100}", "UTF-8")
assert_unescape_all("က", "\\u{1000}", "UTF-8")
assert_unescape_all("တ", "\\u{1010}", "UTF-8")
assert_unescape_all("𐀀", "\\u{10000}", "UTF-8")
assert_unescape_all("𐀐", "\\u{10010}", "UTF-8")
assert_unescape_all("aĀကတ𐀀𐀐", "\\u{ 61\s100\n1000\t1010\r10000\v10010 }", "UTF-8")
assert_nil(unescape_all("\\u{110000}"))
assert_nil(unescape_all("\\u{110000 110001 110002}"))
end
def test_control_characters
each_printable do |chr|
byte = eval("\"\\c#{chr}\"").bytes.first
assert_unescape_all(byte.chr, "\\c#{chr}")
byte = eval("\"\\C-#{chr}\"").bytes.first
assert_unescape_all(byte.chr, "\\C-#{chr}")
end
end
def test_meta_characters
each_printable do |chr|
byte = eval("\"\\M-#{chr}\"").bytes.first
assert_unescape_all(byte.chr, "\\M-#{chr}")
end
end
def test_meta_control_characters
each_printable do |chr|
byte = eval("\"\\M-\\c#{chr}\"").bytes.first
assert_unescape_all(byte.chr, "\\M-\\c#{chr}")
byte = eval("\"\\M-\\C-#{chr}\"").bytes.first
assert_unescape_all(byte.chr, "\\M-\\C-#{chr}")
byte = eval("\"\\c\\M-#{chr}\"").bytes.first
assert_unescape_all(byte.chr, "\\c\\M-#{chr}")
end
end
def test_escaping_normal_characters
assert_unescape_all("d", "\\d")
assert_unescape_all("g", "\\g")
end
private
def unescape_all(source)
YARP.const_get(:Debug).unescape_all(source)
end
def assert_unescape_all(expected, source, forced_encoding = nil)
result = unescape_all(source)
result.force_encoding(forced_encoding) if forced_encoding
assert_equal(expected, result)
end
def each_printable
(1..127).each do |ord|
chr = ord.chr
yield chr if chr.match?(/[[:print:]]/) && chr != " " && chr != "\\"
end
end
end
end
|