alef-e2e 0.15.1

Fixture-driven e2e test generator for alef
Documentation
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
//! Language-specific string escaping for e2e test code generation.

/// Escape a string for embedding in a Python string literal.
pub fn escape_python(s: &str) -> String {
    let mut out = String::with_capacity(s.len());
    for ch in s.chars() {
        match ch {
            '\\' => out.push_str("\\\\"),
            '"' => out.push_str("\\\""),
            '\n' => out.push_str("\\n"),
            '\r' => out.push_str("\\r"),
            '\t' => out.push_str("\\t"),
            c if (c as u32) < 0x20 => {
                // Control character — emit \xHH escape so Python source remains valid.
                out.push_str(&format!("\\x{:02x}", c as u32));
            }
            c => out.push(c),
        }
    }
    out
}

/// Escape a string for embedding in a Rust string literal.
pub fn escape_rust(s: &str) -> String {
    s.replace('\\', "\\\\")
        .replace('"', "\\\"")
        .replace('\n', "\\n")
        .replace('\r', "\\r")
        .replace('\t', "\\t")
}

/// Compute the number of # needed for a Rust raw string literal.
pub fn raw_string_hashes(s: &str) -> usize {
    let mut max_hashes = 0;
    let mut current = 0;
    let mut after_quote = false;
    for ch in s.chars() {
        if ch == '"' {
            after_quote = true;
            current = 0;
        } else if ch == '#' && after_quote {
            current += 1;
            max_hashes = max_hashes.max(current);
        } else {
            after_quote = false;
            current = 0;
        }
    }
    max_hashes + 1
}

/// Format a string as a Rust raw string literal (r#"..."#).
pub fn rust_raw_string(s: &str) -> String {
    let hashes = raw_string_hashes(s);
    let h: String = "#".repeat(hashes);
    format!("r{h}\"{s}\"{h}")
}

/// Escape a string for embedding in a JavaScript/TypeScript double-quoted string literal.
///
/// `$` does not need escaping in double-quoted strings (only in template literals).
/// Escaping it would produce `\$` which Biome flags as `noUselessEscapeInString`.
pub fn escape_js(s: &str) -> String {
    s.replace('\\', "\\\\")
        .replace('"', "\\\"")
        .replace('\n', "\\n")
        .replace('\r', "\\r")
        .replace('\t', "\\t")
}

/// Escape a string for embedding in a JavaScript/TypeScript template literal (backtick string).
///
/// Template literals interpolate `${...}` and use backtick delimiters, so both
/// `` ` `` and `$` must be escaped to prevent unintended interpolation.
pub fn escape_js_template(s: &str) -> String {
    s.replace('\\', "\\\\").replace('`', "\\`").replace('$', "\\$")
}

/// Returns `true` if the string must use a Go interpreted (double-quoted) literal
/// rather than a raw (backtick) literal.
///
/// Go raw string literals cannot contain backtick characters or NUL bytes, and
/// `\r` inside a raw string is passed through as a literal CR which gofmt rejects.
fn go_needs_quoted(s: &str) -> bool {
    s.contains('`') || s.bytes().any(|b| b == 0 || b == b'\r')
}

/// Format a string as a Go string literal (backtick or quoted).
///
/// Prefers backtick raw literals for readability, but falls back to double-quoted
/// interpreted literals when the string contains characters that raw literals
/// cannot represent: backtick `` ` ``, NUL (`\x00`), or carriage return (`\r`).
pub fn go_string_literal(s: &str) -> String {
    if go_needs_quoted(s) {
        format!("\"{}\"", escape_go(s))
    } else {
        format!("`{s}`")
    }
}

/// Escape a string for embedding in a Go double-quoted string.
///
/// Handles all characters that cannot appear literally in a Go interpreted string:
/// `\\`, `"`, `\n`, `\r`, `\t`, and NUL (`\x00`). Other non-printable bytes are
/// emitted as `\xNN` hex escape sequences.
pub fn escape_go(s: &str) -> String {
    let mut out = String::with_capacity(s.len());
    for b in s.bytes() {
        match b {
            b'\\' => out.push_str("\\\\"),
            b'"' => out.push_str("\\\""),
            b'\n' => out.push_str("\\n"),
            b'\r' => out.push_str("\\r"),
            b'\t' => out.push_str("\\t"),
            0 => out.push_str("\\x00"),
            // Other control characters or non-ASCII bytes: hex escape.
            b if b < 0x20 || b == 0x7f => {
                out.push_str(&format!("\\x{b:02x}"));
            }
            _ => out.push(b as char),
        }
    }
    out
}

