swamp-test-runner 0.2.3

test runner for Swamp
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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
/*
 * Copyright (c) Peter Bjorklund. All rights reserved. https://github.com/swamp/swamp
 * Licensed under the MIT License. See LICENSE in the project root for license information.
 */
use source_map_cache::SourceMapWrapper;
use std::fmt::{Display, Formatter};
use std::io;
use std::io::Write;
use std::num::ParseIntError;
use std::path::{Path, PathBuf};
use std::str::FromStr;
use std::thread::sleep;
use std::time::Duration;
use swamp_runtime::prelude::CodeGenOptions;
use swamp_runtime::{
    CompileAndCodeGenOptions, CompileAndVmResult, CompileOptions, RunOptions,
    compile_codegen_and_create_vm,
};
use swamp_std::print::print_fn;
use swamp_vm::VmState;
use swamp_vm::host::HostFunctionCallback;
use time_dilation::ScopedTimer;
use tracing::error;

#[derive(Debug, Default, Clone, Copy)]
struct TestContext;

#[must_use]
pub fn colorize_parts(parts: &[String]) -> String {
    let new_parts: Vec<_> = parts
        .iter()
        .map(|x| format!("{}", tinter::bright_cyan(x)))
        .collect();

    new_parts.join("::")
}

#[must_use]
pub fn colorful_module_name(parts: &[String]) -> String {
    let x = if parts[0] == "crate" {
        &parts[1..]
    } else {
        parts
    };

    colorize_parts(x)
}

#[must_use]
pub fn pretty_module_parts(parts: &[String]) -> String {
    let new_parts: Vec<_> = parts.iter().map(std::string::ToString::to_string).collect();

    new_parts.join("::")
}

#[must_use]
pub fn pretty_module_name(parts: &[String]) -> String {
    let x = if parts[0] == "crate" {
        &parts[1..]
    } else {
        parts
    };

    pretty_module_parts(x)
}

#[must_use]
pub fn matches_pattern(test_name: &str, pattern: &str) -> bool {
    if pattern.ends_with("::") {
        test_name.starts_with(pattern)
    } else if pattern.contains('*') {
        let parts: Vec<&str> = pattern.split('*').collect();
        if parts.len() > 2 {
            return false;
        }

        let prefix = parts[0];
        let suffix = if parts.len() == 2 { parts[1] } else { "" }; // Handle "*suffix" or "prefix*"

        if !test_name.starts_with(prefix) {
            return false;
        }

        let remaining_name = &test_name[prefix.len()..];
        remaining_name.ends_with(suffix)
    } else {
        test_name == pattern
    }
}
#[must_use]
pub fn test_name_matches_filter(test_name: &str, filter_string: &str) -> bool {
    if filter_string.trim().is_empty() {
        return true;
    }

    let patterns: Vec<&str> = filter_string.split(',').collect();

    for pattern in patterns {
        let trimmed_pattern = pattern.trim();

        if matches_pattern(test_name, trimmed_pattern) {
            return true;
        }
    }

    false
}

pub enum StepBehavior {
    ResumeExecution,
    WaitForUserInput,
    Pause(Duration),
}

pub enum StepParseError {
    UnknownVariant(String),
    MissingDuration,
    ParseInt(ParseIntError),
}

impl Display for StepParseError {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::UnknownVariant(_) => write!(f, "unknown"),
            Self::MissingDuration => write!(f, "missing duration"),
            Self::ParseInt(_) => write!(f, "parse int failed"),
        }
    }
}

impl FromStr for StepBehavior {
    type Err = StepParseError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let lower = s.to_lowercase();
        let mut parts = lower.splitn(2, ':');
        let variant = parts.next().unwrap();

        match variant {
            "resume" => Ok(Self::ResumeExecution),
            "wait" => Ok(Self::WaitForUserInput),
            "pause" => match parts.next() {
                Some(ms_str) => {
                    let ms: u64 = ms_str.parse().map_err(StepParseError::ParseInt)?;
                    Ok(Self::Pause(Duration::from_millis(ms)))
                }
                None => Err(StepParseError::MissingDuration),
            },

            other => Err(StepParseError::UnknownVariant(other.to_string())),
        }
    }
}

pub struct TestRunOptions {
    pub should_run: bool,
    pub iteration_count: usize,
    pub debug_output: bool,
    pub print_output: bool,
    pub debug_opcodes: bool,
    pub debug_operations: bool,
    pub debug_stats: bool,
    pub show_semantic: bool,
    pub show_assembly: bool,
    pub assembly_filter: Option<String>,
    pub show_modules: bool,
    pub show_types: bool,
    pub step_behaviour: StepBehavior,
    pub debug_memory_enabled: bool,
}

