diff core-javascript/tutorial.js @ 109:fe40997c9d46 core-javascript

Add tutorial.js, which contains many asserted tests.
author jf3243
date Wed, 26 Oct 2011 15:11:54 +0100
parents
children f015a6838eb7
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/core-javascript/tutorial.js	Wed Oct 26 15:11:54 2011 +0100
@@ -0,0 +1,123 @@
+var telltail = 'global';
+
+var TEST = function(title, code){
+    print( 'Testing: ' + title );
+    code();  
+};
+
+var assert = function(arg){
+    if(!arg){
+        ddt();
+    }  
+};
+
+'0' == 0 || ddt;
+0 == '' || ddt;
+'0' != '' || ddt;
+NaN != NaN || ddt;
+
+
+
+TEST('return_this', function()
+{
+    var return_this = function(){
+        return this;
+    };
+    
+    var default_this = return_this();
+    
+    assert( default_this.telltail === 'global' );
+    
+    var obj = {};
+    obj.return_this = return_this;
+    assert( obj.return_this === return_this );
+    
+    assert( obj.return_this() === obj );
+    
+    assert( return_this.call(obj) === obj );
+    assert( return_this.apply(obj) === obj );
+
+});
+
+
+TEST('millers_device', function()
+{
+    var array = [];
+    var obj = {};
+    var fn = function(){};
+
+    assert( typeof array === 'object' );
+    assert( '' + obj === '[object Object]' );
+    
+    assert( obj.toString() === '[object Object]' );
+
+    var object_toString = obj.toString;
+
+    assert( object_toString.call(array) === '[object Array]' );
+    assert( object_toString.call(fn) === '[object Function]' );
+
+});
+
+
+TEST('return_arguments', function()
+{
+    var return_arguments = function(){
+        return arguments;
+    };
+    
+    var my_arguments = return_arguments(0, 1);
+    
+    assert( my_arguments.length === 2 );
+    
+    assert( my_arguments[0]  === 0 );
+    assert( my_arguments[1]  === 1 );
+    
+    assert( '' + my_arguments === '[object Object]' );
+    assert( '' + [0, 1] === '0,1' );
+    
+    var array_slice = [].slice;
+    var my_arguments_fixed = array_slice.call( my_arguments );
+
+    assert( '' + my_arguments_fixed === '0,1');
+
+    var obj = {};
+    assert( obj.toString.call( my_arguments_fixed ) === '[object Array]' );
+
+});
+
+
+TEST('new', function()
+{
+    var this_in_Fn;
+
+    var Fn = function(){
+        this_in_Fn = this;
+    };
+
+    this_in_Fn = null;
+    var non_instance = Fn();
+    assert( non_instance === undefined );
+    assert( this_in_Fn.telltail === 'global');
+
+    this_in_Fn = null;
+    var instance = new Fn();
+    assert( instance === this_in_Fn );
+    assert( instance.telltail === undefined );
+
+    Fn.prototype.telltail = 'instance-of-Fn';
+    assert( instance.telltail === 'instance-of-Fn' );
+
+    Fn.prototype = {
+        telltail : 'prototype-is-changed'
+    };
+    var second_instance = new Fn();
+    assert( second_instance.telltail === 'prototype-is-changed' );
+
+    Fn.prototype = second_instance;
+    second_instance.i = 42;
+
+    var third_instance = new Fn();
+    assert( third_instance.telltail === 'prototype-is-changed' );
+    assert( third_instance.i === 42 );
+
+});