PageRenderTime 51ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/wu/wu-tests.ts

https://gitlab.com/CatchLabs/DefinitelyTyped
TypeScript | 411 lines | 380 code | 22 blank | 9 comment | 12 complexity | 5416dc7daa300347ad9a45503796dc53 MD5 | raw file
  1. // adapted from `cat wu.js/test/* |sed '/= require/d'> wu-tests.ts`
  2. ///<reference path="wu.d.ts" />
  3. declare var describe: any, it: any, mocha: any, assert: {
  4. iterable:any;
  5. eqSet<T>(expected:Set<T>, actual: Iterable<T>): any;
  6. ok:any;
  7. equal<T>(x:T, y:T): any;
  8. eqArray<T>(x:T[], y:Iterable<T>): any;
  9. deepEqual<T>(x:T, y:T): any;
  10. }
  11. // Helper for asserting that the given thing is iterable.
  12. assert.iterable = thing => {
  13. assert.ok(wu(thing));
  14. };
  15. // Helper for asserting that all the elements yielded from the |actual|
  16. // iterator are in the |expected| set.
  17. assert.eqSet = (expected, actual) => {
  18. assert.iterable(actual);
  19. for (var x of actual) {
  20. assert.ok(expected.has(x));
  21. expected.delete(x);
  22. }
  23. };
  24. // Helper for asserting that all the elements yielded from the |actual|
  25. // iterator are equal to and in the same order as the elements of the
  26. // |expected| array.
  27. assert.eqArray = (expected, actual) => {
  28. assert.iterable(actual);
  29. assert.deepEqual(expected, [...actual]);
  30. };
  31. mocha.setup('bdd');
  32. describe("wu.asyncEach", () => {
  33. it("should iterate over each item", () => {
  34. const arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
  35. let n = 0;
  36. return wu(arr)
  37. .asyncEach(x => {
  38. n++;
  39. const start = Date.now();
  40. while (Date.now() - start <= 3) {
  41. // Kill time.
  42. }
  43. }, 3)
  44. .then(() => {
  45. assert.equal(n, arr.length);
  46. });
  47. });
  48. });
  49. describe("wu.chain", () => {
  50. it("should concatenate iterables", () => {
  51. assert.eqArray([1, 2, 3, 4, 5, 6],
  52. wu.chain([1, 2], [3, 4], [5, 6]));
  53. });
  54. });
  55. describe("wu.chunk", () => {
  56. it("should chunk items into tuples", () => {
  57. assert.eqArray([[1,2,3], [4,5,6]],
  58. wu.chunk(3, [1,2,3,4,5,6]));
  59. });
  60. });
  61. describe("wu.concatMap", () => {
  62. it("should map the function over the iterable and concatenate results", () => {
  63. assert.eqArray([1, 1, 2, 4, 3, 9],
  64. wu.concatMap(x => [x, x * x], [1, 2, 3]));
  65. });
  66. });
  67. describe("wu.count", () => {
  68. it("should keep incrementing", () => {
  69. const count = wu.count();
  70. assert.equal(count.next().value, 0);
  71. assert.equal(count.next().value, 1);
  72. assert.equal(count.next().value, 2);
  73. assert.equal(count.next().value, 3);
  74. assert.equal(count.next().value, 4);
  75. assert.equal(count.next().value, 5);
  76. });
  77. it("should start at the provided number", () => {
  78. const count = wu.count(5);
  79. assert.equal(count.next().value, 5);
  80. assert.equal(count.next().value, 6);
  81. assert.equal(count.next().value, 7);
  82. });
  83. it("should increment by the provided step", () => {
  84. const count = wu.count(0, 2);
  85. assert.equal(count.next().value, 0);
  86. assert.equal(count.next().value, 2);
  87. assert.equal(count.next().value, 4);
  88. });
  89. });
  90. describe("wu.curryable", () => {
  91. it("should wait until its given enough arguments", () => {
  92. var f = wu.curryable((a, b) => a + b);
  93. var f0 = f()()()()();
  94. assert.equal(typeof f0, "function");
  95. var f1 = f(1);
  96. assert.equal(typeof f1, "function");
  97. assert.equal(f1(2), 3);
  98. });
  99. it("should just call the function when given enough arguments", () => {
  100. var f = wu.curryable((a, b) => a + b);
  101. assert.equal(f(1, 2), 3);
  102. });
  103. it("should expect the number of arguments we tell it to", () => {
  104. var f = wu.curryable((...args) => 5, 5);
  105. assert.equal(typeof f(1, 2, 3, 4), "function");
  106. assert.equal(f(1, 2, 3, 4, 5), 5);
  107. });
  108. });
  109. describe("wu.cycle", () => {
  110. it("should keep yielding items from the original iterable", () => {
  111. let i = 0;
  112. const arr = [1, 2, 3];
  113. for (let x of wu.cycle(arr)) {
  114. assert.equal(x, arr[i % 3]);
  115. if (i++ > 9) {
  116. break;
  117. }
  118. }
  119. });
  120. });
  121. describe("wu.drop", () => {
  122. it("should drop the number of items specified", () => {
  123. const count = wu.count().drop(5);
  124. assert.equal(count.next().value, 5);
  125. });
  126. });
  127. describe("wu.dropWhile", () => {
  128. it("should drop items while the predicate is true", () => {
  129. const count = wu.dropWhile(x => x < 5, wu.count());
  130. assert.equal(count.next().value, 5);
  131. });
  132. });
  133. describe("wu.entries", () => {
  134. it("should iterate over entries", () => {
  135. const expected = new Map([["foo", 1], ["bar", 2], ["baz", 3]]);
  136. for (let [k, v] of wu.entries({ foo: 1, bar: 2, baz: 3 })) {
  137. assert.equal(expected.get(k), v);
  138. }
  139. });
  140. });
  141. describe("wu.enumerate", () => {
  142. it("should yield items with their index", () => {
  143. assert.eqArray([["a", 0], ["b", 1], ["c", 2]],
  144. wu.enumerate("abc"));
  145. });
  146. });
  147. describe("wu.every", () => {
  148. it("should return true when the predicate succeeds for all items", () => {
  149. assert.equal(true, wu.every(x => typeof x === "number", [1, 2, 3]));
  150. });
  151. it("should return false when the predicate fails for any item", () => {
  152. assert.equal(false, wu.every(x => typeof x === "number", [1, 2, "3"]));
  153. });
  154. });
  155. describe("wu.filter", () => {
  156. it("should filter based on the predicate", () => {
  157. assert.eqArray(["a", "b", "c"],
  158. wu.filter(x => typeof x === "string",
  159. [1, "a", true, "b", {}, "c"]));
  160. });
  161. });
  162. describe("wu.find", () => {
  163. it("should return the first item that matches the predicate", () => {
  164. assert.deepEqual({ name: "rza" },
  165. wu.find(x => !!x.name.match(/.za$/),
  166. [{ name: "odb" },
  167. { name: "method man" },
  168. { name: "rza" },
  169. { name: "gza" }]));
  170. });
  171. it("should return undefined if no items match the predicate", () => {
  172. assert.equal(undefined,
  173. wu.find(x => (<any>x) === "raekwon",
  174. [{ name: "odb" },
  175. { name: "method man" },
  176. { name: "rza" },
  177. { name: "gza" }]));
  178. });
  179. });
  180. describe("wu.flatten", () => {
  181. it("should flatten iterables", () => {
  182. assert.eqArray(["I", "like", "LISP"],
  183. wu(["I", ["like", ["LISP"]]]).flatten());
  184. });
  185. it("should shallowly flatten iterables", () => {
  186. assert.eqArray([1, 2, 3, [[4]]],
  187. wu.flatten(true, [1, [2], [3, [[4]]]]));
  188. });
  189. });
  190. describe("wu.forEach", () => {
  191. it("should iterate over every item", () => {
  192. const items = [];
  193. wu.forEach(x => items.push(x), [1,2,3]);
  194. assert.eqArray([1,2,3], items);
  195. });
  196. });
  197. describe("wu.has", () => {
  198. it("should return true if the item is in the iterable", () => {
  199. assert.ok(wu.has(3, [1,2,3]));
  200. });
  201. it("should return false if the item is not in the iterable", () => {
  202. assert.ok(!wu.has(<any>"36 chambers", [1,2,3]));
  203. });
  204. });
  205. describe("wu.invoke", () => {
  206. it("should yield the method invokation on each item", () => {
  207. function Greeter(name) {
  208. this.name = name
  209. }
  210. Greeter.prototype.greet = function (tail) {
  211. return "hello " + this.name + tail;
  212. };
  213. assert.eqArray(["hello world!", "hello test!"],
  214. wu.invoke("greet", "!",
  215. [new Greeter("world"), new Greeter("test")]));
  216. });
  217. });
  218. describe("wu.keys", () => {
  219. it("should iterate over keys", () => {
  220. assert.eqSet(new Set(["foo", "bar", "baz"]),
  221. wu.keys({ foo: 1, bar: 2, baz: 3 }));
  222. });
  223. });
  224. describe("wu.map", () => {
  225. it("should map the function over the iterable", () => {
  226. assert.eqArray([1, 4, 9],
  227. wu.map(x => x * x, [1, 2, 3]));
  228. });
  229. });
  230. describe("wu.pluck", () => {
  231. it("should access the named property of each item in the iterable", () => {
  232. assert.eqArray([1, 2, 3],
  233. wu.pluck("i", [{ i: 1 }, { i: 2 }, { i: 3 }]));
  234. });
  235. });
  236. describe("wu.reduce", () => {
  237. it("should reduce the iterable with the function", () => {
  238. assert.equal(6, wu([1,2,3]).reduce((x, y) => x + y));
  239. });
  240. it("should accept an initial state for the reducer function", () => {
  241. assert.equal(16, wu.reduce((x, y) => x + y, 10, [1,2,3]));
  242. });
  243. });
  244. describe("wu.reductions", () => {
  245. it("should yield the intermediate reductions of the iterable", () => {
  246. assert.eqArray([1, 3, 6],
  247. wu.reductions((x, y) => x + y, undefined, [1, 2, 3]));
  248. });
  249. });
  250. describe("wu.reject", () => {
  251. it("should yield items for which the predicate is false", () => {
  252. assert.eqArray([1, true, {}],
  253. wu.reject(x => typeof x === "string",
  254. [1, "a", true, "b", {}, "c"]));
  255. });
  256. });
  257. describe("wu.repeat", () => {
  258. it("should keep yielding its item", () => {
  259. const repeat = wu.repeat(3);
  260. assert.equal(repeat.next().value, 3);
  261. assert.equal(repeat.next().value, 3);
  262. assert.equal(repeat.next().value, 3);
  263. assert.equal(repeat.next().value, 3);
  264. assert.equal(repeat.next().value, 3);
  265. assert.equal(repeat.next().value, 3);
  266. assert.equal(repeat.next().value, 3);
  267. });
  268. it("should repeat n times", () => {
  269. const repeat = wu.repeat(3, 2);
  270. assert.equal(repeat.next().value, 3);
  271. assert.equal(repeat.next().value, 3);
  272. assert.equal(repeat.next().value, undefined);
  273. assert.equal(repeat.next().done, true);
  274. });
  275. });
  276. describe("wu.slice", () => {
  277. it("should slice the front of iterables", () => {
  278. assert.eqArray([3, 4, 5],
  279. wu.slice(3, undefined, [0, 1, 2, 3, 4, 5]));
  280. });
  281. it("should slice the end of iterables", () => {
  282. assert.eqArray([0, 1, 2],
  283. wu.slice(undefined,
  284. 3,
  285. [0, 1, 2, 3, 4, 5]));
  286. });
  287. });
  288. describe("wu.some", () => {
  289. it("should return true if any item matches the predicate", () => {
  290. assert.ok(wu.some(x => x % 2 === 0, [1,2,3]));
  291. });
  292. it("should return false if no items match the predicate", () => {
  293. assert.ok(!wu.some(x => x % 5 === 0, [1,2,3]));
  294. });
  295. });
  296. describe("wu.spreadMap", () => {
  297. it("should map the function over the iterable with spread arguments", () => {
  298. assert.eqArray([32, 9, 1000],
  299. wu.spreadMap(Math.pow, [[2, 5], [3, 2], [10, 3]]));
  300. });
  301. });
  302. describe("wu.take", () => {
  303. it("should yield as many items as requested", () => {
  304. assert.eqArray([0, 1, 2, 3, 4],
  305. wu.take(5, wu.count()));
  306. });
  307. });
  308. describe("wu.takeWhile", () => {
  309. it("should keep yielding items from the iterable until the predicate is false", () => {
  310. assert.eqArray([0, 1, 2, 3, 4],
  311. wu.takeWhile(x => x < 5, wu.count()));
  312. });
  313. });
  314. describe("wu.tap", () => {
  315. it("should perform side effects and yield the original item", () => {
  316. let i = 0;
  317. assert.eqArray([1, 2, 3],
  318. wu.tap(x => i++, [1, 2, 3]));
  319. assert.equal(i, 3);
  320. });
  321. });
  322. describe("wu.tee", () => {
  323. it("should clone iterables", () => {
  324. const factorials = wu(wu.count(1)).reductions((a, b) => a * b);
  325. const [i1, i2] = wu(factorials).tee();
  326. assert.equal(i1.next().value, 1);
  327. assert.equal(i1.next().value, 2);
  328. assert.equal(i1.next().value, 6);
  329. assert.equal(i1.next().value, 24);
  330. assert.equal(i2.next().value, 1);
  331. assert.equal(i2.next().value, 2);
  332. assert.equal(i2.next().value, 6);
  333. assert.equal(i2.next().value, 24);
  334. });
  335. });
  336. describe("wu.unique", () => {
  337. it("should yield only the unique items from the iterable", () => {
  338. assert.eqArray([1, 2, 3],
  339. wu.unique([1,1,2,2,1,1,3,3]));
  340. });
  341. });
  342. describe("wu.unzip", () => {
  343. it("should create iterables from zipped items", () => {
  344. const pairs = [
  345. ["one", 1],
  346. ["two", 2],
  347. ["three", 3]
  348. ];
  349. const [i1, i2] = wu(pairs).unzip();
  350. assert.eqArray(["one", "two", "three"], [...i1]);
  351. assert.eqArray([1, 2, 3], [...i2]);
  352. });
  353. });
  354. describe("wu.values", () => {
  355. it("should iterate over values", () => {
  356. assert.eqSet(new Set([1, 2, 3]),
  357. wu.values({ foo: 1, bar: 2, baz: 3 }));
  358. });
  359. });
  360. describe("wu.zip", () => {
  361. it("should zip two iterables together", () => {
  362. assert.eqArray([["a", 1], ["b", 2], ["c", 3]],
  363. wu.zip("abc", [1, 2, 3]));
  364. });
  365. it("should stop with the shorter iterable", () => {
  366. assert.eqArray([["a", 1], ["b", 2], ["c", 3]],
  367. wu.zip("abc", wu.count(1)));
  368. });
  369. });
  370. describe("wu.zipLongest", () => {
  371. it("should stop with the longer iterable", () => {
  372. const arr1 = [];
  373. arr1[1] = 2;
  374. const arr2 = [];
  375. arr2[1] = 3;
  376. assert.eqArray([["a", 1], arr1, arr2],
  377. wu.zipLongest("a", [1, 2, 3]));
  378. });
  379. });
  380. describe("wu.zipWith", () => {
  381. it("should spread map over the zipped iterables", () => {
  382. const add3 = (a, b, c) => a + b + c;
  383. assert.eqArray([12, 15, 18],
  384. wu.zipWith(add3,
  385. [1, 2, 3],
  386. [4, 5, 6],
  387. [7, 8, 9]));
  388. });
  389. });