-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathFinderTest.php
More file actions
176 lines (159 loc) Β· 6.01 KB
/
FinderTest.php
File metadata and controls
176 lines (159 loc) Β· 6.01 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
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
<?php
declare(strict_types=1);
namespace PHPJava\Tests\Cases\Compiler;
use PHPJava\Compiler\Builder\Collection\ConstantPool;
use PHPJava\Compiler\Builder\Finder\ConstantPoolFinder;
use PHPJava\Compiler\Builder\Maps\EntryMap;
use PHPJava\Compiler\Builder\Structures\Info\ClassInfo;
use PHPJava\Compiler\Builder\Structures\Info\MethodrefInfo;
use PHPJava\Compiler\Builder\Structures\Info\NameAndTypeInfo;
use PHPJava\Compiler\Builder\Structures\Info\StringInfo;
use PHPJava\Compiler\Builder\Structures\Info\Utf8Info;
use PHPJava\Exceptions\FinderException;
use PHPJava\Tests\Cases\Base;
class FinderTest extends Base
{
public function testSuccessfullyUtf8InfoFind()
{
$constantPool = new ConstantPool();
$finder = new ConstantPoolFinder($constantPool);
$constantPool
->add(new Utf8Info('Hello World'));
/**
* @var EntryMap $entry
*/
$entry = $finder->find(Utf8Info::class, 'Hello World')->getResult();
$this->assertSame(1, $entry->getEntryIndex());
$this->assertInstanceOf(Utf8Info::class, $entry->getEntry());
}
public function testSuccessfullyStringInfoFind()
{
$constantPool = new ConstantPool();
$finder = new ConstantPoolFinder($constantPool);
$constantPool
->add(new Utf8Info('Hello World'))
->add(new StringInfo(
$finder->find(Utf8Info::class, 'Hello World')
));
/**
* @var EntryMap $entry
*/
$entry = $finder->find(StringInfo::class, 'Hello World')->getResult();
$this->assertSame(2, $entry->getEntryIndex());
$this->assertInstanceOf(StringInfo::class, $entry->getEntry());
}
public function testSuccessfullyClassInfoInfoFind()
{
$constantPool = new ConstantPool();
$finder = new ConstantPoolFinder($constantPool);
$constantPool
->add(new Utf8Info('HelloWorld'))
->add(new ClassInfo(
$finder->find(Utf8Info::class, 'HelloWorld')
));
/**
* @var EntryMap $entry
*/
$entry = $finder->find(ClassInfo::class, 'HelloWorld')->getResult();
$this->assertSame(2, $entry->getEntryIndex());
$this->assertInstanceOf(ClassInfo::class, $entry->getEntry());
}
public function testSuccessfullyNameAndTypeInfoFind()
{
$constantPool = new ConstantPool();
$finder = new ConstantPoolFinder($constantPool);
$constantPool
->add(new Utf8Info('main'))
->add(new Utf8Info('()V'))
->add(new NameAndTypeInfo(
$finder->find(Utf8Info::class, 'main'),
$finder->find(Utf8Info::class, '()V')
));
/**
* @var EntryMap $entry
*/
$entry = $finder->find(NameAndTypeInfo::class, 'main', '()V')->getResult();
$this->assertSame(3, $entry->getEntryIndex());
$this->assertInstanceOf(NameAndTypeInfo::class, $entry->getEntry());
}
public function testSuccessfullyMethodrefInfoFind()
{
$constantPool = new ConstantPool();
$finder = new ConstantPoolFinder($constantPool);
$constantPool
->add(new Utf8Info('HelloWorld'))
->add(new Utf8Info('main'))
->add(new Utf8Info('()V'))
->add(new ClassInfo(
$finder->find(Utf8Info::class, 'HelloWorld')
))
->add(new NameAndTypeInfo(
$finder->find(Utf8Info::class, 'main'),
$finder->find(Utf8Info::class, '()V')
))
->add(
new MethodrefInfo(
$finder->find(ClassInfo::class, 'HelloWorld'),
$finder->find(NameAndTypeInfo::class, 'main', '()V')
)
);
/**
* @var EntryMap $entry
*/
$entry = $finder->find(MethodrefInfo::class, 'HelloWorld', 'main', '()V')->getResult();
$this->assertSame(6, $entry->getEntryIndex());
$this->assertInstanceOf(MethodrefInfo::class, $entry->getEntry());
}
public function testSuccessfullyFieldrefInfoFind()
{
$constantPool = new ConstantPool();
$finder = new ConstantPoolFinder($constantPool);
$constantPool
->add(new Utf8Info('java/lang/System'))
->add(new Utf8Info('out'))
->add(new Utf8Info('Ljava/io/PrintStream;'))
->add(new ClassInfo(
$finder->find(Utf8Info::class, 'java/lang/System')
))
->add(new NameAndTypeInfo(
$finder->find(Utf8Info::class, 'out'),
$finder->find(Utf8Info::class, 'Ljava/io/PrintStream;')
))
->add(
new MethodrefInfo(
$finder->find(ClassInfo::class, 'java/lang/System'),
$finder->find(NameAndTypeInfo::class, 'out', 'Ljava/io/PrintStream;')
)
);
/**
* @var EntryMap $entry
*/
$entry = $finder->find(MethodrefInfo::class, 'java/lang/System', 'out', 'Ljava/io/PrintStream;')->getResult();
$this->assertSame(6, $entry->getEntryIndex());
$this->assertInstanceOf(MethodrefInfo::class, $entry->getEntry());
}
public function testFailedFindPattern1()
{
$constantPool = new ConstantPool();
$finder = new ConstantPoolFinder($constantPool);
$constantPool
->add(new Utf8Info('Hello World'));
/**
* @var EntryMap $entry
*/
$this->expectException(FinderException::class);
$finder->find(Utf8Info::class, ':thinking:')->getResult();
}
public function testFailedFindPattern2()
{
$constantPool = new ConstantPool();
$finder = new ConstantPoolFinder($constantPool);
$constantPool
->add(new Utf8Info('Hello World'));
/**
* @var EntryMap $entry
*/
$this->expectException(FinderException::class);
$finder->find(StringInfo::class, 'Hello World')->getResult();
}
}