|
| 1 | +import Core from "../lib/core"; |
| 2 | +const Patterns = Core.Patterns; |
| 3 | +const SpecialForms = Core.SpecialForms; |
| 4 | +const Tuple = Core.Tuple; |
| 5 | + |
| 6 | +import Enum from "../lib/enum"; |
| 7 | + |
| 8 | +import chai from 'chai'; |
| 9 | +var expect = chai.expect; |
| 10 | + |
| 11 | +const $ = Patterns.variable(); |
| 12 | + |
| 13 | +function map_fetch(map, key){ |
| 14 | + if(key in map){ |
| 15 | + return new Tuple(Symbol.for('ok'), map[key]); |
| 16 | + } |
| 17 | + |
| 18 | + return Symbol.for('error'); |
| 19 | +} |
| 20 | + |
| 21 | +describe('with', () => { |
| 22 | + |
| 23 | + it('normal', () => { |
| 24 | + /* |
| 25 | + opts = %{width: 10, height: 15} |
| 26 | +
|
| 27 | + with {:ok, width} <- Map.fetch(opts, :width), |
| 28 | + {:ok, height} <- Map.fetch(opts, :height), |
| 29 | + do: {:ok, width * height} |
| 30 | +
|
| 31 | + {:ok, 150} |
| 32 | + */ |
| 33 | + |
| 34 | + let opts = { width: 10, height: 15 }; |
| 35 | + |
| 36 | + let value = SpecialForms._with( |
| 37 | + [new Tuple(Symbol.for('ok'), $), () => map_fetch(opts, "width")], |
| 38 | + [new Tuple(Symbol.for('ok'), $), (width) => map_fetch(opts, "height")], |
| 39 | + (width, height) => new Tuple(Symbol.for('ok'), width * height) |
| 40 | + ); |
| 41 | + |
| 42 | + expect(value).to.eql(new Tuple(Symbol.for('ok'), 150)); |
| 43 | + }); |
| 44 | + |
| 45 | + |
| 46 | + it('without match', () => { |
| 47 | + /* |
| 48 | + opts = %{width: 10} |
| 49 | +
|
| 50 | + with {:ok, width} <- Map.fetch(opts, :width), |
| 51 | + {:ok, height} <- Map.fetch(opts, :height), |
| 52 | + do: {:ok, width * height} |
| 53 | +
|
| 54 | + :error |
| 55 | + */ |
| 56 | + |
| 57 | + let opts = { width: 10 }; |
| 58 | + |
| 59 | + let value = SpecialForms._with( |
| 60 | + [new Tuple(Symbol.for('ok'), $), () => map_fetch(opts, "width")], |
| 61 | + [new Tuple(Symbol.for('ok'), $), (width) => map_fetch(opts, "height")], |
| 62 | + (width, height) => new Tuple(Symbol.for('ok'), width * height) |
| 63 | + ); |
| 64 | + |
| 65 | + expect(value).to.eql(Symbol.for('error')); |
| 66 | + }); |
| 67 | + |
| 68 | + |
| 69 | + it('bare expression', () => { |
| 70 | + /* |
| 71 | + opts = %{width: 10} |
| 72 | +
|
| 73 | + with {:ok, width} <- Map.fetch(opts, :width), |
| 74 | + double_width = width * 2, |
| 75 | + {:ok, height} <- Map.fetch(opts, :height), |
| 76 | + do: {:ok, double_width * height} |
| 77 | +
|
| 78 | + {:ok, 300} |
| 79 | + */ |
| 80 | + |
| 81 | + let opts = { width: 10, height: 15 }; |
| 82 | + |
| 83 | + let value = SpecialForms._with( |
| 84 | + [new Tuple(Symbol.for('ok'), $), () => map_fetch(opts, "width")], |
| 85 | + [$, (width) => width * 2], |
| 86 | + [new Tuple(Symbol.for('ok'), $), (width, double_width) => map_fetch(opts, "height")], |
| 87 | + (width, double_width, height) => new Tuple(Symbol.for('ok'), double_width * height) |
| 88 | + ); |
| 89 | + |
| 90 | + expect(value).to.eql(new Tuple(Symbol.for('ok'), 300)); |
| 91 | + }); |
| 92 | + |
| 93 | +}); |
| 94 | + |
0 commit comments