Skip to content
Snippets Groups Projects
Commit 24e64d1a authored by Tyler Lemburg's avatar Tyler Lemburg
Browse files

Add unit tests

parent bb12c6ec
Branches
Tags
1 merge request!4Update unicode function to allow for ligatures
Pipeline #
...@@ -91,42 +91,28 @@ class EasySVG { ...@@ -91,42 +91,28 @@ class EasySVG {
} }
} }
$thisValue = _ordutf8(mb_substr($str, $i, 1)); $unicode[] = $this->_unicodeOrd(mb_substr($str, $i, 1));
if ($thisValue < 128) $unicode[] = $thisValue;
else {
if ( count( $values ) == 0 ) $lookingFor = ( $thisValue < 224 ) ? 2 : 3;
$values[] = $thisValue;
if ( count( $values ) == $lookingFor ) {
$number = ( $lookingFor == 3 ) ?
( ( $values[0] % 16 ) * 4096 ) + ( ( $values[1] % 64 ) * 64 ) + ( $values[2] % 64 ):
( ( $values[0] % 32 ) * 64 ) + ( $values[1] % 64 );
$unicode[] = $number;
$values = array();
$lookingFor = 1;
}
}
} }
return $unicode; return $unicode;
} }
private function _ordutf8($string) { private function _unicodeOrd($c) {
$k = 0; if (ord($c[0]) >=0 && ord($c[0]) <= 127)
$code = ord(substr($string, $k,1)); return ord($c[0]);
if ($code >= 128) { //otherwise 0xxxxxxx if (ord($c[0]) >= 192 && ord($c[0]) <= 223)
if ($code < 224) $bytesnumber = 2; //110xxxxx return (ord($c[0])-192)*64 + (ord($c[1])-128);
else if ($code < 240) $bytesnumber = 3; //1110xxxx if (ord($c[0]) >= 224 && ord($c[0]) <= 239)
else if ($code < 248) $bytesnumber = 4; //11110xxx return (ord($c[0])-224)*4096 + (ord($c[1])-128)*64 + (ord($c[2])-128);
$codetemp = $code - 192 - ($bytesnumber > 2 ? 32 : 0) - ($bytesnumber > 3 ? 16 : 0); if (ord($c[0]) >= 240 && ord($c[0]) <= 247)
for ($i = 2; $i <= $bytesnumber; $i++) { return (ord($c[0])-240)*262144 + (ord($c[1])-128)*4096 + (ord($c[2])-128)*64 + (ord($c[3])-128);
$k++; if (ord($c[0]) >= 248 && ord($c[0]) <= 251)
$code2 = ord(substr($string, $k, 1)) - 128; //10xxxxxx return (ord($c[0])-248)*16777216 + (ord($c[1])-128)*262144 + (ord($c[2])-128)*4096 + (ord($c[3])-128)*64 + (ord($c[4])-128);
$codetemp = $codetemp*64 + $code2; if (ord($c[0]) >= 252 && ord($c[0]) <= 253)
} return (ord($c[0])-252)*1073741824 + (ord($c[1])-128)*16777216 + (ord($c[2])-128)*262144 + (ord($c[3])-128)*4096 + (ord($c[4])-128)*64 + (ord($c[5])-128);
$code = $codetemp; if (ord($c[0]) >= 254 && ord($c[0]) <= 255) // error
} return FALSE;
return $code; return 0;
} }
/** /**
......
<?php
class EasySVGUTF8ToUnicodeTest extends \PHPUnit_Framework_TestCase {
protected $svg;
protected $reflection;
protected function setUp() {
$this->svg = new EasySVG;
$this->reflection = new \ReflectionClass(get_class($this->svg));
}
protected function runUTF8Method($string) {
$method = $this->reflection->getMethod('_utf8ToUnicode');
$method->setAccessible(true);
return $method->invokeArgs($this->svg, array($string));
}
public function testStandardCharacters() {
$result = $this->runUTF8Method('omg lol');
$this->assertEquals(array(
111,109,103,32,108,111,108
), $result);
}
public function testStrangeASCII() {
$result = $this->runUTF8Method('ümlaut eñye');
$this->assertEquals(array(
252,109,108,97,117,116,32,101,241,121,101
), $result);
}
public function testLigatures() {
$result = $this->runUTF8Method('ff fb fj fi fj fk fl ffb ffj ffi ffj ffk ffl fff');
$this->assertEquals(array(
64256,32,
64261,32,
64263,32,
64257,32,
64263,32,
64264,32,
64258,32,
64265,32,
64267,32,
64259,32,
64267,32,
64268,32,
64260,32,
64256,102
), $result);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment