summaryrefslogtreecommitdiff
path: root/vendor/zordius/lightncandy/tests/mustacheSpecTest.php
blob: 158f82cf65e1cf0ac49b1088f53224995294b7a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php

require_once('src/lightncandy.php');

$tmpdir = sys_get_temp_dir();

class MustacheSpecTest extends PHPUnit_Framework_TestCase
{
    /**
     * @dataProvider jsonSpecProvider
     */
    public function testSpecs($spec)
    {
        global $tmpdir;

        $flag = LightnCandy::FLAG_MUSTACHE | LightnCandy::FLAG_ERROR_EXCEPTION | LightnCandy::FLAG_RUNTIMEPARTIAL;

        foreach (Array($flag, $flag | LightnCandy::FLAG_STANDALONE) as $f) {
            $php = LightnCandy::compile($spec['template'], Array(
                'flags' => $f,
                'partials' => isset($spec['partials']) ? $spec['partials'] : null,
                'basedir' => $tmpdir,
            ));
            $renderer = LightnCandy::prepare($php);
            $this->assertEquals($spec['expected'], $renderer($spec['data']), "[{$spec['file']}.{$spec['name']}]#{$spec['no']}:{$spec['desc']} PHP CODE: $php");
        }
    }

    public function jsonSpecProvider()
    {
        $ret = Array();

        foreach (glob('specs/mustache/specs/*.json') as $file) {
            // Skip lambda extension
            if (preg_match('/lambdas\\.json$/', $file)) {
                continue;
            }

            $i=0;
            $json = json_decode(file_get_contents($file), true);
            $ret = array_merge($ret, array_map(function ($d) use ($file, &$i) {
                $d['file'] = $file;
                $d['no'] = ++$i;
                return Array($d);
            }, $json['tests']));
        }

        return $ret;
    }
}


?>