/specs/core/string.ds

http://github.com/wilkie/djehuty · Unknown · 507 lines · 426 code · 81 blank · 0 comment · 0 complexity · 57883f7a5243935571dd04c58d26439d MD5 · raw file

  1. module specs.core.string;
  2. import testing.support;
  3. import core.string;
  4. import math.currency;
  5. describe string {
  6. describe trim {
  7. it should_handle_empty_string() {
  8. should("".trim() == "");
  9. }
  10. it should_handle_whitespace_on_left() {
  11. should(" \t\nhello".trim() == "hello");
  12. should(" hello".trim() == "hello");
  13. should("\t\t\thello".trim() == "hello");
  14. should("\n\n\nhello".trim() == "hello");
  15. }
  16. it should_handle_whitespace_on_right() {
  17. should("hello \t\n".trim() == "hello");
  18. should("hello\t\t".trim() == "hello");
  19. should("hello\n\n".trim() == "hello");
  20. should("hello ".trim() == "hello");
  21. }
  22. it should_handle_whitespace_on_both_sides() {
  23. should(" \t\nhello \t\n".trim() == "hello");
  24. should("\t\t\thello\n".trim() == "hello");
  25. should("\n\n\t\thello \n".trim() == "hello");
  26. should(" \t hello \t\t\t\n\n\t ".trim() == "hello");
  27. }
  28. }
  29. describe split {
  30. it should_work_on_empty_strings {
  31. string[] foo1 = "".split('a');
  32. string[] foo2 = "".split("a");
  33. should(foo1[0] == "");
  34. should(foo2[0] == "");
  35. }
  36. it should_work_on_characters {
  37. string[] foo = "work.on.characters".split('.');
  38. should(foo.length == 3);
  39. should(foo[0] == "work");
  40. should(foo[1] == "on");
  41. should(foo[2] == "characters");
  42. }
  43. it should_work_on_characters_with_delimiter_at_beginning {
  44. string[] foo = ".work.a.b".split('.');
  45. should(foo.length == 4);
  46. should(foo[0] == "");
  47. should(foo[1] == "work");
  48. should(foo[2] == "a");
  49. should(foo[3] == "b");
  50. }
  51. it should_work_on_characters_with_delimiter_at_end {
  52. string[] foo = "work.a.b.".split('.');
  53. should(foo.length == 4);
  54. should(foo[0] == "work");
  55. should(foo[1] == "a");
  56. should(foo[2] == "b");
  57. should(foo[3] == "");
  58. }
  59. it should_work_on_strings {
  60. string[] foo = "work(on strings.foo)".split("( .)");
  61. should(foo.length == 5);
  62. should(foo[0] == "work");
  63. should(foo[1] == "on");
  64. should(foo[2] == "strings");
  65. should(foo[3] == "foo");
  66. should(foo[4] == "");
  67. }
  68. }
  69. describe nextInt {
  70. it should_work_on_empty_strings {
  71. int foo;
  72. should("".nextInt(foo) == false);
  73. }
  74. it should_return_the_next_int {
  75. int foo;
  76. bool returnVal = "123foo".nextInt(foo);
  77. should(foo == 123);
  78. should(returnVal == true);
  79. }
  80. it should_fail_when_there_is_not_a_next_int {
  81. int foo;
  82. bool returnVal = "foo123".nextInt(foo);
  83. should(returnVal == false);
  84. should(foo == 0);
  85. }
  86. }
  87. describe substring {
  88. it should_work_on_empty_strings {
  89. string foo = "";
  90. foo = foo.substring(0);
  91. should(foo == "");
  92. }
  93. it should_work_for_start_larger_than_length {
  94. string foo = "abc";
  95. foo = foo.substring(4);
  96. should(foo == "");
  97. foo = "abc".substring(3);
  98. should(foo == "");
  99. }
  100. it should_work_for_start_at_zero_and_length_omitted {
  101. string foo = "abc";
  102. foo = foo.substring(0);
  103. should(foo == "abc");
  104. }
  105. it should_work_for_start_at_zero_and_length_longer_than_string {
  106. string foo = "abc";
  107. foo = foo.substring(0, 4);
  108. should(foo == "abc");
  109. }
  110. it should_work_for_start_at_zero_and_length_at_zero {
  111. string foo = "abc";
  112. foo = foo.substring(0,0);
  113. should(foo == "");
  114. }
  115. it should_work_for_start_at_zero_and_length_within_string {
  116. string foo1 = "abc".substring(0, 1);
  117. string foo2 = "abc".substring(0, 2);
  118. should(foo1 == "a");
  119. should(foo2 == "ab");
  120. }
  121. it should_work_for_start_within_string_and_length_omitted {
  122. string foo1 = "abc".substring(1);
  123. string foo2 = "abc".substring(2);
  124. should(foo1 == "bc");
  125. should(foo2 == "c");
  126. }
  127. it should_work_for_start_within_string_and_length_longer_than_string {
  128. string foo1 = "abc".substring(1, 4);
  129. string foo2 = "abc".substring(2, 4);
  130. string foo3 = "abc".substring(3, 4);
  131. should(foo1 == "bc");
  132. should(foo2 == "c");
  133. should(foo3 == "");
  134. }
  135. it should_work_for_start_within_string_and_length_at_zero {
  136. string foo1 = "abc".substring(1,0);
  137. string foo2 = "abc".substring(2,0);
  138. string foo3 = "abc".substring(3,0);
  139. should(foo1 == "");
  140. should(foo2 == "");
  141. should(foo3 == "");
  142. }
  143. }
  144. describe replace {
  145. it should_work_on_empty_strings {
  146. string foo = "".replace('a', 'b');
  147. should(foo == "");
  148. }
  149. it should_work_as_expected {
  150. string foo = "abcaefahi".replace('a', 'x');
  151. should(foo == "xbcxefxhi");
  152. }
  153. }
  154. describe find {
  155. it should_work_on_empty_strings {
  156. int foo = "".find("foo", 0);
  157. should(foo == -1);
  158. }
  159. it should_fail_on_finding_empty_strings {
  160. int foo1 = "".find("", 0);
  161. int foo2 = "abc".find("", 0);
  162. should(foo1 == -1);
  163. should(foo2 == -1);
  164. }
  165. it should_work_when_start_is_omitted {
  166. int foo1 = "abcdebc".find("bc");
  167. int foo2 = "abcdebc".find("ce");
  168. should(foo1 == 1);
  169. should(foo2 == -1);
  170. }
  171. it should_work_when_search_string_is_at_beginning {
  172. int foo = "abcd".find("ab");
  173. should(foo == 0);
  174. }
  175. it should_work_when_search_string_is_at_end {
  176. int foo = "abcd".find("cd");
  177. should(foo == 2);
  178. }
  179. it should_work_when_search_string_is_within_string {
  180. int foo = "abcd".find("bc");
  181. should(foo == 1);
  182. }
  183. it should_work_when_start_is_given {
  184. int foo1 = "abcdab".find("ab", 0);
  185. int foo2 = "abcdab".find("ab", 1);
  186. should(foo1 == 0);
  187. should(foo2 == 4);
  188. }
  189. }
  190. describe findReverse {
  191. it should_work_on_empty_strings {
  192. int foo = "".findReverse("foo", 0);
  193. should(foo == -1);
  194. }
  195. it should_fail_on_finding_empty_strings {
  196. int foo1 = "".findReverse("", 0);
  197. int foo2 = "abc".findReverse("", 0);
  198. should(foo1 == -1);
  199. should(foo2 == -1);
  200. }
  201. it should_work_when_start_is_omitted {
  202. int foo1 = "abcdebc".findReverse("bc");
  203. int foo2 = "abcdebc".findReverse("ce");
  204. should(foo1 == 5);
  205. should(foo2 == -1);
  206. }
  207. it should_work_when_search_string_is_at_beginning {
  208. int foo = "abcd".findReverse("ab");
  209. should(foo == 0);
  210. }
  211. it should_work_when_search_string_is_at_end {
  212. int foo = "abcd".findReverse("cd");
  213. should(foo == 2);
  214. }
  215. it should_work_when_search_string_is_within_string {
  216. int foo = "abcd".findReverse("bc");
  217. should(foo == 1);
  218. }
  219. it should_work_when_start_is_given {
  220. int foo1 = "abcdabcd".findReverse("ab", 0);
  221. int foo2 = "abcdabcd".findReverse("ab", 2);
  222. int foo3 = "abcdabcd".findReverse("ab", 6);
  223. should(foo1 == -1);
  224. should(foo2 == 0);
  225. should(foo3 == 4);
  226. }
  227. }
  228. describe times {
  229. it should_work_on_empty_strings {
  230. should("".times(4) == "");
  231. }
  232. it should_return_empty_string_with_amount_being_zero {
  233. should("abc".times(0) == "");
  234. }
  235. it should_work_with_identity {
  236. should("abc".times(1) == "abc");
  237. }
  238. it should_work_as_expected {
  239. should("abc".times(3) == "abcabcabc");
  240. }
  241. }
  242. describe format {
  243. it should_work_on_empty_strings {
  244. should("".format() == "");
  245. }
  246. it should_work_on_empty_specifier {
  247. should("{:}".format(3) == "3");
  248. should("{0:}".format(3) == "3");
  249. should("{1:}".format(3,4) == "4");
  250. should("{2:}".format(3,4,5) == "5");
  251. }
  252. it should_work_on_d_specifier {
  253. should("a{d}b".format(4) == "a4b");
  254. should("a{D}b".format(4) == "a4b");
  255. }
  256. it should_work_on_x_specifier {
  257. should("a{x}b".format(10) == "aab");
  258. }
  259. it should_work_with_d_specifier_with_width {
  260. should("a{d8}b".format(4) == "a00000004b");
  261. }
  262. it should_work_with_x_specifier_with_width {
  263. should("a{x8}b".format(10) == "a0000000ab");
  264. }
  265. it should_work_on_X_specifier {
  266. should("a{X8}b".format(10) == "a0000000Ab");
  267. }
  268. it should_work_when_specifier_is_at_beginning {
  269. should("{d}xxx".format(4) == "4xxx");
  270. should("{x}xxx".format(10) == "axxx");
  271. should("{X}xxx".format(10) == "Axxx");
  272. }
  273. it should_work_when_specifier_is_at_end {
  274. should("xxx{d}".format(4) == "xxx4");
  275. should("xxx{x}".format(10) == "xxxa");
  276. should("xxx{X}".format(10) == "xxxA");
  277. }
  278. it should_work_when_specifier_is_alone {
  279. should("{d}".format(4) == "4");
  280. should("{x}".format(10) == "a");
  281. should("{X}".format(10) == "A");
  282. }
  283. it should_work_with_two_specifiers_in_a_row {
  284. should("{d}{d}".format(4,5) == "45");
  285. should("{x8}{x8}".format(10,11) == "0000000a0000000b");
  286. should("{x8}{X8}".format(10,11) == "0000000a0000000B");
  287. should("{x}{d}".format(10,4) == "a4");
  288. should("{X}{d}".format(10,4) == "A4");
  289. }
  290. it should_work_with_empty_specifier {
  291. should("{}".format("hello") == "hello");
  292. should("aaa{}bbb{}ccc".format(1,"f") == "aaa1bbbfccc");
  293. }
  294. it should_work_with_position_specifier {
  295. should("{1}{0}".format('a', 'b') == "ba");
  296. should("aaa{0}aaa{1}bbb{0}bbb{1}ccc".format('a', 'b') == "aaaaaaabbbbabbbbccc");
  297. }
  298. it should_fail_with_position_out_of_bounds {
  299. shouldThrow("Invalid Format String");
  300. format("{0}{1}{2}",1,2);
  301. }
  302. it should_work_with_zero_placeholder {
  303. should("{0:00.0000}".format(1500.42) == "1500.4200");
  304. should("{0:00.0000}".format(3.42) == "03.4200");
  305. should("{0:00.0000}".format(3.42555) == "03.42555");
  306. should("{0:00.0000}".format(1234.42555) == "1234.42555");
  307. }
  308. it should_not_require_a_index_with_a_zero_placeholder {
  309. should("{00.0000}".format(1500.42) == "1500.4200");
  310. should("{00.0000}".format(3.42) == "03.4200");
  311. should("{00.0000}".format(3.42555) == "03.42555");
  312. should("{00.0000}".format(1234.42555) == "1234.42555");
  313. }
  314. it should_work_with_currency_specifier {
  315. should("{c}".format(1500.42) == (new Currency(150042,2)).toString());
  316. }
  317. }
  318. describe uppercase {
  319. it should_work_on_empty_strings {
  320. should("".uppercase() == "");
  321. }
  322. it should_work_as_expected {
  323. string foo = "abc123dEFg";
  324. should(foo.uppercase() == "ABC123DEFG");
  325. should(foo == "abc123dEFg");
  326. should("123".uppercase() == "123");
  327. }
  328. }
  329. describe lowercase {
  330. it should_work_on_empty_strings {
  331. should("".lowercase() == "");
  332. }
  333. it should_work_as_expected {
  334. string foo = "aBC123dEFg";
  335. should(foo.lowercase() == "abc123defg");
  336. should(foo == "aBC123dEFg");
  337. should("123".uppercase() == "123");
  338. }
  339. }
  340. describe charAt {
  341. it should_fail_on_empty_strings {
  342. string foo = "".charAt(0);
  343. should(foo is null);
  344. }
  345. it should_work_for_normal_strings {
  346. string foo = "abc";
  347. should(foo.charAt(0) == "a");
  348. should(foo.charAt(1) == "b");
  349. should(foo.charAt(2) == "c");
  350. should(foo.charAt(3) is null);
  351. }
  352. it should_account_for_combining_marks {
  353. string foo = "he\u0364llo";
  354. should(foo.charAt(0) == "h");
  355. should(foo.charAt(1) == "e\u0364");
  356. should(foo.charAt(2) == "l");
  357. should(foo.charAt(3) == "l");
  358. should(foo.charAt(4) == "o");
  359. should(foo.charAt(5) is null);
  360. }
  361. }
  362. describe insertAt {
  363. it should_work_on_empty_strings {
  364. string foo = "";
  365. string f2 = foo.insertAt("abc", 0);
  366. should(foo == "");
  367. should(f2 == "abc");
  368. }
  369. it should_fail_when_index_is_out_of_bounds {
  370. string foo = "abc";
  371. string f2 = foo.insertAt("def", 4);
  372. should(foo == "abc");
  373. should(f2 is null);
  374. }
  375. it should_work_when_index_is_zero {
  376. string foo = "abc";
  377. string f2 = foo.insertAt("def", 0);
  378. should(foo == "abc");
  379. should(f2 == "defabc");
  380. }
  381. it should_work_when_index_is_utflen {
  382. string foo = "abc";
  383. string f2 = foo.insertAt("def", foo.utflen());
  384. should(foo == "abc");
  385. should(f2 == "abcdef");
  386. }
  387. it should_work_when_index_is_within_string {
  388. string foo = "abc";
  389. string f2 = foo.insertAt("def", 1);
  390. string f3 = foo.insertAt("def", 2);
  391. should(foo == "abc");
  392. should(f2 == "adefbc");
  393. should(f3 == "abdefc");
  394. }
  395. it should_account_for_combining_marks {
  396. string foo = "he\u0364llo";
  397. string f1 = foo.insertAt("def", 0);
  398. string f2 = foo.insertAt("def", 1);
  399. string f3 = foo.insertAt("def", 2);
  400. string f4 = foo.insertAt("def", 3);
  401. string f5 = foo.insertAt("def", 4);
  402. string f6 = foo.insertAt("def", 5);
  403. string f7 = foo.insertAt("def", 6);
  404. should(foo == "he\u0364llo");
  405. should(f1 == "defhe\u0364llo");
  406. should(f2 == "hdefe\u0364llo");
  407. should(f3 == "he\u0364defllo");
  408. should(f4 == "he\u0364ldeflo");
  409. should(f5 == "he\u0364lldefo");
  410. should(f6 == "he\u0364llodef");
  411. should(f7 is null);
  412. }
  413. }
  414. describe utflen {
  415. it should_work_on_empty_strings {
  416. should("".utflen() == 0);
  417. }
  418. it should_work_on_normal_strings {
  419. should("abc".utflen() == 3);
  420. }
  421. it should_account_for_combining_marks {
  422. string foo = "hello\u0364world";
  423. should(foo.utflen() == 10);
  424. }
  425. }
  426. }