/lib/Annotation/test/test.js
JavaScript | 113 lines | 103 code | 8 blank | 2 comment | 0 complexity | ae18434d75c1d2ed34c32f64d5097bfd MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.0, Apache-2.0, MIT
- // test for lib/Annotation/main
- // http://localhost/DojoPlay2012/libcommon/DTK/util/doh/runner.html?testModule=lib.Annotation.test.test&paths=lib,../../../lib
-
- define( ["lib/Annotation/main", "doh/runner", "dojo/_base/declare"], function( Annotate, doh, declare )
- {
- var a = { x:"X" };
- doh.register( "lib/Annotation/main",
- [
- function test_get()
- {
- function f(){}
- var fa = Annotate(a,f);
-
- doh.assertEqual( 'X', Annotate.get(fa,'x') );
- }
- ,function AnnotationValueCloned()
- {
- var a = { x:"X" };
- function f0(){}
- var fa0 = Annotate(a,f0);
- a.x="XX"
- function f1(){}
- var fa1 = Annotate(a,f1);
- doh.assertEqual( 'X' , Annotate.get(fa0,'x') );
- doh.assertEqual( 'XX', Annotate.get(fa1,'x') );
- }
- ,function Override()
- {
- var a = { x:"X" };
- function f(){}
- var fa0 = Annotate(a,f);
- a.x="XX"
- var fa1 = Annotate(a,f);
- doh.assertEqual( 'XX', Annotate.get(f,'x') ); //
- doh.assertEqual( 'XX', Annotate.get(fa0,'x') );
- doh.assertEqual( 'XX', Annotate.get(fa1,'x') );
- },function DoubleOverride()
- { function f(){}
- var fa0 = Annotate( {x:'X'}, f );
- var fa1 = Annotate( {x:'Y'}, fa0 );
- var fa2 = Annotate( {x:'Z'}, fa1 );
- doh.assertEqual( 'Z', Annotate.get(f ,'x') );
- doh.assertEqual( 'Z', Annotate.get(fa1,'x') );
- },function Method()
- {
- var W = declare( "lib/Annotation/test/test/Method",[],
- {
- double: Annotate(
- { description: "Multiplies argument by 2 and converts to String"
- , returns:'string'
- , params:
- { // only one parameter
- p0:
- { order:0 // i.e. first argument
- ,type:Number // instance of Number
- }
- }
- }, function(p0)
- {
- return (p0*2)+" points";
- })
- });
- var w = new W();
- doh.assertEqual( 'string', Annotate.get( w.double ,'returns' ) );
- doh.assertEqual( Number, Annotate.get( w.double ).params.p0.type );
- doh.assertEqual( w.double(3), "6 points" );
- doh.assertEqual( Annotate.get( w.double ,'returns' ), typeof w.double(3) );
-
- },function Class()
- {
- var W = Annotate(
- { description: "Annotation for class declaration"
- , x:'X'
- }, declare( "lib/Annotation/test/test/Class",[],function(){} )
- );
-
- doh.assertEqual( 'X', Annotate.get( W ,'x' ) );
- var obj = new W();
- doh.assertEqual( 'X', Annotate.get( obj.constructor ,'x' ) );
-
- },function Object()
- {
- var W = declare( "lib/Annotation/test/test/Object",[],function(){} );
-
- var obj = Annotate(
- { description: "Annotation on object instance"
- , x:'X'
- }, new W() );// instantiation
- doh.assertEqual( 'X', Annotate.get( obj ,'x' ) );
-
- },function DeclaredSeparate()
- {
- var W = declare( "lib/Annotation/test/test/Declared_separated",[],function(){} );
- var WA = Annotate(
- { description: "Annotation for class declaration added separately"
- , x:'X'
- }, W
- );
- doh.assertEqual( 'X', Annotate.get( W ,'x' ) );
- var obj0 = new W();
- doh.assertEqual( 'X', Annotate.get( obj0.constructor ,'x' ) );
- var obj1 = new WA();
- doh.assertEqual( 'X', Annotate.get( obj1.constructor ,'x' ) );
- doh.assertTrue( obj1 instanceof W ); // to make sure that Annotation does not change the type
-
- }, Annotate( { x:'X', description:"function called by doh annotated by itself." }
- ,function SelfAnnotate()
- {
- doh.assertEqual( 'X', Annotate.get( SelfAnnotate, 'x' ) );
- })
- ]);
- doh.run();
- });