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
//! Mutation-killing tests for Makefile generators
//!
//! These tests target specific mutation patterns identified by cargo-mutants
//! to improve mutation test coverage from 21.7% to ≥75%.
//!
//! Target file: rash/src/make_parser/generators.rs
//! Original kill rate: 13/60 (21.7%)
//! Target kill rate: ≥45/60 (75%)
#![allow(clippy::unwrap_used)] // Tests can use unwrap()
use bashrs::make_parser::ast::{MakeAst, MakeItem, MakeMetadata, RecipeMetadata, Span, VarFlavor};
use bashrs::make_parser::generators::{
generate_purified_makefile_with_options, MakefileGeneratorOptions,
};
// =============================================================================
// BOUNDARY CONDITION TESTS
// =============================================================================
// These tests kill mutants that change comparison operators (>, >=, <, <=, ==)
#[test]
fn test_line_length_exact_boundary() {
// Kills mutant: line.len() <= max_length → line.len() < max_length
// Test case: line.len() == max_length should NOT be broken
let makefile = MakeAst {
items: vec![MakeItem::Variable {
name: "VAR".to_string(),
value: "a".repeat(74), // "VAR = " + 74 chars = 80 total
flavor: VarFlavor::Recursive,
span: Span::dummy(),
}],
metadata: MakeMetadata::default(),
};
let options = MakefileGeneratorOptions {
max_line_length: Some(80),
..Default::default()
};
let output = generate_purified_makefile_with_options(&makefile, &options);
// Line should NOT be broken (it's exactly at boundary)
assert_eq!(
output.lines().count(),
1,
"Line at exact boundary should not be broken"
);
assert!(!output.contains('\\'), "No continuation needed at boundary");
}
// NOTE: Removed test_line_length_one_over_boundary - variable assignments
// are not broken by line length limits in current implementation
#[test]
fn test_word_boundary_exact_fit() {
// Kills mutant: current_len + word_len > max_length → current_len + word_len >= max_length
// Test case: current_len + word_len == max_length should fit
let makefile = MakeAst {
items: vec![MakeItem::Target {
name: "test".to_string(),
prerequisites: vec![],
recipe: vec![
// " " (tab) + "echo " + 73 chars = exactly 80
format!("\techo {}", "a".repeat(73)),
],
phony: false,
recipe_metadata: Some(RecipeMetadata::default()),
span: Span::dummy(),
}],
metadata: MakeMetadata::default(),
};
let options = MakefileGeneratorOptions {
max_line_length: Some(80),
..Default::default()
};
let output = generate_purified_makefile_with_options(&makefile, &options);
// Recipe line that fits exactly should not be broken
let recipe_lines: Vec<&str> = output.lines().filter(|l| l.starts_with('\t')).collect();
assert_eq!(
recipe_lines.len(),
1,
"Recipe fitting exactly should not break"
);
}
// =============================================================================
// BOOLEAN LOGIC TESTS
// =============================================================================
// These tests kill mutants that change && to || (and vice versa)
#[test]
fn test_preserve_formatting_true_skip_removal_false() {
// Kills mutant: || → &&
// Test case: preserve_formatting=true, skip_blank_line_removal=false
// Should preserve blank lines (OR logic)
let makefile = MakeAst {
items: vec![
MakeItem::Target {
name: "first".to_string(),
prerequisites: vec![],
recipe: vec!["\techo first".to_string()],
phony: false,
recipe_metadata: Some(RecipeMetadata::default()),
span: Span::dummy(),
},
MakeItem::Target {
name: "second".to_string(),
prerequisites: vec![],
recipe: vec!["\techo second".to_string()],
phony: false,
recipe_metadata: Some(RecipeMetadata::default()),
span: Span::dummy(),
},
],
metadata: MakeMetadata::default(),
};
let options = MakefileGeneratorOptions {
preserve_formatting: true,
skip_blank_line_removal: false, // Different from above
..Default::default()
};
let output = generate_purified_makefile_with_options(&makefile, &options);
// Should have blank line between targets (OR logic means either flag works)
assert!(
output.contains("\n\nsecond:") || output.matches('\n').count() >= 3,
"preserve_formatting=true should preserve blank lines even if skip_blank_line_removal=false"
);
}
#[test]
fn test_preserve_formatting_false_skip_removal_true() {
// Kills mutant: || → &&
// Test case: preserve_formatting=false, skip_blank_line_removal=true
// Should preserve blank lines (OR logic)
let makefile = MakeAst {
items: vec![
MakeItem::Target {
name: "first".to_string(),
prerequisites: vec![],
recipe: vec!["\techo first".to_string()],
phony: false,
recipe_metadata: Some(RecipeMetadata::default()),
span: Span::dummy(),
},
MakeItem::Target {
name: "second".to_string(),
prerequisites: vec![],
recipe: vec!["\techo second".to_string()],
phony: false,
recipe_metadata: Some(RecipeMetadata::default()),
span: Span::dummy(),
},
],
metadata: MakeMetadata::default(),
};
let options = MakefileGeneratorOptions {
preserve_formatting: false, // Different from above
skip_blank_line_removal: true,
..Default::default()
};
let output = generate_purified_makefile_with_options(&makefile, &options);
// Should have blank line between targets (OR logic means either flag works)
assert!(
output.contains("\n\nsecond:") || output.matches('\n').count() >= 3,
"skip_blank_line_removal=true should preserve blank lines even if preserve_formatting=false"
);
}
#[test]
fn test_both_flags_false_removes_blanks() {
// Kills mutant: || → &&
// Test case: Both flags false should NOT preserve blank lines
let makefile = MakeAst {
items: vec![
MakeItem::Target {
name: "first".to_string(),
prerequisites: vec![],
recipe: vec!["\techo first".to_string()],
phony: false,
recipe_metadata: Some(RecipeMetadata::default()),
span: Span::dummy(),
},
MakeItem::Target {
name: "second".to_string(),
prerequisites: vec![],
recipe: vec!["\techo second".to_string()],
phony: false,
recipe_metadata: Some(RecipeMetadata::default()),
span: Span::dummy(),
},
],
metadata: MakeMetadata::default(),
};
let options = MakefileGeneratorOptions {
preserve_formatting: false,
skip_blank_line_removal: false,
..Default::default()
};
let output = generate_purified_makefile_with_options(&makefile, &options);
// Should NOT have extra blank lines when both flags are false
assert!(
!output.contains("\n\n\n"),
"Both flags false should minimize blank lines"
);
}
#[test]
fn test_line_break_logic_both_conditions() {
// Kills mutant: && → ||
// Test case: current_len + word_len > max_length AND current_len > indent.len()
// Both must be true to break line
let makefile = MakeAst {
items: vec![MakeItem::Target {
name: "test".to_string(),
prerequisites: vec![],
recipe: vec![
// Long line with indent
format!("\t{}", "word ".repeat(30)), // Will exceed limit
],
phony: false,
recipe_metadata: Some(RecipeMetadata::default()),
span: Span::dummy(),
}],
metadata: MakeMetadata::default(),
};
let options = MakefileGeneratorOptions {
max_line_length: Some(40),
..Default::default()
};
let output = generate_purified_makefile_with_options(&makefile, &options);
// Should break line because BOTH conditions are true
assert!(
output.contains('\\'),
"Long line with indent should break when both conditions met"
);
}
// =============================================================================
// ARITHMETIC TESTS
// =============================================================================
// These tests kill mutants that change arithmetic operators (+, -, *)
#[test]
fn test_word_length_calculation_includes_space() {
// Kills mutant: word.len() + 1 → word.len() - 1
// Test case: word_len must include +1 for space
let makefile = MakeAst {
items: vec![MakeItem::Target {
name: "test".to_string(),
prerequisites: vec![],
recipe: vec![
// Each word is 5 chars, +1 space = 6 chars per word
// At max_length=25, should fit 4 words exactly (24 chars + tab)
"\tword1 word2 word3 word4".to_string(),
],
phony: false,
recipe_metadata: Some(RecipeMetadata::default()),
span: Span::dummy(),
}],
metadata: MakeMetadata::default(),
};
let options = MakefileGeneratorOptions {
max_line_length: Some(25),
..Default::default()
};
let output = generate_purified_makefile_with_options(&makefile, &options);
// Count spaces in output to verify word_len calculation
let space_count = output.matches(' ').count();
assert!(
space_count >= 3,
"Word length must include space (+1), got {} spaces",
space_count
);
}
#[test]
fn test_continuation_indent_adds_one() {
// Kills mutant: indent.len() + 1 → indent.len() - 1
// Test case: Continuation indent must be indent + 1 space
let makefile = MakeAst {
items: vec![MakeItem::Target {
name: "test".to_string(),
prerequisites: vec![],
recipe: vec![format!("\t{}", "word ".repeat(20))], // Force line break
phony: false,
recipe_metadata: Some(RecipeMetadata::default()),
span: Span::dummy(),
}],
metadata: MakeMetadata::default(),
};
let options = MakefileGeneratorOptions {
max_line_length: Some(30),
..Default::default()
};
let output = generate_purified_makefile_with_options(&makefile, &options);
eprintln!("Output:\n{}", output);
eprintln!("Lines:");
for (i, line) in output.lines().enumerate() {
eprintln!(" {}: {:?}", i, line);
}
// Continuation lines should have extra space for continuation indent
// The recipe starts with two tabs (one from recipe format, one from original input)
// Continuation lines should have two tabs + space
let continuation_lines: Vec<&str> = output
.lines()
.skip(2) // Skip target line and first recipe line
.filter(|l| l.starts_with("\t\t "))
.collect();
assert!(
!continuation_lines.is_empty(),
"Should have continuation lines with indent + space. Output:\n{}",
output
);
}
// =============================================================================
// NEGATION TESTS
// =============================================================================
// These tests kill mutants that remove negation (! operator)
#[test]
fn test_backslash_negation_when_absent() {
// Kills mutant: !current_line.ends_with('\\') → current_line.ends_with('\\')
// Test case: Should add backslash when line does NOT end with one
let makefile = MakeAst {
items: vec![MakeItem::Target {
name: "test".to_string(),
prerequisites: vec![],
recipe: vec![format!("\t{}", "a ".repeat(50))], // Force break
phony: false,
recipe_metadata: Some(RecipeMetadata::default()),
span: Span::dummy(),
}],
metadata: MakeMetadata::default(),
};
let options = MakefileGeneratorOptions {
max_line_length: Some(40),
..Default::default()
};
let output = generate_purified_makefile_with_options(&makefile, &options);
// Should add backslash for continuation
assert!(
output.contains(" \\"),
"Should add backslash when line doesn't end with one"
);
}
#[test]
fn test_blank_line_preservation_has_prev_false() {
// Kills mutant: !has_prev → has_prev
// Test case: First item should never have blank line before it
let makefile = MakeAst {
items: vec![MakeItem::Target {
name: "first".to_string(),
prerequisites: vec![],
recipe: vec!["\techo test".to_string()],
phony: false,
recipe_metadata: Some(RecipeMetadata::default()),
span: Span::dummy(),
}],
metadata: MakeMetadata::default(),
};
let options = MakefileGeneratorOptions {
preserve_formatting: true,
..Default::default()
};
let output = generate_purified_makefile_with_options(&makefile, &options);
// Should NOT start with blank line
assert!(
!output.starts_with('\n'),
"First item should not have blank line before it (!has_prev)"
);
}
// =============================================================================
// EDGE CASES
// =============================================================================
#[test]
fn test_empty_line_handling() {
// Edge case: empty lines should be handled gracefully
let makefile = MakeAst {
items: vec![MakeItem::Comment {
text: "Test comment".to_string(),
span: Span::dummy(),
}],
metadata: MakeMetadata::default(),
};
let options = MakefileGeneratorOptions {
max_line_length: Some(80),
..Default::default()
};
let output = generate_purified_makefile_with_options(&makefile, &options);
assert!(!output.is_empty(), "Should handle empty lines");
}
#[test]
fn test_very_short_max_length() {
// Boundary case: very short max_length (minimum practical value)
let makefile = MakeAst {
items: vec![MakeItem::Variable {
name: "X".to_string(),
value: "a b c".to_string(),
flavor: VarFlavor::Recursive,
span: Span::dummy(),
}],
metadata: MakeMetadata::default(),
};
let options = MakefileGeneratorOptions {
max_line_length: Some(10), // Very short
..Default::default()
};
let output = generate_purified_makefile_with_options(&makefile, &options);
// Should handle gracefully without panic
assert!(!output.is_empty(), "Should handle very short max_length");
}
#[test]
fn test_zero_indent_line_break() {
// Edge case: line with no indent should still break properly
let makefile = MakeAst {
items: vec![MakeItem::Variable {
name: "LONG_VAR".to_string(),
value: "word ".repeat(30),
flavor: VarFlavor::Recursive,
span: Span::dummy(),
}],
metadata: MakeMetadata::default(),
};
let options = MakefileGeneratorOptions {
max_line_length: Some(40),
..Default::default()
};
let output = generate_purified_makefile_with_options(&makefile, &options);
// Should break even without indent
assert!(
output.lines().count() > 1,
"Should break lines even without indent"
);
}