File: Func.ls

package info (click to toggle)
node-prelude-ls 1.1.2%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 208 kB
  • ctags: 27
  • sloc: makefile: 44; sh: 2
file content (71 lines) | stat: -rw-r--r-- 1,608 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
require! \sinon
{apply, curry, flip, fix, over, memoize} = require '..' .Func
{strict-equal: eq, not-strict-equal: not-eq, deep-equal: deep-eq, ok} = require 'assert'

suite 'apply' ->
  test 'empty list' ->
    f = -> 1
    eq 1, apply f, []

  test 'a couple of args' ->
    eq 5, apply (+), [2 3]

  test 'curried' ->
    f = apply (+)
    eq 5, f [2 3]

suite 'curry' ->
  test 'simple function' ->
    add = (x, y) -> x + y
    add-curried = curry add
    add-four = add-curried 4
    eq 6 add-four 2

suite 'flip' ->
  test 'minus op' ->
    eq 10, (flip (-)) 5 15

suite 'fix' ->
  test 'single arg' ->
    eq 89, (fix (fib) -> (n) ->
      | n <= 1      => 1
      | otherwise   => fib(n-1) + fib(n-2))(10)

  test 'multi-arg variation' ->
    eq 89, (fix (fib) -> (n, minus=0) ->
      | (n - minus) <= 1 => 1
      | otherwise        => fib(n, minus+1) + fib(n, minus+2))(10)

suite 'over' ->
  test 'basic' ->
    f = (==) `over` (-> it)
    ok f 2 2
    ok not f 2 3

  test 'with accessor function' ->
    same-length = (==) `over` (.length)
    ok same-length [1 2 3] [4 5 6]
    ok not same-length [1 2] [4 5 6]

suite 'memoize' ->
  spy = f = null

  setup ->
    spy := sinon.spy -> &
    f := memoize spy

  test 'only 1 call when using the same arguments' ->
    [0 to 10].for-each -> f!
    ok spy.called-once

  test 'call again when using different arguments' ->
    f \mung
    f \mung
    f '1,2'
    f [1 2]
    ok spy.called-thrice

  test 'that the correct values are returned' ->
    eq f(\mung), f(\mung)
    eq f(\mung \face), f(\mung \face)
    not-eq f(\mung), f(\mung \face)