| 1 | dojo.provide("dojox.lang.tests.typed"); |
| 2 | dojo.config.typeCheckAllClasses = true; |
| 3 | dojo.require("dojox.lang.typed"); |
| 4 | (function(){ |
| 5 | |
| 6 | dojox.lang.typed( |
| 7 | dojo.declare("dojox.lang.tests.TypedClass", null, { |
| 8 | constructor: function(makeDefaults){ |
| 9 | if(makeDefaults){ |
| 10 | this.aString = "start"; |
| 11 | this.self = this; |
| 12 | } |
| 13 | }, |
| 14 | add: function(a, b){ |
| 15 | return a + b; |
| 16 | }, |
| 17 | withCallback: function(callback, param){ |
| 18 | callback(param); |
| 19 | } |
| 20 | })); |
| 21 | var TypedClass = dojox.lang.tests.TypedClass; |
| 22 | TypedClass.properties = { |
| 23 | aString:String, |
| 24 | self: TypedClass, |
| 25 | anInt: {type:"integer", maximum: 100, optional: true} |
| 26 | }; |
| 27 | TypedClass.methods = { |
| 28 | add: { |
| 29 | parameters:[ |
| 30 | {type:"number"}, |
| 31 | {type:"number"} |
| 32 | ], |
| 33 | returns: {type:"string"} |
| 34 | }, |
| 35 | withCallback:{ |
| 36 | parameters:[ |
| 37 | {type:"function", parameters:[Number]} |
| 38 | ] |
| 39 | } |
| 40 | } |
| 41 | var hasGetters = {}.__defineGetter__; |
| 42 | if(!hasGetters){ |
| 43 | console.warn("This platform does not support getters, property type checking will not be tested"); |
| 44 | } |
| 45 | function mustThrow(testFunc){ |
| 46 | try{ |
| 47 | testFunc(); |
| 48 | }catch(e){ |
| 49 | return; |
| 50 | } |
| 51 | throw new Error("No exception was thrown where an exception was required"); |
| 52 | } |
| 53 | tests.register("dojox.lang.tests.typed", [ |
| 54 | function typedConstructor(){ |
| 55 | mustThrow(function(){ |
| 56 | typedInstance = new TypedClass(); |
| 57 | }); |
| 58 | }, |
| 59 | function typedProperties(t){ |
| 60 | typedInstance = new TypedClass(true); |
| 61 | t.is(typedInstance.aString, "start"); |
| 62 | typedInstance.aString = "hi"; |
| 63 | if(hasGetters){ |
| 64 | mustThrow(function(){ |
| 65 | typedInstance.aString = 44; |
| 66 | }); |
| 67 | } |
| 68 | typedInstance.anInt = 22; |
| 69 | if(hasGetters){ |
| 70 | mustThrow(function(){ |
| 71 | typedInstance.anInt = "hello"; |
| 72 | }); |
| 73 | } |
| 74 | if(hasGetters){ |
| 75 | mustThrow(function(){ |
| 76 | typedInstance.anInt = 44.33; |
| 77 | }); |
| 78 | } |
| 79 | if(hasGetters){ |
| 80 | mustThrow(function(){ |
| 81 | typedInstance.anInt = 144; |
| 82 | }); |
| 83 | } |
| 84 | typedInstance.self = typedInstance; |
| 85 | if(hasGetters){ |
| 86 | mustThrow(function(){ |
| 87 | typedInstance.self = {}; |
| 88 | }); |
| 89 | } |
| 90 | }, |
| 91 | function typedMethods(){ |
| 92 | typedInstance = new TypedClass(true); |
| 93 | mustThrow(function(){ |
| 94 | typedInstance.add("hi",33); |
| 95 | }); |
| 96 | mustThrow(function(){ |
| 97 | typedInstance.add(22,33); |
| 98 | }); |
| 99 | mustThrow(function(){ |
| 100 | typedInstance.withCallback(22,33); |
| 101 | }); |
| 102 | mustThrow(function(){ |
| 103 | typedInstance.withCallback(function(){},"hi"); |
| 104 | }); |
| 105 | TypedClass.methods.add.returns.type = "number"; |
| 106 | typedInstance.add(22,33); |
| 107 | typedInstance.withCallback(function(){},44); |
| 108 | |
| 109 | }, |
| 110 | function typedDeclares(){ |
| 111 | dojo.declare("dojox.lang.tests.AutoTypedClass", null, { |
| 112 | constructor: function(){ |
| 113 | this.foo = "bar"; |
| 114 | }, |
| 115 | subtract: function(a, b){ |
| 116 | return a - b; |
| 117 | } |
| 118 | }); |
| 119 | var AutoTypedClass = dojox.lang.tests.AutoTypedClass; |
| 120 | AutoTypedClass.properties = { |
| 121 | foo:{type:"string"} |
| 122 | }; |
| 123 | AutoTypedClass.methods = { |
| 124 | subtract: { |
| 125 | parameters:[ |
| 126 | {type:"number"}, |
| 127 | {type:"number"} |
| 128 | ] |
| 129 | } |
| 130 | }; |
| 131 | typedInstance = new AutoTypedClass(true); |
| 132 | if(hasGetters){ |
| 133 | mustThrow(function(){ |
| 134 | typedInstance.foo = 33; |
| 135 | }); |
| 136 | } |
| 137 | typedInstance.foo = "baz"; |
| 138 | mustThrow(function(){ |
| 139 | typedInstance.subtract("hi",33); |
| 140 | }); |
| 141 | typedInstance.subtract(22,33); |
| 142 | } |
| 143 | ]); |
| 144 | })(); |