/** * @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()); }
public function testValidateRule() { $syntax = new Syntax(); $syntax->meta = "foo meta"; $syntax->title = "bar title"; $ruleFoo = new Rule($this->getMock('de\\weltraumschaf\\ebnf\\ast\\Node')); $ruleFoo->name = "foo"; $syntax->addChild($ruleFoo); $ruleBar = new Rule($this->getMock('de\\weltraumschaf\\ebnf\\ast\\Node')); $ruleBar->name = "bar"; $syntax->addChild($ruleBar); $tester = new Validator(); $syntax->accept($tester); $this->assertEquals(array("syntax" => array("meta" => "foo meta", "title" => "bar title", "rule" => array("foo" => array(), "bar" => array()))), $tester->getRepresentative()); }