Created
July 12, 2014 11:38
-
-
Save sakura-crowd/b28ba8146f829f3f3254 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe('sakuraCrowd.jsonId.refToObj', function() { | |
it("参照オブジェクトを参照先オブジェクトに変換する", function() { | |
var target = [ | |
{"id":"id1", "value":10}, | |
{"$ref":"id1"} | |
]; | |
sakuraCrowd.jsonId.refToObj(target); | |
expect(target.length).toEqual(2); | |
expect(target[0]).toBe(target[1]); | |
}); | |
it("循環参照を検出したら該当する JsonPath を返して警告する", function() { | |
var target = [ | |
{"id":"id1", "value":10, "other":{"$ref":"id2"}}, | |
{"id":"id2", "other":{"$ref":"id1"}} | |
]; | |
var path = "$[0].other.other"; | |
expect(sakuraCrowd.jsonId.refToObj.bind(null, target)).toThrow("循環参照です。" + path); | |
}); | |
it("指定したオブジェクトの下位の場所から変換する", function() { | |
var target = [ | |
{"id":"id1", "value":10, "other":{"$ref":"id2"}}, | |
{"id":"id2", | |
"other":[ | |
{"id":"id3", "value":30}, | |
{"$ref":"id3"} | |
] | |
}, | |
]; | |
var target2 = [ | |
{"id":"id1", "value":10, "other":{"$ref":"id2"}}, | |
{"id":"id2", | |
"other":[ | |
{"id":"id3", "value":30}, | |
{"$ref":"id3"} | |
] | |
}, | |
]; | |
target2[1].other[1] = target2[1].other[0]; | |
expect(sakuraCrowd.jsonId.refToObj.bind(null, target, "$[1]")).toEqual(target2); | |
}); | |
it("指定したオブジェクトの下位の場所から変換する。それより上位のオブジェクトの循環参照を検知する", function() { | |
var target = [ | |
{"id":"id1", "value":10}, | |
{"id":"id2", | |
"other":[ | |
{"id":"id3", "value":30}, | |
{"$ref":"id2"} | |
] | |
}, | |
]; | |
path = "$[1].other[1]"; | |
expect(sakuraCrowd.jsonId.refToObj.bind(null, target, "$[1].other")).toThrow("循環参照です。" + path); | |
}); | |
it("指定したオブジェクトの下位の場所から変換する。 info に設定した外部の参照にも対応する。", function() { | |
var target = [ | |
{"id":"id1", "value":10}, | |
{"id":"id2", | |
"other":[ | |
{"id":"id3", "value":30}, | |
{"$ref":"id1"} | |
] | |
}, | |
]; | |
var target2 = [ | |
{"id":"id1", "value":10}, | |
{"id":"id2", | |
"other":[ | |
{"id":"id3", "value":30}, | |
{"$ref":"id1"} | |
] | |
}, | |
]; | |
target2[1].other[1] = target[0]; | |
expect(sakuraCrowd.jsonId.refToObj.bind(null, target, "$[1].other")).toEqual(target2); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
refToObj は指定したオブジェクトをルートにして参照を参照先に置換していましたが、利用する際に、子要素を継ぎ足していく必要がでたので機能拡張しました。
指定したオブジェクトがルートなのですが、処理対象はルートからではなく指定した JSONPath の要素から処理するようにします。
また、 info を渡すことで今回の処理では得られない ID の参照先についても置換を可能にします。