-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_static_field_access.cpp
More file actions
57 lines (48 loc) · 1.73 KB
/
test_static_field_access.cpp
File metadata and controls
57 lines (48 loc) · 1.73 KB
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
#include <jaiscript/core/engine.hpp>
#include <jaiscript/stdlib/stdlib.hpp>
#include <iostream>
int main() {
auto engine = jai::engine::make();
jai::stdlib::register_all(*engine);
try {
// Test 1: Static field access in static method
std::cout << "Test 1: Static field in static method\n";
engine->execute(R"(
class Counter {
static int total = 0;
static auto add(int v) {
print("Before: total = " + to_string(total));
total = total + v;
print("After: total = " + to_string(total));
return total;
}
}
)");
auto result1 = engine->execute("Counter::add(10)");
std::cout << "Result: " << result1.to_string() << std::endl;
// Test 2: Static field access in constructor
std::cout << "\nTest 2: Static field in constructor\n";
engine->execute(R"(
class Config {
static int count = 0;
int id;
Config() {
print("Constructor: count = " + to_string(count));
count = count + 1;
id = count;
print("Constructor: new count = " + to_string(count));
}
auto getId() {
return id;
}
}
)");
auto result2 = engine->execute("auto c = Config(); c.getId()");
std::cout << "ID: " << result2.to_string() << std::endl;
std::cout << "\nAll tests passed!\n";
return 0;
} catch (const std::exception& e) {
std::cerr << "ERROR: " << e.what() << std::endl;
return 1;
}
}