示例#1
0
文件: tests.php 项目: mk-pmb/js2php
    Test::assert('date json string', $date->callMethod('toJSON') === '2013-08-06T02:11:08.411Z');
    $date = new Date(1375751468412.0);
    Test::assert('init from value', $date->callMethod('toJSON') === '2013-08-06T01:11:08.412Z');
    $date = new Date('2013-08-06T01:11:08.412Z');
    Test::assert('init from string', $date->callMethod('toJSON') === '2013-08-06T01:11:08.412Z');
    //test Date.UTC
    $ms = $Date->callMethod('UTC', 2013, 7, 5, 18, 11, 8, 411);
    $date = $Date->construct($ms);
    Test::assert('date from UTC', $date->callMethod('toJSON') === '2013-08-05T18:11:08.411Z');
    Test::assert('date from UTC toString', $date->callMethod('toString') === 'Mon Aug 05 2013 10:11:08 GMT-0800 (PST)');
});
Test::suite('RegExp', function () use($RegExp, $Object, $Array) {
    $str = 'xabcdef';
    $reg = new RegExp('a(b|c)', 'i');
    Test::assert('reg source', $reg->get('source') === 'a(b|c)');
    Test::assert('reg toString', $reg->callMethod('toString') === '/a(b|c)/i');
    $match = $reg->callMethod('exec', $str);
    Test::assert('match result', $match->get(0) === 'ab');
    Test::assert('match length', $match->get('length') === 2.0);
    Test::assert('match index', $match->get('index') === 1.0);
    Test::assert('match input', $match->get('input') === $str);
    Test::assert('match lastIndex', $reg->get('lastIndex') === 3.0);
});
Test::suite('String object', function () use($String, $Object) {
    $str = $String->construct('hi');
    Test::assert('instanceof', $str instanceof Str);
    Test::assert('type is object', _typeof($str) === 'object');
    Test::assert('has value', $str->value === 'hi');
    Test::assert('has value', $str->callMethod('toString') === 'hi');
    Test::assert('has value', $str->callMethod('charAt', 0) === 'h');
    $str = $String->call(null, 'hi');