/
context_spec.php
197 lines (160 loc) · 6.65 KB
/
context_spec.php
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
<?php
require_once 'spec_helper.php';
class Describe_local_variable extends SimpleSpec {
function should_retrive_variable_using_array_interface() {
$c = create_context(array(
'name' => 'peter',
'hobbies' => array('football', 'basket ball', 'swimming'),
'location' => array(
'address' => '1st Jones St',
'city' => 'Marry hill', 'state' => 'NSW', 'postcode' => 2320
)
));
expects($c['name'])->should_be("peter");
expects($c['name'])->should_not_be("peter wong");
}
function should_set_variable_using_array_interface() {
$c = create_context();
$c['name'] = 'peter';
$c['age'] = 18;
expects($c['name'])->should_be("peter");
expects($c['age'])->should_be(18);
}
function should_ensure_scope_remain_local_in_a_stack_layer() {
$c= new H2o_Context(array('name'=> 'peter'));
$c->push(array('name'=>'wong'));
$c->push(array('name'=>'lee'));
expects($c['name'])->should_be('lee');
expects($c->resolve(':name'))->should_be('lee');
$c->pop();
expects($c->resolve(':name'))->should_be('wong');
$c->pop();
expects($c['name'])->should_be('peter');
}
}
class Describe_context_lookup_basic_data_types extends SimpleSpec {
function should_resolve_a_integer() {
$c= create_context();
expects($c->resolve('0000'))->should_be(0);
expects($c->resolve('-00001'))->should_be(-1);
expects($c->resolve('20000'))->should_be(20000);
}
function should_resolve_a_float_number() {
$c= create_context();
# Float
expects($c->resolve('0.001'))->should_be(0.001);
expects($c->resolve('99.999'))->should_be(99.999);
}
function should_resolve_a_negative_number() {
$c= create_context();
expects($c->resolve('-00001'))->should_be(-1);
}
function should_resolve_a_string() {
$c= new H2o_Context;
expects($c->resolve('"something"'))->should_be('something');
expects($c->resolve("'he hasn\'t eat it yet'"))->should_be("he hasn't eat it yet");
}
}
class Describe_array_lookup extends SimpleSpec {
function should_be_access_by_array_index() {
$c= create_context(array(
'numbers' => array(1,2,3,4,1,2,3,4,5),
));
expects($c->resolve(':numbers.0'))->should_be(1);
expects($c->resolve(':numbers.1'))->should_be(2);
expects($c->resolve(':numbers.8'))->should_be(5);
}
function should_be_access_by_array_key() {
$c= create_context(array('person' => array(
'name' => 'peter','age' => 26, 'tasks'=> array('shopping','sleep')
)));
expects($c->resolve(':person.name'))->should_be('peter');
expects($c->resolve(':person.age'))->should_be(26);
expects($c->resolve(':person.tasks.first'))->should_be('shopping');
}
function should_resolve_array_like_objects() {
$c= create_context(array(
'list' => new ArrayObject(array(
'item 1', 'item 2', 'item 3'
)),
'dict' => new ArrayObject(array(
'name' => 'peter','seo-url' => 'http://google.com'
))
));
expects($c->resolve(':list.0'))->should_be('item 1');
expects($c->resolve(':list.length'))->should_be(3);
expects($c->resolve(':dict.name'))->should_be('peter');
expects($c->resolve(':dict.seo-url'))->should_be('http://google.com');
}
function should_resolve_additional_array_property() {
$c = create_context(array(
'hobbies'=> array('football', 'basket ball', 'swimming')
));
expects($c->resolve(':hobbies.first'))->should_be('football');
expects($c->resolve(':hobbies.last'))->should_be('swimming');
expects($c->resolve(':hobbies.length'))->should_be(3);
expects($c->resolve(':hobbies.size'))->should_be(3);
}
}
class Describe_context_lookup extends SimpleSpec {
function should_use_dot_to_access_object_property() {
$c = create_context(array(
'location' => (object) array(
'address' => '1st Jones St',
'city' => 'Marry hill', 'state' => 'NSW',
'postcode' => 2320
),
));
expects($c->resolve(':location.address'))->should_be('1st Jones St');
expects($c->resolve(':location.city'))->should_be('Marry hill');
}
function should_return_null_for_undefined_or_private_object_property() {
$c = create_context(array(
'document' => new Document(
'my business report',
'Since Augest 2005, financial projection has..')
));
expects($c->resolve(':document.uuid'))->should_be_null(); // Private
expects($c->resolve(':document.undefined_property'))->should_be_null();
}
function should_use_dot_to_perform_method_call() {
$c = create_context(array(
'document' => new Document(
'my business report',
'Since Augest 2005, financial projection has..')
));
expects($c->resolve(':document.to_pdf'))->should_match('/PDF Version :/');
expects($c->resolve(':document.to_xml'))->should_match('/<title>my business report<\/title>/');
}
function should_return_null_for_undefined_or_private_method_call() {
$c = create_context(array(
'document' => new Document(
'my business report',
'Since Augest 2005, financial projection has..')
));
expects($c->resolve(':document._secret'))->should_be_null(); // Private
expects($c->resolve(':document.undefined_method'))->should_be_null();
}
}
class Document {
var $h2o_safe = array('to_pdf', 'to_xml');
private $uuid;
function __construct($title, $content) {
$this->title = $title;
$this->content = $content;
$this->uuid = md5($title.time());
}
function to_pdf() {
return "PDF Version : {$this->title}";
}
function to_xml() {
return "<title>{$this->title}</title><content>{$this->content}</content>";
}
function _secret() {
return "secret no longer";
}
}
function create_context($c = array()) {
return new H2o_Context($c);
}
?>