stew-select
Version:
CSS selectors that allow regular expressions. Stew is a meatier soup.
183 lines (173 loc) • 7.09 kB
JavaScript
// Generated by CoffeeScript 1.6.3
(function() {
var FACTORY, HOMEDIR, LIB_DIR, PredicateFactory, fs, path, should;
should = require('should');
fs = require('fs');
path = require('path');
HOMEDIR = path.join(__dirname, '..');
LIB_DIR = fs.existsSync(path.join(HOMEDIR, 'lib-cov')) ? path.join(HOMEDIR, 'lib-cov') : path.join(HOMEDIR, 'lib');
PredicateFactory = require(path.join(LIB_DIR, 'predicate-factory')).PredicateFactory;
FACTORY = new PredicateFactory();
describe("PredicateFactory", function() {
describe("by_class_predicate", function() {
it('can handle undefined nodes and other edge cases', function(done) {
FACTORY.by_class_predicate('x')(null).should.not.be.ok;
FACTORY.by_class_predicate('x')({}).should.not.be.ok;
FACTORY.by_class_predicate('x')({
attribs: []
}).should.not.be.ok;
FACTORY.by_class_predicate('x')({
attribs: {}
}).should.not.be.ok;
FACTORY.by_class_predicate('x')({
attribs: {
"class": null
}
}).should.not.be.ok;
return done();
});
it('is case sensitive', function(done) {
var node;
node = {
attribs: {
"class": 'FOO'
}
};
FACTORY.by_class_predicate('foo')(node).should.not.be.ok;
FACTORY.by_class_predicate('FOO')(node).should.be.ok;
return done();
});
it('returns true if the given string is an exact match for the node\'s class', function(done) {
var node;
node = {
attribs: {
"class": 'foo'
}
};
FACTORY.by_class_predicate('foo')(node).should.be.ok;
FACTORY.by_class_predicate('bar')(node).should.not.be.ok;
FACTORY.by_class_predicate('food')(node).should.not.be.ok;
return done();
});
it('returns true if the given string is an exact match to one of the node\'s many classes', function(done) {
var node;
node = {
attribs: {
"class": 'foo bar'
}
};
FACTORY.by_class_predicate('foo')(node).should.be.ok;
FACTORY.by_class_predicate('bar')(node).should.be.ok;
FACTORY.by_class_predicate('food')(node).should.not.be.ok;
FACTORY.by_class_predicate('oo')(node).should.not.be.ok;
FACTORY.by_class_predicate('o b')(node).should.not.be.ok;
FACTORY.by_class_predicate('o ba')(node).should.not.be.ok;
FACTORY.by_class_predicate('foo bar')(node).should.not.be.ok;
return done();
});
it('returns true if the given regex matches the node\'s class', function(done) {
var node;
node = {
attribs: {
"class": 'foo'
}
};
FACTORY.by_class_predicate(/foo/)(node).should.be.ok;
FACTORY.by_class_predicate(/^foo$/)(node).should.be.ok;
FACTORY.by_class_predicate(/fo+/)(node).should.be.ok;
FACTORY.by_class_predicate(/o/)(node).should.be.ok;
FACTORY.by_class_predicate(/^f/)(node).should.be.ok;
FACTORY.by_class_predicate(/f$/)(node).should.not.be.ok;
FACTORY.by_class_predicate(/f[aeiou]{2}$/)(node).should.be.ok;
FACTORY.by_class_predicate(/f[aeiou]{3}$/)(node).should.not.be.ok;
FACTORY.by_class_predicate(/FOO/i)(node).should.be.ok;
return done();
});
return it('returns true if the given regex matches one of the node\'s class', function(done) {
var node;
node = {
attribs: {
"class": 'x foo bar'
}
};
FACTORY.by_class_predicate(/foo/)(node).should.be.ok;
FACTORY.by_class_predicate(/^foo$/)(node).should.be.ok;
FACTORY.by_class_predicate(/fo+/)(node).should.be.ok;
FACTORY.by_class_predicate(/o/)(node).should.be.ok;
FACTORY.by_class_predicate(/^f/)(node).should.be.ok;
FACTORY.by_class_predicate(/f$/)(node).should.not.be.ok;
FACTORY.by_class_predicate(/f[aeiou]{2}$/)(node).should.be.ok;
FACTORY.by_class_predicate(/f[aeiou]{3}$/)(node).should.not.be.ok;
FACTORY.by_class_predicate(/FOO/i)(node).should.be.ok;
FACTORY.by_class_predicate(/bar/)(node).should.be.ok;
return done();
});
});
describe("by_attr_value_predicate", function() {
it('returns true if the value of the given attribute name matches the given string', function(done) {
var node;
node = {
attribs: {
foo: 'bar qux'
}
};
FACTORY.by_attr_value_predicate('foo', 'bar qux')(node).should.be.ok;
FACTORY.by_attr_value_predicate('foo', 'bar')(node).should.not.be.ok;
FACTORY.by_attr_value_predicate('bar', 'bar qux')(node).should.not.be.ok;
return done();
});
it('returns true if the value of the given attribute name matches the given regex', function(done) {
var node;
node = {
attribs: {
foo: 'bar qux'
}
};
FACTORY.by_attr_value_predicate('foo', /bar qux/)(node).should.be.ok;
FACTORY.by_attr_value_predicate('foo', /^BAR\squx$/i)(node).should.be.ok;
FACTORY.by_attr_value_predicate('foo', /bar/)(node).should.be.ok;
return done();
});
return it('can be used with the class attribute', function(done) {
var node;
node = {
attribs: {
"class": 'foo'
}
};
FACTORY.by_attr_value_predicate('class', 'foo')(node).should.be.ok;
FACTORY.by_attr_value_predicate('class', 'fo')(node).should.not.be.ok;
FACTORY.by_attr_value_predicate('class', 'bar')(node).should.not.be.ok;
FACTORY.by_attr_value_predicate('class', 'food')(node).should.not.be.ok;
node = {
attribs: {
"class": 'foo bar'
}
};
FACTORY.by_attr_value_predicate('class', 'foo bar')(node).should.be.ok;
FACTORY.by_attr_value_predicate('class', 'foo')(node).should.not.be.ok;
FACTORY.by_attr_value_predicate('class', 'bar')(node).should.not.be.ok;
FACTORY.by_attr_value_predicate('class', /foo/)(node).should.be.ok;
FACTORY.by_attr_value_predicate('class', /bar/)(node).should.be.ok;
FACTORY.by_attr_value_predicate('class', /^foo b/)(node).should.be.ok;
FACTORY.by_attr_value_predicate('class', /^foo$/)(node).should.not.be.ok;
return done();
});
});
return describe("by_attr_exists_predicate", function() {
return it('returns true if the given attribute exists', function(done) {
var node;
node = {
attribs: {
foo: 'bar'
}
};
FACTORY.by_attr_exists_predicate('foo')(node).should.be.ok;
FACTORY.by_attr_exists_predicate(/^fO{2}$/i)(node).should.be.ok;
FACTORY.by_attr_exists_predicate('bar')(node).should.not.be.ok;
FACTORY.by_attr_exists_predicate(/bar/)(node).should.not.be.ok;
return done();
});
});
});
}).call(this);