Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Initial commit

  • Loading branch information...
commit 679f877d996b53f32dfd102a1506a604223ae6e7 0 parents
@mfncooper authored
1  .gitignore
@@ -0,0 +1 @@
+node_modules
1  .npmignore
@@ -0,0 +1 @@
+coverage/
4 .travis.yml
@@ -0,0 +1,4 @@
+language: node_js
+node_js:
+ - 0.6
+ - 0.8
24 LICENSE
@@ -0,0 +1,24 @@
+ Copyrights for code authored by Yahoo! Inc. is licensed under the following
+ terms:
+
+ MIT License
+
+ Copyright (c) 2011-2012 Yahoo! Inc. All Rights Reserved.
+
+ Permission is hereby granted, free of charge, to any person obtaining a copy
+ of this software and associated documentation files (the "Software"), to
+ deal in the Software without restriction, including without limitation the
+ rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ sell copies of the Software, and to permit persons to whom the Software is
+ furnished to do so, subject to the following conditions:
+
+ The above copyright notice and this permission notice shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ DEALINGS IN THE SOFTWARE.
80 README.md
@@ -0,0 +1,80 @@
+# jsonutil - Convenience functions for working with JSON
+
+Working with JSON in Node.js is pretty straightforward, but there are a couple
+of things that could be a bit more convenient. Examples are copying objects, as
+well as reading them from, and writing them to, files. The jsonutil package
+addresses just those scenarios with a few handy utility functions.
+
+## Installation
+
+Just use npm:
+
+ npm install jsonutil
+
+## Copying JSON objects
+
+### deepCopy(obj)
+
+Makes a deep copy (a.k.a. clone) of the supplied object. This function reliably
+copies only what is valid for a JSON object, array, or other element.
+
+__Arguments__
+
+* _obj_, the object of which a copy is to be made.
+
+### shallowCopy(obj)
+
+Makes a shallow (i.e. top level only) copy of the supplied object. This function
+reliably copies only what is valid for a JSON object, array, or other element.
+
+__Arguments__
+
+* _obj_, the object of which a copy is to be made.
+
+### copy(obj, shallow)
+
+Makes a copy of the supplied object, either shallow or deep, according to the
+second argument. This function reliably copies only what is valid for a JSON
+object, array, or other element.
+
+__Arguments__
+
+* _obj_, the object of which a copy is to be made.
+* _shallow_, a boolean indicating whether the copy should be shallow or deep.
+
+## Working with JSON files
+
+### readFile(file, cb)
+
+Reads a JSON file and parses it, calling the callback with the resulting object.
+
+__Arguments__
+
+* _file_, the file from which to read and parse JSON.
+* _cb(err, obj)_, a callback function that will be called with either the object
+read from the file, or an error.
+
+### readFileSync(file)
+
+A synchronous version of `readFile` (see above) that returns the parsed object
+or throws an error.
+
+### writeFile(file, obj, [indent], cb)
+
+Writes a JSON file with the specified data. Serializing the data will fail if
+the data contains a cyclic structure.
+
+* _file_, the file to which the object will be written.
+* _obj_, the object to be written as JSON.
+* _indent_, the indentation level, in spaces, of the JSON output. [Default: no
+indentation]
+* _cb(err)_, a callback function that will be called with either null on
+success, or an error.
+
+### writeFileSync(file, data, [indent])
+
+A synchronous version of `writeFile` (see above) that throws any error.
+
+## License
+
+jsonutil is licensed under the [MIT License](http://github.com/mfncooper/jsonutil/raw/master/LICENSE).
164 jsonutil.js
@@ -0,0 +1,164 @@
+/*
+ Copyrights for code authored by Yahoo! Inc. is licensed under the following
+ terms:
+
+ MIT License
+
+ Copyright (c) 2011-2012 Yahoo! Inc. All Rights Reserved.
+
+ Permission is hereby granted, free of charge, to any person obtaining a copy
+ of this software and associated documentation files (the "Software"), to
+ deal in the Software without restriction, including without limitation the
+ rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ sell copies of the Software, and to permit persons to whom the Software is
+ furnished to do so, subject to the following conditions:
+
+ The above copyright notice and this permission notice shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ DEALINGS IN THE SOFTWARE.
+*/
+
+/*
+ * Convenience functions for copying JSON-compliant objects, and reading and
+ * writing JSON files. Hides the messy bits like dealing with exceptions,
+ * encodings, etc.
+ */
+
+"use strict";
+
+var fs = require('fs');
+
+/*
+ * Read a JSON file and parse it, calling the callback with the resulting object
+ * or an error.
+ */
+function readFile(file, cb) {
+ fs.readFile(file, "utf8", function (err, json) {
+ if (err) {
+ cb(err);
+ return;
+ }
+
+ var data;
+
+ try {
+ data = JSON.parse(json); // May throw SyntaxError
+ } catch (e) {
+ cb(e);
+ return;
+ }
+
+ cb(null, data);
+ });
+}
+
+/*
+ * Read a JSON file and parse it, calling the callback with the resulting object
+ * or an error. A synchronous version of readFile.
+ */
+function readFileSync(file) {
+ return JSON.parse(fs.readFileSync(file, "utf8"));
+}
+
+/*
+ * Write a JSON file with the specified data. Serializing the data will fail
+ * if the data contains a cyclic structure.
+ */
+function writeFile(file, data, indent, cb) {
+ if (typeof cb !== 'function') {
+ cb = indent;
+ indent = 0;
+ }
+
+ var json;
+
+ try {
+ json = JSON.stringify(data, null, indent); // May throw TypeError
+ } catch (e) {
+ cb(e);
+ return;
+ }
+
+ fs.writeFile(file, json, "utf8", cb);
+}
+
+/*
+ * Write a JSON file with the specified data. Serializing the data will fail
+ * if the data contains a cyclic structure. A synchronous version of writeFile.
+ */
+function writeFileSync(file, data, indent) {
+ if (typeof indent !== 'number') {
+ indent = 0;
+ }
+ fs.writeFileSync(file, JSON.stringify(data, null, indent), "utf8");
+}
+
+/*
+ * Make a deep copy of the supplied object. This function reliably copies only
+ * what is valid for a JSON object, array, or other element.
+ */
+var deepCopy = function (o) {
+ var newObj;
+
+ if (!o || typeof o !== 'object') {
+ return o;
+ }
+ if (Array.isArray(o)) {
+ return o.map(function (it) {
+ return deepCopy(it);
+ });
+ }
+ newObj = {};
+ Object.keys(o).forEach(function (prop) {
+ newObj[prop] = deepCopy(o[prop]);
+ });
+ return newObj;
+};
+
+/*
+ * Make a shallow (i.e. top level only) copy of the supplied object. This
+ * function reliably copies only what is valid for a JSON object, array, or
+ * other element.
+ */
+var shallowCopy = function (o) {
+ var newObj;
+
+ if (!o || typeof o !== 'object') {
+ return o;
+ }
+ if (Array.isArray(o)) {
+ return o.slice(0);
+ }
+ newObj = {};
+ Object.keys(o).forEach(function (prop) {
+ newObj[prop] = o[prop];
+ });
+ return newObj;
+};
+
+/*
+ * Make a copy of the supplied object, either shallow or deep, according to the
+ * second argument. This function reliably copies only what is valid for a JSON
+ * object, array, or other element.
+ */
+var copy = function (o, shallow) {
+ var copyfn = shallow ? shallowCopy : deepCopy;
+
+ return copyfn(o);
+};
+
+// Exported functions
+exports.readFile = readFile;
+exports.readFileSync = readFileSync;
+exports.writeFile = writeFile;
+exports.writeFileSync = writeFileSync;
+exports.deepCopy = deepCopy;
+exports.shallowCopy = shallowCopy;
+exports.copy = copy;
31 package.json
@@ -0,0 +1,31 @@
+{
+ "name": "jsonutil",
+ "version": "0.2.0",
+ "description": "Convenience functions for working with JSON",
+ "keywords": [ "json", "utility", "file", "read", "write", "deep", "copy", "clone" ],
+ "author": "Martin Cooper <mfncooper@gmail.com>",
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/mfncooper/jsonutil.git"
+ },
+ "bugs": {
+ "url" : "http://github.com/mfncooper/jsonutil/issues"
+ },
+ "main": "jsonutil.js",
+ "engines": {
+ "node": ">=0.6.0"
+ },
+ "dependencies": {},
+ "devDependencies": {
+ "nodeunit" : "~0.7"
+ },
+ "scripts": {
+ "test": "nodeunit test/*.js"
+ },
+ "licenses" : [
+ {
+ "type" : "MIT",
+ "url" : "http://github.com/mfncooper/jsonutil/raw/master/LICENSE"
+ }
+ ]
+}
196 test/test-copy.js
@@ -0,0 +1,196 @@
+/*
+ Copyrights for code authored by Yahoo! Inc. is licensed under the following
+ terms:
+
+ MIT License
+
+ Copyright (c) 2011-2012 Yahoo! Inc. All Rights Reserved.
+
+ Permission is hereby granted, free of charge, to any person obtaining a copy
+ of this software and associated documentation files (the "Software"), to
+ deal in the Software without restriction, including without limitation the
+ rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ sell copies of the Software, and to permit persons to whom the Software is
+ furnished to do so, subject to the following conditions:
+
+ The above copyright notice and this permission notice shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ DEALINGS IN THE SOFTWARE.
+*/
+
+"use strict";
+
+var jsoncopy = require('../jsonutil.js');
+
+var testThing1,
+ testThing2 = null,
+ testThing3 = "A string",
+ testThing4 = 42,
+ testThing5 = [ "one", 2, true ],
+ testThing6 = { one: 1, two: "two", three: false },
+ testThing7 = [
+ { one1: 1, one2: "two", one3: false },
+ [ "two1", 22, true ]
+ ],
+ testThing8 = {
+ one: { one1: 1, one2: "two", one3: false },
+ two: [ "two1", 22, true ]
+ };
+
+exports.shallowCopyTests = {
+
+ testSimpleTypes: function (test) {
+ test.strictEqual(jsoncopy.shallowCopy(testThing1), undefined);
+ test.strictEqual(jsoncopy.shallowCopy(testThing2), null);
+ test.strictEqual(jsoncopy.shallowCopy(testThing3), testThing3);
+ test.strictEqual(jsoncopy.shallowCopy(testThing4), testThing4);
+ test.done();
+ },
+
+ testSimpleArray: function (test) {
+ var actual = jsoncopy.shallowCopy(testThing5),
+ i;
+
+ test.ok(Array.isArray(actual));
+ test.notEqual(actual, testThing5);
+ test.strictEqual(actual.length, testThing5.length);
+ for (i = 0; i < actual.length; i += 1) {
+ test.strictEqual(actual[i], testThing5[i]);
+ }
+ test.done();
+ },
+
+ testSimpleObject: function (test) {
+ var actual = jsoncopy.shallowCopy(testThing6);
+
+ test.equal(typeof actual, 'object');
+ test.ok(!Array.isArray(actual));
+ test.notEqual(actual, testThing6);
+ test.strictEqual(Object.keys(actual).length, Object.keys(testThing6).length);
+ Object.keys(actual).forEach(function (key) {
+ test.strictEqual(actual[key], testThing6[key]);
+ });
+ test.done();
+ },
+
+ testComplexArray: function (test) {
+ var actual = jsoncopy.shallowCopy(testThing7);
+
+ test.ok(Array.isArray(actual));
+ test.notEqual(actual, testThing7);
+ test.strictEqual(actual.length, testThing7.length);
+ test.strictEqual(actual[0], testThing7[0]);
+ test.strictEqual(actual[1], testThing7[1]);
+ test.done();
+ },
+
+ testComplexObject: function (test) {
+ var actual = jsoncopy.shallowCopy(testThing8);
+
+ test.equal(typeof actual, 'object');
+ test.ok(!Array.isArray(actual));
+ test.notEqual(actual, testThing8);
+ test.strictEqual(Object.keys(actual).length, Object.keys(testThing8).length);
+ test.strictEqual(actual.one, testThing8.one);
+ test.strictEqual(actual.two, testThing8.two);
+ test.done();
+ }
+};
+
+exports.deepCopyTests = {
+
+ testSimpleTypes: function (test) {
+ test.strictEqual(jsoncopy.deepCopy(testThing1), undefined);
+ test.strictEqual(jsoncopy.deepCopy(testThing2), null);
+ test.strictEqual(jsoncopy.deepCopy(testThing3), testThing3);
+ test.strictEqual(jsoncopy.deepCopy(testThing4), testThing4);
+ test.done();
+ },
+
+ testSimpleArray: function (test) {
+ var actual = jsoncopy.deepCopy(testThing5),
+ i;
+
+ test.ok(Array.isArray(actual));
+ test.notEqual(actual, testThing5);
+ test.strictEqual(actual.length, testThing5.length);
+ for (i = 0; i < actual.length; i += 1) {
+ test.strictEqual(actual[i], testThing5[i]);
+ }
+ test.done();
+ },
+
+ testSimpleObject: function (test) {
+ var actual = jsoncopy.deepCopy(testThing6);
+
+ test.equal(typeof actual, 'object');
+ test.ok(!Array.isArray(actual));
+ test.notEqual(actual, testThing6);
+ test.strictEqual(Object.keys(actual).length, Object.keys(testThing6).length);
+ Object.keys(actual).forEach(function (key) {
+ test.strictEqual(actual[key], testThing6[key]);
+ });
+ test.done();
+ },
+
+ testComplexArray: function (test) {
+ var actual = jsoncopy.deepCopy(testThing7),
+ i;
+
+ test.ok(Array.isArray(actual));
+ test.notEqual(actual, testThing7);
+ test.strictEqual(actual.length, testThing7.length);
+ test.notStrictEqual(actual[0], testThing7[0]);
+ Object.keys(actual[0]).forEach(function (key) {
+ test.strictEqual(actual[0][key], testThing7[0][key]);
+ });
+ test.notStrictEqual(actual[1], testThing7[1]);
+ for (i = 0; i < actual[1].length; i += 1) {
+ test.strictEqual(actual[1][i], testThing7[1][i]);
+ }
+ test.done();
+ },
+
+ testComplexObject: function (test) {
+ var actual = jsoncopy.deepCopy(testThing8),
+ i;
+
+ test.equal(typeof actual, 'object');
+ test.ok(!Array.isArray(actual));
+ test.notEqual(testThing8, actual);
+ test.strictEqual(Object.keys(actual).length, Object.keys(testThing8).length);
+ test.notStrictEqual(actual.one, testThing8.one);
+ Object.keys(actual.one).forEach(function (key) {
+ test.strictEqual(actual.one[key], testThing8.one[key]);
+ });
+ test.notStrictEqual(actual.two, testThing8.two);
+ for (i = 0; i < actual.two.length; i += 1) {
+ test.strictEqual(actual.two[i], testThing8.two[i]);
+ }
+ test.done();
+ }
+};
+
+exports.parameterizedCopyTests = {
+
+ testSimpleTypes: function (test) {
+ test.strictEqual(jsoncopy.copy(testThing1, false), undefined);
+ test.strictEqual(jsoncopy.copy(testThing2, false), null);
+ test.strictEqual(jsoncopy.copy(testThing3, false), testThing3);
+ test.strictEqual(jsoncopy.copy(testThing4, false), testThing4);
+
+ test.strictEqual(jsoncopy.copy(testThing1, true), undefined);
+ test.strictEqual(jsoncopy.copy(testThing2, true), null);
+ test.strictEqual(jsoncopy.copy(testThing3, true), testThing3);
+ test.strictEqual(jsoncopy.copy(testThing4, true), testThing4);
+
+ test.done();
+ }
+};
307 test/test-file.js
@@ -0,0 +1,307 @@
+/*
+ Copyrights for code authored by Yahoo! Inc. is licensed under the following
+ terms:
+
+ MIT License
+
+ Copyright (c) 2012 Yahoo! Inc. All Rights Reserved.
+
+ Permission is hereby granted, free of charge, to any person obtaining a copy
+ of this software and associated documentation files (the "Software"), to
+ deal in the Software without restriction, including without limitation the
+ rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ sell copies of the Software, and to permit persons to whom the Software is
+ furnished to do so, subject to the following conditions:
+
+ The above copyright notice and this permission notice shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ DEALINGS IN THE SOFTWARE.
+*/
+
+"use strict";
+
+var fs = require('fs'),
+ fsReadFile = fs.readFile,
+ fsReadFileSync = fs.readFileSync,
+ fsWriteFile = fs.writeFile,
+ fsWriteFileSync = fs.writeFileSync,
+ jsonfile = require('../jsonutil.js');
+
+function stubReadFile(test, file, err, data) {
+ fs.readFile = function (f, enc, cb) {
+ test.equal(f, file);
+ test.equal(enc, "utf8");
+ test.ok(typeof cb === 'function');
+ cb(err, data);
+ };
+}
+
+function stubReadFileSync(test, file, err, data) {
+ fs.readFileSync = function (f, enc) {
+ test.equal(f, file);
+ test.equal(enc, "utf8");
+ if (err) {
+ throw err;
+ }
+ return data;
+ };
+}
+
+function stubWriteFile(test, file, data, err) {
+ fs.writeFile = function (f, d, enc, cb) {
+ test.equal(f, file);
+ test.equal(d, data);
+ test.equal(enc, "utf8");
+ test.ok(typeof cb === 'function');
+ cb(err);
+ };
+}
+
+function stubWriteFileSync(test, file, data, err) {
+ fs.writeFileSync = function (f, d, enc) {
+ test.equal(f, file);
+ test.equal(d, data);
+ test.equal(enc, "utf8");
+ if (err) {
+ throw err;
+ }
+ };
+}
+
+exports.readTests = {
+
+ setUp: function (callback) {
+ callback();
+ },
+
+ tearDown: function (callback) {
+ fs.readFile = fsReadFile;
+ callback();
+ },
+
+ testSuccess: function (test) {
+ var test_file = 'test_file.json',
+ test_error = null,
+ test_data = '{ "key": "value" }';
+
+ stubReadFile(test, test_file, test_error, test_data);
+ jsonfile.readFile(test_file, function (err, data) {
+ test.ok(!err);
+ test.ok(typeof data === 'object');
+ test.equal(Object.keys(data).length, 1);
+ test.equal(data.key, JSON.parse(test_data).key);
+ test.done();
+ });
+ },
+
+ testFileError: function (test) {
+ var test_file = 'test_file.json',
+ test_error = new Error("EPERM"),
+ test_data = null;
+
+ stubReadFile(test, test_file, test_error, test_data);
+ jsonfile.readFile(test_file, function (err) {
+ test.ok(err instanceof Error);
+ test.equal(err.message, "EPERM");
+ test.done();
+ });
+ },
+
+ testDataError: function (test) {
+ var test_file = 'test_file.json',
+ test_error = null,
+ test_data = 'not json';
+
+ stubReadFile(test, test_file, test_error, test_data);
+ jsonfile.readFile(test_file, function (err) {
+ test.ok(err instanceof SyntaxError);
+ test.ok(/^Unexpected token/.test(err.message));
+ test.done();
+ });
+ }
+};
+
+exports.readSyncTests = {
+
+ setUp: function (callback) {
+ callback();
+ },
+
+ tearDown: function (callback) {
+ fs.readFileSync = fsReadFileSync;
+ callback();
+ },
+
+ testSuccess: function (test) {
+ var test_file = 'test_file.json',
+ test_error = null,
+ test_data = '{ "key": "value" }',
+ data;
+
+ stubReadFileSync(test, test_file, test_error, test_data);
+ data = jsonfile.readFileSync(test_file);
+ test.ok(typeof data === 'object');
+ test.equal(Object.keys(data).length, 1);
+ test.equal(data.key, JSON.parse(test_data).key);
+ test.done();
+ },
+
+ testFileError: function (test) {
+ var test_file = 'test_file.json',
+ test_error = new Error("EPERM"),
+ test_data = null;
+
+ stubReadFileSync(test, test_file, test_error, test_data);
+ test.throws(function () {
+ jsonfile.readFileSync(test_file);
+ }, "EPERM");
+ test.done();
+ },
+
+ testDataError: function (test) {
+ var test_file = 'test_file.json',
+ test_error = null,
+ test_data = 'not json';
+
+ stubReadFileSync(test, test_file, test_error, test_data);
+ test.throws(function () {
+ jsonfile.readFileSync(test_file);
+ }, /^Unexpected token/);
+ test.done();
+ }
+};
+
+exports.writeTests = {
+
+ setUp: function (callback) {
+ callback();
+ },
+
+ tearDown: function (callback) {
+ fs.writeFile = fsWriteFile;
+ callback();
+ },
+
+ testSuccess: function (test) {
+ var test_file = 'test_file.json',
+ test_error = null,
+ test_data = { key: "value" },
+ test_data_out = JSON.stringify(test_data);
+
+ stubWriteFile(test, test_file, test_data_out, test_error);
+ jsonfile.writeFile(test_file, test_data, function (err) {
+ test.ok(!err);
+ test.done();
+ });
+ },
+
+ testSuccessIndent: function (test) {
+ var test_file = 'test_file.json',
+ test_error = null,
+ test_data = { key: "value" },
+ test_indent = 2,
+ test_data_out = JSON.stringify(test_data, null, test_indent);
+
+ stubWriteFile(test, test_file, test_data_out, test_error);
+ jsonfile.writeFile(test_file, test_data, test_indent, function (err) {
+ test.ok(!err);
+ test.done();
+ });
+ },
+
+ testFileError: function (test) {
+ var test_file = 'test_file.json',
+ test_error = new Error("EPERM"),
+ test_data = { key: "value" },
+ test_data_out = JSON.stringify(test_data);
+
+ stubWriteFile(test, test_file, test_data_out, test_error);
+ jsonfile.writeFile(test_file, test_data, function (err) {
+ test.ok(err instanceof Error);
+ test.equal(err.message, "EPERM");
+ test.done();
+ });
+ },
+
+ testDataError: function (test) {
+ var test_file = 'test_file.json',
+ test_error = null,
+ test_data = {};
+
+ test_data.loop = test_data;
+ stubWriteFile(test, test_file, test_data, test_error);
+ jsonfile.writeFile(test_file, test_data, function (err) {
+ test.ok(err instanceof TypeError);
+ test.equal("Converting circular structure to JSON", err.message);
+ test.done();
+ });
+ }
+};
+
+exports.writeSyncTests = {
+
+ setUp: function (callback) {
+ callback();
+ },
+
+ tearDown: function (callback) {
+ fs.writeFileSync = fsWriteFileSync;
+ callback();
+ },
+
+ testSuccess: function (test) {
+ var test_file = 'test_file.json',
+ test_error = null,
+ test_data = { key: "value" },
+ test_data_out = JSON.stringify(test_data);
+
+ stubWriteFileSync(test, test_file, test_data_out, test_error);
+ jsonfile.writeFileSync(test_file, test_data);
+ test.done();
+ },
+
+ testSuccessIndent: function (test) {
+ var test_file = 'test_file.json',
+ test_error = null,
+ test_data = { key: "value" },
+ test_indent = 2,
+ test_data_out = JSON.stringify(test_data, null, test_indent);
+
+ stubWriteFileSync(test, test_file, test_data_out, test_error);
+ jsonfile.writeFileSync(test_file, test_data, test_indent);
+ test.done();
+ },
+
+ testFileError: function (test) {
+ var test_file = 'test_file.json',
+ test_error = new Error("EPERM"),
+ test_data = { key: "value" },
+ test_data_out = JSON.stringify(test_data);
+
+ stubWriteFileSync(test, test_file, test_data_out, test_error);
+ test.throws(function () {
+ jsonfile.writeFileSync(test_file, test_data);
+ }, "EPERM");
+ test.done();
+ },
+
+ testDataError: function (test) {
+ var test_file = 'test_file.json',
+ test_error = null,
+ test_data = {};
+
+ test_data.loop = test_data;
+ stubWriteFileSync(test, test_file, test_data, test_error);
+ test.throws(function () {
+ jsonfile.writeFileSync(test_file, test_data);
+ }, "Converting circular structure to JSON");
+ test.done();
+ }
+};
Please sign in to comment.
Something went wrong with that request. Please try again.