/// Escape a string for embedding in a Java string literal.
pub fn escape_java(s: &str) -> String {
    s.replace('\\', "\\\\")
        .replace('"', "\\\"")
        .replace('\n', "\\n")
        .replace('\r', "\\r")
        .replace('\t', "\\t")
}

/// Escape a string for embedding in a Kotlin double-quoted string literal.
/// Like Java escaping but also escapes `$` which triggers Kotlin string interpolation.
pub fn escape_kotlin(s: &str) -> String {
    s.replace('\\', "\\\\")
        .replace('"', "\\\"")
        .replace('$', "\\$")
        .replace('\n', "\\n")
        .replace('\r', "\\r")
        .replace('\t', "\\t")
}

/// Escape a string for embedding in a C# string literal.
pub fn escape_csharp(s: &str) -> String {
    s.replace('\\', "\\\\")
        .replace('"', "\\\"")
        .replace('\n', "\\n")
        .replace('\r', "\\r")
        .replace('\t', "\\t")
}

/// Escape a string for embedding in a PHP string literal.
pub fn escape_php(s: &str) -> String {
    s.replace('\\', "\\\\")
        .replace('"', "\\\"")
        .replace('$', "\\$")
        .replace('\n', "\\n")
        .replace('\r', "\\r")
        .replace('\t', "\\t")
}

/// Escape a string for embedding in a double-quoted Ruby string literal.
pub fn escape_ruby(s: &str) -> String {
    s.replace('\\', "\\\\")
        .replace('"', "\\\"")
        .replace('#', "\\#")
        .replace('\n', "\\n")
        .replace('\r', "\\r")
        .replace('\t', "\\t")
}

/// Escape a string for embedding in a single-quoted Ruby string literal.
/// Single-quoted Ruby strings only interpret `\\` and `\'`.
pub fn escape_ruby_single(s: &str) -> String {
    s.replace('\\', "\\\\").replace('\'', "\\'")
}

/// Convert a `{param}` template string to a Ruby double-quoted string with `#{param}` interpolation.
///
/// `{key}` placeholders are converted to `#{key}`. All other characters are escaped for
/// Ruby double-quoted strings. The returned value does NOT include the surrounding `"` quotes.
pub fn ruby_template_to_interpolation(template: &str) -> String {
    let mut out = String::with_capacity(template.len() + 8);
    let mut chars = template.chars().peekable();
    while let Some(ch) = chars.next() {
        match ch {
            '{' => {
                // Check if this is a {identifier} placeholder
                let is_ident_start = chars.peek().is_some_and(|&c| c.is_ascii_alphabetic() || c == '_');
                if is_ident_start {
                    // Collect the identifier
                    let mut ident = String::new();
                    while let Some(&c) = chars.peek() {
                        if c.is_ascii_alphanumeric() || c == '_' {
                            ident.push(chars.next().unwrap());
                        } else {
                            break;
                        }
                    }
                    if chars.peek() == Some(&'}') {
                        chars.next(); // consume '}'
                        out.push('#');
                        out.push('{');
                        out.push_str(&ident);
                        out.push('}');
                    } else {
                        // Not a valid {ident} placeholder — emit literally
                        out.push('{');
                        out.push_str(&ident);
                    }
                } else {
                    out.push('{');
                }
            }
            '\\' => out.push_str("\\\\"),
            '"' => out.push_str("\\\""),
            '#' => out.push_str("\\#"),
            '\n' => out.push_str("\\n"),
            '\r' => out.push_str("\\r"),
            '\t' => out.push_str("\\t"),
            c => out.push(c),
        }
    }
    out
}

