Skip to content

Commit

Permalink
Convert tests to use Assert DSL
Browse files Browse the repository at this point in the history
  • Loading branch information
thekid committed Oct 9, 2020
1 parent 11d14b3 commit 17930ea
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 76 deletions.
2 changes: 1 addition & 1 deletion src/main/php/text/StreamTokenizer.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,4 @@ public function nextToken($delimiters= null) {

return array_shift($this->_stack);
}
}
}
3 changes: 1 addition & 2 deletions src/main/php/text/StringTokenizer.class.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php namespace text;

/**
* A string tokenizer allows you to break a string into tokens,
* these being delimited by any character in the delimiter.
Expand Down Expand Up @@ -85,4 +84,4 @@ public function nextToken($delimiters= null) {
}
return array_shift($this->_stack);
}
}
}
4 changes: 2 additions & 2 deletions src/main/php/text/TextTokenizer.class.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php namespace text;

use io\IOException;
use io\streams\Reader;
use lang\IllegalStateException;
Expand Down Expand Up @@ -91,4 +91,4 @@ public function nextToken($delimiters= null) {

return array_shift($this->_stack);
}
}
}
3 changes: 1 addition & 2 deletions src/main/php/text/Tokenizer.class.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php namespace text;

/**
* A tokenizer splits input strings into tokens.
*
Expand Down Expand Up @@ -67,4 +66,4 @@ public abstract function hasMoreTokens();
* @return string
*/
public abstract function nextToken($delimiters= null);
}
}
139 changes: 70 additions & 69 deletions src/test/php/text/unittest/AbstractTokenizerTest.class.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php namespace text\unittest;

use unittest\Assert;
use unittest\{Ignore, Test, TestCase};

