Skip to content
This repository has been archived by the owner on Jun 19, 2020. It is now read-only.

Commit

Permalink
added JSON support to require()
Browse files Browse the repository at this point in the history
  • Loading branch information
n1k0 committed Dec 10, 2012
1 parent 68b3257 commit 125c37f
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ Also, `Casper.mouseEvent()` will now directly trigger an error on failure instea
- fixed [#290](https://github.com/n1k0/casperjs/issues/#290) - add a simplistic RPM spec file to make it easier to (un)install casperjs
- fixed [`utils.betterTypeOf()`](http://casperjs.org/api.html#casper.betterTypeOf) to properly handle `undefined` and `null` values
- fixed `Casper.die()` and `Casper.evaluateOrDie()` were not printing the error onto the console
- added JSON support to `require()`
- added [`Casper.sendKeys()`](http://casperjs.org/api.html#casper.sendKeys) to send native keyboard events to the element matching a given selector
- added [`Casper.getFormValues()`](http://casperjs.org/api.html#casper.getFormValues) to check for the field values of a given form
- added [`Tester.assertTextDoesntExist()`](http://casperjs.org/api.html#tester.assertTextDoesntExist)
Expand Down
8 changes: 8 additions & 0 deletions bin/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,13 @@ function patchRequire(require, requireDirs) {
fileGuesses.push.apply(fileGuesses, [
testPath,
testPath + '.js',
testPath + '.json',
testPath + '.coffee',
fs.pathJoin(testPath, 'index.js'),
fs.pathJoin(testPath, 'index.json'),
fs.pathJoin(testPath, 'index.coffee'),
fs.pathJoin(testPath, 'lib', fs.basename(testPath) + '.js'),
fs.pathJoin(testPath, 'lib', fs.basename(testPath) + '.json'),
fs.pathJoin(testPath, 'lib', fs.basename(testPath) + '.coffee')
]);
});
Expand All @@ -125,6 +128,11 @@ function patchRequire(require, requireDirs) {
if (file in requireCache) {
return requireCache[file].exports;
}
if (/\.json/i.test(file)) {
var parsed = JSON.parse(fs.read(file));
requireCache[file] = parsed;
return parsed;
}
var scriptCode = (function getScriptCode(file) {
var scriptCode = fs.read(file);
if (/\.coffee$/i.test(file)) {
Expand Down
1 change: 1 addition & 0 deletions tests/sample_modules/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"ok": true}
12 changes: 10 additions & 2 deletions tests/suites/require.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/*jshint strict:false*/
var fs = require('fs');
var modroot = fs.pathJoin(phantom.casperPath, 'tests', 'sample_modules');
var jsmod, csmod;
var jsmod, csmod, config;

casper.test.comment('Javascript module loading')
try {
Expand All @@ -20,4 +20,12 @@ try {
casper.test.fail('require() patched version can load a coffeescript module');
}

casper.test.done(2);
casper.test.comment('JSON module loading')
try {
config = require(fs.pathJoin(modroot, 'config.json'));
casper.test.assertTrue(config.ok, 'require() patched version can load a json module');
} catch (e) {
casper.test.fail('require() patched version can load a json module');
}

casper.test.done(3);

0 comments on commit 125c37f

Please sign in to comment.