bd110907b3f023f59130bb09b5e9cb0d7e972c74.svn-base 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. if (!dojo._hasResource["dojox.wire.tests.programmatic.Wire"]) { // _hasResource
  2. // checks added
  3. // by build. Do
  4. // not use
  5. // _hasResource
  6. // directly in
  7. // your code.
  8. dojo._hasResource["dojox.wire.tests.programmatic.Wire"] = true;
  9. dojo.provide("dojox.wire.tests.programmatic.Wire");
  10. dojo.require("dojox.wire.Wire");
  11. // Simple connverter class to try to use.
  12. dojo.declare("dojox.wire.tests.programmatic.Wire.Converter", null, {
  13. convert : function(v) {
  14. return v + 1;
  15. }
  16. });
  17. // Simple converter function to try to use.
  18. // To get it in the global namespace, gotta assign it to the
  19. // 'window' toplevel object. Otherwise it ends up in the
  20. // dojo NS and can't be found.
  21. if (dojo.isBrowser) {
  22. window["__wireTestConverterFunction"] = function(v) {
  23. return v + 1;
  24. };
  25. } else {
  26. var __wireTestConverterFunction = function(v) {
  27. return v + 1;
  28. };
  29. }
  30. tests.register("dojox.wire.tests.programmatic.Wire", [
  31. function test_Wire_property(t) {
  32. var source = {
  33. a : "A",
  34. b : {
  35. c : "B.C"
  36. }
  37. };
  38. var target = {
  39. a : "a",
  40. b : {
  41. c : "b.c"
  42. }
  43. };
  44. var value = new dojox.wire.Wire({
  45. object : source,
  46. property : "a"
  47. }).getValue();
  48. new dojox.wire.Wire({
  49. object : target,
  50. property : "a"
  51. }).setValue(value);
  52. t.assertEqual(source.a, target.a);
  53. // child property
  54. value = new dojox.wire.Wire({
  55. object : source,
  56. property : "b.c"
  57. }).getValue();
  58. new dojox.wire.Wire({
  59. object : target,
  60. property : "b.c"
  61. }).setValue(value);
  62. t.assertEqual(source.b.c, target.b.c);
  63. // new property
  64. target = {};
  65. value = new dojox.wire.Wire({
  66. object : source,
  67. property : "a"
  68. }).getValue();
  69. new dojox.wire.Wire({
  70. object : target,
  71. property : "a"
  72. }).setValue(value);
  73. t.assertEqual(source.a, target.a);
  74. // new parent and child property
  75. target.b = {};
  76. value = new dojox.wire.Wire({
  77. object : source,
  78. property : "b.c"
  79. }).getValue();
  80. new dojox.wire.Wire({
  81. object : target,
  82. property : "b.c"
  83. }).setValue(value);
  84. t.assertEqual(source.b.c, target.b.c);
  85. // new parent and child property
  86. target = {};
  87. value = new dojox.wire.Wire({
  88. object : source,
  89. property : "b.c"
  90. }).getValue();
  91. new dojox.wire.Wire({
  92. object : target,
  93. property : "b.c"
  94. }).setValue(value);
  95. t.assertEqual(source.b.c, target.b.c);
  96. // new array property
  97. source = {
  98. a : ["A"]
  99. };
  100. target = {};
  101. value = new dojox.wire.Wire({
  102. object : source,
  103. property : "a[0]"
  104. }).getValue();
  105. new dojox.wire.Wire({
  106. object : target,
  107. property : "a[0]"
  108. }).setValue(value);
  109. t.assertEqual(source.a[0], target.a[0]);
  110. // by getter/setter
  111. source = {
  112. getA : function() {
  113. return this._a;
  114. },
  115. _a : "A"
  116. };
  117. target = {
  118. setA : function(a) {
  119. this._a = a;
  120. }
  121. };
  122. value = new dojox.wire.Wire({
  123. object : source,
  124. property : "a"
  125. }).getValue();
  126. new dojox.wire.Wire({
  127. object : target,
  128. property : "a"
  129. }).setValue(value);
  130. t.assertEqual(source._a, target._a);
  131. // by get/setPropertyValue
  132. source = {
  133. getPropertyValue : function(p) {
  134. return this["_" + p];
  135. },
  136. _a : "A"
  137. };
  138. target = {
  139. setPropertyValue : function(p, v) {
  140. this["_" + p] = v;
  141. }
  142. };
  143. value = new dojox.wire.Wire({
  144. object : source,
  145. property : "a"
  146. }).getValue();
  147. new dojox.wire.Wire({
  148. object : target,
  149. property : "a"
  150. }).setValue(value);
  151. t.assertEqual(source._a, target._a);
  152. },
  153. function test_Wire_type(t) {
  154. var source = {
  155. a : "1"
  156. };
  157. var string = new dojox.wire.Wire({
  158. object : source,
  159. property : "a"
  160. }).getValue();
  161. t.assertEqual("11", string + 1);
  162. var number = new dojox.wire.Wire({
  163. object : source,
  164. property : "a",
  165. type : "number"
  166. }).getValue();
  167. t.assertEqual(2, number + 1);
  168. },
  169. function test_Wire_converterObject(t) {
  170. var source = {
  171. a : "1"
  172. };
  173. var converter = {
  174. convert : function(v) {
  175. return v + 1;
  176. }
  177. };
  178. var string = new dojox.wire.Wire({
  179. object : source,
  180. property : "a",
  181. converter : converter
  182. }).getValue();
  183. t.assertEqual("11", string);
  184. },
  185. function test_Wire_converterFunction(t) {
  186. var source = {
  187. a : "1"
  188. };
  189. var converter = {
  190. convert : function(v) {
  191. return v + 1;
  192. }
  193. };
  194. var number = new dojox.wire.Wire({
  195. object : source,
  196. property : "a",
  197. type : "number",
  198. converter : converter.convert
  199. }).getValue();
  200. t.assertEqual(2, number);
  201. },
  202. function test_Wire_converterObjectByString(t) {
  203. var source = {
  204. a : "1"
  205. };
  206. var number = new dojox.wire.Wire({
  207. object : source,
  208. property : "a",
  209. type : "number",
  210. converter : "dojox.wire.tests.programmatic.Wire.Converter"
  211. }).getValue();
  212. t.assertEqual(2, number);
  213. },
  214. function test_Wire_converterFunctionByString(t) {
  215. var source = {
  216. a : "1"
  217. };
  218. var number = new dojox.wire.Wire({
  219. object : source,
  220. property : "a",
  221. type : "number",
  222. converter : "__wireTestConverterFunction"
  223. }).getValue();
  224. t.assertEqual(2, number);
  225. },
  226. function test_Wire_converterObjectByStringDynamic(t) {
  227. var source = {
  228. a : "1"
  229. };
  230. var number = new dojox.wire.Wire({
  231. object : source,
  232. property : "a",
  233. type : "number",
  234. converter : "dojox.wire.tests.programmatic.ConverterDynamic"
  235. }).getValue();
  236. t.assertEqual(2, number);
  237. }
  238. ]);
  239. }