/**
Expand All @@ -9,7 +10,7 @@
* @see xp://net.xp_framework.unittest.text.StringTokenizerTest
* @see xp://net.xp_framework.unittest.text.StreamTokenizerTest
*/
abstract class AbstractTokenizerTest extends TestCase {
abstract class AbstractTokenizerTest {

/**
* Retrieve a tokenizer instance
Expand Down Expand Up @@ -49,49 +50,49 @@ protected function allTokens($input, $delim) {
#[Test]
public function testSimpleString() {
$t= $this->tokenizerInstance("Hello World!\nThis is an example", " \n");
$this->assertEquals('Hello', $t->nextToken());
$this->assertEquals('World!', $t->nextToken());
$this->assertEquals('This', $t->nextToken());
$this->assertEquals('is', $t->nextToken());
$this->assertEquals('an', $t->nextToken());
$this->assertEquals('example', $t->nextToken());
$this->assertFalse($t->hasMoreTokens());
Assert::equals('Hello', $t->nextToken());
Assert::equals('World!', $t->nextToken());
Assert::equals('This', $t->nextToken());
Assert::equals('is', $t->nextToken());
Assert::equals('an', $t->nextToken());
Assert::equals('example', $t->nextToken());
Assert::false($t->hasMoreTokens());
}

#[Test]
public function testSimpleStringWithDelims() {
$t= $this->tokenizerInstance("Hello World!\nThis is an example", " \n", true);
$this->assertEquals('Hello', $t->nextToken());
$this->assertEquals(' ', $t->nextToken());
$this->assertEquals('World!', $t->nextToken());
$this->assertEquals("\n", $t->nextToken());
$this->assertEquals('This', $t->nextToken());
$this->assertEquals(' ', $t->nextToken());
$this->assertEquals('is', $t->nextToken());
$this->assertEquals(' ', $t->nextToken());
$this->assertEquals('an', $t->nextToken());
$this->assertEquals(' ', $t->nextToken());
$this->assertEquals('example', $t->nextToken());
$this->assertFalse($t->hasMoreTokens());
Assert::equals('Hello', $t->nextToken());
Assert::equals(' ', $t->nextToken());
Assert::equals('World!', $t->nextToken());
Assert::equals("\n", $t->nextToken());
Assert::equals('This', $t->nextToken());
Assert::equals(' ', $t->nextToken());
Assert::equals('is', $t->nextToken());
Assert::equals(' ', $t->nextToken());
Assert::equals('an', $t->nextToken());
Assert::equals(' ', $t->nextToken());
Assert::equals('example', $t->nextToken());
Assert::false($t->hasMoreTokens());
}

#[Test]
public function repetetiveDelimiters() {
$t= $this->tokenizerInstance("Hello \nWorld!", " \n");
$this->assertEquals('Hello', $t->nextToken());
$this->assertEquals('', $t->nextToken());
$this->assertEquals('World!', $t->nextToken());
$this->assertFalse($t->hasMoreTokens());
Assert::equals('Hello', $t->nextToken());
Assert::equals('', $t->nextToken());
Assert::equals('World!', $t->nextToken());
Assert::false($t->hasMoreTokens());
}

#[Test]
public function repetetiveDelimitersWithDelims() {
$t= $this->tokenizerInstance("Hello \nWorld!", " \n", true);
$this->assertEquals('Hello', $t->nextToken());
$this->assertEquals(' ', $t->nextToken());
$this->assertEquals("\n", $t->nextToken());
$this->assertEquals('World!', $t->nextToken());
$this->assertFalse($t->hasMoreTokens());
Assert::equals('Hello', $t->nextToken());
Assert::equals(' ', $t->nextToken());
Assert::equals("\n", $t->nextToken());
Assert::equals('World!', $t->nextToken());
Assert::false($t->hasMoreTokens());
}

#[Test]
Expand All @@ -100,7 +101,7 @@ public function forIteration() {
for ($t= $this->tokenizerInstance('A B C', ' '); $t->hasMoreTokens(); ) {
$r[]= $t->nextToken();
}
$this->assertEquals(range('A', 'C'), $r);
Assert::equals(range('A', 'C'), $r);
}

#[Test]
Expand All @@ -110,7 +111,7 @@ public function whileIteration() {
while ($t->hasMoreTokens()) {
$r[]= $t->nextToken();
}
$this->assertEquals(range('A', 'C'), $r);
Assert::equals(range('A', 'C'), $r);
}

#[Test]
Expand All @@ -119,62 +120,62 @@ public function foreachIteration() {
foreach ($this->tokenizerInstance('A B C', ' ') as $token) {
$r[]= $token;
}
$this->assertEquals(range('A', 'C'), $r);
Assert::equals(range('A', 'C'), $r);
}

#[Test]
public function reset() {
$t= $this->tokenizerInstance('A B C', ' ');
$this->assertTrue($t->hasMoreTokens());
$this->assertEquals('A', $t->nextToken());
Assert::true($t->hasMoreTokens());
Assert::equals('A', $t->nextToken());
$t->reset();
$this->assertTrue($t->hasMoreTokens());
$this->assertEquals('A', $t->nextToken());
Assert::true($t->hasMoreTokens());
Assert::equals('A', $t->nextToken());
}

#[Test]
public function pushBackTokens() {
$t= $this->tokenizerInstance('1,2,5', ',');
$this->assertEquals('1', $t->nextToken());
$this->assertEquals('2', $t->nextToken());
Assert::equals('1', $t->nextToken());
Assert::equals('2', $t->nextToken());
$t->pushBack('3,4,');
$this->assertEquals('3', $t->nextToken());
$this->assertEquals('4', $t->nextToken());
$this->assertEquals('5', $t->nextToken());
Assert::equals('3', $t->nextToken());
Assert::equals('4', $t->nextToken());
Assert::equals('5', $t->nextToken());
}

#[Test]
public function pushBackOrder() {
$t= $this->tokenizerInstance('1,2,5', ',');
$this->assertEquals('1', $t->nextToken());
$this->assertEquals('2', $t->nextToken());
Assert::equals('1', $t->nextToken());
Assert::equals('2', $t->nextToken());
$t->pushBack('4,');
$t->pushBack('3,');
$this->assertEquals('3', $t->nextToken());
$this->assertEquals('4', $t->nextToken());
$this->assertEquals('5', $t->nextToken());
Assert::equals('3', $t->nextToken());
Assert::equals('4', $t->nextToken());
Assert::equals('5', $t->nextToken());
}

#[Test]
public function pushBackDelimiterAtEnd() {
$t= $this->tokenizerInstance("One\nTwo", "\n");
$this->assertEquals('One', $t->nextToken());
$this->assertEquals('Two', $t->nextToken());
Assert::equals('One', $t->nextToken());
Assert::equals('Two', $t->nextToken());
$t->pushBack("Two\n");
$this->assertEquals('Two', $t->nextToken());
Assert::equals('Two', $t->nextToken());
}

#[Test]
public function pushBackDelimiter() {
$this->assertEquals(
Assert::equals(
['// This is a one-line comment', "\n", 'a', '=', ' ', 'b', ' ', '/', ' ', 'c', ';'],
$this->allTokens("// This is a one-line comment\na= b / c;", "/\n =;", "/\n =;")
);
}

#[Test]
public function pushBackRegex() {
$this->assertEquals(
Assert::equals(
['var', ' ', 'pattern', ' ', '=', ' ', '/', '0?([0-9]+)\.0?([0-9]+)(\.0?([0-9]+))?', '/', ';'],
$this->allTokens('var pattern = /0?([0-9]+)\.0?([0-9]+)(\.0?([0-9]+))?/;', "/\n =;")
);
Expand All @@ -183,30 +184,30 @@ public function pushBackRegex() {
#[Test]
public function pushBackAfterHavingReadUntilEnd() {
$t= $this->tokenizerInstance('1,2,', ',');
$this->assertEquals('1', $t->nextToken());
$this->assertEquals('2', $t->nextToken());
$this->assertFalse($t->hasMoreTokens(), 'Should be at end');
Assert::equals('1', $t->nextToken());
Assert::equals('2', $t->nextToken());
Assert::false($t->hasMoreTokens(), 'Should be at end');
$t->pushBack('6,7');
$this->assertTrue($t->hasMoreTokens(), 'Should have tokens after pushing back');
$this->assertEquals('6', $t->nextToken(), 'Should yield token pushed back');
$this->assertEquals('7', $t->nextToken(), 'Should yield token pushed back');
$this->assertFalse($t->hasMoreTokens(), 'Should be at end again');
Assert::true($t->hasMoreTokens(), 'Should have tokens after pushing back');
Assert::equals('6', $t->nextToken(), 'Should yield token pushed back');
Assert::equals('7', $t->nextToken(), 'Should yield token pushed back');
Assert::false($t->hasMoreTokens(), 'Should be at end again');
}

#[Test]
public function pushBackWithDelimitersAfterHavingReadUntilEnd() {
$t= $this->tokenizerInstance('1,2,', ',', true);
$this->assertEquals('1', $t->nextToken());
$this->assertEquals(',', $t->nextToken());
$this->assertEquals('2', $t->nextToken());
$this->assertEquals(',', $t->nextToken());
$this->assertFalse($t->hasMoreTokens(), 'Should be at end');
Assert::equals('1', $t->nextToken());
Assert::equals(',', $t->nextToken());
Assert::equals('2', $t->nextToken());
Assert::equals(',', $t->nextToken());
Assert::false($t->hasMoreTokens(), 'Should be at end');
$t->pushBack('6,7');
$this->assertTrue($t->hasMoreTokens(), 'Should have tokens after pushing back');
$this->assertEquals('6', $t->nextToken(), 'Should yield token pushed back');
$this->assertEquals(',', $t->nextToken());
$this->assertEquals('7', $t->nextToken(), 'Should yield token pushed back');
$this->assertFalse($t->hasMoreTokens(), 'Should be at end again');
Assert::true($t->hasMoreTokens(), 'Should have tokens after pushing back');
Assert::equals('6', $t->nextToken(), 'Should yield token pushed back');
Assert::equals(',', $t->nextToken());
Assert::equals('7', $t->nextToken(), 'Should yield token pushed back');
Assert::false($t->hasMoreTokens(), 'Should be at end again');
}

#[Test, Ignore('Remove ignore annotation to test performance')]
Expand All @@ -231,6 +232,6 @@ public function reading_past_end_returns_null() {
while ($t->hasMoreTokens()) {
$t->nextToken();
}
$this->assertEquals([null, null], [$t->nextToken(), $t->nextToken()]);
Assert::equals([null, null], [$t->nextToken(), $t->nextToken()]);
}
}
1 change: 1 addition & 0 deletions src/test/php/text/unittest/StreamTokenizerTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use io\streams\MemoryInputStream;
use text\StreamTokenizer;
use unittest\Assert;

class StreamTokenizerTest extends AbstractTokenizerTest {

Expand Down
1 change: 1 addition & 0 deletions src/test/php/text/unittest/StringTokenizerTest.class.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php namespace text\unittest;

use text\StringTokenizer;
use unittest\Assert;

class StringTokenizerTest extends AbstractTokenizerTest {

Expand Down
1 change: 1 addition & 0 deletions src/test/php/text/unittest/TextTokenizerTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use io\streams\{MemoryInputStream, TextReader};
use text\TextTokenizer;
use unittest\Assert;

class TextTokenizerTest extends AbstractTokenizerTest {

Expand Down

0 comments on commit 17930ea

Please sign in to comment.