<?php
ini_set("include_path", "../_tools/PHPJS_Library/PHPJS".PATH_SEPARATOR."../../../../_tools/PHPJS_Library/PHPJS".PATH_SEPARATOR.ini_get("include_path"));
require_once 'PHPUnit/Framework.php';

error_reporting(E_ALL);
if (!function_exists('pr')) {
    function pr($arr) {
        if (is_array($arr) && count($arr)) {
            print_r($arr);
        } else {
            var_dump($arr);
        }
        echo "\n";
    }
}
if (!function_exists('prd')) {
    function prd($arr) {
        pr($arr);
        die();
    }
}

require_once 'Library.php';

/**
 * Test class for PHPJS_Library.
 * Generated by PHPUnit on 2009-03-16 at 18:19:57.
 */
class PHPJS_LibraryCompilerTest extends PHPUnit_Framework_TestCase
{
    /**
     * @var    PHPJS_Library
     * @access protected
     */
    protected $PHPJS;

    /**
     * Sets up the fixture, for example, opens a network connection.
     * This method is called before a test is executed.
     *
     * @access protected
     */
    protected function setUp()
    {
        $p = dirname(dirname(dirname(dirname(dirname(__FILE__))))).'/functions';
        $this->PHPJS = new PHPJS_Library_Compiler($p);
    }

    /**
     * Tears down the fixture, for example, closes a network connection.
     * This method is called after a test is executed.
     *
     * @access protected
     */
    protected function tearDown()
    {
    }

    public function testCompile() {
        $compression = 'none';
        $namespaced  = 'yes';
        $selection   = array(
            '_phpjs_shared_bc' => true,
            'array_shift' => true,
            'bcadd' => true,
        );
        
        $options = array(
            'pref_title' => 'test.php',
            'compression' => $compression,
            'namespaced' => $namespaced
        );

        #$selection = array_flip(array_keys($this->PHPJS->Functions));
        
        // Set selection
        $this->PHPJS->clearSelection();
        foreach ($selection as $functionName =>$bool) {
            $this->PHPJS->addToSelection('function::'.$functionName);
        }

        #pr($this->PHPJS->getSelection());

        // Set flags
        $flags = 0;
        if ($options['namespaced'] == 'yes') {
            $flags = $flags | PHPJS_Library_Compiler::COMPILE_NAMESPACED;
        } elseif ($options['namespaced'] == 'commonjs') {
            $flags = $flags | PHPJS_Library_Compiler::COMPILE_COMMONJS;
        }
        if ($options['compression'] == 'minified') {
            $flags = $flags | PHPJS_Library_Compiler::COMPILE_MINFIED;
        }
        if ($options['compression'] == 'packed') {
            $flags = $flags | PHPJS_Library_Compiler::COMPILE_PACKED;
        }
        
        $code = $this->PHPJS->compile($flags, 't'.date("H:i:s"));
        $tmp  = tempnam('/tmp', 'phpjstest').'.js';
        echo "You could run: \n  rhino -debug ".$tmp."\n";
        file_put_contents($tmp, $code);

        
    }

    /**
     * @todo Implement testGetDirRealTemp().
     */
    public function testIsFlagEnabled() {

        $flags = 0;
        $flags = $flags | PHPJS_Library_Compiler::COMPILE_NAMESPACED;
        $flags = $flags | PHPJS_Library_Compiler::COMPILE_MINFIED;
        $flags = $flags | PHPJS_Library_Compiler::COMPILE_PACKED;
        $flags = $flags | PHPJS_Library_Compiler::COMPILE_COMMONJS;
        $this->assertTrue($this->PHPJS->isFlagEnabled($flags, PHPJS_Library_Compiler::COMPILE_NAMESPACED));
        $this->assertTrue($this->PHPJS->isFlagEnabled($flags, PHPJS_Library_Compiler::COMPILE_MINFIED));
        $this->assertTrue($this->PHPJS->isFlagEnabled($flags, PHPJS_Library_Compiler::COMPILE_PACKED));
        $this->assertTrue($this->PHPJS->isFlagEnabled($flags, PHPJS_Library_Compiler::COMPILE_COMMONJS));

        $flags = 0;
        $flags = $flags | PHPJS_Library_Compiler::COMPILE_NAMESPACED;
        $flags = $flags | PHPJS_Library_Compiler::COMPILE_MINFIED;
        $this->assertTrue($this->PHPJS->isFlagEnabled($flags, PHPJS_Library_Compiler::COMPILE_NAMESPACED));
        $this->assertTrue($this->PHPJS->isFlagEnabled($flags, PHPJS_Library_Compiler::COMPILE_MINFIED));
        $this->assertFalse($this->PHPJS->isFlagEnabled($flags, PHPJS_Library_Compiler::COMPILE_PACKED));

        $flags = 0;
        $flags = $flags | PHPJS_Library_Compiler::COMPILE_NAMESPACED;
        $this->assertTrue($this->PHPJS->isFlagEnabled($flags, PHPJS_Library_Compiler::COMPILE_NAMESPACED));
        $this->assertFalse($this->PHPJS->isFlagEnabled($flags, PHPJS_Library_Compiler::COMPILE_MINFIED));
        $this->assertFalse($this->PHPJS->isFlagEnabled($flags, PHPJS_Library_Compiler::COMPILE_PACKED));

        $flags = 0;
        $this->assertFalse($this->PHPJS->isFlagEnabled($flags, PHPJS_Library_Compiler::COMPILE_NAMESPACED));
        $this->assertFalse($this->PHPJS->isFlagEnabled($flags, PHPJS_Library_Compiler::COMPILE_MINFIED));
        $this->assertFalse($this->PHPJS->isFlagEnabled($flags, PHPJS_Library_Compiler::COMPILE_PACKED));

    }

}