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