Skip to content

Commit

Permalink
add more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jonschlinkert committed May 29, 2017
1 parent 0f65651 commit c3bc209
Show file tree
Hide file tree
Showing 15 changed files with 280 additions and 4 deletions.
27 changes: 27 additions & 0 deletions test/api.all.js
@@ -0,0 +1,27 @@
'use strict';

var path = require('path');
var assert = require('assert');
var mm = require('..');

describe('.all()', function() {
it('should throw an error when value is not a string', function() {
assert.throws(function() {
mm.all();
});
});

it('should return true when all patterns match the given string', function() {
assert(mm.all('z', ['z', '*', '[a-z]']));
assert(mm.all('b', 'b'));
assert(mm.all('b', '*'));
});

it('should return false when some patterns do not match', function() {
assert(!mm.all('a', ['a', 'b', '*']));
});

it('should arrayify a string value', function() {
assert(mm.all('a', ['*']));
});
});
8 changes: 8 additions & 0 deletions test/api.any.js
Expand Up @@ -5,6 +5,14 @@ var assert = require('assert');
var mm = require('..');

describe('.any()', function() {
describe('errors', function() {
it('should throw an error when value is not a string', function() {
assert.throws(function() {
mm.any();
});
});
});

describe('empty patterns', function() {
it('should correctly handle empty patterns', function() {
assert(!mm.any('', ''));
Expand Down
17 changes: 17 additions & 0 deletions test/api.braceExpand.js
@@ -0,0 +1,17 @@
'use strict';

var path = require('path');
var assert = require('assert');
var mm = require('..');

describe('.braceExpand()', function() {
it('should throw an error when arguments are invalid', function() {
assert.throws(function() {
mm.braceExpand();
});
});

it('should expand a brace pattern', function() {
assert.deepEqual(mm.braceExpand('{a,b}'), ['a', 'b']);
});
});
21 changes: 21 additions & 0 deletions test/api.braces.js
@@ -0,0 +1,21 @@
'use strict';

var path = require('path');
var assert = require('assert');
var mm = require('..');

describe('.braces()', function() {
it('should throw an error when arguments are invalid', function() {
assert.throws(function() {
mm.braces();
});
});

it('should create a regex source string from a brace pattern', function() {
assert.deepEqual(mm.braces('{a,b}'), ['(a|b)']);
});

it('should expand a brace pattern', function() {
assert.deepEqual(mm.braces('{a,b}', {expand: true}), ['a', 'b']);
});
});
47 changes: 47 additions & 0 deletions test/api.compile.js
@@ -0,0 +1,47 @@
'use strict';

var path = require('path');
var assert = require('assert');
var mm = require('..');

describe('.compile()', function() {
it('should throw an error when arguments are invalid', function() {
assert.throws(function() {
mm.compile();
});
});

it('should return an AST for a glob', function() {
var foo = mm.compile('a/*');
delete foo.ast.state;
assert.deepEqual(foo.ast, {
type: 'root',
errors: [],
nodes: [
{ type: 'bos', val: '' },
{ type: 'text', val: 'a' },
{ type: 'slash', val: '/' },
{ type: 'star', val: '*' },
{ type: 'eos', val: '' }
],
input: 'a/*'
});

var bar = mm.compile('a/**/*');
delete bar.ast.state;
assert.deepEqual(bar.ast, {
type: 'root',
errors: [],
nodes: [
{ type: 'bos', val: '' },
{ type: 'text', val: 'a' },
{ type: 'slash', val: '/' },
{ type: 'globstar', val: '**' },
{ type: 'slash', val: '/' },
{ type: 'star', val: '*' },
{ type: 'eos', val: '' }
],
input: 'a/**/*'
});
});
});
8 changes: 8 additions & 0 deletions test/api.contains.js
Expand Up @@ -6,6 +6,14 @@ var assert = require('assert');
var mm = require('..');

describe('.contains()', function() {
describe('errors', function() {
it('should throw an error arguments are invalid', function() {
assert.throws(function() {
mm.contains();
});
});
});

describe('patterns', function() {
it('should correctly deal with empty patterns', function() {
assert(!mm.contains('ab', ''));
Expand Down
21 changes: 21 additions & 0 deletions test/api.every.js
@@ -0,0 +1,21 @@
'use strict';

var path = require('path');
var assert = require('assert');
var mm = require('..');

describe('.every()', function() {
it('should return true if every string matches', function() {
var fixtures = ['a/a', 'a/b', 'a/c', 'b/a', 'b/b', 'b/c'];
assert(mm.every(fixtures, ['z', '*/*']));
});

it('should return false when not all strings match', function() {
var fixtures = ['a/a', 'a/b', 'a/c', 'b/a', 'b/b', 'b/c'];
assert(!mm.every(fixtures, ['a/*', 'x/*']));
});

it('should arrayify a string value', function() {
assert(mm.every('a', ['*']));
});
});
12 changes: 12 additions & 0 deletions test/api.js
Expand Up @@ -5,6 +5,18 @@ var sep = path.sep;
var mm = require('./support/match');

describe('micromatch', function() {
describe('empty list', function() {
it('should return an empty array', function() {
mm([], '*', []);
});
});

describe('options.nodupes', function() {
it('should return an array with duplicates', function() {
mm(['a', 'a', 'a'], ['*', 'a*'], {nodupes: false}, ['a', 'a', 'a', 'a', 'a', 'a']);
});
});

describe('posix paths', function() {
it('should return an array of matches for a literal string', function() {
mm(['a/a', 'a/b', 'a/c', 'b/a', 'b/b', 'b/c'], '(a/b)', ['a/b']);
Expand Down
21 changes: 21 additions & 0 deletions test/api.makeRe.js
@@ -0,0 +1,21 @@
'use strict';

var path = require('path');
var assert = require('assert');
var mm = require('..');

describe('.makeRe()', function() {
it('should throw an error when value is not a string', function() {
assert.throws(function() {
mm.makeRe();
});
});

it('should create a regex for a glob pattern', function() {
assert(mm.makeRe('*') instanceof RegExp);
});

it('should create a regex for a string', function() {
assert.deepEqual(mm.makeRe('abc').source, '^(?:abc)$');
});
});
11 changes: 10 additions & 1 deletion test/api.match.js
@@ -1,9 +1,18 @@
'use strict';

var path = require('path');
var assert = require('assert');
var mm = require('./support/match');

describe('.match method', function() {
describe('.match()', function() {
describe('errors', function() {
it('should throw an error when pattern is not a string', function() {
assert.throws(function() {
require('../').match([], []);
});
});
});

describe('posix paths', function() {
it('should return an array of matches for a literal string', function() {
var fixtures = ['a/a', 'a/b', 'a/c', 'b/a', 'b/b', 'b/c'];
Expand Down
2 changes: 1 addition & 1 deletion test/api.matchKeys.js
Expand Up @@ -3,7 +3,7 @@
var assert = require('assert');
var mm = require('..');

describe('.matchKeys method', function() {
describe('.matchKeys()', function() {
describe('error handling', function() {
it('should throw when the first argument is not an object', function() {
assert.throws(function() {
Expand Down
19 changes: 18 additions & 1 deletion test/api.matcher.js
@@ -1,9 +1,26 @@
'use strict';

var path = require('path');
var assert = require('assert');
var mm = require('./support/match');

describe('.match method', function() {
describe('.matcher()', function() {
describe('errors', function() {
it('should throw an error when arguments are invalid', function() {
assert.throws(function() {
mm.matcher(null);
});

assert.throws(function() {
mm.matcher();
});

assert.throws(function() {
mm.matcher(function() {});
});
});
});

describe('posix paths', function() {
it('should return an array of matches for a literal string', function() {
var fixtures = ['a/a', 'a/b', 'a/c', 'b/a', 'b/b', 'b/c'];
Expand Down
2 changes: 1 addition & 1 deletion test/api.not.js
Expand Up @@ -4,7 +4,7 @@ var path = require('path');
var sep = path.sep;
var mm = require('./support/match');

describe('.not method', function() {
describe('.not()', function() {
describe('posix paths', function() {
it('should return an array of matches for a literal string', function() {
var fixtures = ['a/a', 'a/b', 'a/c', 'b/a', 'b/b', 'b/c'];
Expand Down
47 changes: 47 additions & 0 deletions test/api.parse.js
@@ -0,0 +1,47 @@
'use strict';

var path = require('path');
var assert = require('assert');
var mm = require('..');

describe('.parse()', function() {
it('should throw an error when arguments are invalid', function() {
assert.throws(function() {
mm.parse();
});
});

it('should return an AST for a glob', function() {
var ast = mm.parse('a/*');
delete ast.state;
assert.deepEqual(ast, {
type: 'root',
errors: [],
nodes: [
{ type: 'bos', val: '' },
{ type: 'text', val: 'a' },
{ type: 'slash', val: '/' },
{ type: 'star', val: '*' },
{ type: 'eos', val: '' }
],
input: 'a/*'
});

ast = mm.parse('a/**/*');
delete ast.state;
assert.deepEqual(ast, {
type: 'root',
errors: [],
nodes: [
{ type: 'bos', val: '' },
{ type: 'text', val: 'a' },
{ type: 'slash', val: '/' },
{ type: 'globstar', val: '**' },
{ type: 'slash', val: '/' },
{ type: 'star', val: '*' },
{ type: 'eos', val: '' }
],
input: 'a/**/*'
});
});
});
21 changes: 21 additions & 0 deletions test/api.some.js
@@ -0,0 +1,21 @@
'use strict';

var path = require('path');
var assert = require('assert');
var mm = require('..');

describe('.some()', function() {
it('should return true if any matches are found', function() {
var fixtures = ['a/a', 'a/b', 'a/c', 'b/a', 'b/b', 'b/c'];
assert(mm.some(fixtures, ['z', 'b/*']));
});

it('should return false if no matches are found', function() {
var fixtures = ['a/a', 'a/b', 'a/c', 'b/a', 'b/b', 'b/c'];
assert(!mm.some(fixtures, ['z', 'x/*']));
});

it('should arrayify a string value', function() {
assert(mm.some('a', ['*']));
});
});

0 comments on commit c3bc209

Please sign in to comment.