-
Notifications
You must be signed in to change notification settings - Fork 0
wbecker/jsContract
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
jsContracts aims to port cofoja (http://code.google.com/p/cofoja/) to Javascript. Instead of using annotations, jsContract wraps your class up in a jsContract object. If you don't want to use contracts (ie this is production code), you can turn it off, by setting contractsOn = false. In this case, your functions are defined normally and no checking occurs. It works well in Firefox, but it does rely on being able to toString() functions to work out parameter names (this could be got around by only specifying parameter indexes for non-supportive browsers, instead of using named parameters) and rich array functions, eg forEach. If your browser doesn't support this, you could use Underscore.js (http://documentcloud.github.com/underscore/). It handles the 4 main contract types: * Invariants (invariant), checked when entering and exiting methods, and exiting the constructor. * Preconditions (pre), checked when entering methods. * Postconditions (post), checked when exiting methods normally. * ThrowEnsures (throwEnsures), checked when exiting methods normally. as well as the old() keyword. Syntax: <your variable> = jsContract(<rules>, <function definition>); where rules => { constructor: true/false, //non-optional if constructor with invariants, optional otherwise invariant: [statement1, statement2, ...], //optional pre: [statement1, statement2, ...], //optional post: [statement1, statement2, ...], //optional throwEnsures: [ ["ExceptionType1", "condition1"], ["ExceptionType2", "condition2"], ...] //optional } Differences from cofoja: * uses pre and post instead of Requires and Ensures. * slight changing of syntax, eg uses square braces instead of curly braces for arrays of a contract type. * only handles multi-clause contracts, not single simple-contracts(though this won't be hard to implement) * constructors have to be explictly defined, since Javascript doesn't really let you create explicit objects. (see http://stackoverflow.com/questions/367768/) * Class variables and methods need to be specified by "this", ie "this.myVariable" - cofoja doesn't require such a prefix, but such is the nature of Javascript! * It doesn't handle the => operator. As issue #7 of cofoja suggests (http://code.google.com/p/cofoja/issues/detail?id=7) you can use the ?: operator instead. See the two test files, test/Person.js and test/time.js for examples.
About
A contract library for javascript
Resources
Stars
Watchers
Forks
Releases
No releases published
Packages 0
No packages published