Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

--grep option now supports real regular expressions but not masks only. #204

Merged
merged 1 commit into from
May 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions src/adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,13 +300,30 @@ var getGrepOption = function (clientArguments) {
}
}

var createRegExp = function (filter) {
filter = filter || ''
if (filter === '') {
return new RegExp() // to match all
}

var regExp = /^[/](.*)[/]([gmixXsuUAJD]*)$/ // pattern to check whether the string is RegExp pattern

var parts = regExp.exec(filter)
if (parts === null) {
return new RegExp(filter.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&')) // escape functional symbols
}

var patternExpression = parts[1]
var patternSwitches = parts[2]
return new RegExp(patternExpression, patternSwitches)
}

/**
* Create jasmine spec filter
* @param {Object} options Spec filter options
*/
var KarmaSpecFilter = function (options) {
var filterString = options && options.filterString() && options.filterString().replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&')
var filterPattern = new RegExp(filterString)
var filterPattern = createRegExp(options && options.filterString())

this.matches = function (specName) {
return filterPattern.test(specName)
Expand Down
69 changes: 67 additions & 2 deletions test/adapter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
These tests are executed in browser.
*/
/* global getJasmineRequireObj, jasmineRequire, MockSocket, KarmaReporter */
/* global formatFailedStep, , createStartFn, getGrepOption, KarmaSpecFilter, createSpecFilter */
/* global formatFailedStep, , createStartFn, getGrepOption, createRegExp, KarmaSpecFilter, createSpecFilter */
/* global getRelevantStackFrom: true, isExternalStackEntry: true */

'use strict'
Expand Down Expand Up @@ -481,7 +481,72 @@ describe('jasmine adapter', function () {
})
})

describe('KarmaSpecFilter', function () {
describe('createRegExp', function () {
it('should match all string by null pattern', function () {
var regExp = createRegExp(null)

expect(regExp.test(null)).toEqual(true)
expect(regExp.test('bar')).toEqual(true)
expect(regExp.test('test')).toEqual(true)
expect(regExp.test('test.asfsdf.sdfgfdh')).toEqual(true)
})

it('should match all string by empty pattern', function () {
var regExp = createRegExp('')

expect(regExp.test(null)).toEqual(true)
expect(regExp.test('bar')).toEqual(true)
expect(regExp.test('test')).toEqual(true)
expect(regExp.test('test.asfsdf.sdfgfdh')).toEqual(true)
})

it('should match strings by pattern with flags', function () {
var regExp = createRegExp('/test.*/i')
expect(regExp.test(null)).toEqual(false)
expect(regExp.test('bar')).toEqual(false)
expect(regExp.test('test')).toEqual(true)
expect(regExp.test('test.asfsdf.sdfgfdh')).toEqual(true)
})

it('should match strings by pattern without flags', function () {
var regExp = createRegExp('/test.*/')
expect(regExp.test(null)).toEqual(false)
expect(regExp.test('bar')).toEqual(false)
expect(regExp.test('test')).toEqual(true)
expect(regExp.test('test.asfsdf.sdfgfdh')).toEqual(true)
})

it('should match strings by complex pattern', function () {
var regExp = createRegExp('/test.*[/]i/i')
expect(regExp.test(null)).toEqual(false)
expect(regExp.test('bar')).toEqual(false)
expect(regExp.test('test')).toEqual(false)
expect(regExp.test('test/i')).toEqual(true)
})
})

describe('KarmaSpecFilter(RegExp)', function () {
var specFilter

beforeEach(function () {
specFilter = new KarmaSpecFilter({
filterString: function () {
return '/test.*/'
}
})
})

it('should create spec filter', function () {
expect(specFilter).toBeDefined()
})

it('should filter spec by name', function () {
expect(specFilter.matches('bar')).toEqual(false)
expect(specFilter.matches('test')).toEqual(true)
})
})

describe('KarmaSpecFilter(non-RegExp)', function () {
var specFilter

beforeEach(function () {
Expand Down