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
// REPL Diff Display Module
//
// Task: REPL-005-002 - Show original vs purified side-by-side
// Test Approach: RED → GREEN → REFACTOR → PROPERTY → MUTATION
//
// Quality targets:
// - Unit tests: 3+ scenarios
// - Property tests: 2+ generators
// - Complexity: <10 per function
use crate::repl::purifier::purify_bash;
/// Display original and purified bash side-by-side
///
/// # Examples
///
/// ```
/// use bashrs::repl::diff::display_diff;
///
/// let original = "mkdir /tmp/test";
/// let result = display_diff(original);
/// assert!(result.is_ok());
/// ```
pub fn display_diff(original: &str) -> anyhow::Result<String> {
// Purify the bash code
let purified = purify_bash(original)?;
// Build side-by-side diff display
let mut output = String::new();
output.push_str("Original → Purified\n");
output.push_str("─────────────────────\n");
// Show original with - marker
output.push_str("- ");
output.push_str(original);
output.push('\n');
// Show purified with + marker
output.push_str("+ ");
output.push_str(&purified);
output.push('\n');
Ok(output)
}
#[cfg(test)]
mod tests {
use super::*;
// ===== UNIT TESTS (RED PHASE) =====
/// Test: REPL-005-002-001 - Basic diff display
#[test]
fn test_REPL_005_002_diff_display() {
let original = "mkdir /tmp/test";
let result = display_diff(original);
assert!(result.is_ok(), "Should display diff: {:?}", result);
let diff = result.unwrap();
// Should show original and purified side-by-side
assert!(
diff.contains("mkdir /tmp/test") && diff.contains("mkdir -p"),
"Should show both original and purified: {}",
diff
);
}
/// Test: REPL-005-002-002 - Diff highlighting with markers
#[test]
fn test_REPL_005_002_diff_highlighting() {
let original = "echo $RANDOM";
let result = display_diff(original);
assert!(
result.is_ok(),
"Should display diff with highlighting: {:?}",
result
);
let diff = result.unwrap();
// Should have markers or indicators for changes
assert!(
diff.contains("-") || diff.contains("+") || diff.contains("|"),
"Should have diff markers: {}",
diff
);
}
}
#[cfg(test)]
mod property_tests {
use super::*;
use proptest::prelude::*;
// ===== PROPERTY TESTS (PROPERTY PHASE) =====
// Property: display_diff should never panic on any input
proptest! {
#[test]
fn prop_diff_never_panics(input in ".*{0,1000}") {
// Test that diff display gracefully handles any input without panicking
let _ = display_diff(&input);
// If we get here without panic, test passes
}
}
// Property: display_diff should be deterministic
proptest! {
#[test]
fn prop_diff_deterministic(input in "[a-z ]{1,50}") {
// Same input should always produce same output
let result1 = display_diff(&input);
let result2 = display_diff(&input);
match (result1, result2) {
(Ok(out1), Ok(out2)) => {
prop_assert_eq!(out1, out2, "Diff display should be deterministic");
}
(Err(_), Err(_)) => {
// Both failed - consistent behavior
}
_ => {
prop_assert!(false, "Inconsistent results for same input");
}
}
}
}
// Property: diff output always contains markers
proptest! {
#[test]
fn prop_diff_has_markers(input in "[a-z ]{1,30}") {
if let Ok(diff) = display_diff(&input) {
// If diff succeeded, should have - and + markers
prop_assert!(
diff.contains("-") && diff.contains("+"),
"Diff should have - and + markers: {}",
diff
);
}
}
}
// Property: diff output preserves original input
proptest! {
#[test]
fn prop_diff_preserves_original(input in "[a-z ]{1,30}") {
if let Ok(diff) = display_diff(&input) {
// Original input should appear in diff output
prop_assert!(
diff.contains(&input),
"Diff should contain original input '{}': {}",
input,
diff
);
}
}
}
// Property: diff output always has header
proptest! {
#[test]
fn prop_diff_has_header(input in "[a-z ]{1,30}") {
if let Ok(diff) = display_diff(&input) {
// Should have header showing original vs purified
prop_assert!(
diff.contains("Original") || diff.contains("Purified"),
"Diff should have header: {}",
diff
);
}
}
}
}