Commit 125c37f0 125c37f0f2099de3ae7a18e30d179c69f2bd05de by Nicolas Perriault

added JSON support to require()

1 parent 68b32572
......@@ -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)
......
......@@ -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')
]);
});
......@@ -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)) {
......
......@@ -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 {
......@@ -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);
......