test-functional.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. "use strict";
  5. const { setTimeout } = require('sdk/timers');
  6. const utils = require('sdk/lang/functional');
  7. const { invoke, defer, partial, compose, memoize, once, is, isnt,
  8. delay, wrap, curry, chainable, field, query, isInstance } = utils;
  9. const { LoaderWithHookedConsole } = require('sdk/test/loader');
  10. exports['test forwardApply'] = function(assert) {
  11. function sum(b, c) { return this.a + b + c; }
  12. assert.equal(invoke(sum, [2, 3], { a: 1 }), 6,
  13. 'passed arguments and pseoude-variable are used');
  14. assert.equal(invoke(sum.bind({ a: 2 }), [2, 3], { a: 1 }), 7,
  15. 'bounded `this` pseoudo variable is used');
  16. };
  17. exports['test deferred function'] = function(assert, done) {
  18. let nextTurn = false;
  19. function sum(b, c) {
  20. assert.ok(nextTurn, 'enqueued is called in next turn of event loop');
  21. assert.equal(this.a + b + c, 6,
  22. 'passed arguments an pseoude-variable are used');
  23. done();
  24. }
  25. let fixture = { a: 1, method: defer(sum) };
  26. fixture.method(2, 3);
  27. nextTurn = true;
  28. };
  29. exports['test partial function'] = function(assert) {
  30. function sum(b, c) { return this.a + b + c; }
  31. let foo = { a : 5 };
  32. foo.sum7 = partial(sum, 7);
  33. foo.sum8and4 = partial(sum, 8, 4);
  34. assert.equal(foo.sum7(2), 14, 'partial one arguments works');
  35. assert.equal(foo.sum8and4(), 17, 'partial both arguments works');
  36. };
  37. exports["test curry defined numeber of arguments"] = function(assert) {
  38. var sum = curry(function(a, b, c) {
  39. return a + b + c;
  40. });
  41. assert.equal(sum(2, 2, 1), 5, "sum(2, 2, 1) => 5");
  42. assert.equal(sum(2, 4)(1), 7, "sum(2, 4)(1) => 7");
  43. assert.equal(sum(2)(4, 2), 8, "sum(2)(4, 2) => 8");
  44. assert.equal(sum(2)(4)(3), 9, "sum(2)(4)(3) => 9");
  45. };
  46. exports['test compose'] = function(assert) {
  47. let greet = function(name) { return 'hi: ' + name; };
  48. let exclaim = function(sentence) { return sentence + '!'; };
  49. assert.equal(compose(exclaim, greet)('moe'), 'hi: moe!',
  50. 'can compose a function that takes another');
  51. assert.equal(compose(greet, exclaim)('moe'), 'hi: moe!',
  52. 'in this case, the functions are also commutative');
  53. let target = {
  54. name: 'Joe',
  55. greet: compose(function exclaim(sentence) {
  56. return sentence + '!';
  57. }, function(title) {
  58. return 'hi : ' + title + ' ' + this.name;
  59. })
  60. };
  61. assert.equal(target.greet('Mr'), 'hi : Mr Joe!',
  62. 'this can be passed in');
  63. assert.equal(target.greet.call({ name: 'Alex' }, 'Dr'), 'hi : Dr Alex!',
  64. 'this can be applied');
  65. let single = compose(function(value) {
  66. return value + ':suffix';
  67. });
  68. assert.equal(single('text'), 'text:suffix', 'works with single function');
  69. let identity = compose();
  70. assert.equal(identity('bla'), 'bla', 'works with zero functions');
  71. };
  72. exports['test wrap'] = function(assert) {
  73. let greet = function(name) { return 'hi: ' + name; };
  74. let backwards = wrap(greet, function(f, name) {
  75. return f(name) + ' ' + name.split('').reverse().join('');
  76. });
  77. assert.equal(backwards('moe'), 'hi: moe eom',
  78. 'wrapped the saluation function');
  79. let inner = function () { return 'Hello '; };
  80. let target = {
  81. name: 'Matteo',
  82. hi: wrap(inner, function(f) { return f() + this.name; })
  83. };
  84. assert.equal(target.hi(), 'Hello Matteo', 'works with this');
  85. function noop() { }
  86. let wrapped = wrap(noop, function(f) {
  87. return Array.slice(arguments);
  88. });
  89. let actual = wrapped([ 'whats', 'your' ], 'vector', 'victor');
  90. assert.deepEqual(actual, [ noop, ['whats', 'your'], 'vector', 'victor' ],
  91. 'works with fancy stuff');
  92. };
  93. exports['test memoize'] = function(assert) {
  94. const fib = n => n < 2 ? n : fib(n - 1) + fib(n - 2);
  95. let fibnitro = memoize(fib);
  96. assert.equal(fib(10), 55,
  97. 'a memoized version of fibonacci produces identical results');
  98. assert.equal(fibnitro(10), 55,
  99. 'a memoized version of fibonacci produces identical results');
  100. function o(key, value) { return value; }
  101. let oo = memoize(o), v1 = {}, v2 = {};
  102. assert.equal(oo(1, v1), v1, 'returns value back');
  103. assert.equal(oo(1, v2), v1, 'memoized by a first argument');
  104. assert.equal(oo(2, v2), v2, 'returns back value if not memoized');
  105. assert.equal(oo(2), v2, 'memoized new value');
  106. assert.notEqual(oo(1), oo(2), 'values do not override');
  107. assert.equal(o(3, v2), oo(2, 3), 'returns same value as un-memoized');
  108. let get = memoize(function(attribute) { return this[attribute]; });
  109. let target = { name: 'Bob', get: get };
  110. assert.equal(target.get('name'), 'Bob', 'has correct `this`');
  111. assert.equal(target.get.call({ name: 'Jack' }, 'name'), 'Bob',
  112. 'name is memoized');
  113. assert.equal(get('name'), 'Bob', 'once memoized can be called without this');
  114. };
  115. exports['test delay'] = function(assert, done) {
  116. let delayed = false;
  117. delay(function() {
  118. assert.ok(delayed, 'delayed the function');
  119. done();
  120. }, 1);
  121. delayed = true;
  122. };
  123. exports['test delay with this'] = function(assert, done) {
  124. let context = {};
  125. delay.call(context, function(name) {
  126. assert.equal(this, context, 'this was passed in');
  127. assert.equal(name, 'Tom', 'argument was passed in');
  128. done();
  129. }, 10, 'Tom');
  130. };
  131. exports['test once'] = function(assert) {
  132. let n = 0;
  133. let increment = once(function() { n ++; });
  134. increment();
  135. increment();
  136. assert.equal(n, 1, 'only incremented once');
  137. let target = {
  138. state: 0,
  139. update: once(function() {
  140. return this.state ++;
  141. })
  142. };
  143. target.update();
  144. target.update();
  145. assert.equal(target.state, 1, 'this was passed in and called only once');
  146. };
  147. exports['test once with argument'] = function(assert) {
  148. let n = 0;
  149. let increment = once(a => n++);
  150. increment();
  151. increment('foo');
  152. assert.equal(n, 1, 'only incremented once');
  153. increment();
  154. increment('foo');
  155. assert.equal(n, 1, 'only incremented once');
  156. };
  157. exports['test complement'] = assert => {
  158. let { complement } = require("sdk/lang/functional");
  159. let isOdd = x => Boolean(x % 2);
  160. assert.equal(isOdd(1), true);
  161. assert.equal(isOdd(2), false);
  162. let isEven = complement(isOdd);
  163. assert.equal(isEven(1), false);
  164. assert.equal(isEven(2), true);
  165. let foo = {};
  166. let isFoo = function() { return this === foo; };
  167. let insntFoo = complement(isFoo);
  168. assert.equal(insntFoo.call(foo), false);
  169. assert.equal(insntFoo.call({}), true);
  170. };
  171. exports['test constant'] = assert => {
  172. let { constant } = require("sdk/lang/functional");
  173. let one = constant(1);
  174. assert.equal(one(1), 1);
  175. assert.equal(one(2), 1);
  176. };
  177. exports['test apply'] = assert => {
  178. let { apply } = require("sdk/lang/functional");
  179. let dashify = (...args) => args.join("-");
  180. assert.equal(apply(dashify, 1, [2, 3]), "1-2-3");
  181. assert.equal(apply(dashify, "a"), "a");
  182. assert.equal(apply(dashify, ["a", "b"]), "a-b");
  183. assert.equal(apply(dashify, ["a", "b"], "c"), "a,b-c");
  184. assert.equal(apply(dashify, [1, 2], [3, 4]), "1,2-3-4");
  185. };
  186. exports['test flip'] = assert => {
  187. let { flip } = require("sdk/lang/functional");
  188. let append = (left, right) => left + " " + right;
  189. let prepend = flip(append);
  190. assert.equal(append("hello", "world"), "hello world");
  191. assert.equal(prepend("hello", "world"), "world hello");
  192. let wrap = function(left, right) {
  193. return left + " " + this + " " + right;
  194. };
  195. let invertWrap = flip(wrap);
  196. assert.equal(wrap.call("@", "hello", "world"), "hello @ world");
  197. assert.equal(invertWrap.call("@", "hello", "world"), "world @ hello");
  198. let reverse = flip((...args) => args);
  199. assert.deepEqual(reverse(1, 2, 3, 4), [4, 3, 2, 1]);
  200. assert.deepEqual(reverse(1), [1]);
  201. assert.deepEqual(reverse(), []);
  202. // currying still works
  203. let prependr = curry(prepend);
  204. assert.equal(prependr("hello", "world"), "world hello");
  205. assert.equal(prependr("hello")("world"), "world hello");
  206. };
  207. exports["test when"] = assert => {
  208. let { when } = require("sdk/lang/functional");
  209. let areNums = (...xs) => xs.every(x => typeof(x) === "number");
  210. let sum = when(areNums, (...xs) => xs.reduce((y, x) => x + y, 0));
  211. assert.equal(sum(1, 2, 3), 6);
  212. assert.equal(sum(1, 2, "3"), undefined);
  213. let multiply = when(areNums,
  214. (...xs) => xs.reduce((y, x) => x * y, 1),
  215. (...xs) => xs);
  216. assert.equal(multiply(2), 2);
  217. assert.equal(multiply(2, 3), 6);
  218. assert.deepEqual(multiply(2, "4"), [2, "4"]);
  219. function Point(x, y) {
  220. this.x = x;
  221. this.y = y;
  222. }
  223. let isPoint = x => x instanceof Point;
  224. let inc = when(isPoint, ({x, y}) => new Point(x + 1, y + 1));
  225. assert.equal(inc({}), undefined);
  226. assert.deepEqual(inc(new Point(0, 0)), { x: 1, y: 1 });
  227. let axis = when(isPoint,
  228. ({ x, y }) => [x, y],
  229. _ => [0, 0]);
  230. assert.deepEqual(axis(new Point(1, 4)), [1, 4]);
  231. assert.deepEqual(axis({ foo: "bar" }), [0, 0]);
  232. };
  233. exports["test chainable"] = function(assert) {
  234. let Player = function () { this.volume = 5; };
  235. Player.prototype = {
  236. setBand: chainable(function (band) { return (this.band = band); }),
  237. incVolume: chainable(function () { return this.volume++; })
  238. };
  239. let player = new Player();
  240. player
  241. .setBand('Animals As Leaders')
  242. .incVolume().incVolume().incVolume().incVolume().incVolume().incVolume();
  243. assert.equal(player.band, 'Animals As Leaders', 'passes arguments into chained');
  244. assert.equal(player.volume, 11, 'accepts no arguments in chain');
  245. };
  246. exports["test field"] = assert => {
  247. let Num = field("constructor", 0);
  248. assert.equal(Num.name, Number.name);
  249. assert.ok(typeof(Num), "function");
  250. let x = field("x");
  251. [
  252. [field("foo", { foo: 1 }), 1],
  253. [field("foo")({ foo: 1 }), 1],
  254. [field("bar", {}), undefined],
  255. [field("bar")({}), undefined],
  256. [field("hey", undefined), undefined],
  257. [field("hey")(undefined), undefined],
  258. [field("how", null), null],
  259. [field("how")(null), null],
  260. [x(1), undefined],
  261. [x(undefined), undefined],
  262. [x(null), null],
  263. [x({ x: 1 }), 1],
  264. [x({ x: 2 }), 2],
  265. ].forEach(([actual, expected]) => assert.equal(actual, expected));
  266. };
  267. exports["test query"] = assert => {
  268. let Num = query("constructor", 0);
  269. assert.equal(Num.name, Number.name);
  270. assert.ok(typeof(Num), "function");
  271. let x = query("x");
  272. let xy = query("x.y");
  273. [
  274. [query("foo", { foo: 1 }), 1],
  275. [query("foo")({ foo: 1 }), 1],
  276. [query("foo.bar", { foo: { bar: 2 } }), 2],
  277. [query("foo.bar")({ foo: { bar: 2 } }), 2],
  278. [query("foo.bar", { foo: 1 }), undefined],
  279. [query("foo.bar")({ foo: 1 }), undefined],
  280. [x(1), undefined],
  281. [x(undefined), undefined],
  282. [x(null), null],
  283. [x({ x: 1 }), 1],
  284. [x({ x: 2 }), 2],
  285. [xy(1), undefined],
  286. [xy(undefined), undefined],
  287. [xy(null), null],
  288. [xy({ x: 1 }), undefined],
  289. [xy({ x: 2 }), undefined],
  290. [xy({ x: { y: 1 } }), 1],
  291. [xy({ x: { y: 2 } }), 2]
  292. ].forEach(([actual, expected]) => assert.equal(actual, expected));
  293. };
  294. exports["test isInstance"] = assert => {
  295. function X() {}
  296. function Y() {}
  297. let isX = isInstance(X);
  298. [
  299. isInstance(X, new X()),
  300. isInstance(X)(new X()),
  301. !isInstance(X, new Y()),
  302. !isInstance(X)(new Y()),
  303. isX(new X()),
  304. !isX(new Y())
  305. ].forEach(x => assert.ok(x));
  306. };
  307. exports["test is"] = assert => {
  308. assert.deepEqual([ 1, 0, 1, 0, 1 ].map(is(1)),
  309. [ true, false, true, false, true ],
  310. "is can be partially applied");
  311. assert.ok(is(1, 1));
  312. assert.ok(!is({}, {}));
  313. assert.ok(is()(1)()(1), "is is curried");
  314. assert.ok(!is()(1)()(2));
  315. };
  316. exports["test isnt"] = assert => {
  317. assert.deepEqual([ 1, 0, 1, 0, 1 ].map(isnt(0)),
  318. [ true, false, true, false, true ],
  319. "is can be partially applied");
  320. assert.ok(!isnt(1, 1));
  321. assert.ok(isnt({}, {}));
  322. assert.ok(!isnt()(1)()(1));
  323. assert.ok(isnt()(1)()(2));
  324. };
  325. require('test').run(exports);