ltx-xpath
Version:
xpath evaluator for ltx
274 lines (256 loc) • 8.41 kB
JavaScript
(function() {
var Element, debug, evaluate, inspect, parse;
Element = require('ltx').Element;
parse = require('xpath-parser').parse;
evaluate = require('../lib/evaluate').evaluate;
debug = require('debug')('test:eval');
inspect = require('eyes').inspector({
stream: null
});
module.exports = {
simple: function(æ) {
var el, elem, expressions, match, _i, _len;
expressions = parse("/über/o");
elem = new Element('über').c('c').up().c('o').up().c('o').up().c('l').up();
debug(inspect(expressions));
debug(elem.toString());
match = evaluate(expressions, [elem]);
for (_i = 0, _len = match.length; _i < _len; _i++) {
el = match[_i];
debug(el.toString());
æ.equals("o", el.name);
}
æ.equals(2, match.length);
return æ.done();
},
root: function(æ) {
var el, elem, expressions, match, _i, _len;
expressions = parse("/");
elem = new Element('root').c('beer').up();
debug(inspect(expressions));
debug(elem.toString());
match = evaluate(expressions, [elem]);
for (_i = 0, _len = match.length; _i < _len; _i++) {
el = match[_i];
debug(el.toString());
æ.equals("root", el.name);
}
æ.equals(1, match.length);
return æ.done();
},
descendant: function(æ) {
var el, elem, expressions, match, _i, _len, _ref, _ref1;
expressions = parse("//o");
elem = new Element('ü').c('b').c('o').c('r').c('c').up().c('o').up().c('o').up().c('l').up().up().up().up();
debug(inspect(expressions));
debug(elem.toString());
match = evaluate(expressions, [elem]);
æ.equals(1, (_ref = match[0]) != null ? (_ref1 = _ref.children) != null ? _ref1.length : void 0 : void 0);
for (_i = 0, _len = match.length; _i < _len; _i++) {
el = match[_i];
debug(el.toString());
æ.equals("o", el.name);
}
æ.equals(3, match.length);
return æ.done();
},
op: function(æ) {
var el, elem, expressions, match, _i, _len;
expressions = parse("c | o | l");
elem = new Element('über').c('c').up().c('o').up().c('o').up().c('l').up();
debug(inspect(expressions));
debug(elem.toString());
match = evaluate(expressions, [elem]);
for (_i = 0, _len = match.length; _i < _len; _i++) {
el = match[_i];
debug(el.toString());
æ.equals("o", el.name.replace(/c|l/, "o"));
}
æ.equals(4, match.length);
return æ.done();
},
presence: function(æ) {
var el, elem1, elem2, expressions, match, _i, _len;
expressions = parse("/presence[@type='chat']");
elem1 = new Element('presence', {
type: 'chat'
}).c('show').t('chat').up().c('status').t('foo').root();
elem2 = new Element('presence', {
type: 'subscribed',
to: "juliet@domain.lit"
});
debug(inspect(expressions));
debug(elem1.toString());
debug(elem2.toString());
match = evaluate(expressions, [elem1, elem2]);
for (_i = 0, _len = match.length; _i < _len; _i++) {
el = match[_i];
debug(el.toString());
æ.equals("presence", el.name);
æ.equals("chat", el.attrs.type);
}
æ.equals(1, match.length);
return æ.done();
},
and_presence: function(æ) {
var el, elem1, elem2, expressions, match, _i, _len;
expressions = parse("/presence[@type='chat' or @id='id']");
debug(inspect(expressions));
elem1 = new Element('presence', {
type: 'chat',
id: 'id'
}).c('show').t('chat').up().c('status').t('foo').root();
elem2 = new Element('presence', {
type: 'subscribed',
to: "juliet@domain.lit"
});
debug(elem1.toString());
debug(elem2.toString());
match = evaluate(expressions, [elem1, elem2]);
for (_i = 0, _len = match.length; _i < _len; _i++) {
el = match[_i];
debug(el.toString());
æ.equals("presence", el.name);
æ.equals("chat", el.attrs.type);
}
æ.equals(1, match.length);
return æ.done();
},
info: function(æ) {
var el, elem1, elem2, expressions, m, match, ns, _i, _len, _ref;
ns = "http://jabber.org/protocol/disco#info";
expressions = parse("/iq[@type=get]/info:query");
debug(inspect(expressions));
elem1 = new Element("iq", {
to: "juliet@domain.lit",
id: "id",
type: "get"
}).c("query", {
xmlns: ns
}).root();
elem2 = new Element("iq", {
to: "juliet@domain.lit",
id: "id",
type: "get"
}).c("query", {
xmlns: "other"
}).root();
debug(elem1.toString());
debug(elem2.toString());
match = evaluate(expressions, [elem1, elem2], {
info: ns
});
for (_i = 0, _len = match.length; _i < _len; _i++) {
m = match[_i];
el = m.root();
debug(el.toString());
æ.equals("iq", el.name);
æ.equals("get", el.attrs.type);
æ.equals(ns, el != null ? (_ref = el.children) != null ? _ref[0].getNS() : void 0 : void 0);
}
æ.equals(1, match.length);
return æ.done();
},
'self::info': function(æ) {
var el, elem1, elem2, expressions, m, match, ns, _i, _len, _ref;
ns = "http://jabber.org/protocol/disco#info";
expressions = parse("self::iq[@type=get]/info:query");
debug(inspect(expressions));
elem1 = new Element("iq", {
to: "juliet@domain.lit",
id: "id",
type: "get"
}).c("query", {
xmlns: ns
}).root();
elem2 = new Element("iq", {
to: "juliet@domain.lit",
id: "id",
type: "get"
}).c("query", {
xmlns: "other"
}).root();
debug(elem1.toString());
debug(elem2.toString());
match = evaluate(expressions, [elem1, elem2], {
info: ns
});
for (_i = 0, _len = match.length; _i < _len; _i++) {
m = match[_i];
el = m.root();
debug(el.toString());
æ.equals("iq", el.name);
æ.equals("get", el.attrs.type);
æ.equals(ns, el != null ? (_ref = el.children) != null ? _ref[0].getNS() : void 0 : void 0);
}
æ.equals(1, match.length);
return æ.done();
},
'not': function(æ) {
var el, elem1, elem2, expressions, match, ns, _i, _len;
ns = "http://jabber.org/protocol/disco#info";
expressions = parse("/iq[not(@type)]/info:query/self::*");
elem1 = new Element("iq", {
to: "juliet@domain.lit",
id: "id"
}).c("query", {
xmlns: ns
}).root();
elem2 = new Element("iq", {
to: "juliet@domain.lit",
id: "id",
type: "get"
}).c("query", {
xmlns: ns
}).root();
debug(elem1.toString());
debug(elem2.toString());
match = evaluate(expressions, [elem1, elem2], {
info: ns
});
debug(inspect(expressions));
for (_i = 0, _len = match.length; _i < _len; _i++) {
el = match[_i];
debug(el.toString());
æ.equals("query", el.name);
æ.equals(void 0, el.attrs.type);
æ.equals(ns, el.getNS());
}
æ.equals(1, match.length);
return æ.done();
},
'union': function(æ) {
var el, elem1, elem2, expressions, match, ns, _i, _len;
ns = "http://jabber.org/protocol/disco#info";
expressions = parse("/foobar | /iq[not(@type)]/info:query/self::*");
elem1 = new Element("iq", {
to: "juliet@domain.lit",
id: "id"
}).c("query", {
xmlns: ns
}).root();
elem2 = new Element("iq", {
to: "juliet@domain.lit",
id: "id",
type: "get"
}).c("query", {
xmlns: ns
}).root();
debug(elem1.toString());
debug(elem2.toString());
match = evaluate(expressions, [elem1, elem2], {
info: ns
});
debug(inspect(expressions));
for (_i = 0, _len = match.length; _i < _len; _i++) {
el = match[_i];
debug(el.toString());
æ.equals("query", el.name);
æ.equals(void 0, el.attrs.type);
æ.equals(ns, el.getNS());
}
æ.equals(1, match.length);
return æ.done();
}
};
}).call(this);