@@ -132,3 +132,162 @@ let quoted s =
132132 Bytes. unsafe_set s' (n + 1 ) '"' ;
133133 Bytes. unsafe_to_string s'
134134;;
135+
136+ let % expect_test " escaped - plain strings pass through unchanged" =
137+ let test s =
138+ let r = escaped s in
139+ Printf. printf " %S -> %S\n " s r
140+ in
141+ test " hello" ;
142+ test " foo_bar" ;
143+ test " 123" ;
144+ test " a/b/c" ;
145+ [% expect
146+ {|
147+ " hello" -> " hello"
148+ " foo_bar" -> " foo_bar"
149+ " 123" -> " 123"
150+ " a/b/c" -> " a/b/c"
151+ | }]
152+ ;;
153+
154+ let % expect_test " escaped - special characters are escaped" =
155+ let test s =
156+ let r = escaped s in
157+ Printf. printf " %S -> %S\n " s r
158+ in
159+ test " has\" quote" ;
160+ test " back\\ slash" ;
161+ test " new\n line" ;
162+ test " tab\t here" ;
163+ test " car\r ret" ;
164+ test " back\b space" ;
165+ [% expect
166+ {|
167+ " has\" quote" -> " has\\\" quote"
168+ " back\\ slash" -> " back\\\\ slash"
169+ " new\n line" -> " new\\ nline"
170+ " tab\t here" -> " tab\\ there"
171+ " car\r ret" -> " car\\ rret"
172+ " back\b space" -> " back\\ bspace"
173+ | }]
174+ ;;
175+
176+ let % expect_test " escaped - percent brace escaping" =
177+ let test s =
178+ let r = escaped s in
179+ Printf. printf " %S -> %S\n " s r
180+ in
181+ test " %{var}" ;
182+ test " 100%" ;
183+ test " %%" ;
184+ test " %alone" ;
185+ [% expect
186+ {|
187+ " %{var}" -> " \\ %{var}"
188+ " 100%" -> " 100%"
189+ " %%" -> " %%"
190+ " %alone" -> " %alone"
191+ | }]
192+ ;;
193+
194+ let % expect_test " escaped - empty string" =
195+ let r = escaped " " in
196+ Printf. printf " %S -> %S\n " " " r;
197+ [% expect {| " " -> " " | }]
198+ ;;
199+
200+ let % expect_test " escaped - non-ascii bytes are octal-escaped" =
201+ let test s =
202+ let r = escaped s in
203+ Printf. printf " %S -> %S\n " s r
204+ in
205+ test " \x00 " ;
206+ test " \x01 " ;
207+ test " \x7f " ;
208+ test " \xff " ;
209+ [% expect
210+ {|
211+ " \000 " -> " \000 "
212+ " \001 " -> " \001 "
213+ " \127 " -> " \127 "
214+ " \255 " -> " \\ 255"
215+ | }]
216+ ;;
217+
218+ let % expect_test " escaped - valid utf8 passes through" =
219+ let test s =
220+ let r = escaped s in
221+ Printf. printf " %S -> %S\n " s r
222+ in
223+ (* 2-byte: é *)
224+ test " \xc3\xa9 " ;
225+ (* 3-byte: € *)
226+ test " \xe2\x82\xac " ;
227+ (* 4-byte: 𝄞 *)
228+ test " \xf0\x9d\x84\x9e " ;
229+ [% expect
230+ {|
231+ " \195\169 " -> " \195\169 "
232+ " \226\130\172 " -> " \226\130\172 "
233+ " \240\157\132\158 " -> " \240\157\132\158 "
234+ | }]
235+ ;;
236+
237+ let % expect_test " quoted - wraps in double quotes" =
238+ let test s =
239+ let r = quoted s in
240+ Printf. printf " %S -> %s\n " s r
241+ in
242+ test " " ;
243+ test " hello" ;
244+ test " has space" ;
245+ test " has\" quote" ;
246+ test " new\n line" ;
247+ test " %{var}" ;
248+ [% expect
249+ {|
250+ " " -> " "
251+ " hello" -> " hello"
252+ " has space" -> " has space"
253+ " has\" quote" -> " has\" quote"
254+ " new\n line" -> " new\n line"
255+ " %{var}" -> " \% {var}"
256+ | }]
257+ ;;
258+
259+ let % expect_test " quote_length - matches actual escaped length" =
260+ let test s =
261+ let ql = quote_length s in
262+ let actual = String. length (escaped s) in
263+ if ql <> actual
264+ then Printf. printf " MISMATCH %S: quote_length=%d escaped_length=%d\n " s ql actual
265+ in
266+ test " " ;
267+ test " hello" ;
268+ test " has\" quote" ;
269+ test " new\n line" ;
270+ test " %{var}" ;
271+ test " \x00\xff " ;
272+ test " \xc3\xa9 " ;
273+ test " \xe2\x82\xac " ;
274+ test " \xf0\x9d\x84\x9e " ;
275+ print_endline " all match" ;
276+ [% expect {| all match | }]
277+ ;;
278+
279+ let % expect_test " escaped - mixed content" =
280+ let test s =
281+ let r = escaped s in
282+ Printf. printf " %S -> %S\n " s r
283+ in
284+ test " hello\n world\t !" ;
285+ test " say \" hi\" and \\ go" ;
286+ test " %{x} is 100%" ;
287+ [% expect
288+ {|
289+ " hello\n world\t !" -> " hello\\ nworld\\ t!"
290+ " say \" hi\" and \\ go" -> " say \\\" hi\\\" and \\\\ go"
291+ " %{x} is 100%" -> " \\ %{x} is 100%"
292+ | }]
293+ ;;
0 commit comments