Skip to content

Commit

Permalink
fix(condition): Match words and trim leading/trailing puncuation
Browse files Browse the repository at this point in the history
  • Loading branch information
timkinnane committed Sep 4, 2018
1 parent a9cfacc commit 7e441db
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
13 changes: 9 additions & 4 deletions src/lib/condition.ts
Expand Up @@ -95,6 +95,9 @@ export class Expression {
const config = Object.assign({}, _defaults, options)
const b = (config.matchWord) ? '\\b' : '' // word boundary regex toggle
const i = (config.ignoreCase) ? 'i' : '' // ignore case flag toggle
const p = (config.ignorePunctuation)
? `\\,\\-\\:\\[\\]\\/\\(\\)\\+\\?\\.\\'\\$`
: '\\,\\-\\:'

const patterns: string[] = []
for (let cKey of Object.keys(condition)) {
Expand All @@ -112,8 +115,8 @@ export class Expression {
case 'ends': patterns.push(`${b}(?:${value})$`); break
case 'contains': patterns.push(`${b}(${value})${b}`); break
case 'excludes': patterns.push(`^((?!${b}${value}${b}).)*$`); break
case 'after': patterns.push(`(?:${value}\\s?)([\\w\\-\\s]+?)`); break
case 'before': patterns.push(`([\\w\\-\\s]+?)(?:\\s?${value})`); break
case 'after': patterns.push(`(?:${value}\\s?)([\\w\\-\\s${p}]+)`); break
case 'before': patterns.push(`([\\w\\-\\s${p}]+)(?:\\s?${value})`); break
case 'range':
const rangeExp = rangeRegex(value.split('-')[0], value.split('-')[1])
patterns.push(`${b}(${rangeExp})${b}`)
Expand Down Expand Up @@ -212,9 +215,11 @@ export class Conditions {
/** Test a string against all expressions. */
exec (str: string) {
for (let key in this.expressions) {
const match = str.match(this.expressions[key])
const match: any = str.match(this.expressions[key])
this.matches[key] = match
this.captures[key] = (match && match.length > 0) ? match[1] : undefined
this.captures[key] = (match && typeof match[1] === 'string')
? match[1].replace(/(^[\,\-\:\s]*)|([\,\-\:\s]*$)/g, '')
: undefined
}
return this.matches
}
Expand Down
36 changes: 23 additions & 13 deletions src/lib/e2e-tests/e2e.spec.ts
@@ -1,31 +1,41 @@
import 'mocha'
import sinon from 'sinon'
// import { expect } from 'chai'
import { expect } from 'chai'
import * as bot from '../..'

class MockMessenger extends bot.MessageAdapter {
name = 'mock-messenger'
async dispatch () { return }
async start () { return }
async shutdown () { return }
}
const sandbox = sinon.createSandbox()
const mocks = sinon.createStubInstance(MockMessenger)
mocks.name = 'mock-messenger'

describe('[E2E]', () => {
beforeEach(() => {
bot.adapters.message = new MockMessenger(bot)
return bot.start()
beforeEach(async () => {
await bot.reset()
bot.adapters.message = mocks
await bot.start()
})
afterEach(() => {
sandbox.restore()
return bot.reset()
mocks.dispatch.resetHistory()
})
it('responds from middleware', async () => {
bot.adapters.message!.dispatch = sandbox.stub()
bot.middlewares.load()
bot.middleware.hear((b, _, done) => b.respond('test').then(() => done()))
bot.middleware.hear((b, _, done) => {
return b.respond('test').then(() => done())
})
await bot.receive(new bot.TextMessage(new bot.User(), ''))
sinon.assert.calledOnce((bot.adapters.message!.dispatch as sinon.SinonStub))
bot.middlewares.unload()
sinon.assert.calledOnce(mocks.dispatch)
})
it('captures input matching conditions', async () => {
let captured: any[] = []
bot.global.text({ after: 'call me', before: 'please' }, (b) => {
captured.push(b.conditions.captured)
}, { force: true })
bot.global.text({ after: 'call me' }, (b) => {
captured.push(b.conditions.captured)
}, { force: true })
await bot.receive(new bot.TextMessage(new bot.User(), 'Call me bb, please'))
expect(captured).to.eql(['bb', 'bb, please'])
})
})

0 comments on commit 7e441db

Please sign in to comment.