/// Convert a `{param}` template string to an R expression using `paste0()`.
///
/// `{key}` placeholders are converted to variable references in a `paste0(...)` call.
/// Literal text segments are R string literals. If the template has no placeholders,
/// a plain R string literal is returned. If the template is a single bare placeholder
/// like `{text}`, just the variable name is returned.
///
/// Examples:
/// - `"[BTN:{text}]"` → `paste0("[BTN:", text, "]")`
/// - `"--- {text} ---"` → `paste0("--- ", text, " ---")`
/// - `"{text}"` → `text`
/// - `"static"` → `"static"`
pub fn r_template_to_paste0(template: &str) -> String {
    enum Seg {
        Lit(String),
        Param(String),
    }
    let mut segments: Vec<Seg> = Vec::new();
    let mut lit = String::new();
    let mut chars = template.chars().peekable();
    while let Some(ch) = chars.next() {
        if ch == '{' {
            let is_ident_start = chars.peek().is_some_and(|&c| c.is_ascii_alphabetic() || c == '_');
            if is_ident_start {
                let mut ident = String::new();
                while let Some(&c) = chars.peek() {
                    if c.is_ascii_alphanumeric() || c == '_' {
                        ident.push(chars.next().unwrap());
                    } else {
                        break;
                    }
                }
                if chars.peek() == Some(&'}') {
                    chars.next();
                    if !lit.is_empty() {
                        segments.push(Seg::Lit(lit.clone()));
                        lit.clear();
                    }
                    segments.push(Seg::Param(ident));
                    continue;
                }
                lit.push('{');
                lit.push_str(&ident);
            } else {
                lit.push('{');
            }
        } else {
            lit.push(ch);
        }
    }
    if !lit.is_empty() {
        segments.push(Seg::Lit(lit));
    }
    match segments.as_slice() {
        [] => r#""""#.to_string(),
        [Seg::Param(p)] => p.clone(),
        segs => {
            let args: Vec<String> = segs
                .iter()
                .map(|s| match s {
                    Seg::Lit(l) => format!("\"{}\"", escape_r(l)),
                    Seg::Param(p) => p.clone(),
                })
                .collect();
            format!("paste0({})", args.join(", "))
        }
    }
}

/// Returns true if the string needs double quotes (contains control characters
/// that require escape sequences only available in double-quoted strings).
pub fn ruby_needs_double_quotes(s: &str) -> bool {
    s.contains('\n') || s.contains('\r') || s.contains('\t') || s.contains('\0')
}

/// Format a string as a Ruby literal, preferring single quotes.
pub fn ruby_string_literal(s: &str) -> String {
    if ruby_needs_double_quotes(s) {
        format!("\"{}\"", escape_ruby(s))
    } else {
        format!("'{}'", escape_ruby_single(s))
    }
}

/// Escape a string for embedding in an Elixir string literal.
pub fn escape_elixir(s: &str) -> String {
    s.replace('\\', "\\\\")
        .replace('"', "\\\"")
        .replace('#', "\\#")
        .replace('\n', "\\n")
        .replace('\r', "\\r")
        .replace('\t', "\\t")
}

/// Escape a string for embedding in an R string literal.
pub fn escape_r(s: &str) -> String {
    s.replace('\\', "\\\\")
        .replace('"', "\\\"")
        .replace('\n', "\\n")
        .replace('\r', "\\r")
        .replace('\t', "\\t")
}

/// Escape a string for embedding in a C string literal.
pub fn escape_c(s: &str) -> String {
    s.replace('\\', "\\\\")
        .replace('"', "\\\"")
        .replace('\n', "\\n")
        .replace('\r', "\\r")
        .replace('\t', "\\t")
}

/// Sanitize an identifier for use as a test function name.
/// Replaces non-alphanumeric characters with underscores, strips leading digits.
pub fn sanitize_ident(s: &str) -> String {
    let mut result = String::with_capacity(s.len());
    for ch in s.chars() {
        if ch.is_ascii_alphanumeric() || ch == '_' {
            result.push(ch);
        } else {
            result.push('_');
        }
    }
    // Strip leading digits
    let trimmed = result.trim_start_matches(|c: char| c.is_ascii_digit());
    if trimmed.is_empty() {
        "_".to_string()
    } else {
        trimmed.to_string()
    }
}

/// Convert a category name to a sanitized filename component.
pub fn sanitize_filename(s: &str) -> String {
    s.chars()
        .map(|c| if c.is_ascii_alphanumeric() || c == '_' { c } else { '_' })
        .collect::<String>()
        .to_lowercase()
}

