Skip to content

Commit

Permalink
Add a file based example and tweak excel example to be reuseable
Browse files Browse the repository at this point in the history
  • Loading branch information
mallardduck committed Dec 15, 2020
1 parent 3588c33 commit 1b32450
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 53 deletions.
57 changes: 57 additions & 0 deletions tests/Examples/ExcelClasses.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace Tests\Verraes\Parsica\Examples;

class Cell
{
private $col;
private $row;

function __construct($col, $row)
{
$this->col = $col;
$this->row = $row;
}
}
class Range
{
private Cell $from;
private Cell $to;

function __construct(Cell $from, Cell $to)
{
$this->from = $from;
$this->to = $to;
}
}
class Intersection
{
private Range $l;
private Range $r;

function __construct(Range $l, Range $r)
{
$this->l = $l;
$this->r = $r;
}
}
class Sum
{
private Intersection $intersection;

function __construct(Intersection $intersection)
{
$this->intersection = $intersection;
}
}
class Ampersand
{
private Cell $l;
private Cell $r;

function __construct(Cell $l, Cell $r)
{
$this->l = $l;
$this->r = $r;
}
}
54 changes: 1 addition & 53 deletions tests/Examples/ExcelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,56 +76,4 @@ private function excelParser(): Parser

}

class Cell
{
private $col;
private $row;

function __construct($col, $row)
{
$this->col = $col;
$this->row = $row;
}
}
class Range
{
private Cell $from;
private Cell $to;

function __construct(Cell $from, Cell $to)
{
$this->from = $from;
$this->to = $to;
}
}
class Intersection
{
private Range $l;
private Range $r;

function __construct(Range $l, Range $r)
{
$this->l = $l;
$this->r = $r;
}
}
class Sum
{
private Intersection $intersection;

function __construct(Intersection $intersection)
{
$this->intersection = $intersection;
}
}
class Ampersand
{
private Cell $l;
private Cell $r;

function __construct(Cell $l, Cell $r)
{
$this->l = $l;
$this->r = $r;
}
}
require_once __DIR__ . '/ExcelClasses.php';
86 changes: 86 additions & 0 deletions tests/Examples/ExcelTextFileStreamTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php declare(strict_types=1);
/*
* This file is part of the Parsica library.
*
* Copyright (c) 2020 Mathias Verraes <mathias@verraes.net>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Tests\Verraes\Parsica\Examples;

use PHPUnit\Framework\TestCase;
use Verraes\Parsica\Parser;
use Verraes\Parsica\PHPUnit\ParserAssertions;
use Verraes\Parsica\TextFileStream;
use function Verraes\Parsica\{alphaChar, between, char, collect, digitChar, skipHSpace1, space, string};

final class ExcelTextFileStreamTest extends TestCase
{
use ParserAssertions;

private string $stubBasePath;

public function setUp(): void
{
parent::setUp(); // TODO: Change the autogenerated stub
$this->stubBasePath = dirname(__DIR__) . "/stubs/";
}

/** @test */
public function spaceOrOperatorDependingOnContext()
{
// https://twitter.com/Mark_Baker/status/1309919606887374849?s=20
// and https://twitter.com/Mark_Baker/status/1309960902482026498?s=20
// `=SUM(B7:D7 C6:C8)` where space is the intersection operator for the
// intersection between the two ranges B7:D7 and C6:C8 (ie. C7),
// and `=A1 & B1` where the space is simply whitespace and should be ignored

$parser = $this->excelParser();

$input = new TextFileStream($this->stubBasePath . 'example-excel-1.txt');
$expected = new Sum(
new Intersection(
new Range(new Cell("B", "7"), new Cell("D", "7")),
new Range(new Cell("C", "6"), new Cell("C", "8")),
)
);
$this->assertParsesStream($input, $parser, $expected);

$input = new TextFileStream($this->stubBasePath . 'example-excel-2.txt');
$expected = new Ampersand(
new Cell("A", "1"),
new Cell("B", "1"),
);
$this->assertParsesStream($input, $parser, $expected);
}

private function excelParser(): Parser
{
$parens = fn(Parser $p): Parser => between(char('('), char(')'), $p);
$cell = collect(alphaChar(), digitChar())
->map(fn($o) => new Cell($o[0], $o[1]));
$range = collect($cell, char(':'), $cell)
->map(fn($o) => new Range($o[0], $o[2]));
$intersection = collect($range, space(), $range)
->map(fn($o) => new Intersection($o[0], $o[2]));
$sum = (string('=SUM')->followedBy($parens($intersection)))
->map(fn($o) => new Sum($o));


// consumes space before and after Parser $p
$token = fn(Parser $p): Parser => between(skipHSpace1(), skipHSpace1(), $p);
$ampersand = char('=')->followedBy(collect(
$cell,
$token(char('&')),
$cell
))->map(fn($o) => new Ampersand($o[0], $o[2]));


return $sum->or($ampersand);
}

}

require_once __DIR__ . '/ExcelClasses.php';
1 change: 1 addition & 0 deletions tests/stubs/example-excel-1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
=SUM(B7:D7 C6:C8)
1 change: 1 addition & 0 deletions tests/stubs/example-excel-2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
=A1 & B1

0 comments on commit 1b32450

Please sign in to comment.