Created
July 16, 2014 05:26
-
-
Save TooTallNate/baa25c0a58b7bd181bab to your computer and use it in GitHub Desktop.
C-style assert() function for sweet.js, which include the assertion code in the Error object (as opposed to the calculated result). Similar to: https://github.com/visionmedia/better-assert
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
macro assert { | |
rule {($test ...)} => { | |
if (!($test ...)) { | |
throw new Error('AssertionError: ' + #{ $test ... }); | |
} | |
} | |
} | |
var foo = 100; | |
assert(100 == foo); | |
// passes | |
assert(99 == foo); | |
// fails! | |
// Error: AssertionError: 99 == foo | |
// at blah.js:1:1 | |
// at ... |
One important use of assertions in C, is that the assertions go away once compiled. Can this be done using sweet?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Does not work with SweetJS v.0.7.1.
You'd want this to be a case macro and have some code to string macro.