PageRenderTime 86ms CodeModel.GetById 20ms RepoModel.GetById 10ms app.codeStats 0ms

/tine20/library/OpenLayers/tests/BaseTypes.html

https://gitlab.com/rsilveira1987/Expresso
HTML | 366 lines | 296 code | 70 blank | 0 comment | 0 complexity | 3522f661163d4d88aff98aa6e9e8755a MD5 | raw file
  1. <html>
  2. <head>
  3. <script src="../lib/OpenLayers.js"></script>
  4. <script type="text/javascript">
  5. function test_String_startsWith(t) {
  6. t.plan(3);
  7. var str = "chickenHead";
  8. var test1 = "chicken";
  9. var test2 = "beet";
  10. t.ok(OpenLayers.String.startsWith(str, "chicken"),
  11. "'chickenHead' starts with 'chicken'");
  12. t.ok(!OpenLayers.String.startsWith(str, "Head"),
  13. "'chickenHead' does not start with 'Head'");
  14. t.ok(!OpenLayers.String.startsWith(str, "beet"),
  15. "'chickenHead' doesnt start with 'beet'");
  16. }
  17. function test_String_contains(t) {
  18. t.plan(4);
  19. var str = "chickenHead";
  20. t.ok(OpenLayers.String.contains(str, "chicken"),
  21. "(beginning) 'chickenHead' contains with 'chicken'");
  22. t.ok(OpenLayers.String.contains(str, "ick"),
  23. "(middle) 'chickenHead' contains with 'ick'");
  24. t.ok(OpenLayers.String.contains(str, "Head"),
  25. "(end) 'chickenHead' contains with 'Head'");
  26. t.ok(!OpenLayers.String.startsWith(str, "beet"),
  27. "'chickenHead' doesnt start with 'beet'");
  28. }
  29. function test_String_trim(t) {
  30. t.plan(6);
  31. var str = "chickenHead";
  32. t.eq(OpenLayers.String.trim(str),
  33. "chickenHead", "string with no extra whitespace is left alone");
  34. str = " chickenHead";
  35. t.eq(OpenLayers.String.trim(str),
  36. "chickenHead", "string with extra whitespace at beginning is trimmed correctly");
  37. str = "chickenHead ";
  38. t.eq(OpenLayers.String.trim(str),
  39. "chickenHead", "string with extra whitespace at end is trimmed correctly");
  40. str = " chickenHead ";
  41. t.eq(OpenLayers.String.trim(str),
  42. "chickenHead", "string with extra whitespace at beginning and end is trimmed correctly");
  43. str = "chicken\nHead ";
  44. t.eq(OpenLayers.String.trim(str),
  45. "chicken\nHead", "multi-line string with extra whitespace at end is trimmed correctly");
  46. str = " ";
  47. t.eq(OpenLayers.String.trim(str), "", "whitespace string is trimmed correctly");
  48. }
  49. function test_String_camelize(t) {
  50. t.plan(7);
  51. var str = "chickenhead";
  52. t.eq(OpenLayers.String.camelize(str), "chickenhead", "string with no hyphens is left alone");
  53. str = "chicken-head";
  54. t.eq(OpenLayers.String.camelize(str), "chickenHead", "string with one middle hyphen is camelized correctly");
  55. str = "chicken-head-man";
  56. t.eq(OpenLayers.String.camelize(str), "chickenHeadMan", "string with multiple middle hyphens is camelized correctly");
  57. str = "-chickenhead";
  58. t.eq(OpenLayers.String.camelize(str), "Chickenhead", "string with starting hyphen is camelized correctly (capitalized)");
  59. str = "-chicken-head-man";
  60. t.eq(OpenLayers.String.camelize(str), "ChickenHeadMan", "string with starting hypen and multiple middle hyphens is camelized correctly");
  61. str = "chicken-";
  62. t.eq(OpenLayers.String.camelize(str), "chicken", "string ending in hyphen is camelized correctly (hyphen dropped)");
  63. str = "chicken-head-man-";
  64. t.eq(OpenLayers.String.camelize(str), "chickenHeadMan", "string with multiple middle hyphens and end hyphen is camelized correctly (end hyphen dropped)");
  65. }
  66. function test_String_format(t) {
  67. var unchanged = [
  68. "", "${ ", "${", " ${", "${${", "${}", "${${}}", " ${ ${",
  69. "}", "${${} }"
  70. ]
  71. t.plan(7 + unchanged.length);
  72. var format = OpenLayers.String.format;
  73. var expected;
  74. for(var i=0; i<unchanged.length; ++i) {
  75. expected = unchanged[i];
  76. t.eq(format(expected), expected,
  77. "'" + expected + "' left unchanged");
  78. }
  79. t.eq(format("${foo} none"),
  80. "undefined none", "undefined properties don't bomb");
  81. window.foo = "bar";
  82. t.eq(format("${foo} none"),
  83. "bar none", "window context used if none passed");
  84. var context = {bar: "foo"};
  85. t.eq(format("${bar} foo", context), "foo foo",
  86. "properties accessed from context");
  87. var context = {bar: "foo", foo: "bar"};
  88. t.eq(format("a ${bar} is a ${foo}", context), "a foo is a bar",
  89. "multiple properties replaced correctly");
  90. // test context with properties that are functions
  91. var context = {
  92. bar: "church",
  93. getDrunk: function() {
  94. return arguments[0];
  95. }
  96. };
  97. t.eq(
  98. format("I go to the ${bar} to ${getDrunk}.", context, ["eat pretzels"]),
  99. "I go to the church to eat pretzels.",
  100. "function correctly called in context with arguments"
  101. );
  102. // test that things don't break
  103. var context = {
  104. meaning: function(truth) {
  105. return truth;
  106. }
  107. };
  108. t.eq(
  109. format("In life, truth is ${meaning}.", context),
  110. "In life, truth is undefined.",
  111. "still works if arguments are not supplied"
  112. );
  113. // test contexts where attribute values can be objects
  114. var context = {
  115. a: {
  116. b: {
  117. c: 'd',
  118. e: function() {
  119. return 'f';
  120. }
  121. }
  122. }
  123. };
  124. t.eq(
  125. format("${a.b.c} ${a.b.e} ${a.b.q} ${a} ${a...b...c}", context),
  126. "d f undefined [object Object] d",
  127. "attribute values that are objects are supported"
  128. );
  129. }
  130. function test_String_isNumeric(t) {
  131. var cases = [
  132. {value: "3", expect: true},
  133. {value: "+3", expect: true},
  134. {value: "-3", expect: true},
  135. {value: "3.0", expect: true},
  136. {value: "+3.0", expect: true},
  137. {value: "-3.0", expect: true},
  138. {value: "6.02e23", expect: true},
  139. {value: "+1.0e-100", expect: true},
  140. {value: "-1.0e+100", expect: true},
  141. {value: "1E100", expect: true},
  142. {value: null, expect: false},
  143. {value: true, expect: false},
  144. {value: false, expect: false},
  145. {value: undefined, expect: false},
  146. {value: "", expect: false},
  147. {value: "3 ", expect: false},
  148. {value: " 3", expect: false},
  149. {value: "1e", expect: false},
  150. {value: "1+e", expect: false},
  151. {value: "1-e", expect: false}
  152. ];
  153. t.plan(cases.length);
  154. var func = OpenLayers.String.isNumeric;
  155. var obj, val, got, exp;
  156. for(var i=0; i<cases.length; ++i) {
  157. obj = cases[i];
  158. val = obj.value;
  159. exp = obj.expect;
  160. got = func(val);
  161. t.eq(got, exp, "'" + val + "' returns " + exp);
  162. }
  163. }
  164. function test_Number_numericIf(t) {
  165. var cases = [
  166. {value: "3", expect: 3},
  167. {value: "+3", expect: 3},
  168. {value: "-3", expect: -3},
  169. {value: "3.0", expect: 3},
  170. {value: "+3.0", expect: 3},
  171. {value: "-3.0", expect: -3},
  172. {value: "6.02e23", expect: 6.02e23},
  173. {value: "+1.0e-100", expect: 1e-100},
  174. {value: "-1.0e+100", expect: -1e100},
  175. {value: "1E100", expect: 1e100},
  176. {value: null, expect: null},
  177. {value: true, expect: true},
  178. {value: false, expect: false},
  179. {value: undefined, expect: undefined},
  180. {value: "", expect: ""},
  181. {value: "3 ", expect: "3 "},
  182. {value: " 3", expect: " 3"},
  183. {value: "1e", expect: "1e"},
  184. {value: "1+e", expect: "1+e"},
  185. {value: "1-e", expect: "1-e"}
  186. ];
  187. t.plan(cases.length);
  188. var func = OpenLayers.String.numericIf;
  189. var obj, val, got, exp;
  190. for(var i=0; i<cases.length; ++i) {
  191. obj = cases[i];
  192. val = obj.value;
  193. exp = obj.expect;
  194. got = func(val);
  195. t.eq(got, exp, "'" + val + "' returns " + exp);
  196. }
  197. }
  198. function test_Number_limitSigDigs(t) {
  199. t.plan(9);
  200. var num = 123456789;
  201. t.eq(OpenLayers.Number.limitSigDigs(num), 0, "passing 'null' as sig returns 0");
  202. t.eq(OpenLayers.Number.limitSigDigs(num, -1), 0, "passing -1 as sig returns 0");
  203. t.eq(OpenLayers.Number.limitSigDigs(num, 0), 0, "passing 0 as sig returns 0");
  204. t.eq(OpenLayers.Number.limitSigDigs(num, 15), 123456789, "passing sig greater than num digits in number returns number unmodified");
  205. t.eq(OpenLayers.Number.limitSigDigs(num, 1), 100000000, "passing sig 1 works");
  206. t.eq(OpenLayers.Number.limitSigDigs(num, 3), 123000000, "passing middle sig works (rounds down)");
  207. t.eq(OpenLayers.Number.limitSigDigs(num, 5), 123460000, "passing middle sig works (rounds up)");
  208. t.eq(OpenLayers.Number.limitSigDigs(num, 9), 123456789, "passing sig equal to num digits in number works");
  209. num = 1234.56789;
  210. t.eq(OpenLayers.Number.limitSigDigs(num, 5), 1234.6, "running limSigDig() on a floating point number works fine");
  211. }
  212. function test_Number_format(t) {
  213. t.plan(9);
  214. var format = OpenLayers.Number.format;
  215. t.eq(format(12345), "12,345", "formatting an integer number works");
  216. t.eq(format(12345, 3), "12,345.000", "zero padding an integer works");
  217. t.eq(format(12345, null, ","), "12,345", "adding thousands separator to an integer works");
  218. t.eq(format(12345, 0, ","), "12,345", "adding thousands separator to an integer with defined 0 decimal places works");
  219. var num = 12345.6789
  220. t.eq(format(num, null, "", ","), "12345,6789", "only changing decimal separator and leaving everything else untouched works");
  221. t.eq(format(num, 5), "12,345.67890", "filling up decimals with trailing zeroes works");
  222. t.eq(format(num, 3, ".", ","), "12.345,679", "rounding and changing decimal/thousands separator in function call works");
  223. t.eq(format(num, 0, ""), "12346", "empty thousands separator in function call works");
  224. OpenLayers.Number.thousandsSeparator = ".";
  225. OpenLayers.Number.decimalSeparator = ",";
  226. t.eq(format(num, 3), "12.345,679", "changing thousands/decimal separator globally works");
  227. }
  228. function test_Function_bind(t) {
  229. t.plan(12);
  230. g_obj = {};
  231. g_Arg1 = {};
  232. g_Arg2 = {};
  233. g_Arg3 = {};
  234. g_Arg4 = {};
  235. var foo = function(x,y,z,a) {
  236. t.ok(this == g_obj, "context correctly set");
  237. t.ok(x == g_Arg1, "arg1 passed correctly");
  238. t.ok(y == g_Arg2, "arg2 passed correctly");
  239. t.ok(z == g_Arg3, "arg3 passed correctly");
  240. t.ok(a == g_Arg4, "arg4 passed correctly");
  241. t.eq(arguments.length, 4, "correct number of arguments ((regression test for #876))");
  242. };
  243. var newFoo = OpenLayers.Function.bind(foo, g_obj, g_Arg1, g_Arg2);
  244. newFoo(g_Arg3, g_Arg4);
  245. //run again to make sure the arguments are handled correctly
  246. newFoo(g_Arg3, g_Arg4);
  247. }
  248. function test_Function_bindAsEventListener(t) {
  249. t.plan(4);
  250. g_obj = {};
  251. g_Event = {};
  252. g_WindowEvent = {};
  253. var foo = function(x) {
  254. t.ok(this == g_obj, "context correctly set");
  255. g_X = x;
  256. };
  257. var newFoo = OpenLayers.Function.bindAsEventListener(foo, g_obj);
  258. g_X = null;
  259. newFoo(g_Event);
  260. t.ok(g_X == g_Event, "event properly passed as first argument when event specified");
  261. g_X = null;
  262. newFoo();
  263. t.ok(g_X == window.event, "window.event properly passed as first argument when nothing specified");
  264. }
  265. function test_Array_filter(t) {
  266. t.plan(8);
  267. OpenLayers.Array.filter(["foo"], function(item, index, array) {
  268. t.eq(item, "foo", "callback called with proper item");
  269. t.eq(index, 0, "callback called with proper index");
  270. t.eq(array, ["foo"], "callback called with proper array");
  271. t.eq(this, {"foo": "bar"}, "callback called with this set properly");
  272. }, {"foo": "bar"});
  273. var array = [0, 1, 2, 3];
  274. var select = OpenLayers.Array.filter(array, function(value) {
  275. return value > 1;
  276. });
  277. t.eq(select, [2, 3], "filter works for basic callback");
  278. t.eq(array, [0, 1, 2, 3], "filter doesn't modify original");
  279. var obj = {
  280. test: function(value) {
  281. if(value > 1) {
  282. return true;
  283. }
  284. }
  285. };
  286. var select = OpenLayers.Array.filter(array, function(value) {
  287. return this.test(value);
  288. }, obj);
  289. t.eq(select, [2, 3], "filter works for callback and caller");
  290. t.eq(array, [0, 1, 2, 3], "filter doesn't modify original");
  291. }
  292. </script>
  293. </head>
  294. <body>
  295. </body>
  296. </html>