test angular support as well
This commit is contained in:
24
Gruntfile.js
24
Gruntfile.js
@@ -33,11 +33,28 @@ module.exports = function (grunt) {
|
||||
},
|
||||
mochacli: {
|
||||
options: {
|
||||
reporter: 'nyan',
|
||||
reporter: 'spec',
|
||||
'harmony-generators': true
|
||||
},
|
||||
all: ['test/*.js']
|
||||
},
|
||||
karma: {
|
||||
unit: {
|
||||
options: {
|
||||
files: [
|
||||
'node_modules/angular/angular.js',
|
||||
'node_modules/angular-mocks/angular-mocks.js',
|
||||
'wildstring.js',
|
||||
'karma-test/**/*.js'
|
||||
],
|
||||
reporters: 'mocha',
|
||||
frameworks: ['mocha', 'chai'],
|
||||
singleRun: true,
|
||||
browsers: ['PhantomJS'],
|
||||
logLevel: 'ERROR'
|
||||
},
|
||||
}
|
||||
},
|
||||
watch: {
|
||||
gruntfile: {
|
||||
files: '<%= jshint.gruntfile.src %>',
|
||||
@@ -66,7 +83,10 @@ module.exports = function (grunt) {
|
||||
}
|
||||
});
|
||||
|
||||
grunt.registerTask('default', ['jshint', 'mochacli']);
|
||||
grunt.loadNpmTasks('grunt-karma');
|
||||
|
||||
grunt.registerTask('test', ['karma', 'mochacli']);
|
||||
grunt.registerTask('default', ['jshint', 'test']);
|
||||
grunt.registerTask('document', ['jsdoc']);
|
||||
grunt.registerTask('update', ['document', 'versioner:default']);
|
||||
};
|
||||
|
||||
316
karma-test/test.js
Normal file
316
karma-test/test.js
Normal file
@@ -0,0 +1,316 @@
|
||||
'use strict';
|
||||
|
||||
var wildstring;
|
||||
|
||||
beforeEach(module('wildstring'));
|
||||
beforeEach(inject(function(_wildstring_) {
|
||||
wildstring = _wildstring_;
|
||||
|
||||
chai.should();
|
||||
}));
|
||||
|
||||
describe('wildstring - angular', function() {
|
||||
it('should create an object and set a default wildcard', function() {
|
||||
assert.equal(wildstring.wildcard, '*');
|
||||
});
|
||||
|
||||
describe('#match', function() {
|
||||
it('should match exactly when no wildcard is given', function() {
|
||||
// Given: a string and a pattern that match
|
||||
var pattern = 'test',
|
||||
string = 'test';
|
||||
|
||||
// When: we call wildcard.match
|
||||
var result = wildstring.match(pattern, string);
|
||||
|
||||
// Then: we should see that they match
|
||||
assert.equal(result, true);
|
||||
});
|
||||
|
||||
it('should return false if the string doesn\'t match the pattern and no wildcard is given', function() {
|
||||
// Given: a string and a pattern that don't match
|
||||
var pattern = 'test',
|
||||
string = 'testing';
|
||||
|
||||
// When: we call wildcard.match
|
||||
var result = wildstring.match(pattern, string);
|
||||
|
||||
// Then: we should see that they don't match
|
||||
assert.equal(result, false);
|
||||
});
|
||||
|
||||
it('should return false if the string doesn\'t match the pattern and no wildcard is given, even when shorter', function() {
|
||||
// Given: a string and a pattern that don't match
|
||||
var pattern = 'testing',
|
||||
string = '';
|
||||
|
||||
// When: we call wildcard.match
|
||||
var result = wildstring.match(pattern, string);
|
||||
|
||||
// Then: we should see that they don't match
|
||||
assert.equal(result, false);
|
||||
});
|
||||
|
||||
it('should match everything if the pattern is only wildcards', function() {
|
||||
// Given: any string and a pattern that is only wildcards
|
||||
var pattern = '***',
|
||||
string = 'test';
|
||||
|
||||
// When: we call wildstring.match
|
||||
var result = wildstring.match(pattern, string);
|
||||
|
||||
// Then: we should see that they match
|
||||
assert.equal(result, true);
|
||||
});
|
||||
|
||||
it('should match longer strings if the pattern ends with a wildcard', function() {
|
||||
// Given: a string that is longer than the pattern, but matches up to the wildcard, and the pattern
|
||||
var pattern = 'test*',
|
||||
string = 'testing';
|
||||
|
||||
// When: we call wildstring.match
|
||||
var result = wildstring.match(pattern, string);
|
||||
|
||||
// Then: we should see that they match
|
||||
assert.equal(result, true);
|
||||
});
|
||||
|
||||
it('should match longer strings if the pattern begins with a wildcard', function() {
|
||||
// Given: a string that is longer than the pattern, but matches after to the wildcard, and the pattern
|
||||
var pattern = '*ing',
|
||||
string = 'testing';
|
||||
|
||||
// When: we call wildstring.match
|
||||
var result = wildstring.match(pattern, string);
|
||||
|
||||
// Then: we should see that they match
|
||||
assert.equal(result, true);
|
||||
});
|
||||
|
||||
it('should match longer strings if the pattern begins with multiple wildcards', function() {
|
||||
// Given: a string that is longer than the pattern, but matches after to the wildcard, and the pattern
|
||||
var pattern = '***ing',
|
||||
string = 'testing';
|
||||
|
||||
// When: we call wildstring.match
|
||||
var result = wildstring.match(pattern, string);
|
||||
|
||||
// Then: we should see that they match
|
||||
assert.equal(result, true);
|
||||
});
|
||||
|
||||
it('should match matching strings even if the pattern ends with a wildcard', function() {
|
||||
// Given: a string that is longer than the pattern, but matches up to the wildcard, and the pattern
|
||||
var pattern = 'test*',
|
||||
string = 'test';
|
||||
|
||||
// When: we call wildstring.match
|
||||
var result = wildstring.match(pattern, string);
|
||||
|
||||
// Then: we should see that they match
|
||||
assert.equal(result, true);
|
||||
});
|
||||
|
||||
it('should match matching strings even if the pattern ends with multiple wildcards', function() {
|
||||
// Given: a string that is longer than the pattern, but matches up to the wildcard, and the pattern
|
||||
var pattern = 'test***',
|
||||
string = 'test';
|
||||
|
||||
// When: we call wildstring.match
|
||||
var result = wildstring.match(pattern, string);
|
||||
|
||||
// Then: we should see that they match
|
||||
assert.equal(result, true);
|
||||
});
|
||||
|
||||
it('should match matching strings even if the pattern begins with a wildcard', function() {
|
||||
// Given: a string that is longer than the pattern, but matches after to the wildcard, and the pattern
|
||||
var pattern = '*ing',
|
||||
string = 'testing';
|
||||
|
||||
// When: we call wildstring.match
|
||||
var result = wildstring.match(pattern, string);
|
||||
|
||||
// Then: we should see that they match
|
||||
assert.equal(result, true);
|
||||
});
|
||||
|
||||
it('should not match strings that have extra characters at the end when the pattern doesn\'t end with a wildcard', function() {
|
||||
// Given: a string that is longer than the pattern, but matches after to the wildcard, and the pattern
|
||||
var pattern = '*ing',
|
||||
string = 'ings';
|
||||
|
||||
// When: we call wildstring.match
|
||||
var result = wildstring.match(pattern, string);
|
||||
|
||||
// Then: we should see that they match
|
||||
assert.equal(result, false);
|
||||
});
|
||||
|
||||
it('should not match strings that have extra characters at the beginning when the pattern doesn\'t begin with a wildcard', function() {
|
||||
// Given: a string that is longer than the pattern, but matches after to the wildcard, and the pattern
|
||||
var pattern = 'ing*',
|
||||
string = 'testing';
|
||||
|
||||
// When: we call wildstring.match
|
||||
var result = wildstring.match(pattern, string);
|
||||
|
||||
// Then: we should see that they match
|
||||
assert.equal(result, false);
|
||||
});
|
||||
|
||||
it('should match strings that match the beginning and end with a wildcard in the middle', function() {
|
||||
// Given: a string that is longer than the pattern, but matches after to the wildcard, and the pattern
|
||||
var pattern = 'bow*ing',
|
||||
string = 'bowstring';
|
||||
|
||||
// When: we call wildstring.match
|
||||
var result = wildstring.match(pattern, string);
|
||||
|
||||
// Then: we should see that they match
|
||||
assert.equal(result, true);
|
||||
});
|
||||
|
||||
it('should match matching strings even if there\'s a wildcard in the middle', function() {
|
||||
// Given: a string that is longer than the pattern, but matches after to the wildcard, and the pattern
|
||||
var pattern = 'test*ing',
|
||||
string = 'testing';
|
||||
|
||||
// When: we call wildstring.match
|
||||
var result = wildstring.match(pattern, string);
|
||||
|
||||
// Then: we should see that they match
|
||||
assert.equal(result, true);
|
||||
});
|
||||
|
||||
it('should work with multiple wildcards in the middle', function() {
|
||||
// Given: a string that is longer than the pattern, but matches after to the wildcard, and the pattern
|
||||
var pattern = 'te*st*ing',
|
||||
string = 'tea string';
|
||||
|
||||
// When: we call wildstring.match
|
||||
var result = wildstring.match(pattern, string);
|
||||
|
||||
// Then: we should see that they match
|
||||
assert.equal(result, true);
|
||||
});
|
||||
|
||||
it('should work with multiple wildcards in the middle and at the beginning', function() {
|
||||
// Given: a string that is longer than the pattern, but matches after to the wildcard, and the pattern
|
||||
var pattern = '*test*ing',
|
||||
string = 'I\'m testing';
|
||||
|
||||
// When: we call wildstring.match
|
||||
var result = wildstring.match(pattern, string);
|
||||
|
||||
// Then: we should see that they match
|
||||
assert.equal(result, true);
|
||||
});
|
||||
|
||||
it('should work with multiple wildcards in the middle and at the end', function() {
|
||||
// Given: a string that is longer than the pattern, but matches after to the wildcard, and the pattern
|
||||
var pattern = 'te*st*ing*',
|
||||
string = 'tea stings';
|
||||
|
||||
// When: we call wildstring.match
|
||||
var result = wildstring.match(pattern, string);
|
||||
|
||||
// Then: we should see that they match
|
||||
assert.equal(result, true);
|
||||
});
|
||||
|
||||
it('should move on if a wildcard doesn\'t continue to match but can later', function() {
|
||||
// Given: a string that has a pattern after the wildcard twice
|
||||
var pattern = '*test*ing',
|
||||
string = 'I\'m testing this thing';
|
||||
|
||||
// When: we call wildstring.match
|
||||
var result = wildstring.match(pattern, string);
|
||||
|
||||
// Then: we should see that they match
|
||||
assert.equal(result, true);
|
||||
});
|
||||
|
||||
it('should handle wildcarding duplicate characters well', function() {
|
||||
// Given: a string that is longer than the pattern, but matches after to the wildcard, and the pattern
|
||||
var pattern = '*||test*',
|
||||
string = '|||||testing';
|
||||
|
||||
// When: we call wildstring.match
|
||||
var result = wildstring.match(pattern, string);
|
||||
|
||||
// Then: we should see that they match
|
||||
assert.equal(result, true);
|
||||
});
|
||||
|
||||
it('should fail correctly with duplicate characters', function() {
|
||||
// Given: a string that is longer than the pattern, but matches after to the wildcard, and the pattern
|
||||
var pattern = '*))))))*',
|
||||
string = ')))))';
|
||||
|
||||
// When: we call wildstring.match
|
||||
var result = wildstring.match(pattern, string);
|
||||
|
||||
// Then: we should see that they match
|
||||
assert.equal(result, false);
|
||||
});
|
||||
|
||||
it('should work with case sensitivity off', function() {
|
||||
// Given: a string that does not match the case of the pattern, and case sensitivity is off
|
||||
wildstring.caseSensitive = false;
|
||||
var pattern = '*TEST',
|
||||
string = 'TeSt';
|
||||
|
||||
// When: we call wildstring.match
|
||||
var result = wildstring.match(pattern, string);
|
||||
|
||||
// Then: we should see that they match
|
||||
assert.equal(result, true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('#replace', function() {
|
||||
it('sholud return the pattern when no wildcard is given', function() {
|
||||
// Given: a string array and a pattern with no wildcards
|
||||
var pattern = 'test',
|
||||
strings = ['testing'];
|
||||
|
||||
// When: we call wildstring.replace
|
||||
var result = wildstring.replace(pattern, strings);
|
||||
|
||||
// Then: we should see that the pattern is unchanged
|
||||
assert.equal(result, pattern);
|
||||
});
|
||||
|
||||
it('should do ok as a date parser', function() {
|
||||
var date = new Date(2015, 6, 15); // month is 0 based for some reason
|
||||
var strings = [ date.getMonth() + 1, date.getDate(), date.getFullYear() ];
|
||||
var pattern = '*/*/*';
|
||||
var result = wildstring.replace(pattern, strings);
|
||||
|
||||
assert.equal(result, '7/15/2015');
|
||||
});
|
||||
|
||||
it('should tell you when you add too many strings', function() {
|
||||
// Given: more strings than wildcards in the pattern
|
||||
var pattern = 'Test *',
|
||||
strings = [ 'testing', 'strings' ];
|
||||
|
||||
// When: we call wildstring.replace with the pattern and strings
|
||||
assert.throws(function() { wildstring.replace(pattern, strings); });
|
||||
|
||||
// Then: we should get an error
|
||||
});
|
||||
|
||||
it('should tell you when you add too many wildcards', function() {
|
||||
// Given: more strings than wildcards in the pattern
|
||||
var pattern = '* Te*st *',
|
||||
strings = [ 'testing', 'strings' ];
|
||||
|
||||
// When: we call wildstring.replace with the pattern and strings
|
||||
assert.throws(function() { wildstring.replace(pattern, strings); });
|
||||
|
||||
// Then: we should get an error
|
||||
});
|
||||
});
|
||||
});
|
||||
19
package.json
19
package.json
@@ -10,9 +10,13 @@
|
||||
"type": "git",
|
||||
"url": "https://github.com/deltreey/wildstring"
|
||||
},
|
||||
"keywords": ["wildcard", "string", "interpolation"],
|
||||
"keywords": [
|
||||
"wildcard",
|
||||
"string",
|
||||
"interpolation"
|
||||
],
|
||||
"author": {
|
||||
"name":"Ted",
|
||||
"name": "Ted",
|
||||
"email": "suicidolt@gmail.com",
|
||||
"url": "https://github.com/deltreey"
|
||||
},
|
||||
@@ -22,15 +26,26 @@
|
||||
},
|
||||
"dependencies": {},
|
||||
"devDependencies": {
|
||||
"angular": "^1.4.4",
|
||||
"angular-mocks": "^1.4.4",
|
||||
"chai": "^3.2.0",
|
||||
"grunt-cli": "^0.1.13",
|
||||
"grunt-contrib-jshint": "^0.11.0",
|
||||
"grunt-contrib-nodeunit": "^0.4.1",
|
||||
"grunt-contrib-watch": "^0.6.1",
|
||||
"grunt-jsdoc": "^0.6.7",
|
||||
"grunt-karma": "^0.12.0",
|
||||
"grunt-mocha-cli": "^1.12.0",
|
||||
"grunt-versioner": "^0.1.7",
|
||||
"jshint-stylish": "^1.0.1",
|
||||
"karma": "^0.13.9",
|
||||
"karma-chai": "^0.1.0",
|
||||
"karma-mocha": "^0.2.0",
|
||||
"karma-mocha-reporter": "^1.1.1",
|
||||
"karma-phantomjs-launcher": "^0.2.1",
|
||||
"load-grunt-tasks": "^3.1.0",
|
||||
"mocha": "^2.2.5",
|
||||
"phantomjs": "^1.9.18",
|
||||
"time-grunt": "^1.1.0"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
var assert = require('assert'),
|
||||
wildstring = require('../wildstring');
|
||||
|
||||
describe('wildstring', function() {
|
||||
describe('wildstring - node', function() {
|
||||
it('should create an object and set a default wildcard', function() {
|
||||
assert.equal(wildstring.wildcard, '*');
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user