1
//! Entity-accessor native fn codegen.
2

            
3
use nomiscript::SymbolTable;
4

            
5
use super::common::{compile_and_validate, wrap_with_runtime_i32};
6

            
7
#[test]
8
1
fn entity_count_alone() {
9
1
    compile_and_validate("(entity-count)");
10
1
}
11

            
12
#[test]
13
1
fn context_type() {
14
1
    compile_and_validate("(context-type)");
15
1
}
16

            
17
#[test]
18
1
fn primary_entity_type() {
19
1
    compile_and_validate("(primary-entity-type)");
20
1
}
21

            
22
#[test]
23
1
fn primary_entity_idx() {
24
1
    compile_and_validate("(primary-entity-idx)");
25
1
}
26

            
27
#[test]
28
1
fn entity_count_in_comparison() {
29
1
    compile_and_validate("(if (= (entity-count) 0) \"empty\" \"has-entities\")");
30
1
}
31

            
32
#[test]
33
1
fn entity_count_in_do_loop() {
34
1
    compile_and_validate(
35
1
        "(let* ((n (entity-count)) (sum 0)) \
36
1
           (do ((i 0 (+ i 1))) ((>= i n) sum) \
37
1
             (setf sum (+ sum 1))))",
38
    );
39
1
}
40

            
41
#[test]
42
1
fn entity_type_with_runtime_idx() {
43
    // `IDX` is a runtime i32 via `(entity-count)`; the entity-type
44
    // host fn takes it as an arg through the standard wasm-stack arg
45
    // path. Pre-fix this test injected `Expr::WasmRuntime(I32)` as
46
    // the symbol value directly.
47
1
    compile_and_validate(&wrap_with_runtime_i32("(entity-type IDX)"));
48
1
}
49

            
50
#[test]
51
1
fn entity_parent_idx_with_runtime_idx() {
52
1
    compile_and_validate(&wrap_with_runtime_i32("(entity-parent-idx IDX)"));
53
1
}
54

            
55
#[test]
56
1
fn transaction_split_count() {
57
1
    compile_and_validate(&wrap_with_runtime_i32("(transaction-split-count IDX)"));
58
1
}
59

            
60
#[test]
61
1
fn transaction_tag_count() {
62
1
    compile_and_validate(&wrap_with_runtime_i32("(transaction-tag-count IDX)"));
63
1
}
64

            
65
#[test]
66
1
fn transaction_is_multi_currency() {
67
1
    compile_and_validate(&wrap_with_runtime_i32(
68
1
        "(transaction-is-multi-currency IDX)",
69
1
    ));
70
1
}
71

            
72
#[test]
73
1
fn transaction_post_date() {
74
1
    compile_and_validate(&wrap_with_runtime_i32("(transaction-post-date IDX)"));
75
1
}
76

            
77
#[test]
78
1
fn transaction_enter_date() {
79
1
    compile_and_validate(&wrap_with_runtime_i32("(transaction-enter-date IDX)"));
80
1
}
81

            
82
#[test]
83
1
fn split_value_num() {
84
1
    compile_and_validate(&wrap_with_runtime_i32("(split-value-num IDX)"));
85
1
}
86

            
87
#[test]
88
1
fn split_value_denom() {
89
1
    compile_and_validate(&wrap_with_runtime_i32("(split-value-denom IDX)"));
90
1
}
91

            
92
#[test]
93
1
fn split_value() {
94
1
    compile_and_validate(&wrap_with_runtime_i32("(split-value IDX)"));
95
1
}
96

            
97
#[test]
98
1
fn split_reconcile_state() {
99
1
    compile_and_validate(&wrap_with_runtime_i32("(split-reconcile-state IDX)"));
100
1
}
101

            
102
#[test]
103
1
fn split_reconcile_date() {
104
1
    compile_and_validate(&wrap_with_runtime_i32("(split-reconcile-date IDX)"));
105
1
}
106

            
107
#[test]
108
1
fn tag_name() {
109
1
    compile_and_validate(&wrap_with_runtime_i32("(tag-name IDX)"));
110
1
}
111

            
112
#[test]
113
1
fn tag_value() {
114
1
    compile_and_validate(&wrap_with_runtime_i32("(tag-value IDX)"));
115
1
}
116

            
117
#[test]
118
1
fn get_input_entities() {
119
1
    compile_and_validate("(get-input-entities)");
120
1
}
121

            
122
#[test]
123
1
fn create_tag_static() {
124
    // create-tag args: (entity-idx, name, value).
125
1
    compile_and_validate("(create-tag 0 \"my-tag\" \"value\")");
126
1
}
127

            
128
#[test]
129
1
fn create_tag_runtime_idx() {
130
1
    compile_and_validate(&wrap_with_runtime_i32(
131
1
        "(create-tag IDX \"my-tag\" \"value\")",
132
1
    ));
133
1
}
134

            
135
#[test]
136
1
fn create_tag_runtime_name_routes_through_runtime_path() {
137
    // Runtime-typed name forces compile_create_tag_runtime instead
138
    // of the static layout-pack path. Exercises the alternate branch
139
    // of the runtime/static decision in compile_create_tag.
140
1
    compile_and_validate("(let* ((n (upcase-string \"my-tag\"))) (create-tag 0 n \"value\"))");
141
1
}
142

            
143
#[test]
144
1
fn delete_entity_runtime_idx() {
145
1
    compile_and_validate(&wrap_with_runtime_i32("(delete-entity IDX)"));
146
1
}
147

            
148
#[test]
149
1
fn entity_constants_defined() {
150
1
    let symbols = SymbolTable::with_builtins_for_wasm();
151
9
    for name in [
152
1
        "+ENTITY-TRANSACTION+",
153
1
        "+ENTITY-SPLIT+",
154
1
        "+ENTITY-TAG+",
155
1
        "+ENTITY-ACCOUNT+",
156
1
        "+ENTITY-COMMODITY+",
157
1
        "+CONTEXT-CREATE+",
158
1
        "+CONTEXT-UPDATE+",
159
1
        "+CONTEXT-DELETE+",
160
1
        "+CONTEXT-BATCH+",
161
1
    ] {
162
9
        assert!(symbols.lookup(name).is_some(), "missing constant: {name}");
163
    }
164
1
}