pub fn init_logger() {
    tracing_subscriber::fmt()
        .with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
        .with_writer(std::io::stderr)
        .init();
}

#[derive(Clone)]
pub struct TestInfo {
    pub name: String,
}
impl Display for TestInfo {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
        write!(f, "{}", self.name)
    }
}

pub struct TestResult {
    pub passed_tests: Vec<TestInfo>,
    pub failed_tests: Vec<TestInfo>,
}

impl TestResult {
    #[must_use]
    pub const fn succeeded(&self) -> bool {
        self.failed_tests.is_empty()
    }
}

pub struct TestExternals {}

impl HostFunctionCallback for TestExternals {
    fn dispatch_host_call(&mut self, args: swamp_vm::host::HostArgs) {
        if args.function_id == 1 {
            print_fn(args);
        }
    }
}

/// # Panics
#[allow(clippy::too_many_lines)]
pub fn run_tests(
    test_dir: &Path,
    options: &TestRunOptions,
    filter: &str,
    module_suffix: &str,
) -> TestResult {
    let crate_main_path = &["crate".to_string(), module_suffix.to_string()];
    let compile_and_code_gen_options = CompileAndCodeGenOptions {
        compile_options: CompileOptions {
            show_semantic: options.show_semantic,
            show_modules: options.show_modules,
            show_types: options.show_types,
            show_errors: true,
        },
        code_gen_options: CodeGenOptions {
            show_disasm: options.show_assembly,
            disasm_filter: options.assembly_filter.clone(),
            show_debug: options.debug_output,
            show_types: options.show_types,
            ignore_host_call: true,
        },
        skip_codegen: false,
    };
    let internal_result =
        compile_codegen_and_create_vm(test_dir, crate_main_path, &compile_and_code_gen_options)
            .unwrap();

    let CompileAndVmResult::CompileAndVm(mut result) = internal_result else {
        panic!("didn't work to compile")
    };

    let mut passed_tests = Vec::new();

    let mut panic_tests = Vec::new();
    let mut trap_tests = Vec::new();
    let mut failed_tests = Vec::new();
    let mut expected_panic_passed: Vec<TestInfo> = Vec::new();
    let mut expected_trap_passed: Vec<TestInfo> = Vec::new();

    if options.should_run {
        let should_run_in_debug_mode = true; // TODO: Until very stable, always run in debug.  options.debug_opcodes || options.debug_output;

        let run_first_options = RunOptions {
            debug_stats_enabled: options.debug_stats,
            debug_opcodes_enabled: options.debug_opcodes,
            debug_operations_enabled: options.debug_operations,
            debug_memory_enabled: options.debug_memory_enabled,
            max_count: 0,
            use_color: true,
            debug_info: &result.codegen.code_gen_result.debug_info,
            source_map_wrapper: SourceMapWrapper {
                source_map: &result.codegen.source_map,
                current_dir: PathBuf::default(),
            },
        };

        swamp_runtime::run_first_time(
            &mut result.codegen.vm,
            &result.codegen.code_gen_result.constants_in_order,
            &mut TestExternals {},
            &run_first_options,
        );

        {
            let _bootstrap_timer = ScopedTimer::new("run tests a bunch of times");

            for (module_name, module) in result.compile.program.modules.modules() {
                let mut has_shown_mod_name = false;
                for internal_fn in module.symbol_table.internal_functions() {
                    if !internal_fn.attributes.has_attribute("test") {
                        continue;
                    }
                    if options.debug_output && !has_shown_mod_name {
                        //eprintln!(">> module {module_name:?}");
                        has_shown_mod_name = true;
                    }
                    let function_to_run = result
                        .codegen
                        .code_gen_result
                        .functions
                        .get(&internal_fn.program_unique_id)
                        .unwrap();

                    let all_attributes = &function_to_run.internal_function_definition.attributes;

                    let mut expected_vm_state = VmState::Normal;

                    if !all_attributes.is_empty() {
                        let code =
                            all_attributes.get_string_from_fn_arg("should_trap", "expected", 0);
                        if let Some(code) = code {
                            expected_vm_state = VmState::Trap(code.parse().unwrap());
                        } else {
                            let panic_message = all_attributes.get_string_from_fn_arg(
                                "should_panic",
                                "expected",
                                0,
                            );
                            if let Some(panic_message) = panic_message {
                                expected_vm_state = VmState::Panic(panic_message.clone());
                            }
                        }
                    }

                    let complete_name = format!(
                        "{}::{}",
                        colorful_module_name(module_name),
                        tinter::blue(&function_to_run.internal_function_definition.assigned_name)
                    );
                    let formal_name = format!(
                        "{}::{}",
                        pretty_module_name(module_name),
                        &function_to_run.internal_function_definition.assigned_name
                    );

                    if !test_name_matches_filter(&formal_name, filter) {
                        continue;
                    }

                    let test_info = TestInfo { name: formal_name };

                    eprintln!("🚀starting test '{complete_name}'");

                    if should_run_in_debug_mode {
                        for _ in 0..options.iteration_count {
                            swamp_runtime::run_function_with_debug(
                                &mut result.codegen.vm,
                                &function_to_run.ip_range,
                                &mut TestExternals {},
                                &RunOptions {
                                    debug_stats_enabled: options.debug_stats,
                                    debug_opcodes_enabled: options.debug_opcodes,
                                    debug_operations_enabled: options.debug_operations,
                                    debug_memory_enabled: options.debug_memory_enabled,
                                    max_count: 0,
                                    use_color: true,
                                    debug_info: &result.codegen.code_gen_result.debug_info,
                                    source_map_wrapper: SourceMapWrapper {
                                        source_map: &result.codegen.source_map,
                                        current_dir: PathBuf::default(),
                                    },
                                },
                            );

                            while result.codegen.vm.state == VmState::Step {
                                handle_step(&options.step_behaviour);
                                result.codegen.vm.state = VmState::Normal;
                                result.codegen.vm.resume(&mut TestExternals {});
                            }

                            if result.codegen.vm.state != expected_vm_state {
                                break;
                            }
                        }
                    } else {
                        for _ in 0..options.iteration_count {
                            swamp_runtime::run_as_fast_as_possible(
                                &mut result.codegen.vm,
                                function_to_run,
                                &mut TestExternals {},
                                RunOptions {
                                    debug_stats_enabled: options.debug_stats,
                                    debug_opcodes_enabled: options.debug_opcodes,
                                    debug_operations_enabled: options.debug_operations,
                                    max_count: 0,
                                    use_color: true,
                                    debug_info: &result.codegen.code_gen_result.debug_info,
                                    source_map_wrapper: SourceMapWrapper {
                                        source_map: &result.codegen.source_map,
                                        current_dir: PathBuf::default(),
                                    },
                                    debug_memory_enabled: options.debug_memory_enabled,
                                },
                            );
                            while result.codegen.vm.state == VmState::Step {
                                handle_step(&options.step_behaviour);
                                result.codegen.vm.state = VmState::Normal;
                                result.codegen.vm.resume(&mut TestExternals {});
                            }
                            if result.codegen.vm.state != expected_vm_state {
                                break;
                            }
                        }
                    }

                    if expected_vm_state == VmState::Normal {
                        match &result.codegen.vm.state {
                            VmState::Panic(message) => {
                                panic_tests.push(test_info);
                                error!(message, "PANIC!");
                                eprintln!("❌ Panic {complete_name} {message}");
                            }
                            VmState::Normal => {
                                passed_tests.push(test_info);
                                eprintln!("{complete_name} worked!");
                            }
                            VmState::Trap(trap_code) => {
                                trap_tests.push(test_info);
                                error!(%trap_code, "TRAP");
                                eprintln!("❌ trap {complete_name} {trap_code}");
                            }

                            VmState::Step | VmState::Halt => {
                                panic_tests.push(test_info);
                                error!("Step or Halt");
                                eprintln!("❌ trap {complete_name}");
                            }
                        }
                    } else if let VmState::Trap(expected_trap_code) = expected_vm_state {
                        match &result.codegen.vm.state {
                            VmState::Trap(actual_trap_code) => {
                                if actual_trap_code == &expected_trap_code {
                                    expected_trap_passed.push(test_info.clone());
                                    eprintln!(
                                        "✅ Expected Trap {complete_name} (code: {actual_trap_code})"
                                    );
                                } else {
                                    failed_tests.push(test_info.clone());
                                    error!(%expected_trap_code, %actual_trap_code, "WRONG TRAP CODE");
                                    eprintln!(
                                        "❌ Wrong Trap Code {complete_name} (Expected: {expected_trap_code}, Got: {actual_trap_code})"
                                    );
                                }
                            }
                            VmState::Normal => {
                                failed_tests.push(test_info.clone());
                                error!("Expected TRAP, got NORMAL");
                                eprintln!("❌ Expected Trap {complete_name}, but it ran normally.");
                            }
                            VmState::Panic(message) => {
                                failed_tests.push(test_info.clone());
                                error!(message, "Expected TRAP, got PANIC");
                                eprintln!(
                                    "❌ Expected Trap {complete_name}, but it panicked: {message}"
                                );
                            }
                            VmState::Step | VmState::Halt => {
                                panic_tests.push(test_info);
                                error!("Step or Halt");
                                eprintln!("❌ trap {complete_name}");
                            }
                        }
                    } else if let VmState::Panic(expected_panic_message) = expected_vm_state {
                        match &result.codegen.vm.state {
                            VmState::Panic(actual_panic_message) => {
                                if actual_panic_message.contains(&expected_panic_message) {
                                    expected_panic_passed.push(test_info.clone());
                                    eprintln!(
                                        "✅ Expected Panic {complete_name} (message contains: \"{expected_panic_message}\")",
                                    );
                                } else {
                                    failed_tests.push(test_info.clone());
                                    error!(
                                        expected_panic_message,
                                        actual_panic_message, "WRONG PANIC MESSAGE"
                                    );
                                    eprintln!(
                                        "❌ Wrong Panic Message {complete_name} (Expected contains: \"{expected_panic_message}\", Got: \"{actual_panic_message}\")"
                                    );
                                }
                            }
                            VmState::Normal => {
                                failed_tests.push(test_info.clone());
                                error!("Expected PANIC, got NORMAL");
                                eprintln!(
                                    "❌ Expected Panic {complete_name}, but it ran normally."
                                );
                            }
                            VmState::Trap(trap_code) => {
                                failed_tests.push(test_info.clone());
                                error!(%trap_code, "Expected PANIC, got TRAP");
                                eprintln!(
                                    "❌ Expected Panic {complete_name}, but it trapped: {trap_code}"
                                );
                            }
                            VmState::Step | VmState::Halt => {
                                panic_tests.push(test_info);
                                error!("Step or Halt");
                                eprintln!("❌ trap {complete_name}");
                            }
                        }
                    }
                }
            }
        }

        // Calculate counts for each category
        let passed_normal_count = passed_tests.len();
        let unexpected_panic_count = panic_tests.len();
        let unexpected_trap_count = trap_tests.len();
        let failed_mismatch_count = failed_tests.len(); // These are tests that failed for reasons other than an unexpected panic/trap

        let expected_panic_pass_count = expected_panic_passed.len();
        let expected_trap_pass_count = expected_trap_passed.len();

        // Total counts
        let total_passed_count =
            passed_normal_count + expected_panic_pass_count + expected_trap_pass_count;
        let total_failed_count =
            unexpected_panic_count + unexpected_trap_count + failed_mismatch_count;
        let total_tests_run = total_passed_count + total_failed_count;

        // ---
        // ## Test Run Summary
        // ---
        println!("\n---\n🚀 Test Run Complete! 🚀\n");

        println!("Results:");
        println!("  ✅ Passed Normally: {passed_normal_count}");
        println!("  ✅ Passed (Expected Panic): {expected_panic_pass_count}");
        println!("  ✅ Passed (Expected Trap): {expected_trap_pass_count}");

        if total_failed_count > 0 {
            println!("  ❌ **TOTAL FAILED:** {total_failed_count}",);
        }

        println!("  Total Tests Run: {total_tests_run}",);

        // ---
        // ## Failing Test Details
        // ---
        if total_failed_count > 0 {
            println!("\n--- Failing Tests Details ---");

            if unexpected_panic_count > 0 {
                println!("\n### Unexpected Panics:");
                for test in &panic_tests {
                    println!("- ❌ {}", test.name);
                }
            }

            if unexpected_trap_count > 0 {
                println!("\n### Unexpected Traps:");
                for test in &trap_tests {
                    println!("- ❌ {}", test.name);
                }
            }

            if failed_mismatch_count > 0 {
                println!("\n### Other Failures:");
                for test in &failed_tests {
                    println!("- ❌ {}", test.name);
                }
            }
        }

        eprintln!("\n\nvm stats {:?}", result.codegen.vm.debug);
    }

    let failed_tests = [trap_tests, panic_tests].concat();
    TestResult {
        passed_tests,
        failed_tests,
    }
}

fn handle_step(step_behavior: &StepBehavior) {
    match step_behavior {
        StepBehavior::ResumeExecution => {}
        StepBehavior::WaitForUserInput => {
            wait_for_user_pressing_enter();
        }
        StepBehavior::Pause(duration) => {
            eprintln!("step. waiting {duration:?}");
            sleep(*duration);
        }
    }
}

fn wait_for_user_pressing_enter() {
    let mut buf = String::new();
    print!("Step detected. press ENTER to continue");
    io::stdout().flush().unwrap();

    // Blocks until the user hits Enter
    io::stdin().read_line(&mut buf).expect("should work");
}