PageRenderTime 79ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Annotation/test/test.js

https://bitbucket.org/sasha.firsov/dojoplay2012
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
  1. // test for lib/Annotation/main
  2. // http://localhost/DojoPlay2012/libcommon/DTK/util/doh/runner.html?testModule=lib.Annotation.test.test&paths=lib,../../../lib
  3. define( ["lib/Annotation/main", "doh/runner", "dojo/_base/declare"], function( Annotate, doh, declare )
  4. {
  5. var a = { x:"X" };
  6. doh.register( "lib/Annotation/main",
  7. [
  8. function test_get()
  9. {
  10. function f(){}
  11. var fa = Annotate(a,f);
  12. doh.assertEqual( 'X', Annotate.get(fa,'x') );
  13. }
  14. ,function AnnotationValueCloned()
  15. {
  16. var a = { x:"X" };
  17. function f0(){}
  18. var fa0 = Annotate(a,f0);
  19. a.x="XX"
  20. function f1(){}
  21. var fa1 = Annotate(a,f1);
  22. doh.assertEqual( 'X' , Annotate.get(fa0,'x') );
  23. doh.assertEqual( 'XX', Annotate.get(fa1,'x') );
  24. }
  25. ,function Override()
  26. {
  27. var a = { x:"X" };
  28. function f(){}
  29. var fa0 = Annotate(a,f);
  30. a.x="XX"
  31. var fa1 = Annotate(a,f);
  32. doh.assertEqual( 'XX', Annotate.get(f,'x') ); //
  33. doh.assertEqual( 'XX', Annotate.get(fa0,'x') );
  34. doh.assertEqual( 'XX', Annotate.get(fa1,'x') );
  35. },function DoubleOverride()
  36. { function f(){}
  37. var fa0 = Annotate( {x:'X'}, f );
  38. var fa1 = Annotate( {x:'Y'}, fa0 );
  39. var fa2 = Annotate( {x:'Z'}, fa1 );
  40. doh.assertEqual( 'Z', Annotate.get(f ,'x') );
  41. doh.assertEqual( 'Z', Annotate.get(fa1,'x') );
  42. },function Method()
  43. {
  44. var W = declare( "lib/Annotation/test/test/Method",[],
  45. {
  46. double: Annotate(
  47. { description: "Multiplies argument by 2 and converts to String"
  48. , returns:'string'
  49. , params:
  50. { // only one parameter
  51. p0:
  52. { order:0 // i.e. first argument
  53. ,type:Number // instance of Number
  54. }
  55. }
  56. }, function(p0)
  57. {
  58. return (p0*2)+" points";
  59. })
  60. });
  61. var w = new W();
  62. doh.assertEqual( 'string', Annotate.get( w.double ,'returns' ) );
  63. doh.assertEqual( Number, Annotate.get( w.double ).params.p0.type );
  64. doh.assertEqual( w.double(3), "6 points" );
  65. doh.assertEqual( Annotate.get( w.double ,'returns' ), typeof w.double(3) );
  66. },function Class()
  67. {
  68. var W = Annotate(
  69. { description: "Annotation for class declaration"
  70. , x:'X'
  71. }, declare( "lib/Annotation/test/test/Class",[],function(){} )
  72. );
  73. doh.assertEqual( 'X', Annotate.get( W ,'x' ) );
  74. var obj = new W();
  75. doh.assertEqual( 'X', Annotate.get( obj.constructor ,'x' ) );
  76. },function Object()
  77. {
  78. var W = declare( "lib/Annotation/test/test/Object",[],function(){} );
  79. var obj = Annotate(
  80. { description: "Annotation on object instance"
  81. , x:'X'
  82. }, new W() );// instantiation
  83. doh.assertEqual( 'X', Annotate.get( obj ,'x' ) );
  84. },function DeclaredSeparate()
  85. {
  86. var W = declare( "lib/Annotation/test/test/Declared_separated",[],function(){} );
  87. var WA = Annotate(
  88. { description: "Annotation for class declaration added separately"
  89. , x:'X'
  90. }, W
  91. );
  92. doh.assertEqual( 'X', Annotate.get( W ,'x' ) );
  93. var obj0 = new W();
  94. doh.assertEqual( 'X', Annotate.get( obj0.constructor ,'x' ) );
  95. var obj1 = new WA();
  96. doh.assertEqual( 'X', Annotate.get( obj1.constructor ,'x' ) );
  97. doh.assertTrue( obj1 instanceof W ); // to make sure that Annotation does not change the type
  98. }, Annotate( { x:'X', description:"function called by doh annotated by itself." }
  99. ,function SelfAnnotate()
  100. {
  101. doh.assertEqual( 'X', Annotate.get( SelfAnnotate, 'x' ) );
  102. })
  103. ]);
  104. doh.run();
  105. });