1 | dojo.provide("dojox.wire.tests.programmatic.Wire"); |
---|
2 | dojo.require("dojox.wire.Wire"); |
---|
3 | |
---|
4 | //Simple connverter class to try to use. |
---|
5 | dojo.declare("dojox.wire.tests.programmatic.Wire.Converter", null, { |
---|
6 | convert: function(v){ |
---|
7 | return v + 1; |
---|
8 | } |
---|
9 | }); |
---|
10 | |
---|
11 | //Simple converter function to try to use. |
---|
12 | //To get it in the global namespace, gotta assign it to the |
---|
13 | //'window' toplevel object. Otherwise it ends up in the |
---|
14 | //dojo NS and can't be found. |
---|
15 | if (dojo.isBrowser) { |
---|
16 | window["__wireTestConverterFunction"] = function(v){ |
---|
17 | return v + 1; |
---|
18 | }; |
---|
19 | }else{ |
---|
20 | var __wireTestConverterFunction = function(v){ |
---|
21 | return v + 1; |
---|
22 | }; |
---|
23 | } |
---|
24 | |
---|
25 | tests.register("dojox.wire.tests.programmatic.Wire", [ |
---|
26 | |
---|
27 | function test_Wire_property(t){ |
---|
28 | var source = {a: "A", b: {c: "B.C"}}; |
---|
29 | var target = {a: "a", b: {c: "b.c"}}; |
---|
30 | var value = new dojox.wire.Wire({object: source, property: "a"}).getValue(); |
---|
31 | new dojox.wire.Wire({object: target, property: "a"}).setValue(value); |
---|
32 | t.assertEqual(source.a, target.a); |
---|
33 | |
---|
34 | // child property |
---|
35 | value = new dojox.wire.Wire({object: source, property: "b.c"}).getValue(); |
---|
36 | new dojox.wire.Wire({object: target, property: "b.c"}).setValue(value); |
---|
37 | t.assertEqual(source.b.c, target.b.c); |
---|
38 | |
---|
39 | // new property |
---|
40 | target = {}; |
---|
41 | value = new dojox.wire.Wire({object: source, property: "a"}).getValue(); |
---|
42 | new dojox.wire.Wire({object: target, property: "a"}).setValue(value); |
---|
43 | t.assertEqual(source.a, target.a); |
---|
44 | |
---|
45 | // new parent and child property |
---|
46 | target.b = {}; |
---|
47 | value = new dojox.wire.Wire({object: source, property: "b.c"}).getValue(); |
---|
48 | new dojox.wire.Wire({object: target, property: "b.c"}).setValue(value); |
---|
49 | t.assertEqual(source.b.c, target.b.c); |
---|
50 | |
---|
51 | // new parent and child property |
---|
52 | target = {}; |
---|
53 | value = new dojox.wire.Wire({object: source, property: "b.c"}).getValue(); |
---|
54 | new dojox.wire.Wire({object: target, property: "b.c"}).setValue(value); |
---|
55 | t.assertEqual(source.b.c, target.b.c); |
---|
56 | |
---|
57 | // new array property |
---|
58 | source = {a: ["A"]}; |
---|
59 | target = {}; |
---|
60 | value = new dojox.wire.Wire({object: source, property: "a[0]"}).getValue(); |
---|
61 | new dojox.wire.Wire({object: target, property: "a[0]"}).setValue(value); |
---|
62 | t.assertEqual(source.a[0], target.a[0]); |
---|
63 | |
---|
64 | // by getter/setter |
---|
65 | source = {getA: function() { return this._a; }, _a: "A"}; |
---|
66 | target = {setA: function(a) { this._a = a; }}; |
---|
67 | value = new dojox.wire.Wire({object: source, property: "a"}).getValue(); |
---|
68 | new dojox.wire.Wire({object: target, property: "a"}).setValue(value); |
---|
69 | t.assertEqual(source._a, target._a); |
---|
70 | |
---|
71 | // by get/setPropertyValue |
---|
72 | source = {getPropertyValue: function(p) { return this["_" + p]; }, _a: "A"}; |
---|
73 | target = {setPropertyValue: function(p, v) { this["_" + p] = v; }}; |
---|
74 | value = new dojox.wire.Wire({object: source, property: "a"}).getValue(); |
---|
75 | new dojox.wire.Wire({object: target, property: "a"}).setValue(value); |
---|
76 | t.assertEqual(source._a, target._a); |
---|
77 | }, |
---|
78 | |
---|
79 | function test_Wire_type(t){ |
---|
80 | var source = {a: "1"}; |
---|
81 | var string = new dojox.wire.Wire({object: source, property: "a"}).getValue(); |
---|
82 | t.assertEqual("11", string + 1); |
---|
83 | var number = new dojox.wire.Wire({object: source, property: "a", type: "number"}).getValue(); |
---|
84 | t.assertEqual(2, number + 1); |
---|
85 | }, |
---|
86 | |
---|
87 | function test_Wire_converterObject(t){ |
---|
88 | var source = {a: "1"}; |
---|
89 | var converter = {convert: function(v) { return v + 1; }}; |
---|
90 | var string = new dojox.wire.Wire({object: source, property: "a", converter: converter}).getValue(); |
---|
91 | t.assertEqual("11", string); |
---|
92 | }, |
---|
93 | |
---|
94 | function test_Wire_converterFunction(t){ |
---|
95 | var source = {a: "1"}; |
---|
96 | var converter = {convert: function(v) { return v + 1; }}; |
---|
97 | var number = new dojox.wire.Wire({object: source, property: "a", type: "number", converter: converter.convert}).getValue(); |
---|
98 | t.assertEqual(2, number); |
---|
99 | }, |
---|
100 | |
---|
101 | function test_Wire_converterObjectByString(t){ |
---|
102 | var source = {a: "1"}; |
---|
103 | var number = new dojox.wire.Wire({object: source, property: "a", type: "number", converter: "dojox.wire.tests.programmatic.Wire.Converter"}).getValue(); |
---|
104 | t.assertEqual(2, number); |
---|
105 | }, |
---|
106 | |
---|
107 | function test_Wire_converterFunctionByString(t){ |
---|
108 | var source = {a: "1"}; |
---|
109 | var number = new dojox.wire.Wire({object: source, property: "a", type: "number", converter: "__wireTestConverterFunction"}).getValue(); |
---|
110 | t.assertEqual(2, number); |
---|
111 | }, |
---|
112 | |
---|
113 | function test_Wire_converterObjectByStringDynamic(t){ |
---|
114 | var source = {a: "1"}; |
---|
115 | var number = new dojox.wire.Wire({object: source, property: "a", type: "number", converter: "dojox.wire.tests.programmatic.ConverterDynamic"}).getValue(); |
---|
116 | t.assertEqual(2, number); |
---|
117 | } |
---|
118 | |
---|
119 | ]); |
---|