-
Notifications
You must be signed in to change notification settings - Fork 1
/
object_test.rs
240 lines (215 loc) · 5.92 KB
/
object_test.rs
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// Copyright 2023 Silvio Mayolo
//
// This file is part of GDLisp.
//
// GDLisp 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.
//
// GDLisp 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 GDLisp. If not, see <https://www.gnu.org/licenses/>.
extern crate gdlisp;
use gdlisp::compile::error::{GDError, GDErrorF};
use gdlisp::pipeline::error::PError;
use gdlisp::pipeline::source::SourceOffset;
use super::common::*;
#[test]
pub fn empty_object_test() {
assert_eq!(parse_compile_decl("((defobject Foo (Reference)))"),
r#"extends Reference
class _AnonymousClass extends Reference:
func _init():
pass
static func _lazy_0():
var _this_file_1 = load("res://TEST.gd")
var _cond = null
if _this_file_1.has_meta("__gdlisp_Lazy__G_3"):
_cond = _this_file_1.get_meta("__gdlisp_Lazy__G_3")
else:
if true:
var _value_2 = _AnonymousClass.new()
_this_file_1.set_meta("__gdlisp_Lazy__G_3", _value_2)
_cond = _value_2
else:
_cond = null
return _cond
static func __gdlisp_SymbolMacroFunction_Foo():
return GDLisp.cons(GDLisp.cons(GDLisp.intern("access-slot"), GDLisp.cons(GDLisp.cons(GDLisp.intern("contextual-load"), GDLisp.cons("res://TEST.gd", null)), GDLisp.cons(GDLisp.intern("_lazy_0"), null))), null)
"#);
}
#[test]
pub fn main_object_test() {
assert_eq!(
parse_compile_decl_err("((defobject Foo (Reference) main))"),
Err(PError::from(GDError::new(GDErrorF::DottedListError, SourceOffset(1)))),
);
}
#[test]
pub fn simple_self_run_object_test_1() {
assert_eq!(parse_and_run(r#"
((defobject Foo (Reference)
(defvar x 1)
(defn _init ())
(defn double ()
(* self:x 2)))
(print Foo:x)
(print (Foo:double))
(set Foo:x 10)
(print (Foo:double)))
"#), "\n1\n2\n20\n");
}
#[test]
pub fn simple_self_run_object_test_2() {
assert_eq!(parse_and_run(r#"
((defobject Foo Reference
(defvar x 1)
(defn _init ())
(defn double ()
(* self:x 2)))
(print Foo:x)
(print (Foo:double))
(set Foo:x 10)
(print (Foo:double)))
"#), "\n1\n2\n20\n");
}
#[test]
pub fn simple_self_run_object_test_3() {
assert_eq!(parse_and_run(r#"
((defobject Foo (Reference) public
(defvar x 1)
(defn _init ())
(defn double ()
(* self:x 2)))
(print Foo:x)
(print (Foo:double))
(set Foo:x 10)
(print (Foo:double)))
"#), "\n1\n2\n20\n");
}
#[test]
pub fn simple_self_run_object_test_4() {
assert_eq!(parse_and_run(r#"
((defobject Foo (Reference) private
(defvar x 1)
(defn _init ())
(defn double ()
(* self:x 2)))
(print Foo:x)
(print (Foo:double))
(set Foo:x 10)
(print (Foo:double)))
"#), "\n1\n2\n20\n");
}
#[test]
pub fn empty_object_run_test() {
assert_eq!(parse_and_run(r#"
((defobject Foo (Reference)) Foo (print 1))
"#), "\n1\n");
}
#[test]
pub fn self_with_closure_run_object_test() {
assert_eq!(parse_and_run(r#"
((defobject Foo (Reference)
(defvar x 1)
(defn increment ()
(lambda ()
(set self:x (+ self:x 1)))))
(let ((fn (Foo:increment)))
(print (funcall fn))
(print (funcall fn))
(print (funcall fn))))
"#), "\n2\n3\n4\n");
}
#[test]
pub fn macro_in_object_test_1() {
assert_eq!(parse_and_run(r#"
((defmacro declare-fn (name)
`(defn ,name () 99))
(defobject Foo (Reference)
(declare-fn aa)
(declare-fn bb))
(print (Foo:aa))
(print (Foo:bb)))
"#), "\n99\n99\n");
}
#[test]
pub fn macro_in_object_test_2() {
assert_eq!(parse_and_run(r#"
((defmacro my-value ()
630)
(defobject Foo (Reference)
(defn foo () (my-value)))
(print (Foo:foo)))
"#), "\n630\n");
}
#[test]
pub fn macro_in_object_test_3() {
assert_eq!(parse_and_run(r#"
((defmacro declare-fns ()
`(progn (defn a () 67) (defn b () 68)))
(defobject Foo (Reference)
(declare-fns))
(print (Foo:a))
(print (Foo:b)))
"#), "\n67\n68\n");
}
/* /////
#[test]
pub fn macro_uses_object_test() {
assert_eq!(parse_and_run(r#"
((defobject Foo (Reference)
(defvar x))
(defmacro through-foo ()
(set Foo:x 5)
Foo:x)
(print (through-foo)))"#),
"\n5\n");
}
*/
/* /////
#[test]
pub fn reference_to_outer_in_object_test_1() {
// See Issue #85
let output = parse_and_run(r#"
((defn outer () 100)
(defobject Foo (Reference)
(defn foo () (outer)))
(print (Foo:foo))
(set (elt Foo "__gdlisp_outer_class_1") nil))"#); // Nasty hack to break the cyclic reference (have to use elt and a string or GDLisp will catch on to what I'm doing)
assert_eq!(output, "\n100\n");
}
*/
#[test]
pub fn initialization_of_object_test() {
// Make sure that a singleton object is only initialized once, even
// if we reference it a couple of times.
let output = parse_and_run(r#"
((defobject Foo (Reference)
(defn _init ()
(print "Initializing"))
(defn foo ()
18))
(print "Start")
(print (Foo:foo))
(print "Middle")
(print (Foo:foo))
(print "End"))"#);
assert_eq!(output, "\nStart\nInitializing\n18\nMiddle\n18\nEnd\n");
}
#[test]
pub fn object_uses_gdlisp_functions_test() {
let output = parse_and_run(r#"
((defobject MyObject (Reference)
(defvar example-var [1 2 3 4])
(defn sum ()
(array/fold #'+ @example-var 0)))
(print (MyObject:sum)))
"#);
assert_eq!(output, "\n10\n");
}