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
//! Printing a long non-ASCII value must not crash the shell.
//!
//! `value::pretty::truncate` sliced `&s[..80]` on a *byte* index. Any string
//! long enough to be truncated whose 80th byte fell inside a multi-byte
//! character panicked the evaluator:
//!
//! ```text
//! thread 'ironshell-eval' panicked at src/value.rs:470:
//! end byte index 80 is not a char boundary; it is inside '─' (bytes 79..82)
//! ```
//!
//! Five shipped examples did exactly that, because a row of box-drawing
//! characters is the most natural way to draw a banner. Any accented prose or
//! emoji of the same length did it too.
//!
//! These run the real binary: a panic inside the library would abort the test
//! process rather than fail an assertion, and the exit status is the signal.
use std::process::{Command, Stdio};
fn ae() -> std::path::PathBuf {
let mut p = std::env::current_exe().expect("test exe");
p.pop();
if p.ends_with("deps") {
p.pop();
}
p.join(format!("ish{}", std::env::consts::EXE_SUFFIX))
}
fn run(code: &str) -> (bool, String) {
let out = Command::new(ae())
.args(["-c", code])
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.output()
.expect("run ae");
let merged = format!(
"{}{}",
String::from_utf8_lossy(&out.stdout),
String::from_utf8_lossy(&out.stderr)
);
(out.status.success(), merged)
}
fn assert_no_panic(label: &str, code: &str) {
let (_, out) = run(code);
assert!(
!out.contains("panicked"),
"{label} panicked the shell:\n{out}"
);
assert!(
!out.contains("char boundary"),
"{label} hit a char-boundary slice:\n{out}"
);
}
/// The exact shape from the examples: a rule of box-drawing characters whose
/// length crosses the truncation limit.
#[test]
fn a_long_box_drawing_rule_does_not_panic() {
for ch in ['─', '═', '━', '·'] {
let rule: String = std::iter::repeat_n(ch, 60).collect();
assert_no_panic(
&format!("a 60-char rule of {ch:?}"),
&format!("print(\"{rule}\")"),
);
}
}
/// Every byte length around the limit, so an off-by-one cannot hide: a
/// three-byte character is placed so that the boundary falls at each of its
/// bytes in turn.
#[test]
fn multibyte_characters_at_every_offset_around_the_limit() {
for pad in 70..90 {
let s = format!("{}★ tail", "a".repeat(pad));
assert_no_panic(
&format!("{pad} ASCII then a 3-byte char"),
&format!("print(\"{s}\")"),
);
}
}
#[test]
fn emoji_and_cjk_do_not_panic() {
let emoji: String = std::iter::repeat_n('🎨', 40).collect();
assert_no_panic("40 emoji", &format!("print(\"{emoji}\")"));
let cjk: String = std::iter::repeat_n('漢', 50).collect();
assert_no_panic("50 CJK characters", &format!("print(\"{cjk}\")"));
}
#[test]
fn long_non_ascii_inside_containers_does_not_panic() {
let rule: String = std::iter::repeat_n('═', 50).collect();
assert_no_panic(
"array of rules",
&format!("print([\"{rule}\", \"{rule}\"])"),
);
assert_no_panic(
"record with a rule",
&format!("print({{banner: \"{rule}\"}})"),
);
}
/// Truncation should count characters, not bytes — otherwise the limit silently
/// means a third as much text in any non-Latin script.
#[test]
fn truncation_counts_characters_not_bytes() {
// 50 CJK characters is 150 bytes but well under an 80-character limit, so
// nothing should be elided.
let cjk: String = std::iter::repeat_n('漢', 50).collect();
let (ok, out) = run(&format!("print(\"{cjk}\")"));
assert!(ok, "printing 50 CJK characters failed: {out}");
assert!(
!out.contains('…'),
"50 characters were truncated as though they were 150: {out}"
);
}