Skip to content

Commit 5ef55fa

Browse files
committed
Add tests for various output implementations
1 parent 2d593cf commit 5ef55fa

File tree

4 files changed

+171
-0
lines changed

4 files changed

+171
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php namespace lang\ast\unittest\cli;
2+
3+
use io\{File, Folder};
4+
use lang\Environment;
5+
use lang\archive\ArchiveClassLoader;
6+
use unittest\{After, Assert, Before, Test};
7+
use xp\compiler\ToArchive;
8+
9+
class ToArchiveTest {
10+
private $folder, $archive;
11+
12+
#[Before]
13+
public function folder() {
14+
$this->folder= new Folder(realpath(Environment::tempDir()), '.xp-'.crc32(self::class));
15+
$this->folder->exists() && $this->folder->unlink();
16+
$this->folder->create();
17+
18+
$this->archive= new File($this->folder, 'dist.xar');
19+
$this->archive->touch();
20+
}
21+
22+
#[After]
23+
public function cleanup() {
24+
$this->folder->unlink();
25+
}
26+
27+
#[Test]
28+
public function can_create() {
29+
new ToArchive($this->archive);
30+
}
31+
32+
#[Test]
33+
public function write_to_target_then_load_via_class_loader() {
34+
$class= '<?php class Test { }';
35+
36+
$fixture= new ToArchive($this->archive);
37+
with ($fixture->target('Test.php'), function($out) use($class) {
38+
$out->write($class);
39+
$out->flush();
40+
$out->close();
41+
});
42+
$fixture->close();
43+
44+
Assert::equals($class, (new ArchiveClassLoader($this->archive->getURI()))->loadClassBytes('Test'));
45+
}
46+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php namespace lang\ast\unittest\cli;
2+
3+
use io\{File, Folder};
4+
use lang\Environment;
5+
use lang\FileSystemClassLoader;
6+
use unittest\{After, Assert, Before, Test};
7+
use xp\compiler\ToFile;
8+
9+
class ToFileTest {
10+
private $folder, $target;
11+
12+
#[Before]
13+
public function folder() {
14+
$this->folder= new Folder(realpath(Environment::tempDir()), '.xp-'.crc32(self::class));
15+
$this->folder->exists() && $this->folder->unlink();
16+
$this->folder->create();
17+
18+
$this->target= new File($this->folder, 'Test.class.php');
19+
}
20+
21+
#[After]
22+
public function cleanup() {
23+
$this->folder->unlink();
24+
}
25+
26+
#[Test]
27+
public function can_create() {
28+
new ToFile($this->target);
29+
}
30+
31+
#[Test]
32+
public function write_to_target_then_load_via_class_loader() {
33+
$class= '<?php class Test { }';
34+
35+
$fixture= new ToFile($this->target);
36+
with ($fixture->target('Test.php'), function($out) use($class) {
37+
$out->write($class);
38+
$out->flush();
39+
$out->close();
40+
});
41+
$fixture->close();
42+
43+
Assert::equals($class, (new FileSystemClassLoader($this->folder->getURI()))->loadClassBytes('Test'));
44+
}
45+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php namespace lang\ast\unittest\cli;
2+
3+
use io\{Folder, File};
4+
use lang\Environment;
5+
use lang\FileSystemClassLoader;
6+
use unittest\{After, Assert, Before, Test};
7+
use xp\compiler\ToFolder;
8+
9+
class ToFolderTest {
10+
private $folder;
11+
12+
#[Before]
13+
public function folder() {
14+
$this->folder= new Folder(realpath(Environment::tempDir()), '.xp-'.crc32(self::class));
15+
$this->folder->exists() && $this->folder->unlink();
16+
$this->folder->create();
17+
}
18+
19+
#[After]
20+
public function cleanup() {
21+
$this->folder->unlink();
22+
}
23+
24+
#[Test]
25+
public function can_create() {
26+
new ToFolder($this->folder);
27+
}
28+
29+
#[Test]
30+
public function dash_special_case() {
31+
with ((new ToFolder($this->folder))->target('-'), function($out) {
32+
$out->write('<?php ...');
33+
});
34+
Assert::true((new File($this->folder, 'out'.\xp::CLASS_FILE_EXT))->exists());
35+
}
36+
37+
#[Test]
38+
public function write_to_target_then_load_via_class_loader() {
39+
$class= '<?php class Test { }';
40+
41+
$fixture= new ToFolder($this->folder);
42+
with ($fixture->target('Test.php'), function($out) use($class) {
43+
$out->write($class);
44+
$out->flush();
45+
$out->close();
46+
});
47+
$fixture->close();
48+
49+
Assert::equals($class, (new FileSystemClassLoader($this->folder->getURI()))->loadClassBytes('Test'));
50+
}
51+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php namespace lang\ast\unittest\cli;
2+
3+
use io\streams\MemoryOutputStream;
4+
use unittest\{After, Assert, Before, Test};
5+
use xp\compiler\ToStream;
6+
7+
class ToStreamTest {
8+
9+
#[Test]
10+
public function can_create() {
11+
new ToStream(new MemoryOutputStream());
12+
}
13+
14+
#[Test]
15+
public function write_to_stream() {
16+
$class= '<?php class Test { }';
17+
18+
$out= new MemoryOutputStream();
19+
$fixture= new ToStream($out);
20+
with ($fixture->target('Test.php'), function($out) use($class) {
21+
$out->write($class);
22+
$out->flush();
23+
$out->close();
24+
});
25+
$fixture->close();
26+
27+
Assert::equals($class, $out->bytes());
28+
}
29+
}

0 commit comments

Comments
 (0)