public function testIntegration() { $syntax = new Syntax(); $syntax->meta = "foo"; $syntax->title = "bar"; $it = $syntax->getIterator(); $this->assertEquals(0, $it->count()); $this->assertFalse($it->offsetExists(0)); $rule1 = new Rule($syntax); $this->assertSame($syntax, $rule1->getParent()); $rule1->name = "first"; $syntax->addChild($rule1); $it = $syntax->getIterator(); $this->assertEquals(1, $it->count()); $this->assertTrue($it->offsetExists(0)); $this->assertSame($rule1, $it->offsetGet(0)); $rule2 = new Rule($syntax); $rule2->name = "second"; $this->assertSame($syntax, $rule1->getParent()); $syntax->addChild($rule2); $it = $syntax->getIterator(); $this->assertEquals(2, $it->count()); $this->assertTrue($it->offsetExists(0)); $this->assertSame($rule1, $it->offsetGet(0)); $this->assertTrue($it->offsetExists(1)); $this->assertSame($rule2, $it->offsetGet(1)); }
/** * @todo use ast builder */ public function testGenerateXml() { $visitor = new Xml(); $this->assertEquals('<?xml version="1.0" encoding="utf-8"?>', $visitor->getXmlString()); $syntax = new Syntax(); $syntax->meta = "EBNF defined in itself."; $syntax->title = "xis/ebnf v2.0 http://wiki.karmin.ch/ebnf/ gpl3"; $visitor = new Xml(); $syntax->accept($visitor); $xml = file_get_contents(EBNF_TESTS_FIXTURS . "/visitor/syntax.xml"); $this->assertEquals('<?xml version="1.0" encoding="utf-8"?>' . "\n" . '<syntax title="xis/ebnf v2.0 http://wiki.karmin.ch/ebnf/ gpl3" meta="EBNF defined in itself."/>', $visitor->getXmlString()); $syntax = new Syntax(); $syntax->title = "EBNF defined in itself."; $syntax->meta = "xis/ebnf v2.0 http://wiki.karmin.ch/ebnf/ gpl3"; $syntaxRule = new Rule($this->getMock('de\\weltraumschaf\\ebnf\\ast\\Node')); $syntaxRule->name = "syntax"; $seq = new Sequence($this->getMock('de\\weltraumschaf\\ebnf\\ast\\Node')); $opt = new Option($this->getMock('de\\weltraumschaf\\ebnf\\ast\\Node')); $ident = new Identifier($this->getMock('de\\weltraumschaf\\ebnf\\ast\\Node')); $ident->value = "title"; $opt->addChild($ident); $seq->addChild($opt); $terminal = new Terminal($this->getMock('de\\weltraumschaf\\ebnf\\ast\\Node')); $terminal->value = "{"; $seq->addChild($terminal); $loop = new Loop($this->getMock('de\\weltraumschaf\\ebnf\\ast\\Node')); $ident = new Identifier($this->getMock('de\\weltraumschaf\\ebnf\\ast\\Node')); $ident->value = "rule"; $loop->addChild($ident); $seq->addChild($loop); $terminal = new Terminal($this->getMock('de\\weltraumschaf\\ebnf\\ast\\Node')); $terminal->value = "}"; $seq->addChild($terminal); $opt = new Option($this->getMock('de\\weltraumschaf\\ebnf\\ast\\Node')); $ident = new Identifier($this->getMock('de\\weltraumschaf\\ebnf\\ast\\Node')); $ident->value = "comment"; $opt->addChild($ident); $seq->addChild($opt); $syntaxRule->addChild($seq); $syntax->addChild($syntaxRule); $ruleRule = new Rule($this->getMock('de\\weltraumschaf\\ebnf\\ast\\Node')); $ruleRule->name = "rule"; $seq = new Sequence($this->getMock('de\\weltraumschaf\\ebnf\\ast\\Node')); $ident = new Identifier($this->getMock('de\\weltraumschaf\\ebnf\\ast\\Node')); $ident->value = "identifier"; $seq->addChild($ident); $choice = new Choice($this->getMock('de\\weltraumschaf\\ebnf\\ast\\Node')); foreach (array("=", ":", ":==") as $literal) { $terminal = new Terminal($this->getMock('de\\weltraumschaf\\ebnf\\ast\\Node')); $terminal->value = $literal; $choice->addChild($terminal); } $seq->addChild($choice); $ident = new Identifier($this->getMock('de\\weltraumschaf\\ebnf\\ast\\Node')); $ident->value = "expression"; $seq->addChild($ident); $choice = new Choice($this->getMock('de\\weltraumschaf\\ebnf\\ast\\Node')); foreach (array(".", ";") as $literal) { $terminal = new Terminal($this->getMock('de\\weltraumschaf\\ebnf\\ast\\Node')); $terminal->value = $literal; $choice->addChild($terminal); } $seq->addChild($choice); $ruleRule->addChild($seq); $syntax->addChild($ruleRule); $literalRule = new Rule($this->getMock('de\\weltraumschaf\\ebnf\\ast\\Node')); $literalRule->name = "literal"; $choice = new Choice($this->getMock('de\\weltraumschaf\\ebnf\\ast\\Node')); $seq = new Sequence($this->getMock('de\\weltraumschaf\\ebnf\\ast\\Node')); $terminal = new Terminal($this->getMock('de\\weltraumschaf\\ebnf\\ast\\Node')); $terminal->value = "'"; $seq->addChild($terminal); $ident = new Identifier($this->getMock('de\\weltraumschaf\\ebnf\\ast\\Node')); $ident->value = "character"; $seq->addChild($ident); $loop = new Loop($this->getMock('de\\weltraumschaf\\ebnf\\ast\\Node')); $ident = new Identifier($this->getMock('de\\weltraumschaf\\ebnf\\ast\\Node')); $ident->value = "character"; $loop->addChild($ident); $seq->addChild($loop); $seq->addChild($terminal); $choice->addChild($seq); $seq = new Sequence($this->getMock('de\\weltraumschaf\\ebnf\\ast\\Node')); $terminal = new Terminal($this->getMock('de\\weltraumschaf\\ebnf\\ast\\Node')); $terminal->value = '"'; $seq->addChild($terminal); $ident = new Identifier($this->getMock('de\\weltraumschaf\\ebnf\\ast\\Node')); $ident->value = "character"; $seq->addChild($ident); $loop = new Loop($this->getMock('de\\weltraumschaf\\ebnf\\ast\\Node')); $ident = new Identifier($this->getMock('de\\weltraumschaf\\ebnf\\ast\\Node')); $ident->value = "character"; $loop->addChild($ident); $seq->addChild($loop); $seq->addChild($terminal); $choice->addChild($seq); $literalRule->addChild($choice); $syntax->addChild($literalRule); $visitor = new Xml(); $syntax->accept($visitor); $xml = file_get_contents(EBNF_TESTS_FIXTURS . "/visitor/syntax.xml"); $this->assertEquals($xml, $visitor->getXmlString()); }
/** * @large * @expectedException de\weltraumschaf\ebnf\visitor\ValidaorException * @expectedExceptionMessage You must specify a syntax at very first! * @expectedExceptionCode 2 */ public function testThrowExcpetionValidateRuleBeforeSyntax() { $rule = new Rule($this->getMock('de\\weltraumschaf\\ebnf\\ast\\Node')); $rule->accept(new Validator()); }