/
test_access_variable.hpp
95 lines (65 loc) · 2.33 KB
/
test_access_variable.hpp
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
#include "vm/test/test.hpp"
#include "builtin/access_variable.hpp"
#include "builtin/symbol.hpp"
#include "builtin/class.hpp"
#include "builtin/string.hpp"
#include "call_frame.hpp"
#include "arguments.hpp"
#include "dispatch.hpp"
#include <cxxtest/TestSuite.h>
#include <map>
#include <vector>
using namespace rubinius;
class TestAccessVariable : public CxxTest::TestSuite, public VMTest {
public:
void setUp() {
create();
}
void tearDown() {
destroy();
}
void test_access_variable_is_slot() {
AccessVariable* av = AccessVariable::allocate(state);
CallFrame cf;
Arguments args(state->symbol("blah"), G(object), 0, 0);
av->name(state, state->symbol("@module_name"));
Object* ret = av->access_execute(state, &cf, av, G(object), args);
TS_ASSERT(try_as<Symbol>(ret));
TS_ASSERT_EQUALS(std::string("Object"), as<Symbol>(ret)->cpp_str(state));
}
void test_write_variable_is_slot() {
AccessVariable* av = AccessVariable::allocate(state);
CallFrame cf;
Symbol* val = state->symbol("Blah");
Object* ary[1] = {val};
Arguments args(state->symbol("blah"), G(object), 1, ary);
av->name(state, state->symbol("@module_name"));
av->write(state, cTrue);
Object* ret = av->access_execute(state, &cf, av, G(object), args);
TS_ASSERT_EQUALS(ret, val);
TS_ASSERT_EQUALS(val, G(object)->module_name());
}
void test_access_variable() {
AccessVariable* av = AccessVariable::allocate(state);
CallFrame cf;
Arguments args(state->symbol("blah"), G(object), 0, 0);
av->name(state, state->symbol("@blah"));
G(object)->set_ivar(state, av->name(), state->symbol("Sweet"));
Object* ret = av->execute(state, &cf, av, G(object), args);
TS_ASSERT(try_as<Symbol>(ret));
TS_ASSERT_EQUALS(std::string("Sweet"), as<Symbol>(ret)->cpp_str(state));
}
void test_write_variable() {
AccessVariable* av = AccessVariable::allocate(state);
CallFrame cf;
Symbol* val = state->symbol("Blah");
Object* ary[1] = {val};
Arguments args(state->symbol("blah"), G(object), 1, ary);
av->name(state, state->symbol("@blah"));
av->write(state, cTrue);
Object* ret = av->execute(state, &cf, av, G(object), args);
TS_ASSERT_EQUALS(ret, val);
Symbol* out = as<Symbol>(G(object)->get_ivar(state, av->name()));
TS_ASSERT_EQUALS(val, out);
}
};