Documentation.php

<?php

namespace Tlf\Lexer\Test;


class DocumentationExamples extends Tester {


    public function testDocLexString(){
        //@export_start(Test.Doc.LexString)
        $lexer = new \Tlf\Lexer();
        $docGrammar = new \Tlf\Lexer\DocblockGrammar();
        $lexer->addGrammar($docGrammar);
        $lexer->addDirective($docGrammar->getDirectives(':/*')['/*']);
        $str = "/** I am docblock */";
        $ast = $lexer->lex($str);

        $tree = $ast->getTree();
        $actual = $tree['docblock'][0];
        $expect = [
            'type'=>'docblock',
            'description'=>'I am docblock',
        ];
        //@export_end(Test.Doc.LexString)

        $this->compare($expect, $actual);
        
        $this->compare($ast->src, $str);
    }

    public function testDocLexAst(){
        //@export_start(Test.Doc.LexAst)
        $lexer = new \Tlf\Lexer();
        $phpGrammar = new \Tlf\Lexer\PhpGrammar();
        $lexer->addGrammar($phpGrammar);

        // set up the ast
        $ast = new \Tlf\Lexer\Ast('code');
        $ast->set ('language', 'php');
        $code = '<?php class Abc extends Alphabet {}';
        $ast->set('src', $code);


        $ast = $lexer->lex($code, $ast);
        $actual = $ast->getTree();
        $expect = [
            'type'=>'code',
            'language'=>'php',
            'src'=>$code,
            'class'=>[
                0=>[
                    'type'=>'class',
                    'docblock'=>'',
                    'namespace'=>'',
                    'name'=>'Abc',
                    'declaration'=>'class Abc extends Alphabet ',
                ],
            ],
        ];
        //@export_end(Test.Doc.LexAst)

        $this->compare($actual, $expect);
        $this->compare($ast->src, $code);
    }

    public function testDocLexFile(){
        $dir = dirname(__DIR__).'/php/';
        $file = $dir.'SampleClass.php';
        $targetTree = include($dir.'SampleClass.tree.php');

        //@export_start(Test.Doc.LexFile)
        $lexer = new \Tlf\Lexer();
        $lexer->useCache = false; // cache is disabled only for testing
        $lexer->addGrammar($phpGrammar = new \Tlf\Lexer\PhpGrammar());

        $ast = $lexer->lexFile(dirname(__DIR__).'/php/SampleClass.php');

        // An array detailing the file 
        $tree = $ast->getTree(); 
        //@export_end(Test.Doc.LexFile)

        $this->compare(
            $targetTree, 
            $tree
        );
    }

}