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
//  Testing library for the Zia programming language.
// Copyright (C) 2019 Charles Johnson
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

#[macro_use]
extern crate lazy_static;
extern crate proptest;

use std::collections::HashSet;

// Common pattern in tests where a pair of symbols reduces to another symbol. If `b == c` and `b` and `c` are previously unused symbols the tests that use this macro fail as if they forgot the reduction rule.
#[macro_export]
macro_rules! reduce_pair {
    ($cont:ident, $a:ident, $b:ident, $c:ident) => {
        assume_symbols!($a, $b, $c);
        let reduction = format!("let ({} {}) -> {}", $a, $b, $c);
        prop_assert_eq!($cont.execute(&reduction), "");
    };
}

// Checks if a string can respresent a symbol
#[macro_export]
macro_rules! assume_symbol {
    ($a:ident) => {
        prop_assume!($a.len() > 0);
        prop_assume!(!$a.contains(' '));
        prop_assume!(!$a.contains('('));
        prop_assume!(!$a.contains(')'));
        prop_assume!(!$a.starts_with('_') || !$a.ends_with('_'));
    };
}

// Checks if all strings each represent a symbol
#[macro_export]
macro_rules! assume_symbols {
	($($a:ident),*) => ($(assume_symbol!($a);)*)
}

// Saves having to construct a new `HashSet` each time.
#[macro_export]
lazy_static! {
    pub static ref CONCRETE_SYMBOLS: HashSet<String> = {
        let mut cs = HashSet::new();
        cs.insert("label_of".to_string());
        cs.insert("let".to_string());
        cs.insert(":=".to_string());
        cs.insert("->".to_string());
        cs.insert("true".to_string());
        cs.insert("false".to_string());
        cs.insert("assoc".to_string());
        cs.insert("right".to_string());
        cs.insert("left".to_string());
        cs.insert("prec".to_string());
        cs.insert("default".to_string());
        cs.insert(">".to_string());
        cs
    };
}

// A symbol is abstract if isn't the same as one of the concrete symbols
#[macro_export]
macro_rules! assume_abstract {
    ($a:ident) => {
        prop_assume!(!CONCRETE_SYMBOLS.contains(&$a))
    };
}

// Common pattern in tests where a concept is defined from a pair of symbols
#[macro_export]
macro_rules! let_definition {
    ($cont:ident, $a:ident, $b:ident, $c:ident) => {
        assume_abstract!($a);
        assume_symbols!($a, $b, $c);
        prop_assume!($a != $b);
        prop_assume!($a != $c);
        let let_command = format!("let {} := {} {}", $a, $b, $c);
        prop_assert_eq!($cont.execute(&let_command), "");
    };
}