File: prelude_and_examples.rs

package info (click to toggle)
rust-numbat 1.11.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 996 kB
  • sloc: makefile: 2
file content (107 lines) | stat: -rw-r--r-- 2,841 bytes parent folder | download
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
mod common;

use common::get_test_context;

use numbat::resolver::{CodeSource, ResolverError};
use numbat::{InterpreterResult, NumbatError};

use std::ffi::OsStr;
use std::fs;

use crate::common::get_test_context_without_prelude;

fn assert_runs(code: &str) {
    let result = get_test_context().interpret(code, CodeSource::Internal);
    assert!(result.is_ok());
    assert!(matches!(
        result.unwrap().1,
        InterpreterResult::Value(_) | InterpreterResult::Continue
    ));
}

fn assert_runs_without_prelude(code: &str) {
    let result = get_test_context_without_prelude().interpret(code, CodeSource::Internal);
    assert!(result.is_ok());
    assert!(matches!(
        result.unwrap().1,
        InterpreterResult::Value(_) | InterpreterResult::Continue
    ));
}

fn assert_parse_error(code: &str) {
    assert!(matches!(
        get_test_context().interpret(code, CodeSource::Internal),
        Err(NumbatError::ResolverError(
            ResolverError::ParseErrors { .. }
        ))
    ));
}

fn assert_name_resolution_error(code: &str) {
    assert!(matches!(
        get_test_context().interpret(code, CodeSource::Internal),
        Err(NumbatError::NameResolutionError(_))
    ));
}

fn assert_typecheck_error(code: &str) {
    assert!(matches!(
        get_test_context().interpret(code, CodeSource::Internal),
        Err(NumbatError::TypeCheckError(_))
    ));
}

fn assert_runtime_error(code: &str) {
    assert!(matches!(
        get_test_context().interpret(code, CodeSource::Internal),
        Err(NumbatError::RuntimeError(_))
    ));
}

fn run_for_each_file(glob_pattern: &str, f: impl Fn(&str)) {
    for entry in glob::glob(glob_pattern).unwrap() {
        let path = entry.unwrap();
        if path.extension() != Some(OsStr::new("nbt")) {
            continue;
        }

        println!("Testing example {example:?}", example = path);
        let example_code = fs::read_to_string(path).unwrap();

        f(&example_code);
    }
}

#[test]
#[cfg(feature = "fetch-exchangerates")]
fn modules_are_self_consistent() {
    run_for_each_file("modules/**/*.nbt", assert_runs_without_prelude);
}

#[test]
fn examples_can_be_parsed_and_interpreted() {
    run_for_each_file("../examples/*.nbt", assert_runs);
}

#[test]
fn parse_error_examples_fail_as_expected() {
    run_for_each_file("../examples/parse_error/*.nbt", assert_parse_error);
}

#[test]
fn name_resolution_error_examples_fail_as_expected() {
    run_for_each_file(
        "../examples/name_resolution_error/*.nbt",
        assert_name_resolution_error,
    );
}

#[test]
fn typecheck_error_examples_fail_as_expected() {
    run_for_each_file("../examples/typecheck_error/*.nbt", assert_typecheck_error);
}

#[test]
fn runtime_error_examples_fail_as_expected() {
    run_for_each_file("../examples/runtime_error/*.nbt", assert_runtime_error);
}