Permalink
Browse files

adding unit tests for the simple selector regex and fixing it on sele…

…ctors like '**'. Adding specs for the empty attribute selector regex.
  • Loading branch information...
1 parent 33b1bdc commit 76a538340ae6a44699cdeb2cbf4967567dda24e5 @fabiomcosta fabiomcosta committed Feb 19, 2011
Showing with 96 additions and 3 deletions.
  1. +1 −1 SlickSpec/Configuration.js
  2. +7 −1 SlickSpec/setup.js
  3. +87 −0 SlickSpec/specs/unit.js
  4. +1 −1 Source/Slick.Finder.js
@@ -21,7 +21,7 @@ Configuration.sets = {
'specs': {
path: 'specs/',
files: [
- 'syntax', 'api', 'engine_bugs', 'html', 'html5',
+ 'unit', 'syntax', 'api', 'engine_bugs', 'html', 'html5',
'select_nth-child', 'select_exhaustive', 'mock_template',
'isxml', 'xml',
'match', 'parser',
View
@@ -5,6 +5,12 @@ var runnerOnLoad = global.onload;
global.onload = function(){
+ // Unit tests
+
+ if (unitTests) unitTests();
+
+ // Mock creation
+
Mock.CreateTemplate('Generic HTML4 (transitional)', '../mocks/template-transitional.html');
Mock.CreateTemplate('Generic HTML4 (standard)', '../mocks/template-standard.html');
Mock.CreateTemplate('Generic HTML4 (almost-standard)', '../mocks/template-almost.html');
@@ -40,7 +46,7 @@ global.onload = function(){
setupMethods(this); new Mock('', setupMethods);
verifySetupMethods(this); new Mock('', verifySetupMethods);
if (verifySetupContext) new Mock('', verifySetupContext);
- specsSlickAPI(this);
+ if (specsSlickAPI) specsSlickAPI(this);
// Parser specs
@@ -0,0 +1,87 @@
+var unitTests = function(){
+
+ describe('Simple Selector Regex', function(){
+
+ // TODO: how could i access this regex from here?
+ var reSimpleSelector = /^([#.]?)((?:[\w-]+|\*))$/;
+
+ beforeEach(function(){
+ this.addMatchers({
+ toBeSimpleSelector: function(){
+ return reSimpleSelector.test(this.actual);
+ }
+ });
+ });
+
+ it('should match simple tag selectors', function(){
+ expect('div').toBeSimpleSelector();
+ expect('span').toBeSimpleSelector();
+ expect('header').toBeSimpleSelector();
+ expect('*').toBeSimpleSelector();
+ expect('.a-more_complex-tag').toBeSimpleSelector();
+ });
+
+ it('should match simple id selectors', function(){
+ expect('#id').toBeSimpleSelector();
+ expect('#a-more_complex-id').toBeSimpleSelector();
+ });
+
+ it('should match simple class selectors', function(){
+ expect('.class').toBeSimpleSelector();
+ expect('.a-more_complex-class').toBeSimpleSelector();
+ });
+
+ it('should NOT match names with non-wordy chars', function(){
+ expect('#dsdßå∂').not.toBeSimpleSelector();
+ expect('.dsdßå∂').not.toBeSimpleSelector();
+ expect('dsd߆¨∂ƒ').not.toBeSimpleSelector();
+ expect('***').not.toBeSimpleSelector();
+ });
+
+ it('should NOT match attr selectors', function(){
+ expect('[attr]').not.toBeSimpleSelector();
+ expect('[attr=value]').not.toBeSimpleSelector();
+ expect('[attr="value"]').not.toBeSimpleSelector();
+ expect('[attr*="value"]').not.toBeSimpleSelector();
+ });
+
+ it('should NOT match pseudo-class selectors', function(){
+ expect(':check').not.toBeSimpleSelector();
+ expect(':custom').not.toBeSimpleSelector();
+ });
+
+ it('should NOT match complex selectors', function(){
+ expect('div .class').not.toBeSimpleSelector();
+ expect('div>.class').not.toBeSimpleSelector();
+ expect('div #id .class').not.toBeSimpleSelector();
+ });
+
+ });
+
+ describe('Empty Attribute Regex', function(){
+ var reEmptyAttribute = /\[.+[*$^]=(?:""|'')?\]/;
+
+ beforeEach(function(){
+ this.addMatchers({
+ toBeEmptyAtributeSelector: function(){
+ return reEmptyAttribute.test(this.actual);
+ }
+ });
+ });
+
+ it('should match empty attribute selectors', function(){
+ expect('[attr*=""]').toBeEmptyAtributeSelector();
+ expect('[attr$=""]').toBeEmptyAtributeSelector();
+ expect('[attr^=""]').toBeEmptyAtributeSelector();
+ expect("[attr*='']").toBeEmptyAtributeSelector();
+ expect("[attr$='']").toBeEmptyAtributeSelector();
+ expect("[attr^='']").toBeEmptyAtributeSelector();
+ expect('[attr*=]').toBeEmptyAtributeSelector();
+ expect('[attr$=]').toBeEmptyAtributeSelector();
+ expect('[attr^=]').toBeEmptyAtributeSelector();
+ expect('#some .class [attr^=]').toBeEmptyAtributeSelector();
+ });
+ });
+
+};
+
@@ -197,7 +197,7 @@ local.setDocument = function(document){
// Main Method
-var reSimpleSelector = /^([#.]?)([\w*-]+)$/,
+var reSimpleSelector = /^([#.]?)((?:[\w-]+|\*))$/,
reEmptyAttribute = /\[.+[*$^]=(?:""|'')?\]/,
qsaFailExpCache = {};

0 comments on commit 76a5383

Please sign in to comment.