#!perl -T BEGIN { require './t/test.pl' } use Test::More tests => 58; use strict; use utf8; # Test 1: See if the module loads BEGIN { use_ok('JE') }; my $j = new JE; # Tests 2-3: Bind the ok and diag functions isa_ok( $j->new_function( ok => \&ok ), 'JE::Object::Function' ); isa_ok( $j->new_function( diag => \&diag ), 'JE::Object::Function' ); # Run JS tests defined $j->eval( <<'--end--' ) or die; // --------------------------------------------------- /* Tests 4-6: Verify that accessing unimplemented internal properties caus- es TypeErrors */ var is_TypeError; try { new 'Object'() } catch (e) { e instanceof TypeError && (is_TypeError = true) } ok(is_TypeError, '"string".[[Construct]] throws TypeError') //diag("Did I fail this test?: " + is_TypeError); is_TypeError = false try { 'function'() } catch (e) { e instanceof TypeError && (is_TypeError = true) } ok(is_TypeError, '"string".[[Call]] throws TypeError') is_TypeError = false try { function(){} instanceof 'Function' } catch (e) { e instanceof TypeError && (is_TypeError = true) } ok(is_TypeError, 'x instanceof "string" throws TypeError') // --------------------------------------------------- /* Tests 7-11: [[Get]] */ ok(String.length !== undefined, '[[Get]] when object has its own property') ok((f = {}.toString) !== undefined && f === Object.prototype.toString, '[[Get]] when object inherits from its prototype') ok((f = {}.oString) === undefined, '[[Get]] when neither the object nor its prototype has the property') ok( Object.prototype.et === undefined, '[[Get]] when object has neither the named property nor a prototype') Object.prototype.gizmo = 'ansthstyyyyyyyyyyyyyyyy'; ok( new String().gizmo === 'ansthstyyyyyyyyyyyyyyyy', '[[Get]] when object inherits from its prototype\'s prototype') // --------------------------------------------------- /* Tests 12-17: [[Put]] */ Object.prototype = new Function; ok(typeof Object.prototype == 'object', '[[Put]] when property is readonly') Infinity = 7 // :-) var found_inf for(var p in this) if (p == 'Infinity') { found_inf = true; break } ok(Infinity === 7 && !found_inf /* verifies that the dontenum attr is unchanged */, '[[Put]] when property exists and is not readonly') function Constructor(){} Constructor.prototype = Object // This is how I get read-only properties thing = new Constructor // on to the prototype chain. thing.prototype = 7 ok(thing.prototype !== 7, '[[Put]] can\'t obscure read-only properties of prototypes') Object.prototype.fibungle = 'antidisestablishmentarianism'; (thing = {}) .fibungle = 'floccipaucinihilopilification' ok({}.fibungle == 'antidisestablishmentarianism' && thing.fibungle == 'floccipaucinihilopilification', '[[Put]] creates a new property and leaves the prototype\'s one alone'); (thing = {}).codswallop = 'pneumonoultramicroscopicsilicovolcanoconiosis' ok(!('codswallop' in Object.prototype) && thing.codswallop === 'pneumonoultramicroscopicsilicovolcanoconiosis', '[[Put]] creates a new property and leaves the prototype alone') Object.prototype["Bob's my uncle!"] = 'something' ok(Object.prototype["Bob's my uncle!"] === 'something', '[[Put]], when object has no prototype') // --------------------------------------------------- /* Tests 18-21: [[HasProperty]] */ ok('String' in this, '[[HasProperty]] when the object has an uninherited property') ok('toString' in {}, '[[HasProperty]] when the object has an inherited property') ok(!('$@C@YH