425 lines
12 KiB
PHP
425 lines
12 KiB
PHP
<?php
|
|
/**
|
|
* Created by PhpStorm.
|
|
* User: richard
|
|
* Date: 11/30/17
|
|
* Time: 8:58 AM
|
|
*/
|
|
|
|
namespace Tests\Unit\AbcParser\Domain\Modules\Interpreter;
|
|
|
|
use Enzyme\Axiom\Atoms\StringAtom;
|
|
use Enzyme\Collection\CollectionException;
|
|
use XaiCorp\AbcParser\Domain\Atoms\EmailAtom;
|
|
use XaiCorp\AbcParser\Domain\Core\Author;
|
|
use XaiCorp\AbcParser\Domain\Core\Composer;
|
|
use XaiCorp\AbcParser\Domain\Core\Tune;
|
|
use XaiCorp\AbcParser\Domain\Modules\Interpreter\Builder;
|
|
use XaiCorp\AbcParser\Domain\Modules\Interpreter\Context;
|
|
use XaiCorp\AbcParser\Domain\Modules\Interpreter\DefaultInterpreter;
|
|
use XaiCorp\AbcParser\Domain\Modules\Interpreter\Interpreter;
|
|
|
|
class DefaultInterpreterTest extends \PHPUnit_Framework_TestCase
|
|
{
|
|
/**
|
|
* @var Interpreter
|
|
*/
|
|
protected $interpreter;
|
|
|
|
protected function setUp()
|
|
{
|
|
$builder = new Builder();
|
|
$this->interpreter = new DefaultInterpreter($builder);
|
|
}
|
|
|
|
/**
|
|
* @param string $filename
|
|
* @return bool|string
|
|
*/
|
|
private function getAbc($filename = '/abc/valid_abc_1.abc')
|
|
{
|
|
return file_get_contents(codecept_data_dir($filename));
|
|
}
|
|
|
|
/**
|
|
* @param string $string
|
|
* @return StringAtom
|
|
*/
|
|
private function createStringAtom($string = '')
|
|
{
|
|
try {
|
|
return new StringAtom($string);
|
|
} catch (\Exception $e) {
|
|
//
|
|
}
|
|
}
|
|
|
|
private function createAuthor($name, $email = '')
|
|
{
|
|
return new Author(
|
|
$this->createStringAtom($name),
|
|
new EmailAtom($email)
|
|
);
|
|
}
|
|
|
|
private function createComposer($name, $email = '')
|
|
{
|
|
return new Composer(
|
|
$this->createStringAtom($name),
|
|
new EmailAtom($email)
|
|
);
|
|
}
|
|
|
|
public function testCreate()
|
|
{
|
|
$builder = new Builder();
|
|
$interpreter = new DefaultInterpreter($builder);
|
|
|
|
$this->assertInstanceOf(Interpreter::class, $interpreter);
|
|
}
|
|
|
|
public function testExecuteReturnsInterpreter()
|
|
{
|
|
$abc = $this->createStringAtom($this->getAbc());
|
|
$context = new Context($abc);
|
|
|
|
$result = $this->interpreter->execute($context);
|
|
|
|
$this->assertInstanceOf(Interpreter::class, $result);
|
|
}
|
|
|
|
public function testExecuteParsesTitles()
|
|
{
|
|
$expectedTitle = ['Abbotts Bromley Horn Dance'];
|
|
$abc = $this->createStringAtom($this->getAbc('/abc/valid_abc_2.abc'));
|
|
$context = new Context($abc);
|
|
|
|
$tune = $this->interpreter
|
|
->execute($context)
|
|
->getTunes()
|
|
->first();
|
|
$result = $tune->getTitles();
|
|
|
|
$this->assertEquals($expectedTitle, $result);
|
|
}
|
|
|
|
public function testExecuteParsesAuthor()
|
|
{
|
|
$expected = 'trad';
|
|
$abc = $this->createStringAtom($this->getAbc('/abc/valid_abc_2.abc'));
|
|
$context = new Context($abc);
|
|
|
|
$tune = $this->interpreter
|
|
->execute($context)
|
|
->getTunes()
|
|
->first();
|
|
$result = $tune->getAuthors()[0]->get('name');
|
|
|
|
$this->assertEquals($expected, $result->getValue());
|
|
}
|
|
|
|
public function testExecuteParsesComposer()
|
|
{
|
|
$expected = [$this->createComposer('trad.')];
|
|
$abc = $this->createStringAtom($this->getAbc('/abc/valid_abc_2.abc'));
|
|
$context = new Context($abc);
|
|
|
|
$tune = $this->interpreter
|
|
->execute($context)
|
|
->getTunes()
|
|
->first();
|
|
$result = $tune->getComposers()[0]->get('name');
|
|
|
|
$this->assertEquals($expected[0]->get('name'), $result);
|
|
}
|
|
|
|
public function testExecuteParsesTranscriber()
|
|
{
|
|
$expected = [$this->createComposer('Andy Hornby')];
|
|
$abc = $this->createStringAtom($this->getAbc('/abc/valid_abc_2.abc'));
|
|
$context = new Context($abc);
|
|
|
|
$tune = $this->interpreter
|
|
->execute($context)
|
|
->getTunes()
|
|
->first();
|
|
$setting = $tune->getSettings()[0];
|
|
$result = $setting->getTranscribers()[0]->get('name');
|
|
|
|
$this->assertEquals($expected[0]->get('name'), $result);
|
|
}
|
|
|
|
public function testExecuteParsesDiscography()
|
|
{
|
|
$expected = 'None';
|
|
$abc = $this->createStringAtom($this->getAbc('/abc/valid_abc_2.abc'));
|
|
$context = new Context($abc);
|
|
|
|
$tune = $this->interpreter
|
|
->execute($context)
|
|
->getTunes()
|
|
->first();
|
|
$setting = $tune->getSettings()[0];
|
|
$result = $setting->getDiscography()[0];
|
|
|
|
$this->assertEquals($expected, $result);
|
|
}
|
|
|
|
public function testExecuteParsesGroups()
|
|
{
|
|
$expected = 'lute';
|
|
$abc = $this->createStringAtom($this->getAbc('/abc/valid_abc_2.abc'));
|
|
$context = new Context($abc);
|
|
|
|
$tune = $this->interpreter
|
|
->execute($context)
|
|
->getTunes()
|
|
->first();
|
|
$result = $tune->getGroups()[0];
|
|
|
|
$this->assertEquals($expected, $result);
|
|
}
|
|
|
|
public function testExecuteParsesKey()
|
|
{
|
|
$expected = 'G';
|
|
$abc = $this->createStringAtom($this->getAbc('/abc/valid_abc_2.abc'));
|
|
$context = new Context($abc);
|
|
|
|
$tune = $this->interpreter
|
|
->execute($context)
|
|
->getTunes()
|
|
->first();
|
|
$setting = $tune->getSettings()[0];
|
|
$result = $setting->getKey();
|
|
|
|
$this->assertEquals($expected, $result);
|
|
}
|
|
|
|
public function testExecuteParsesMusic()
|
|
{
|
|
$expected = implode('\n', [
|
|
'e|B2e G2e|B2e E2G|FGA GAB|1AGF G2:|2AGF E2|',
|
|
'|e|c2e cde|A2c ABc|FGA GFE|_E=EF B,2g|',
|
|
'e2g efg|c2e cde|dcB AGF|E3E2',
|
|
'|:A|BcB e3|BcB f3|BcB AGF|1G2F G2:|2E3-E2|]',
|
|
]);
|
|
$abc = $this->createStringAtom($this->getAbc('/abc/valid_abc_2.abc'));
|
|
$context = new Context($abc);
|
|
|
|
$tune = $this->interpreter
|
|
->execute($context)
|
|
->getTunes()
|
|
->first();
|
|
$setting = $tune->getSettings()[0];
|
|
$result = $setting->getMusic();
|
|
|
|
$this->assertEquals($expected, $result);
|
|
}
|
|
|
|
public function testExecuteParsesMeter()
|
|
{
|
|
$expected = '6/8';
|
|
$abc = $this->createStringAtom($this->getAbc('/abc/valid_abc_2.abc'));
|
|
$context = new Context($abc);
|
|
|
|
$tune = $this->interpreter
|
|
->execute($context)
|
|
->getTunes()
|
|
->first();
|
|
$setting = $tune->getSettings()[0];
|
|
$result = $setting->getMeter();
|
|
|
|
$this->assertEquals($expected, $result);
|
|
}
|
|
|
|
public function testExecuteParsesParts()
|
|
{
|
|
$expected = 'ABC';
|
|
$abc = $this->createStringAtom($this->getAbc('/abc/valid_abc_2.abc'));
|
|
$context = new Context($abc);
|
|
|
|
$tune = $this->interpreter
|
|
->execute($context)
|
|
->getTunes()
|
|
->first();
|
|
$setting = $tune->getSettings()[0];
|
|
$result = $setting->getPart();
|
|
|
|
$this->assertEquals($expected, $result);
|
|
}
|
|
|
|
public function testExecuteParsesSources()
|
|
{
|
|
$expected = 'http://www.leeds.ac.uk/music/Info/RRTuneBk/gettune/00000dab.html';
|
|
$abc = $this->createStringAtom($this->getAbc('/abc/valid_abc_2.abc'));
|
|
$context = new Context($abc);
|
|
|
|
$tune = $this->interpreter
|
|
->execute($context)
|
|
->getTunes()
|
|
->first();
|
|
$setting = $tune->getSettings()[0];
|
|
$result = $setting->getSources()[0];
|
|
|
|
$this->assertEquals($expected, $result);
|
|
}
|
|
|
|
public function testExecuteParsesHistory()
|
|
{
|
|
$expected = [
|
|
'Thousand year old tradition',
|
|
'The Horns live in the church and are removed for the dance once a year',
|
|
];
|
|
$abc = $this->createStringAtom($this->getAbc('/abc/valid_abc_2.abc'));
|
|
$context = new Context($abc);
|
|
|
|
$tune = $this->interpreter
|
|
->execute($context)
|
|
->getTunes()
|
|
->first();
|
|
$result = $tune->getHistory();
|
|
|
|
$this->assertEquals($expected, $result);
|
|
}
|
|
|
|
public function testExecuteParsesRhythms()
|
|
{
|
|
$expected = 'Jig';
|
|
$abc = $this->createStringAtom($this->getAbc('/abc/valid_abc_2.abc'));
|
|
$context = new Context($abc);
|
|
|
|
$tune = $this->interpreter
|
|
->execute($context)
|
|
->getTunes()
|
|
->first();
|
|
$result = $tune->getRhythms()[0];
|
|
|
|
$this->assertEquals($expected, $result);
|
|
}
|
|
|
|
public function testExecuteParsesFilename()
|
|
{
|
|
$expected = 'none.abc';
|
|
$abc = $this->createStringAtom($this->getAbc('/abc/valid_abc_2.abc'));
|
|
$context = new Context($abc);
|
|
|
|
$tune = $this->interpreter
|
|
->execute($context)
|
|
->getTunes()
|
|
->first();
|
|
$setting = $tune->getSettings()[0];
|
|
$result = $setting->getFilenames()[0];
|
|
|
|
$this->assertEquals($expected, $result);
|
|
}
|
|
|
|
public function testExecuteParsesBook()
|
|
{
|
|
$expected = 'Traditional English tunes';
|
|
$abc = $this->createStringAtom($this->getAbc('/abc/valid_abc_2.abc'));
|
|
$context = new Context($abc);
|
|
|
|
$tune = $this->interpreter
|
|
->execute($context)
|
|
->getTunes()
|
|
->first();
|
|
$setting = $tune->getSettings()[0];
|
|
$result = $setting->getBooks()[0];
|
|
|
|
$this->assertEquals($expected, $result);
|
|
}
|
|
|
|
public function testExecuteParsesWords()
|
|
{
|
|
$expected = 'None';
|
|
$abc = $this->createStringAtom($this->getAbc('/abc/valid_abc_2.abc'));
|
|
$context = new Context($abc);
|
|
|
|
$tune = $this->interpreter
|
|
->execute($context)
|
|
->getTunes()
|
|
->first();
|
|
$setting = $tune->getSettings()[0];
|
|
$result = $setting->getWords()[0];
|
|
|
|
$this->assertEquals($expected, $result);
|
|
}
|
|
|
|
public function testExecuteParsesTempo()
|
|
{
|
|
$expected = '1/8=80';
|
|
$abc = $this->createStringAtom($this->getAbc('/abc/valid_abc_2.abc'));
|
|
$context = new Context($abc);
|
|
|
|
$tune = $this->interpreter
|
|
->execute($context)
|
|
->getTunes()
|
|
->first();
|
|
$setting = $tune->getSettings()[0];
|
|
$result = $setting->getTempo();
|
|
|
|
$this->assertEquals($expected, $result);
|
|
}
|
|
|
|
public function testExecuteParsesNoteLength()
|
|
{
|
|
$expected = '1/8';
|
|
$abc = $this->createStringAtom($this->getAbc('/abc/valid_abc_2.abc'));
|
|
$context = new Context($abc);
|
|
|
|
$tune = $this->interpreter
|
|
->execute($context)
|
|
->getTunes()
|
|
->first();
|
|
$setting = $tune->getSettings()[0];
|
|
$result = $setting->getNoteLength();
|
|
|
|
$this->assertEquals($expected, $result);
|
|
}
|
|
|
|
public function testExecuteParsesNotes()
|
|
{
|
|
$expected = 'Traditional English';
|
|
$abc = $this->createStringAtom($this->getAbc('/abc/valid_abc_2.abc'));
|
|
$context = new Context($abc);
|
|
|
|
$tune = $this->interpreter
|
|
->execute($context)
|
|
->getTunes()
|
|
->first();
|
|
$setting = $tune->getSettings()[0];
|
|
$result = $setting->getNotes()[0];
|
|
|
|
$this->assertEquals($expected, $result);
|
|
}
|
|
|
|
public function testExecuteParsesOrigin()
|
|
{
|
|
$expected = 'England';
|
|
$abc = $this->createStringAtom($this->getAbc('/abc/valid_abc_2.abc'));
|
|
$context = new Context($abc);
|
|
|
|
$tune = $this->interpreter
|
|
->execute($context)
|
|
->getTunes()
|
|
->first();
|
|
$result = $tune->getOrigins()[0];
|
|
|
|
$this->assertEquals($expected, $result);
|
|
}
|
|
|
|
public function testMultipleTunesInAFile()
|
|
{
|
|
$abc = $this->createStringAtom($this->getAbc('/abc/jigs.abc'));
|
|
|
|
$context = new Context($abc);
|
|
|
|
$result = $this->interpreter
|
|
->execute($context)
|
|
->getTunes();
|
|
|
|
$this->assertCount(52, $result);
|
|
}
|
|
}
|