~earboxer/bare-mess-php

cee6e38a2cf914f937a492f2d8038bda56a4fd76 — Zach DeCook 4 years ago d4ac394
* Primitives: Construct and test String type
2 files changed, 27 insertions(+), 0 deletions(-)

M src/Bare.php
M tests/PrimitivesTest.php
M src/Bare.php => src/Bare.php +12 -0
@@ 4,6 4,18 @@ namespace BareMess;

class Bare
{
    public static function construct(& $mess, $startingValue)
    {
        switch(gettype($startingValue)) {
            case 'string': // string
                // TODO: Use uint for string's length.
                $length = (new U8($mess))->get();
                $val = substr($mess, 0, $length);
                $mess = substr($mess, $length);
                return $val;
        };
    }

    public static function mess($value): string
    {
        switch(gettype($value)) {

M tests/PrimitivesTest.php => tests/PrimitivesTest.php +15 -0
@@ 2,6 2,7 @@

namespace BareMess\Tests;

use BareMess\Bare;
use BareMess\Example\PublicKey;
use BareMess\{I8,I16,I32,I64};
use BareMess\{U8,U16,U32,U64};


@@ 57,6 58,20 @@ class PrimitivesTest extends TestCase
        $this->assertEquals("\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF", $i64->mess());
    }

    public function testString()
    {
        $mess = chr(5) . "hello" . chr(7) . " world!";
        $string1 = Bare::construct($mess, '');
        $string2 = Bare::construct($mess, '');
        $this->assertEquals("", $mess);
        $this->assertEquals("hello", $string1);
        $this->assertEquals(" world!", $string2);
        $mess = Bare::mess($string1, '');
        $mess .= Bare::mess($string2, '');
        $this->assertEquals(chr(5) . "hello" . chr(7) . " world!", $mess);
        // TODO: Test strings longer than 127 characters
    }

    public function testUs()
    {
        $u8 = new U8();