/
ValidateTypeTest.php
144 lines (112 loc) · 4.08 KB
/
ValidateTypeTest.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
<?php
namespace spec\watoki\reflect;
use watoki\reflect\type\ArrayType;
use watoki\reflect\type\BooleanType;
use watoki\reflect\type\ClassType;
use watoki\reflect\type\DoubleType;
use watoki\reflect\type\FloatType;
use watoki\reflect\type\IntegerType;
use watoki\reflect\type\LongType;
use watoki\reflect\type\MultiType;
use watoki\reflect\type\NullableType;
use watoki\reflect\type\NullType;
use watoki\reflect\type\StringType;
use watoki\reflect\type\UnknownType;
use watoki\scrut\Specification;
class ValidateTypeTest extends Specification {
function testBoolean() {
$type = new BooleanType();
$this->assertTrue($type->is(true));
$this->assertTrue($type->is(false));
$this->assertFalse($type->is(0));
$this->assertEquals($type->__toString(), 'boolean');
}
function testDouble() {
$type = new DoubleType();
$this->assertTrue($type->is(0.0));
$this->assertFalse($type->is(0));
$this->assertEquals($type->__toString(), 'double');
}
function testFloat() {
$type = new FloatType();
$this->assertTrue($type->is(0.0));
$this->assertFalse($type->is(0));
$this->assertEquals($type->__toString(), 'float');
}
function testLong() {
$type = new LongType();
$this->assertTrue($type->is(0));
$this->assertFalse($type->is(0.0));
$this->assertEquals($type->__toString(), 'long');
}
function testInteger() {
$type = new IntegerType();
$this->assertTrue($type->is(0));
$this->assertFalse($type->is(0.0));
$this->assertEquals($type->__toString(), 'integer');
}
function testString() {
$type = new StringType();
$this->assertTrue($type->is("foo"));
$this->assertFalse($type->is(0));
$this->assertEquals($type->__toString(), 'string');
}
function testArray() {
$type = new ArrayType(new BooleanType());
$this->assertTrue($type->is(array()));
$this->assertTrue($type->is(array(true, false)));
$this->assertFalse($type->is(array(true, 0)));
$this->assertEquals($type->__toString(), 'boolean[]');
}
function testClass() {
$type = new ClassType(ValidateTypeTest_Foo::$CLASS);
$this->assertTrue($type->is(new ValidateTypeTest_Foo()));
$this->assertTrue($type->is(new ValidateTypeTest_Bar()));
$this->assertFalse($type->is(new \StdClass()));
$this->assertFalse($type->is("foo"));
$this->assertEquals($type->__toString(), ValidateTypeTest_Foo::$CLASS);
}
function testMulti() {
$type = new MultiType(array(new StringType(), new BooleanType()));
$this->assertTrue($type->is("foo"));
$this->assertTrue($type->is(true));
$this->assertFalse($type->is(0));
$this->assertEquals($type->__toString(), 'string|boolean');
}
function testNullable() {
$type = new NullableType(new StringType());
$this->assertTrue($type->is(null));
$this->assertTrue($type->is("foo"));
$this->assertFalse($type->is(0));
$this->assertEquals($type->__toString(), 'null|string');
}
function testNull() {
$type = new NullType();
$this->assertTrue($type->is(null));
$this->assertFalse($type->is("foo"));
$this->assertEquals($type->__toString(), 'null');
}
function testUnknown() {
$type = new UnknownType('foo bar');
$this->assertTrue($type->is(null));
$this->assertTrue($type->is("foo"));
$this->assertTrue($type->is(0));
$this->assertEquals($type->__toString(), 'foo bar');
}
function testUnknwonUnknown() {
$type = new UnknownType();
$this->assertTrue($type->is(null));
$this->assertTrue($type->is("foo"));
$this->assertTrue($type->is(0));
$this->assertEquals($type->__toString(), 'mixed');
}
}
class ValidateTypeTest_Foo {
public static $CLASS = __CLASS__;
function __toString() {
return "foo";
}
}
class ValidateTypeTest_Bar extends ValidateTypeTest_Foo {
public static $CLASS = __CLASS__;
}