Skip to content

Commit

Permalink
feat(coffeescript test): add coffeescript test
Browse files Browse the repository at this point in the history
  • Loading branch information
Jiansen committed Jul 3, 2016
1 parent 7b24ba5 commit e9b485d
Show file tree
Hide file tree
Showing 7 changed files with 245 additions and 4 deletions.
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@
"commit": "git-cz",
"dev": "webpack --progress --colors --watch --mode=dev",
"dev:single": "webpack --progress --colors --mode=dev",
"test:single": "npm run dev:single && npm run test:single:es5 && npm run test:single:es6",
"test:single": "npm run dev:single && npm run test:single:es5 && npm run test:single:es6 && npm run test:single:coffee",
"test:es5": "eslint ./test/*.spec.es5.js && mocha --colors --watch ./test/*.spec.es5.js",
"test:single:es5": "eslint ./test/*.spec.es5.js && mocha --colors ./test/*.spec.es5.js",
"test:es6": "mocha --compilers js:babel-core/register --colors --watch ./test/*.spec.es6.js",
"_test:coffee": "coffee --compile --output ./coffee-test/ ./test/*.spec.coffee && mocha --colors --watch ./coffee-test/*.spec.js",
"test:coffee": "mocha --compilers coffee:coffee-script/register --colors --watch ./test/*.spec.coffee",
"test:single:es5": "eslint ./test/*.spec.es5.js && mocha --colors ./test/*.spec.es5.js",
"test:single:es6": "mocha --compilers js:babel-core/register --colors ./test/*.spec.es6.js",
"test:single:coffee": "mocha --compilers coffee:coffee-script/register --colors ./test/*.spec.coffee",
"semantic-release": "semantic-release pre && npm publish && semantic-release post"
},
"devDependencies": {
Expand Down
49 changes: 49 additions & 0 deletions test/adt_calculator.spec.coffee
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
43 changes: 43 additions & 0 deletions test/misc.spec.coffee
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
56 changes: 56 additions & 0 deletions test/primitive_type.spec.coffee
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
90 changes: 90 additions & 0 deletions test/twice_class.spec.coffee
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"
2 changes: 1 addition & 1 deletion test/twice_class.spec.es5.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ object TwiceTest extends App {
val x = Twice(21)
x match { case Twice(n) => Console.println(n) } // prints 21
}
``
```
*/

var chai = require('chai');
Expand Down
2 changes: 1 addition & 1 deletion test/twice_class.spec.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ object TwiceTest extends App {
val x = Twice(21)
x match { case Twice(n) => Console.println(n) } // prints 21
}
``
```
*/

import chai from 'chai';
Expand Down

0 comments on commit e9b485d

Please sign in to comment.