1
//! Plan 0014 Stage 0 (ISS-027, ISS-054, ISS-056, ISS-057): what the `nms`
2
//! command line shows for a condition, a void program, a PRINT, and where
3
//! `--compile` writes.
4

            
5
use std::path::PathBuf;
6
use std::process::{Command, Output};
7

            
8
6
fn nms(args: &[&str]) -> Output {
9
6
    Command::new(env!("CARGO_BIN_EXE_nms"))
10
6
        .args(args)
11
6
        .output()
12
6
        .expect("invoke nms")
13
6
}
14

            
15
2
fn stdout(out: &Output) -> String {
16
2
    String::from_utf8_lossy(&out.stdout).into_owned()
17
2
}
18

            
19
3
fn stderr(out: &Output) -> String {
20
3
    String::from_utf8_lossy(&out.stderr).into_owned()
21
3
}
22

            
23
#[test]
24
1
fn an_uncaught_condition_prints_its_code_and_message() {
25
1
    let out = nms(&["-e", r#"(error 'boom "m")"#]);
26
1
    assert!(!out.status.success());
27
1
    assert!(stderr(&out).contains("BOOM: m"), "{}", stderr(&out));
28
1
}
29

            
30
#[test]
31
1
fn a_quote_form_condition_code_is_the_same_as_the_reader_quote() {
32
1
    let out = nms(&["-e", r#"(error (quote boom) "m")"#]);
33
1
    assert!(!out.status.success());
34
1
    assert!(stderr(&out).contains("BOOM: m"), "{}", stderr(&out));
35
1
}
36

            
37
#[test]
38
1
fn a_void_program_is_not_an_error() {
39
1
    let out = nms(&["-e", "(defvar *x* 1)"]);
40
1
    assert!(out.status.success(), "{}", stderr(&out));
41
1
    assert_eq!(stdout(&out), "");
42
1
}
43

            
44
#[test]
45
1
fn print_output_is_echoed_before_the_value() {
46
1
    let out = nms(&["-e", r#"(progn (print "x") 5)"#]);
47
1
    assert!(out.status.success(), "{}", stderr(&out));
48
1
    assert_eq!(stdout(&out), "x\n5\n");
49
1
}
50

            
51
#[test]
52
1
fn compile_writes_to_the_output_path() {
53
1
    let dir = PathBuf::from(env!("CARGO_TARGET_TMPDIR")).join("nms-compile-output");
54
1
    let source = dir.join("prog.nms");
55
1
    let target = dir.join("out").join("prog.bin");
56
1
    std::fs::create_dir_all(dir.join("out")).unwrap();
57
1
    std::fs::write(&source, "(+ 1 2)").unwrap();
58
1
    let out = nms(&[
59
1
        "--compile",
60
1
        source.to_str().unwrap(),
61
1
        "--output",
62
1
        target.to_str().unwrap(),
63
1
    ]);
64
1
    assert!(out.status.success(), "{}", stderr(&out));
65
1
    assert!(target.exists(), "the bytecode lands at --output");
66
1
    assert!(
67
1
        !source.with_extension("wasm").exists(),
68
        "nothing beside the source"
69
    );
70
1
    std::fs::remove_dir_all(&dir).ok();
71
1
}
72

            
73
#[test]
74
1
fn compile_reports_a_script_compile_error() {
75
1
    let dir = PathBuf::from(env!("CARGO_TARGET_TMPDIR")).join("nms-compile-error");
76
1
    let source = dir.join("bad.nms");
77
1
    std::fs::create_dir_all(&dir).unwrap();
78
1
    std::fs::write(&source, "(unclosed").unwrap();
79
1
    let out = nms(&["--compile", source.to_str().unwrap()]);
80
1
    assert!(!out.status.success());
81
1
    assert!(
82
1
        stderr(&out).starts_with("script compile error: "),
83
        "{}",
84
        stderr(&out)
85
    );
86
1
    std::fs::remove_dir_all(&dir).ok();
87
1
}