added JSON support to require()
Showing
4 changed files
with
20 additions
and
2 deletions
... | @@ -94,6 +94,7 @@ Also, `Casper.mouseEvent()` will now directly trigger an error on failure instea | ... | @@ -94,6 +94,7 @@ Also, `Casper.mouseEvent()` will now directly trigger an error on failure instea |
94 | - fixed [#290](https://github.com/n1k0/casperjs/issues/#290) - add a simplistic RPM spec file to make it easier to (un)install casperjs | 94 | - fixed [#290](https://github.com/n1k0/casperjs/issues/#290) - add a simplistic RPM spec file to make it easier to (un)install casperjs |
95 | - fixed [`utils.betterTypeOf()`](http://casperjs.org/api.html#casper.betterTypeOf) to properly handle `undefined` and `null` values | 95 | - fixed [`utils.betterTypeOf()`](http://casperjs.org/api.html#casper.betterTypeOf) to properly handle `undefined` and `null` values |
96 | - fixed `Casper.die()` and `Casper.evaluateOrDie()` were not printing the error onto the console | 96 | - fixed `Casper.die()` and `Casper.evaluateOrDie()` were not printing the error onto the console |
97 | - added JSON support to `require()` | ||
97 | - added [`Casper.sendKeys()`](http://casperjs.org/api.html#casper.sendKeys) to send native keyboard events to the element matching a given selector | 98 | - added [`Casper.sendKeys()`](http://casperjs.org/api.html#casper.sendKeys) to send native keyboard events to the element matching a given selector |
98 | - added [`Casper.getFormValues()`](http://casperjs.org/api.html#casper.getFormValues) to check for the field values of a given form | 99 | - added [`Casper.getFormValues()`](http://casperjs.org/api.html#casper.getFormValues) to check for the field values of a given form |
99 | - added [`Tester.assertTextDoesntExist()`](http://casperjs.org/api.html#tester.assertTextDoesntExist) | 100 | - added [`Tester.assertTextDoesntExist()`](http://casperjs.org/api.html#tester.assertTextDoesntExist) | ... | ... |
... | @@ -106,10 +106,13 @@ function patchRequire(require, requireDirs) { | ... | @@ -106,10 +106,13 @@ function patchRequire(require, requireDirs) { |
106 | fileGuesses.push.apply(fileGuesses, [ | 106 | fileGuesses.push.apply(fileGuesses, [ |
107 | testPath, | 107 | testPath, |
108 | testPath + '.js', | 108 | testPath + '.js', |
109 | testPath + '.json', | ||
109 | testPath + '.coffee', | 110 | testPath + '.coffee', |
110 | fs.pathJoin(testPath, 'index.js'), | 111 | fs.pathJoin(testPath, 'index.js'), |
112 | fs.pathJoin(testPath, 'index.json'), | ||
111 | fs.pathJoin(testPath, 'index.coffee'), | 113 | fs.pathJoin(testPath, 'index.coffee'), |
112 | fs.pathJoin(testPath, 'lib', fs.basename(testPath) + '.js'), | 114 | fs.pathJoin(testPath, 'lib', fs.basename(testPath) + '.js'), |
115 | fs.pathJoin(testPath, 'lib', fs.basename(testPath) + '.json'), | ||
113 | fs.pathJoin(testPath, 'lib', fs.basename(testPath) + '.coffee') | 116 | fs.pathJoin(testPath, 'lib', fs.basename(testPath) + '.coffee') |
114 | ]); | 117 | ]); |
115 | }); | 118 | }); |
... | @@ -125,6 +128,11 @@ function patchRequire(require, requireDirs) { | ... | @@ -125,6 +128,11 @@ function patchRequire(require, requireDirs) { |
125 | if (file in requireCache) { | 128 | if (file in requireCache) { |
126 | return requireCache[file].exports; | 129 | return requireCache[file].exports; |
127 | } | 130 | } |
131 | if (/\.json/i.test(file)) { | ||
132 | var parsed = JSON.parse(fs.read(file)); | ||
133 | requireCache[file] = parsed; | ||
134 | return parsed; | ||
135 | } | ||
128 | var scriptCode = (function getScriptCode(file) { | 136 | var scriptCode = (function getScriptCode(file) { |
129 | var scriptCode = fs.read(file); | 137 | var scriptCode = fs.read(file); |
130 | if (/\.coffee$/i.test(file)) { | 138 | if (/\.coffee$/i.test(file)) { | ... | ... |
tests/sample_modules/config.json
0 → 100644
1 | {"ok": true} |
... | @@ -2,7 +2,7 @@ | ... | @@ -2,7 +2,7 @@ |
2 | /*jshint strict:false*/ | 2 | /*jshint strict:false*/ |
3 | var fs = require('fs'); | 3 | var fs = require('fs'); |
4 | var modroot = fs.pathJoin(phantom.casperPath, 'tests', 'sample_modules'); | 4 | var modroot = fs.pathJoin(phantom.casperPath, 'tests', 'sample_modules'); |
5 | var jsmod, csmod; | 5 | var jsmod, csmod, config; |
6 | 6 | ||
7 | casper.test.comment('Javascript module loading') | 7 | casper.test.comment('Javascript module loading') |
8 | try { | 8 | try { |
... | @@ -20,4 +20,12 @@ try { | ... | @@ -20,4 +20,12 @@ try { |
20 | casper.test.fail('require() patched version can load a coffeescript module'); | 20 | casper.test.fail('require() patched version can load a coffeescript module'); |
21 | } | 21 | } |
22 | 22 | ||
23 | casper.test.done(2); | 23 | casper.test.comment('JSON module loading') |
24 | try { | ||
25 | config = require(fs.pathJoin(modroot, 'config.json')); | ||
26 | casper.test.assertTrue(config.ok, 'require() patched version can load a json module'); | ||
27 | } catch (e) { | ||
28 | casper.test.fail('require() patched version can load a json module'); | ||
29 | } | ||
30 | |||
31 | casper.test.done(3); | ... | ... |
-
Please register or sign in to post a comment