1
use super::compile_upcase_string;
2
use crate::ast::Expr;
3
use crate::compiler::context::CompileContext;
4
use crate::compiler::emit::FunctionEmitter;
5
use crate::error::Error;
6
use crate::reader::Reader;
7
use crate::runtime::SymbolTable;
8

            
9
3
fn upcase(arg: &str) -> Result<bool, Error> {
10
3
    let mut ctx = CompileContext::new_with_host_fns(&[])?;
11
3
    let mut emit = FunctionEmitter::new();
12
3
    let mut symbols = SymbolTable::with_builtins();
13
3
    let arg = Reader::parse_expr(arg)?;
14
3
    compile_upcase_string(&mut ctx, &mut emit, &mut symbols, &[arg])?;
15
1
    Ok(emit.is_reachable())
16
3
}
17

            
18
#[test]
19
1
fn a_constant_string_folds() {
20
1
    assert!(upcase("\"a\"").unwrap());
21
1
}
22

            
23
#[test]
24
1
fn a_plain_non_string_stays_a_compile_error() {
25
1
    assert!(matches!(upcase("1"), Err(Error::Compile(_))));
26
1
}
27

            
28
#[test]
29
1
fn an_effectful_non_string_is_a_compile_error() {
30
    // user decision ISS-087: was the operand's effects, then a runtime TYPE-ERROR.
31
1
    let err = upcase("(progn (print \"x\") 1)");
32
1
    assert!(
33
1
        matches!(&err, Err(Error::Compile(message)) if message == "UPCASE-STRING expects a string, got i32"),
34
        "{err:?}"
35
    );
36
1
}
37

            
38
#[test]
39
1
fn a_wrong_arity_is_an_arity_error() {
40
1
    let mut ctx = CompileContext::new_with_host_fns(&[]).unwrap();
41
1
    let mut emit = FunctionEmitter::new();
42
1
    let mut symbols = SymbolTable::with_builtins();
43
1
    let err = compile_upcase_string(&mut ctx, &mut emit, &mut symbols, &[Expr::Nil, Expr::Nil]);
44
1
    assert!(matches!(err, Err(Error::Arity { .. })));
45
1
}