Skip to content

Commit 93cd629

Browse files
committed
Add test
1 parent 946c6c3 commit 93cd629

1 file changed

Lines changed: 116 additions & 1 deletion

File tree

tests/Cases/Compiler/FinderTest.php

Lines changed: 116 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
use PHPJava\Compiler\Builder\Collection\ConstantPool;
55
use PHPJava\Compiler\Builder\Finder\ConstantPoolFinder;
66
use PHPJava\Compiler\Builder\Maps\EntryMap;
7+
use PHPJava\Compiler\Builder\Structures\Info\ClassInfo;
8+
use PHPJava\Compiler\Builder\Structures\Info\MethodrefInfo;
9+
use PHPJava\Compiler\Builder\Structures\Info\NameAndTypeInfo;
710
use PHPJava\Compiler\Builder\Structures\Info\StringInfo;
811
use PHPJava\Compiler\Builder\Structures\Info\Utf8Info;
912
use PHPJava\Exceptions\FinderException;
@@ -44,7 +47,105 @@ public function testSuccessfullyStringInfoFind()
4447
$this->assertInstanceOf(StringInfo::class, $entry->getEntry());
4548
}
4649

47-
public function testFailedFind()
50+
public function testSuccessfullyClassInfoInfoFind()
51+
{
52+
$constantPool = new ConstantPool();
53+
$finder = new ConstantPoolFinder($constantPool);
54+
$constantPool
55+
->add(new Utf8Info('HelloWorld'))
56+
->add(new ClassInfo(
57+
$finder->find(Utf8Info::class, 'HelloWorld')
58+
));
59+
60+
/**
61+
* @var EntryMap $entry
62+
*/
63+
$entry = $finder->find(ClassInfo::class, 'HelloWorld')->getResult();
64+
$this->assertSame(2, $entry->getEntryIndex());
65+
$this->assertInstanceOf(ClassInfo::class, $entry->getEntry());
66+
}
67+
68+
public function testSuccessfullyNameAndTypeInfoFind()
69+
{
70+
$constantPool = new ConstantPool();
71+
$finder = new ConstantPoolFinder($constantPool);
72+
$constantPool
73+
->add(new Utf8Info('main'))
74+
->add(new Utf8Info('()V'))
75+
->add(new NameAndTypeInfo(
76+
$finder->find(Utf8Info::class, 'main'),
77+
$finder->find(Utf8Info::class, '()V')
78+
));
79+
80+
/**
81+
* @var EntryMap $entry
82+
*/
83+
$entry = $finder->find(NameAndTypeInfo::class, 'main', '()V')->getResult();
84+
$this->assertSame(3, $entry->getEntryIndex());
85+
$this->assertInstanceOf(NameAndTypeInfo::class, $entry->getEntry());
86+
}
87+
88+
public function testSuccessfullyMethodrefInfoFind()
89+
{
90+
$constantPool = new ConstantPool();
91+
$finder = new ConstantPoolFinder($constantPool);
92+
$constantPool
93+
->add(new Utf8Info('HelloWorld'))
94+
->add(new Utf8Info('main'))
95+
->add(new Utf8Info('()V'))
96+
->add(new ClassInfo(
97+
$finder->find(Utf8Info::class, 'HelloWorld')
98+
))
99+
->add(new NameAndTypeInfo(
100+
$finder->find(Utf8Info::class, 'main'),
101+
$finder->find(Utf8Info::class, '()V')
102+
))
103+
->add(
104+
new MethodrefInfo(
105+
$finder->find(ClassInfo::class, 'HelloWorld'),
106+
$finder->find(NameAndTypeInfo::class, 'main', '()V')
107+
)
108+
);
109+
110+
/**
111+
* @var EntryMap $entry
112+
*/
113+
$entry = $finder->find(MethodrefInfo::class, 'HelloWorld', 'main', '()V')->getResult();
114+
$this->assertSame(6, $entry->getEntryIndex());
115+
$this->assertInstanceOf(MethodrefInfo::class, $entry->getEntry());
116+
}
117+
118+
public function testSuccessfullyFieldrefInfoFind()
119+
{
120+
$constantPool = new ConstantPool();
121+
$finder = new ConstantPoolFinder($constantPool);
122+
$constantPool
123+
->add(new Utf8Info('java/lang/System'))
124+
->add(new Utf8Info('out'))
125+
->add(new Utf8Info('Ljava/io/PrintStream;'))
126+
->add(new ClassInfo(
127+
$finder->find(Utf8Info::class, 'java/lang/System')
128+
))
129+
->add(new NameAndTypeInfo(
130+
$finder->find(Utf8Info::class, 'out'),
131+
$finder->find(Utf8Info::class, 'Ljava/io/PrintStream;')
132+
))
133+
->add(
134+
new MethodrefInfo(
135+
$finder->find(ClassInfo::class, 'java/lang/System'),
136+
$finder->find(NameAndTypeInfo::class, 'out', 'Ljava/io/PrintStream;')
137+
)
138+
);
139+
140+
/**
141+
* @var EntryMap $entry
142+
*/
143+
$entry = $finder->find(MethodrefInfo::class, 'java/lang/System', 'out', 'Ljava/io/PrintStream;')->getResult();
144+
$this->assertSame(6, $entry->getEntryIndex());
145+
$this->assertInstanceOf(MethodrefInfo::class, $entry->getEntry());
146+
}
147+
148+
public function testFailedFindPattern1()
48149
{
49150
$constantPool = new ConstantPool();
50151
$finder = new ConstantPoolFinder($constantPool);
@@ -57,4 +158,18 @@ public function testFailedFind()
57158
$this->expectException(FinderException::class);
58159
$finder->find(Utf8Info::class, ':thinking:')->getResult();
59160
}
161+
162+
public function testFailedFindPattern2()
163+
{
164+
$constantPool = new ConstantPool();
165+
$finder = new ConstantPoolFinder($constantPool);
166+
$constantPool
167+
->add(new Utf8Info('Hello World'));
168+
169+
/**
170+
* @var EntryMap $entry
171+
*/
172+
$this->expectException(FinderException::class);
173+
$finder->find(StringInfo::class, 'Hello World')->getResult();
174+
}
60175
}

0 commit comments

Comments
 (0)