-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(coffeescript test): add coffeescript test
- Loading branch information
Showing
7 changed files
with
245 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
chai = require 'chai' | ||
matchJS = require '../lib/match-js' | ||
match = matchJS.match | ||
|
||
chai.expect() | ||
expect = chai.expect | ||
|
||
describe 'Matching on Algebraic Data Type: An Calculator', -> | ||
class BinOpExp | ||
constructor:(x, y) -> | ||
@x = x | ||
@y = y | ||
|
||
class Add extends BinOpExp | ||
unapply: (exp) -> | ||
if exp instanceof Add then [exp.x, exp.y] else undefined | ||
|
||
class Min extends BinOpExp | ||
unapply: (exp) -> | ||
if exp instanceof Min then [exp.x, exp.y] else undefined | ||
|
||
class Mul extends BinOpExp | ||
unapply: (exp) -> | ||
if exp instanceof Mul then [exp.x, exp.y] else undefined | ||
|
||
class Div extends BinOpExp | ||
unapply: (exp) -> | ||
if exp instanceof Div then [exp.x, exp.y] else undefined | ||
|
||
class Number | ||
unapply: (e) -> | ||
if typeof e == 'number' then e else undefined | ||
|
||
evaluate = (exp) -> | ||
match(exp)( | ||
[Add, ([x, y]) -> evaluate(x)+evaluate(y)] | ||
[Min, ([x, y]) -> evaluate(x)-evaluate(y)] | ||
[Mul, ([x, y]) -> evaluate(x)*evaluate(y)] | ||
[Div, ([x, y]) -> evaluate(x)/evaluate(y)] | ||
[Number, (x) -> x]) | ||
|
||
it 'evaluate(5) should eval to 5', -> | ||
expect(evaluate(5)).to.be.equal 5 | ||
|
||
it '/( *( +(3)(2) )(6) ) (2) should eval to 15', -> | ||
exp = new Div(new Mul( new Add(3, 2), 6), 2) | ||
expect(evaluate(exp)).to.be.equal(15) | ||
|
||
return |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
chai = require 'chai' | ||
matchJS = require '../lib/match-js' | ||
match = matchJS.match | ||
_ = matchJS._ | ||
id = matchJS.id | ||
|
||
chai.expect() | ||
expect = chai.expect | ||
|
||
describe 'Miscellaneous useages', -> | ||
it 'execute statements should be allowed', -> | ||
tenfun = ->"TEN" | ||
m = match(10)( | ||
[1, "ONE"], | ||
[10, tenfun, null] | ||
) | ||
expect(m).to.be.equal 'TEN' | ||
|
||
it 'pass additional parameters to matched patial function', -> | ||
m = match(1)( | ||
[1, (x,y,z)-> | ||
"Match "+x+" "+y+" "+z | ||
, 2,3] | ||
) | ||
expect(m).to.be.equal 'Match 1 2 3' | ||
|
||
it 'default pattern that matches everything', -> | ||
i = 3 | ||
m = match(i)( | ||
[1, "ONE"], | ||
[2, "TOW"], | ||
[_, ()->"ANY is "+i+""] | ||
) | ||
expect(m).to.be.equal 'ANY is 3' | ||
|
||
it 'id functions returns the same input value', -> | ||
x = 10 | ||
m = match(x)( | ||
[1, "ONE"], | ||
[2, "TOW"], | ||
[10, id] | ||
) | ||
expect(m).to.be.equal x |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
chai = require 'chai' | ||
matchJS = require '../lib/match-js' | ||
match = matchJS.match | ||
|
||
chai.expect() | ||
expect = chai.expect | ||
|
||
describe 'Matching on build-in primitive types', -> | ||
it 'Integer value 10 should match value 10', -> | ||
m = match(10)( | ||
[1, "ONE"], | ||
[10, "TEN"] | ||
) | ||
expect(m).to.be.equal 'TEN' | ||
|
||
it 'Integer veriable of value 2 should match the value 2', -> | ||
two = 2; | ||
m = match(two)( | ||
[1, "ONE"], | ||
[2, "TWO"] | ||
) | ||
expect(m).to.be.equal 'TWO' | ||
|
||
|
||
it 'Boolean value true should match value true', -> | ||
m = match(true)( | ||
[1, "ONE"], | ||
[10, "TEN"], | ||
[true, "TRUE"] | ||
) | ||
expect(m).to.be.equal 'TRUE' | ||
|
||
|
||
it 'String value \'Hello World\' should match value \'Hello World\'', -> | ||
m = match('Hello World')( | ||
[1, "ONE"], | ||
[10, "TEN"], | ||
['Hello World', "Hello World Matched"] | ||
) | ||
expect(m).to.be.equal 'Hello World Matched' | ||
|
||
|
||
it 'Integer 1 should match int 1, not String \'1\'', -> | ||
m = match(1)( | ||
['1', "String ONE"], | ||
[1, "Integer ONE"] | ||
) | ||
expect(m).to.be.equal 'Integer ONE' | ||
|
||
it 'String \'1\' should match String \'1\', not int 1', -> | ||
m = match(1)( | ||
[1, "Integer ONE"], | ||
['1', "String ONE"] | ||
) | ||
expect(m).to.be.equal 'Integer ONE' | ||
return |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
### | ||
Ported From the Scala Example in: | ||
http://docs.scala-lang.org/tutorials/tour/extractor-objects.html | ||
``` | ||
object Twice | ||
def apply(x: Int): Int = x * 2 | ||
def unapply(z: Int): Option[Int] = if (z%2 == 0) Some(z/2) else None | ||
object TwiceTest extends App | ||
val x = Twice(21) | ||
x match case Twice(n) => Console.println(n) // prints 21 | ||
``` | ||
### | ||
|
||
chai = require 'chai' | ||
matchJS = require '../lib/match-js' | ||
match = matchJS.match | ||
|
||
chai.expect() | ||
expect = chai.expect | ||
|
||
describe 'Matching on class contructor Twice:', -> | ||
class Twice | ||
constructor:(x)-> | ||
@value = x*2; | ||
@funny_prop = 'funny'; | ||
|
||
unapply:(x)-> | ||
if (x instanceof Twice) and (x.value%2 == 0) | ||
return x.value / 2 | ||
|
||
if x%2 == 0 | ||
return x/2; | ||
else | ||
return undefined; | ||
|
||
it '10 should match Twice(x), where x is assigned to 5', -> | ||
m = match(10)( | ||
[1, "ONE"], | ||
[2, "TWO"], | ||
[Twice, (x) => x] | ||
) | ||
expect(m).to.be.equal 5 | ||
|
||
it '10 should match Twice(5)', -> | ||
m = match(10)( | ||
[1, "ONE"], | ||
[2, "TWO"], | ||
[new Twice(5), "Twice(5)"] | ||
) | ||
expect(m).to.be.equal "Twice(5)" | ||
|
||
it 'Twice(5) should match Twice(x), where x is assigned to 5', -> | ||
twice5 = new Twice(5) | ||
m = match(twice5)( | ||
[1, "ONE"], | ||
[2, "TWO"], | ||
[Twice, (x) => x] | ||
) | ||
expect(m).to.be.equal 5 | ||
|
||
it 'Twice(5) should match new Twice(5), before match with Twice(x)', -> | ||
twice5 = new Twice(5) | ||
m = match(twice5)( | ||
[1, "ONE"], | ||
[new Twice(5), "Twice(5)"], | ||
[Twice, (x) => x] | ||
) | ||
expect(m).to.be.equal "Twice(5)" | ||
|
||
it 'Twice(5) should not match Twice(6), but Twice(x)', -> | ||
twice5 = new Twice(5) | ||
m = match(twice5)( | ||
[1, "ONE"], | ||
[new Twice(6), "Twice(6)"], | ||
[Twice, (x) => x] | ||
) | ||
expect(m).to.be.equal 5 | ||
|
||
it 'Twice(6) should match value: 12', -> | ||
twice6 = new Twice(6) | ||
m = match(twice6)( | ||
[1, "ONE"], | ||
[value: 12, "Twice(6) matches value:12"], | ||
[Twice, (x) => x] | ||
) | ||
expect(m).to.be.equal "Twice(6) matches value:12" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters