@hn3000/json-ref
Version:
simple resolver for json reference and json pointer
248 lines • 11.1 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.JsonReferenceTest = void 0;
var fs = require("fs");
var index_1 = require("../src/index");
var json_ref_processor_1 = require("../src/json-ref-processor");
var tsunit_async_1 = require("@hn3000/tsunit-async");
var JsonReferenceTest = /** @class */ (function (_super) {
__extends(JsonReferenceTest, _super);
function JsonReferenceTest() {
return _super !== null && _super.apply(this, arguments) || this;
}
JsonReferenceTest.prototype.testFileReferenceGet = function () {
var ref = index_1.JsonReference.get("lala.json#/blah");
this.areIdentical("lala.json", ref.filename);
this.areCollectionsIdentical(['blah'], ref.pointer.keys);
};
JsonReferenceTest.prototype.testFileReferenceGetCopy = function () {
var orig = index_1.JsonReference.get("lala.json#/blah");
var ref = index_1.JsonReference.get(orig);
this.areIdentical(orig, ref, 'JsonReference.get should not copy');
this.areIdentical("lala.json", ref.filename);
this.areCollectionsIdentical(['blah'], ref.pointer.keys);
};
JsonReferenceTest.prototype.testGetFilenameFromFullRef = function () {
var name = index_1.JsonReference.getFilename("lala.json#/blah");
this.areIdentical("lala.json", name);
};
JsonReferenceTest.prototype.testGetFilenameFromPtrOnlyRef = function () {
var name = index_1.JsonReference.getFilename("#/blah");
this.areIdentical("", name);
};
JsonReferenceTest.prototype.testGetFilenameFromNullRef = function () {
var name = index_1.JsonReference.getFilename(null);
this.areIdentical("", name);
};
JsonReferenceTest.prototype.testGetFilenameFromFilenameOnlyRef = function () {
var name = index_1.JsonReference.getFilename('lala.json');
this.areIdentical("lala.json", name);
};
JsonReferenceTest.prototype.testSimpleReference = function () {
var ref = new index_1.JsonReference("");
this.areIdentical("", ref.filename);
this.areCollectionsIdentical([], ref.pointer.keys);
};
JsonReferenceTest.prototype.testPointerReference = function () {
var ref = new index_1.JsonReference("#/lala");
this.areIdentical("", ref.filename);
this.areCollectionsIdentical(['lala'], ref.pointer.keys);
};
JsonReferenceTest.prototype.testFileReference = function () {
var ref = new index_1.JsonReference("./test/resource/json-ptr.test.json#/foo");
this.areIdentical("./test/resource/json-ptr.test.json", ref.filename);
this.areCollectionsIdentical(['foo'], ref.pointer.keys);
};
JsonReferenceTest.prototype.testResolveRefs = function () {
var _this = this;
var expander = new index_1.JsonReferenceProcessor(fetchFile);
var r = expander.expandRef("./test/resource/json-ptr.test.json#/foo");
return r.then(function (x) {
if (Array.isArray(x) && x[0] == 'bar' && x[1] == 'baz') {
_this.isTrue(true); // success!
}
else {
_this.fail("failure, didn't get [ 'bar', 'baz' ] got:" + JSON.stringify(x));
}
return x;
});
};
JsonReferenceTest.prototype.testResolveIndirectRefs = function () {
var _this = this;
var expander = new index_1.JsonReferenceProcessor(fetchFile);
var r = expander.expandRef("./test/resource/json-ptr.refs.json#/c");
return r.then(function (x) {
_this.areIdentical(1, x);
});
};
JsonReferenceTest.prototype.testResolveInvalidRefs = function () {
var _this = this;
var expander = new index_1.JsonReferenceProcessor(fetchFile);
var r = expander.expandRef("./test/resource/json-ptr.refs.json#/e");
return r.then(function (x) {
_this.isTruthy(x.$$ref);
_this.isFalse(x.$$filenotfound);
_this.isTrue(x.$$refnotfound);
});
};
JsonReferenceTest.prototype.testResolveNofileRefs = function () {
var _this = this;
var expander = new index_1.JsonReferenceProcessor(fetchFile);
var r = expander.expandRef("./test/resource/json-ptr.refs.json#/f");
return r.then(function (x) {
_this.isTruthy(x.$$ref);
_this.isTrue(x.$$filenotfound);
_this.isTrue(x.$$refnotfound);
});
};
JsonReferenceTest.prototype.testResolveIndirectIntermediateRefs = function () {
var _this = this;
var expander = new index_1.JsonReferenceProcessor(fetchFile);
var r = expander.expandRef("./test/resource/json-ptr.refs.json#/i");
return r.then(function (x) {
if (Array.isArray(x) && x[0] == 'bar' && x[1] == 'baz') {
_this.isTrue(true); // success!
}
else {
_this.fail("failure, didn't get [ 'bar', 'baz' ] got:" + JSON.stringify(x));
}
});
};
JsonReferenceTest.prototype.testExpandDynamicLiteralObj = function () {
var expander = new index_1.JsonReferenceProcessor(fetchFile);
var x = expander.expandDynamic({
a: { "$ref": "#/d/a" },
b: { "$ref": "#/d/b" },
d: {
a: "actual-a",
b: ["b1", "b2"]
}
}, "json");
if (Array.isArray(x.b) && x.b[0] == 'b1' && x.b[1] == 'b2' && x.a == "actual-a") {
this.isTrue(true); // success!
}
else {
this.fail('failure, didn\'t get { "a": "actual-a", "b": [ ... ] } got:' + JSON.stringify(x));
}
};
JsonReferenceTest.prototype.testExpandDynamicLiteralObjWithBasePointer = function () {
var expander = new index_1.JsonReferenceProcessor(fetchFile);
var x = expander.expandDynamic({
a: { "$ref": "#/x/d/a" },
b: { "$ref": "#/x/d/b" },
d: {
a: "actual-a",
b: ["b1", "b2"]
}
}, "json#/x");
if (Array.isArray(x.b) && x.b[0] == 'b1' && x.b[1] == 'b2' && x.a == "actual-a") {
this.isTrue(true); // success!
}
else {
this.fail('failure, didn\'t get { "a": "actual-a", "b": [ ... ] } got:' + JSON.stringify(x));
}
};
JsonReferenceTest.prototype.testExpandRefsInLiteralObj = function () {
var _this = this;
var expander = new index_1.JsonReferenceProcessor(fetchFile);
var p = expander.expandRefs({
a: { "$ref": "#/d/a" },
b: { "$ref": "#/d/b" },
d: {
a: "actual-a",
b: ["b1", "b2"]
}
}, "json");
p.then(function (x) {
if (Array.isArray(x.b) && x.b[0] == 'b1' && x.b[1] == 'b2' && x.a == "actual-a") {
_this.isTrue(true); // success!
}
else {
_this.fail('failure, didn\'t get { "a": "actual-a", "b": [ ... ] } got:' + JSON.stringify(x));
}
});
};
JsonReferenceTest.prototype.testResolveCycle = function () {
var processor = new index_1.JsonReferenceProcessor();
};
JsonReferenceTest.prototype.testNormalizePathUnchangedForDotlessPath = function () {
var path = './foo/bar/blub.bb';
var normalized = (0, json_ref_processor_1.normalizePath)(path);
this.areIdentical(path, normalized);
};
JsonReferenceTest.prototype.testNormalizePathHasSingleDotsRemovedPath = function () {
var path = './foo/./bar/././blub.bb';
var expected = './foo/bar/blub.bb';
var normalized = (0, json_ref_processor_1.normalizePath)(path);
this.areIdentical(expected, normalized);
};
JsonReferenceTest.prototype.testNormalizePathHasDoubleDotSegmentsRemovedPath = function () {
var path = './foo/../bar/../../blub.bb';
var expected = './../blub.bb';
var normalized = (0, json_ref_processor_1.normalizePath)(path);
this.areIdentical(expected, normalized);
};
JsonReferenceTest.prototype.testNormalizePathHasSingleAndDoubleDotSegmentsRemovedPath = function () {
var path = './foo/./../bar/.././../blub.bb';
var expected = './../blub.bb';
var normalized = (0, json_ref_processor_1.normalizePath)(path);
this.areIdentical(expected, normalized);
};
JsonReferenceTest.prototype.testJsonParseUnderstandsCommentedJson = function () {
var json = '{ "x": 1 /* lala */ }';
var obj = (0, json_ref_processor_1.jsonParse)(json);
this.areIdenticalObjects(obj, { x: 1 });
};
JsonReferenceTest.prototype.testJsonParseUnderstandsCommentedJsonWithRegexInIt = function () {
var json = '{ "x": 1 /* lala */, "re": "\\\\d{2}\\\\-?\\\\d{3}/?\\\\d{4}" }';
var obj = (0, json_ref_processor_1.jsonParse)(json);
this.areIdenticalObjects(obj, { x: 1, re: "\\d{2}\\-?\\d{3}/?\\d{4}" });
};
JsonReferenceTest.prototype.testJsonParseFailsWithIncompatiblyCommentedJson = function () {
var json = '{ "x": "http://a.b" /* lala */ }';
var obj = (0, json_ref_processor_1.jsonParse)(json);
this.areIdenticalObjects(obj, undefined);
};
JsonReferenceTest.prototype.areIdenticalObjects = function (a, b) {
if (a === b) {
return;
}
var ta = typeof (a);
this.areIdentical(ta, typeof (b));
if (ta !== 'object') {
this.areIdentical(a, b);
}
var ka = Object.keys(a);
var kb = Object.keys(b);
this.areIdentical(ka.length, kb.length, "objects have different number of keys: " + JSON.stringify(ka) + " != " + JSON.stringify(kb));
ka.sort();
for (var _i = 0, ka_1 = ka; _i < ka_1.length; _i++) {
var k = ka_1[_i];
this.areIdenticalObjects(a[k], b[k]);
}
};
return JsonReferenceTest;
}(tsunit_async_1.TestClass));
exports.JsonReferenceTest = JsonReferenceTest;
function fetchFile(x) {
return Promise.resolve(x).then(function (x) {
//console.log("reading ", x, process.cwd());
return fs.readFileSync(x, 'utf-8');
});
}
//# sourceMappingURL=json-ref.test.js.map