/// Expand fixture template expressions in a string value.
///
/// Supported templates:
/// - `{{ repeat 'X' N times }}` — expands to the character X repeated N times
///
/// If no templates are found, the original string is returned unchanged.
pub fn expand_fixture_templates(s: &str) -> String {
    const PREFIX: &str = "{{ repeat '";
    const SUFFIX: &str = " times }}";

    let mut result = String::with_capacity(s.len());
    let mut remaining = s;

    while let Some(start) = remaining.find(PREFIX) {
        result.push_str(&remaining[..start]);
        let after_prefix = &remaining[start + PREFIX.len()..];

        // Expect character(s) followed by `' N times }}`
        if let Some(quote_pos) = after_prefix.find("' ") {
            let ch = &after_prefix[..quote_pos];
            let after_quote = &after_prefix[quote_pos + 2..];

            if let Some(end) = after_quote.find(SUFFIX) {
                let count_str = after_quote[..end].trim();
                if let Ok(count) = count_str.parse::<usize>() {
                    result.push_str(&ch.repeat(count));
                    remaining = &after_quote[end + SUFFIX.len()..];
                    continue;
                }
            }
        }

        // Template didn't match — emit the prefix literally and continue
        result.push_str(PREFIX);
        remaining = after_prefix;
    }
    result.push_str(remaining);
    result
}

/// Escape a string for embedding in a POSIX single-quoted shell string literal.
///
/// Wraps the string in single quotes and escapes embedded single quotes as `'\''`.
/// Single-quoted shell strings treat every character literally except `'`, so
/// no other escaping is needed.
pub fn escape_shell(s: &str) -> String {
    s.replace('\'', r"'\''")
}

/// Escape a string for embedding in a Gleam string literal.
pub fn escape_gleam(s: &str) -> String {
    s.replace('\\', "\\\\")
        .replace('"', "\\\"")
        .replace('\n', "\\n")
        .replace('\r', "\\r")
        .replace('\t', "\\t")
}

/// Escape a string for embedding in a Zig string literal.
pub fn escape_zig(s: &str) -> String {
    s.replace('\\', "\\\\")
        .replace('"', "\\\"")
        .replace('\n', "\\n")
        .replace('\r', "\\r")
        .replace('\t', "\\t")
}

#[cfg(test)]
mod tests {
    use super::*;

    /// Go raw string literals (backticks) cannot contain NUL bytes — gofmt rejects them.
    /// Strings with NUL must fall back to a double-quoted interpreted literal with `\x00`.
    #[test]
    fn go_string_literal_nul_bytes_use_quoted_form() {
        let s = "Hello\x00World";
        let lit = go_string_literal(s);
        // Must not contain a raw NUL byte
        assert!(
            !lit.as_bytes().contains(&0u8),
            "go_string_literal emitted a NUL byte — gofmt would reject this: {lit:?}"
        );
        // Must be a double-quoted string, not a backtick raw string
        assert!(
            lit.starts_with('"'),
            "expected double-quoted string for NUL input, got: {lit:?}"
        );
        // The NUL must be represented as \\x00
        assert!(
            lit.contains("\\x00"),
            "expected \\x00 escape sequence for NUL byte, got: {lit:?}"
        );
    }

    /// Strings with carriage return must also use the double-quoted form
    /// because Go raw strings cannot represent `\r`.
    #[test]
    fn go_string_literal_carriage_return_uses_quoted_form() {
        let s = "line1\r\nline2";
        let lit = go_string_literal(s);
        assert!(
            !lit.as_bytes().contains(&b'\r'),
            "go_string_literal emitted a literal CR — gofmt would reject this: {lit:?}"
        );
        assert!(
            lit.starts_with('"'),
            "expected double-quoted string for CR input, got: {lit:?}"
        );
    }

    /// Strings with only printable chars and no backtick should still use the
    /// readable backtick form.
    #[test]
    fn go_string_literal_plain_string_uses_backtick() {
        let s = "Hello World\nwith newline";
        let lit = go_string_literal(s);
        assert!(
            lit.starts_with('`'),
            "expected backtick form for plain string, got: {lit:?}"
        );
    }

    /// Strings that contain a backtick must fall back to double-quoted form.
    #[test]
    fn go_string_literal_backtick_in_string_uses_quoted_form() {
        let s = "has `backtick`";
        let lit = go_string_literal(s);
        assert!(
            lit.starts_with('"'),
            "expected double-quoted form when string contains backtick, got: {lit:?}"
        );
    }
}