-
Notifications
You must be signed in to change notification settings - Fork 4
/
test.html
104 lines (101 loc) · 7.09 KB
/
test.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>QUnit Example</title>
<link rel="stylesheet" href="http://code.jquery.com/qunit/qunit-1.10.0.css">
</head>
<body>
<div id="qunit"></div>
<script src="http://code.jquery.com/qunit/qunit-1.10.0.js"></script>
<script src="jpath.js"></script>
<script type="text/javascript">
var obj = {
items: [
{a:1, foo:"test", b:true},
{a:2, foo:"test1", b:true},
{a:3, foo:"test2", b:false},
{a:4, foo:"test3", b:true}
],
items2: [
{names:[
{name:"Sam"},
{name:"John"},
{name:"Dave"},
{name:"Bob"}
], start:1},
{names:[
{name:"Dave"},
{name:"Joe"},
{name:"Jon"},
{name:"Steve"}
], start:2}
],
books: [
{ title: 'Merry Christmas' },
{ title: 'Pride & Heritage' },
{ title: 'Once ^on a time'},
{ title: '#1 Story' },
{ title: 'Escape from island\'s'}
],
other: [
{ foo:'buzz', buzz:null }
],
primitive: ["Sam", "John", "Steve", "Joe"]
};
test("Supported patterns", function(){
deepEqual(obj.items, jPath.filter(obj, "items"), 'jPath.filter(obj, "items")'); //console.log( "get items", jPath.filter(obj, "items"));
deepEqual([obj], jPath.filter(obj, "*"), 'jPath.filter(obj, "*")');
deepEqual(obj.items, jPath.filter(obj, "*.items"), 'jPath.filter(obj, "*.items")'); //console.log( "get *.items", jPath.filter(obj, "*.items"));
deepEqual(obj.items.slice(0,1), jPath.filter(obj, "items(0)"),'jPath.filter(obj, "items(0)")');
deepEqual([{a:3, foo:"test2", b:false}, {a:4, foo:"test3", b:true}], jPath.filter(obj, "items[a > 2]"), 'jPath.filter(obj, "items[a > 2]")'); //console.log( "get item w/condition (items[a > 2])", jPath.filter(obj, "items[a > 2]"));
deepEqual(["Sam","John","Dave","Bob"], jPath.filter(obj, "items2(0).names.name"), 'jPath.filter(obj, "items2(0).names.name")');
deepEqual(["Sam","John","Dave","Bob", "Dave","Joe","Jon","Steve"], jPath.filter(obj, "items2.names.name"),'jPath.filter(obj, "items2.names.name")'); //console.log( "get children of an arrays (items2.names.name)", jPath.filter(obj, "items2.names.name"));
deepEqual([{name:"Sam"}], jPath.filter(obj, "items2.names[name == Sam]"), 'jPath.filter(obj, "items2.names[name == Sam]")');
deepEqual([{name:"Sam"}], jPath.filter(obj, "items2.names[name == 'Sam']"), 'jPath.filter(obj, "items2.names[name == \'Sam\']")'); //console.log( "get children w/ condition (items2.names[name == Sam])", jPath.filter(obj, "items2.names[name == Sam]"));
deepEqual(["Sam"], jPath.filter(obj, "items2.names[name == Sam].name"), 'jPath.filter(obj, "items2.names[name == Sam].name")');
deepEqual(["Sam", "Jon"], jPath.filter(obj, "items2.names[name == Sam || name == Jon].name"), 'jPath.filter(obj, "items2.names[name == Sam || name == Jon].name")'); //Using and in the condition
deepEqual(["Sam"], jPath.filter(obj, "items2.names[name == Sam && name.length == 3].name"), 'jPath.filter(obj, "items2.names[name == Sam && name.length == 3].name")');
deepEqual(["Sam", "Jon"], jPath.filter(obj, "items2.names[(name == Sam && name.length == 3) || name == Jon].name"), 'jPath.filter(obj, "items2.names[(name == Sam && name.length == 3) || name == Jon].name")');
deepEqual(["Sam"], jPath.filter(obj, "primitive[. == Sam]"), 'jPath.filter(obj, "primitive[. == Sam]")');
});
test("Condition tests", function(){
deepEqual([3,4], jPath.filter(obj, "items[a > 2].a"), 'jPath.filter(obj, "items[a > 2].a")');
deepEqual([2, 3,4], jPath.filter(obj, "items[a >= 2].a"), 'jPath.filter(obj, "items[a >= 2].a")');
deepEqual([1], jPath.filter(obj, "items[a < 2].a"), 'jPath.filter(obj, "items[a < 2].a")');
deepEqual([1,2], jPath.filter(obj, "items[a <= 2].a"), 'jPath.filter(obj, "items[a <= 2].a")');
deepEqual([2], jPath.filter(obj, "items[a == 2].a"), 'jPath.filter(obj, "items[a == 2].a")');
deepEqual([1,3,4], jPath.filter(obj, "items[a != 2].a"), 'jPath.filter(obj, "items[a != 2].a")');
deepEqual(["Sam", "Steve"], jPath.filter(obj, "items2.names[name ^= S].name"), 'jPath.filter(obj, "items2.names[name ^= S].name")');
deepEqual(["Dave", "Dave", "Joe", "Steve"], jPath.filter(obj, "items2.names[name $= e].name"), 'jPath.filter(obj, "items2.names[name $= e].name")');
deepEqual(["Sam"], jPath.filter(obj, "items2.names[name ~= sam].name"), 'jPath.filter(obj, "items2.names[name ~= sam].name")');
deepEqual(["John"], jPath.filter(obj, "items2.names[name *= oh].name"), 'jPath.filter(obj, "items2.names[name *= oh].name")');
deepEqual(["Dave", "Dave"], jPath.filter(obj, "items2.names[name ? Dave].name", function(l,r){ return l === r; }), "Custom compare function w/ ? as operator");
});
test("Complex value matching", function(){
deepEqual([{ title: 'Merry Christmas' }], jPath.filter(obj, "books[title == 'Merry Christmas']"), 'jPath.filter(obj, "books[title == \'Merry Christmas\']")');
deepEqual([{ title: 'Pride & Heritage' }], jPath.filter(obj, "books[title == 'Pride & Heritage']"), 'jPath.filter(obj, "books[title == \'Pride & Heritage\']")');
deepEqual([{ title: 'Once ^on a time' }], jPath.filter(obj, "books[title *= '^']"), 'jPath.filter(obj, "books[title *= \'^\']")');
deepEqual([{ title: '#1 Story' }], jPath.filter(obj, "books[title ^= '#']"), 'jPath.filter(obj, "books[title ^= \'#\']")');
deepEqual([{ title: 'Escape from island\'s' }], jPath.filter(obj, 'books[title *= "\'"]'), 'jPath.filter(obj, \'books[title *= "\\\'"]\')');
deepEqual([{ title: 'Escape from island\'s' }], jPath.filter(obj, 'books[title == "Escape from island\'s"]'), 'jPath.filter(obj, \'books[title *= "Escape from island\\\'s"]\')');
deepEqual([{ title: 'Escape from island\'s' }], jPath.filter(obj, "books[title == 'Escape from island\\\'s']"), 'jPath.filter(obj, "books[title == \'Escape from island\\\\\\\'s\']")');
});
test("Reserved words matching", function(){
deepEqual([{foo:'buzz', buzz:null}], jPath.filter(obj, 'other[foo != undefined]'), 'jPath.filter(obj, "other[foo != undefined]")');
deepEqual([{foo:'buzz', buzz:null}], jPath.filter(obj, 'other[buzz == null]'), 'jPath.filter(obj, "other[buzz == null]")');
deepEqual([{foo:'buzz', buzz:null}], jPath.filter(obj, 'other[fake == undefined]'), 'jPath.filter(obj, "other[fake == undefined]")');
});
test("Using select and native jPath methods", function(){
deepEqual(obj.items, jPath.select(obj, "*.items").val(), 'jPath.select(obj, "*.items").val()');
deepEqual({a:1, foo:"test", b:true}, jPath.select(obj, "*.items").first(), 'jPath.select(obj, "*.items").first()');
deepEqual({a:4, foo:"test3", b:true}, jPath.select(obj, "*.items").last(), 'jPath.select(obj, "*.items").last()');
deepEqual([{a:1, foo:"test", b:true},{a:4, foo:"test3", b:true}], jPath.select(obj, "items[a == 1]").and("items[a == 4]").val(), 'jPath.select(obj, "items[a == 1]").and("items[a == 4]").val()');
deepEqual({a:1, foo:"test", b:true}, jPath.select(obj, "*.items").eq(0), 'jPath.select(obj, "*.items").eq(0)');
deepEqual([{a:1, foo:"test", b:true},{ title: 'Merry Christmas' }], jPath.select(obj.items, "*[a == 1]").from(obj.books).and("*[title == 'Merry Christmas']").val(), 'jPath.select(obj.items, "*[a == 1]").from(obj.books).and("*[title == \'Merry Christmas\']").val()');
});
</script>
</head>
<body>
</body>
</html>