123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258 |
- if (!dojo._hasResource["dojox.wire.tests.programmatic.Wire"]) { // _hasResource
- // checks added
- // by build. Do
- // not use
- // _hasResource
- // directly in
- // your code.
- dojo._hasResource["dojox.wire.tests.programmatic.Wire"] = true;
- dojo.provide("dojox.wire.tests.programmatic.Wire");
- dojo.require("dojox.wire.Wire");
- // Simple connverter class to try to use.
- dojo.declare("dojox.wire.tests.programmatic.Wire.Converter", null, {
- convert : function(v) {
- return v + 1;
- }
- });
- // Simple converter function to try to use.
- // To get it in the global namespace, gotta assign it to the
- // 'window' toplevel object. Otherwise it ends up in the
- // dojo NS and can't be found.
- if (dojo.isBrowser) {
- window["__wireTestConverterFunction"] = function(v) {
- return v + 1;
- };
- } else {
- var __wireTestConverterFunction = function(v) {
- return v + 1;
- };
- }
- tests.register("dojox.wire.tests.programmatic.Wire", [
- function test_Wire_property(t) {
- var source = {
- a : "A",
- b : {
- c : "B.C"
- }
- };
- var target = {
- a : "a",
- b : {
- c : "b.c"
- }
- };
- var value = new dojox.wire.Wire({
- object : source,
- property : "a"
- }).getValue();
- new dojox.wire.Wire({
- object : target,
- property : "a"
- }).setValue(value);
- t.assertEqual(source.a, target.a);
- // child property
- value = new dojox.wire.Wire({
- object : source,
- property : "b.c"
- }).getValue();
- new dojox.wire.Wire({
- object : target,
- property : "b.c"
- }).setValue(value);
- t.assertEqual(source.b.c, target.b.c);
- // new property
- target = {};
- value = new dojox.wire.Wire({
- object : source,
- property : "a"
- }).getValue();
- new dojox.wire.Wire({
- object : target,
- property : "a"
- }).setValue(value);
- t.assertEqual(source.a, target.a);
- // new parent and child property
- target.b = {};
- value = new dojox.wire.Wire({
- object : source,
- property : "b.c"
- }).getValue();
- new dojox.wire.Wire({
- object : target,
- property : "b.c"
- }).setValue(value);
- t.assertEqual(source.b.c, target.b.c);
- // new parent and child property
- target = {};
- value = new dojox.wire.Wire({
- object : source,
- property : "b.c"
- }).getValue();
- new dojox.wire.Wire({
- object : target,
- property : "b.c"
- }).setValue(value);
- t.assertEqual(source.b.c, target.b.c);
- // new array property
- source = {
- a : ["A"]
- };
- target = {};
- value = new dojox.wire.Wire({
- object : source,
- property : "a[0]"
- }).getValue();
- new dojox.wire.Wire({
- object : target,
- property : "a[0]"
- }).setValue(value);
- t.assertEqual(source.a[0], target.a[0]);
- // by getter/setter
- source = {
- getA : function() {
- return this._a;
- },
- _a : "A"
- };
- target = {
- setA : function(a) {
- this._a = a;
- }
- };
- value = new dojox.wire.Wire({
- object : source,
- property : "a"
- }).getValue();
- new dojox.wire.Wire({
- object : target,
- property : "a"
- }).setValue(value);
- t.assertEqual(source._a, target._a);
- // by get/setPropertyValue
- source = {
- getPropertyValue : function(p) {
- return this["_" + p];
- },
- _a : "A"
- };
- target = {
- setPropertyValue : function(p, v) {
- this["_" + p] = v;
- }
- };
- value = new dojox.wire.Wire({
- object : source,
- property : "a"
- }).getValue();
- new dojox.wire.Wire({
- object : target,
- property : "a"
- }).setValue(value);
- t.assertEqual(source._a, target._a);
- },
- function test_Wire_type(t) {
- var source = {
- a : "1"
- };
- var string = new dojox.wire.Wire({
- object : source,
- property : "a"
- }).getValue();
- t.assertEqual("11", string + 1);
- var number = new dojox.wire.Wire({
- object : source,
- property : "a",
- type : "number"
- }).getValue();
- t.assertEqual(2, number + 1);
- },
- function test_Wire_converterObject(t) {
- var source = {
- a : "1"
- };
- var converter = {
- convert : function(v) {
- return v + 1;
- }
- };
- var string = new dojox.wire.Wire({
- object : source,
- property : "a",
- converter : converter
- }).getValue();
- t.assertEqual("11", string);
- },
- function test_Wire_converterFunction(t) {
- var source = {
- a : "1"
- };
- var converter = {
- convert : function(v) {
- return v + 1;
- }
- };
- var number = new dojox.wire.Wire({
- object : source,
- property : "a",
- type : "number",
- converter : converter.convert
- }).getValue();
- t.assertEqual(2, number);
- },
- function test_Wire_converterObjectByString(t) {
- var source = {
- a : "1"
- };
- var number = new dojox.wire.Wire({
- object : source,
- property : "a",
- type : "number",
- converter : "dojox.wire.tests.programmatic.Wire.Converter"
- }).getValue();
- t.assertEqual(2, number);
- },
- function test_Wire_converterFunctionByString(t) {
- var source = {
- a : "1"
- };
- var number = new dojox.wire.Wire({
- object : source,
- property : "a",
- type : "number",
- converter : "__wireTestConverterFunction"
- }).getValue();
- t.assertEqual(2, number);
- },
- function test_Wire_converterObjectByStringDynamic(t) {
- var source = {
- a : "1"
- };
- var number = new dojox.wire.Wire({
- object : source,
- property : "a",
- type : "number",
- converter : "dojox.wire.tests.programmatic.ConverterDynamic"
- }).getValue();
- t.assertEqual(2, number);
- }
- ]);
- }
|