Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added nodeunit dev dependency,

tests based on examples here: http://goessner.net/articles/JsonPath/
tests with properties containing $ and @ symbols for the purpose of testing common cases of JSON produced by xml2js
  • Loading branch information...
commit 07b9e9780da9a7a7081880afac754096b209d6bd 1 parent 9e38d0e
Mike Brevoort authored
View
1  .gitignore
@@ -0,0 +1 @@
+node_modules
View
5 Makefile
@@ -0,0 +1,5 @@
+test:
+ ./node_modules/nodeunit/bin/nodeunit test/*.js
+
+.PHONY: test
+
View
4 package.json
@@ -9,5 +9,7 @@
},
"main" : "./lib/jsonpath",
"dependencies": {},
- "devDependencies": {}
+ "devDependencies": {
+ "nodeunit": "latest"
+ }
}
View
54 test/test.at_and_dollar.js
@@ -0,0 +1,54 @@
+var jsonpath = require("../").eval
+ , testCase = require('nodeunit').testCase
+
+
+var t1 = {
+ simpleString: "simpleString",
+ "@" : "@asPropertyName",
+ "$" : "$asPropertyName",
+ "a$a": "$inPropertyName",
+ "$": {
+ "@": "withboth",
+ },
+ a: {
+ b: {
+ c: "food"
+ }
+ }
+};
+
+
+module.exports = testCase({
+
+
+ // ============================================================================
+ "test undefined, null": function(test) {
+ // ============================================================================
+ test.expect(5);
+ test.equal(undefined, jsonpath(undefined, "foo"));
+ test.equal(null, jsonpath(null, "foo"));
+ test.equal(undefined, jsonpath({}, "foo")[0]);
+ test.equal(undefined, jsonpath({ a: "b" }, "foo")[0]);
+ test.equal(undefined, jsonpath({ a: "b" }, "foo")[100]);
+ test.done();
+ },
+
+
+ // ============================================================================
+ "test $ and @": function(test) {
+ // ============================================================================
+ test.expect(7);
+ test.equal(t1["$"], jsonpath(t1, "\$")[0]);
+ test.equal(t1["$"], jsonpath(t1, "$")[0]);
+ test.equal(t1["a$a"], jsonpath(t1, "a$a")[0]);
+ test.equal(t1["@"], jsonpath(t1, "\@")[0]);
+ test.equal(t1["@"], jsonpath(t1, "@")[0]);
+ test.equal(t1["$"]["@"], jsonpath(t1, "$.$.@")[0]);
+ test.equal(undefined, jsonpath(t1, "\@")[1]);
+
+ test.done();
+ }
+
+});
+
+
View
177 test/test.examples.js
@@ -0,0 +1,177 @@
+var jsonpath = require("../").eval
+ , testCase = require('nodeunit').testCase
+
+// tests based on examples at http://goessner.net/articles/JsonPath/
+
+var json = {"store": {
+ "book": [
+ { "category": "reference",
+ "author": "Nigel Rees",
+ "title": "Sayings of the Century",
+ "price": 8.95
+ },
+ { "category": "fiction",
+ "author": "Evelyn Waugh",
+ "title": "Sword of Honour",
+ "price": 12.99
+ },
+ { "category": "fiction",
+ "author": "Herman Melville",
+ "title": "Moby Dick",
+ "isbn": "0-553-21311-3",
+ "price": 8.99
+ },
+ { "category": "fiction",
+ "author": "J. R. R. Tolkien",
+ "title": "The Lord of the Rings",
+ "isbn": "0-395-19395-8",
+ "price": 22.99
+ }
+ ],
+ "bicycle": {
+ "color": "red",
+ "price": 19.95
+ }
+ }
+};
+
+
+module.exports = testCase({
+
+ // ============================================================================
+ "wildcards": function(test) {
+ // ============================================================================
+ test.expect(1);
+ var books = json.store.book;
+ var expected = [books[0].author, books[1].author, books[2].author, books[3].author];
+ var result = jsonpath(json, "$.store.book[*].author");
+ test.deepEqual(expected, result);
+
+ test.done();
+ },
+
+ // ============================================================================
+ "all properties, entire tree": function(test) {
+ // ============================================================================
+ test.expect(1);
+ var books = json.store.book;
+ var expected = [books[0].author, books[1].author, books[2].author, books[3].author];
+ var result = jsonpath(json, "$..author");
+ test.deepEqual(expected, result);
+
+ test.done();
+ },
+
+ // ============================================================================
+ "all sub properties, single level": function(test) {
+ // ============================================================================
+ test.expect(1);
+ var expected = [json.store.book, json.store.bicycle];
+ var result = jsonpath(json, "$.store.*");
+ test.deepEqual(expected, result);
+
+ test.done();
+ },
+
+ // ============================================================================
+ "all sub properties, entire tree": function(test) {
+ // ============================================================================
+ test.expect(1);
+ var books = json.store.book;
+ var expected = [books[0].price, books[1].price, books[2].price, books[3].price, json.store.bicycle.price];
+ var result = jsonpath(json, "$.store..price");
+ test.deepEqual(expected, result);
+
+ test.done();
+ },
+
+ // ============================================================================
+ "n property of entire tree": function(test) {
+ // ============================================================================
+ test.expect(1);
+ var books = json.store.book;
+ var expected = [books[2]];
+ var result = jsonpath(json, "$..book[2]");
+ test.deepEqual(expected, result);
+
+ test.done();
+ },
+
+ // ============================================================================
+ "last property of entire tree": function(test) {
+ // ============================================================================
+ test.expect(2);
+ var books = json.store.book;
+ var expected = [books[3]];
+ var result = jsonpath(json, "$..book[(@.length-1)]");
+ test.deepEqual(expected, result);
+
+ result = jsonpath(json, "$..book[-1:]");
+ test.deepEqual(expected, result);
+
+ test.done();
+ },
+
+ // ============================================================================
+ "range of property of entire tree": function(test) {
+ // ============================================================================
+ test.expect(2);
+ var books = json.store.book;
+ var expected = [books[0], books[1]];
+ var result = jsonpath(json, "$..book[0,1]");
+ test.deepEqual(expected, result);
+
+ result = jsonpath(json, "$..book[:2]");
+ test.deepEqual(expected, result);
+
+ test.done();
+ },
+
+ // ============================================================================
+ "filter all properties if sub property exists,o entire tree": function(test) {
+ // ============================================================================
+ test.expect(1);
+ var books = json.store.book;
+ var expected = [books[2], books[3]];
+ var result = jsonpath(json, "$..book[?(@.isbn)]");
+ test.deepEqual(expected, result);
+
+ test.done();
+ },
+
+ // ============================================================================
+ "filter all properties if sub property greater than of entire tree": function(test) {
+ // ============================================================================
+ test.expect(1);
+ var books = json.store.book;
+ var expected = [books[0], books[2]];
+ var result = jsonpath(json, "$..book[?(@.price<10)]");
+ test.deepEqual(expected, result);
+
+ test.done();
+ },
+
+ // ============================================================================
+ "all properties of a json structure": function(test) {
+ // ============================================================================
+ // test.expect(1);
+ var expected = [
+ json.store,
+ json.store.book,
+ json.store.bicycle,
+ ];
+ json.store.book.forEach(function(book) { expected.push(book); });
+ json.store.book.forEach(function(book) { Object.keys(book).forEach(function(p) { expected.push(book[p]); })});
+ expected.push(json.store.bicycle.color);
+ expected.push(json.store.bicycle.price);
+
+ var result = jsonpath(json, "$..*");
+ test.deepEqual(expected, result);
+
+ test.done();
+ }
+
+
+
+
+});
Please sign in to comment.
Something went wrong with that request. Please try again.