Here's a quick example of a test written in LiveScript for Mocha and ExpectThat:
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
describe "When testing for the weekend", -> | |
weekend = <[ sat sun ]> | |
isWeekend = (dayOfWeek) -> | |
| dayOfWeek in weekend => true | |
| otherwise => false | |
expectThat -> ('sun' |> isWeekend).should equal true |
The pattern matching syntax gives you a concise way of defining a basic switch statement in JavaScript. The pipe-forward operator allows you to compose things together. In the example above, the string 'sun' (which also could have been written in LiveScript as \sun) is being passed into the isWeekend function. This becomes even more powerful when the value to be passed into the next function is coming from another function, as shown in this example:
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
describe "When testing for the weekend", -> | |
weekend = <[ sat sun ]> | |
isWeekend = (dayOfWeek) -> | |
| dayOfWeek in weekend => true | |
| otherwise => false | |
getDayOfWeek = -> 'sun' | |
expectThat -> (getDayOfWeek() |> isWeekend).should equal true |
There are a number of additional LiveScript features that assist in using a functional style. For more examples of LiveScript with Mocha and ExpectThat visit https://github.com/dmohl/expectThat/tree/master/example/mocha-LiveScript. To learn more about LiveScript, see a number of examples, and/or get setup for use, visit the LiveScript site at http://gkz.github.com/LiveScript/.
No comments:
Post a Comment