fix #6: compatibility with 8.2
[awesomized/ext-ion] / tests / Reader.phpt
1 --TEST--
2 ion\Reader
3 --EXTENSIONS--
4 ion
5 --INI--
6 date.timezone=UTC
7 --FILE--
8 TEST
9 <?php
10
11 $tests = [
12 "getType" => [
13 ["0d0", ion\Type::Decimal],
14 ["1.23", ion\Type::Decimal],
15 ["abc", ion\Type::Symbol],
16 ["()", ion\Type::SExp],
17 ],
18 "hasAnnotations" => [
19 ["foo::1", true],
20 ["1", false],
21 ],
22 "hasAnnotation" => [
23 ["foo::1", false, "bar"],
24 ["foo::1", true, "foo"],
25 ],
26 "isNull" => [
27 ["null", true],
28 ["1", false],
29 ],
30 "isInStruct" => [
31 ["{a:b}", false],
32 "step" => ["{a:b}", true],
33 ],
34 "getFieldName" => [
35 ["{a:b}", ""],
36 "step" => ["{a:b}", "a"],
37 ],
38 "getFieldNameSymbol" => [
39 ["{a:b}", new ion\Symbol("")],
40 "step" => ["{a:b}", new ion\Symbol("a")],
41 ],
42 "getAnnotations" => [
43 ["foo::bar::1", ["foo", "bar"]],
44 ["1", []],
45 ],
46 "getAnnotationSymbols" => [
47 ["f::b::1", [new ion\Symbol("f"), new ion\Symbol("b")]],
48 ["1", []],
49 ],
50 "countAnnotations" => [
51 ["f::b::z::1", 3],
52 ["1", 0],
53 ],
54 "getAnnotation" => [
55 ["foo::bar::1", "bar", 1],
56 ],
57 "getAnnotationSymbol" => [
58 ["foo::bar::1", new ion\Symbol("foo"), 0],
59 ],
60 "readNull" => [
61 ["null.int", ion\Type::Int],
62 ["null", ion\Type::Null],
63 ],
64 "readBool" => [
65 ["true", true],
66 ["false", false],
67 ],
68 "readInt" => [
69 ["0", 0],
70 ["1234", 1234],
71 [PHP_INT_MAX, PHP_INT_MAX],
72 [PHP_INT_MIN, PHP_INT_MIN],
73 ],
74 "readFloat" => [
75 ["1e", 1e0],
76 ],
77 "readDecimal" => [
78 ["1.23", new ion\Decimal("1.23")],
79 ],
80 "readTimestamp" => [
81 ["2003-05T", new ion\Timestamp(ion\Timestamp\Precision::Month, "!Y-m", "2003-05")],
82 ],
83 "readSymbol" => [
84 ["abc", new ion\Symbol("abc")],
85 ],
86 "readString" => [
87 ["'''abc'''", "abc"],
88 ],
89 "readLob" => [
90 ["{{'''abcdef'''}}", "abcdef"],
91 ],
92 "getPosition" => [
93 ["{}", 1],
94 "next" => ["{}", 2],
95 ],
96 "getDepth" => [
97 ["{}", 0],
98 "step" => ["{}", 1],
99 ],
100 "hasChildren" => [
101 ["{a:b}", true],
102 "step" => ["{a:b}", false],
103 ],
104 "getValueOffset" => [
105 ["a", 0],
106 "step" => ["{ab:'''cdefgh'''", 4],
107 ],
108 "getValueLength" => [ // always returns -1 for text readers
109 ["a", -1],
110 "step" => ["{ab:'''cdefgh'''", -1],
111 ],
112 ];
113
114 foreach ($tests as $test => $specs) {
115 foreach ($specs as $prep => $spec) {
116 [$data, $check] = $spec;
117 $args = array_slice($spec, 2);
118
119 $r = new ion\Reader\Buffer\Reader($data);
120 $r->next();
121 try {
122 switch ($prep) {
123 case "step": $r->getChildren(); /* fall through */
124 case "next": $r->next(); break;
125 }
126 $result = $r->$test(...$args);
127 } catch (Throwable $e) {
128 $result = $e;
129 }
130
131 if ($check != $result) {
132 echo "$test@$prep\n";
133 var_dump(compact("check", "result"));
134 var_dump((string)$result);
135 echo "\n";
136 }
137 }
138 }
139 ?>
140 DONE
141 --EXPECT--
142 TEST
143 DONE