Permalink
Browse files

Add a test runner and some tests.

The tests only pass with the other pull requests applied.
  • Loading branch information...
1 parent cf123a3 commit 4dc708cba1fd4145181b5f30a32e9a90dcca516b @SimonSapin SimonSapin committed Jul 15, 2012
Showing with 101 additions and 2 deletions.
  1. +1 −0 .gitignore
  2. +7 −1 parser.js
  3. +85 −0 tests.js
  4. +8 −1 tokenizer.js
View
@@ -0,0 +1 @@
+/node_modules
View
@@ -353,4 +353,10 @@ FunctionArg.prototype = new CSSParserRule;
FunctionArg.prototype.ruleType = "FUNCTION-ARG";
FunctionArg.prototype.toJSON = function() {
return this.value.map(function(e){return e.toJSON();});
-}
+}
+
+// Make this usable from NodeJS
+// TODO: also export the various rule objects?
+if (typeof(exports) != 'undefined') {
+ exports.parse = parse;
+}
View
@@ -0,0 +1,85 @@
+// This test suite runs on NodeJS and uses the 'ansidiff' package.
+// Once you have NodeJS, install ansidiff with:
+//
+// npm install ansidiff
+//
+// Then run the test suite with:
+//
+// node tests.js
+
+var TESTS = [
+ {
+ css: 'foo { bar: baz; }',
+ expected: {"type": "stylesheet", "value": [
+ {
+ "type": "selector",
+ "selector": ["IDENT(foo)", "WS"],
+ "value": [
+ {
+ "type": "decl",
+ "name": "bar",
+ "value": ["WS", "IDENT(baz)"]}]}]
+ }
+ }, {
+ css: 'foo { bar: rgb(255, 0, 127); }',
+ expected: {"type": "stylesheet", "value": [
+ {
+ "type": "selector",
+ "selector": ["IDENT(foo)", "WS"],
+ "value": [
+ {
+ "type": "decl",
+ "name": "bar",
+ "value": ["WS", {"type": "func", "name": "rgb", "value": [
+ ["INT(255)"], ["WS", "INT(0)"], ["WS", "INT(127)"]]}]}]}]
+ }
+ }, {
+ css: '#foo {}',
+ expected: {"type": "stylesheet", "value": [
+ {
+ "type": "selector",
+ "selector": ["HASH(foo)", "WS"],
+ "value": []}]
+ }
+ }, {
+ css: '@media{ }',
+ expected: {"type": "stylesheet", "value": [
+ {
+ "type": "at", "name": "media",
+ "prelude": [],
+ "value": []}]}
+ }
+];
+
+
+var ansidiff = require('ansidiff'),
+ tokenize = require('./tokenizer').tokenize,
+ parse = require('./parser').parse;
+
+var total = TESTS.length, failures = 0,
+ i, test, tokens, sheet, dump, expected_dump;
+
+for (i = 0; i < total; i++) {
+ test = TESTS[i];
+ tokens = tokenize(test.css);
+ sheet = parse(tokens);
+ dump = sheet.toString(' ');
+ expected_dump = JSON.stringify(test.expected, null, ' ');
+ if (dump == expected_dump) {
+ console.log('Test %d of %d: PASS', i, total);
+ } else {
+ console.log('Test %d of %d: FAIL\nCSS: %s\nTokens: %s',
+ i, total, test.css, tokens.join(' '));
+ console.log(ansidiff.lines(expected_dump, dump));
+ failures++;
+ }
+}
+
+// Abuse the differ to get colored output
+if (failures == 0) {
+ console.log(ansidiff.words('%d tests, ', '%d tests, all passed :)'),
+ total);
+} else {
+ console.log(ansidiff.words('%d tests, %d failures :(', '%d tests, '),
+ total, failures);
+}
View
@@ -720,4 +720,11 @@ UnicodeRangeToken.prototype.toString = function() {
}
UnicodeRangeToken.prototype.contains = function(code) {
return code >= this.start && code < this.end;
-}
+}
+
+
+// Make this usable from NodeJS
+// TODO: also export the various tokens objects?
+if (typeof(exports) != 'undefined') {
+ exports.tokenize = tokenize;
+}

0 comments on commit 4dc708c

Please sign in to comment.