提交 b3075a11 编写于 作者: Huan (李卓桓)'s avatar Huan (李卓桓)

#40 fix unit test

上级 12deab2f
......@@ -29,21 +29,20 @@ test('Firer.checkFriendConfirm', t => {
'你已添加了李卓桓,现在可以开始聊天了。'
, '李卓桓'
]
]
let result: boolean
contentList.forEach((content, friend) => {
contentList.forEach(([content]) => {
result = Firer.checkFriendConfirm(content)
t.truthy(result, 'should be truthy for confirm msg: ' + content)
t.true(result, 'should be truthy for confirm msg: ' + content)
})
result = Firer.checkFriendConfirm('fsdfsdfasdfasdfadsa')
t.falsy(result, 'should be falsy for other msg')
t.false(result, 'should be falsy for other msg')
})
test('Firer.checkRoomJoin', t => {
const contentList = [
const contentList: [string, string, string[]][] = [
[
`You've invited "李卓桓" to the group chat`
, `You've`
......@@ -73,7 +72,7 @@ test('Firer.checkRoomJoin', t => {
let result
contentList.forEach(([content, inviter, inviteeList]) => {
result = Firer.checkRoomJoin(content as string)
result = Firer.checkRoomJoin(content)
t.truthy(result, 'should check room join message right for ' + content)
t.deepEqual(result[0], inviteeList, 'should get inviteeList right')
t.is(result[1], inviter, 'should get inviter right')
......@@ -81,7 +80,7 @@ test('Firer.checkRoomJoin', t => {
t.throws(() => {
Firer.checkRoomJoin('fsadfsadfsdfsdfs')
}, 'should throws if message is not expected')
}, Error, 'should throws if message is not expected')
})
test('Firer.checkRoomLeave', t => {
......@@ -105,7 +104,7 @@ test('Firer.checkRoomLeave', t => {
t.throws(() => {
Firer.checkRoomLeave('fafdsfsdfafa')
}, 'should throw if message is not expected')
}, Error, 'should throw if message is not expected')
})
test('Firer.checkRoomTopic', t => {
......@@ -126,12 +125,12 @@ test('Firer.checkRoomTopic', t => {
contentList.forEach(([content, changer, topic]) => {
result = Firer.checkRoomTopic(content)
t.truthy(result, 'should check topic right for content: ' + content)
t.is(topic , result[2], 'should get right topic')
t.is(topic , result[0], 'should get right topic')
t.is(changer, result[1], 'should get right changer')
})
t.throws(() => {
Firer.checkRoomTopic('fafdsfsdfafa')
}, 'should throw if message is not expected')
}, Error, 'should throw if message is not expected')
})
......@@ -93,9 +93,10 @@ async function fireFriendRequest(m: Message) {
/**
* try to find FriendRequest Confirmation Message
*/
function checkFriendConfirm(content) {
function checkFriendConfirm(content: string): boolean {
const reList = regexConfig.friendConfirm
let found = false
reList.some(re => !!(found = re.test(content)))
if (found) {
return true
......@@ -140,7 +141,7 @@ function checkRoomJoin(content: string): [string[], string] {
let found: string[]|null = []
reList.some(re => !!(found = content.match(re)))
if (!found || !found.length) {
throw new Error('checkRoomJoin() not found')
throw new Error('checkRoomJoin() not found matched re of ' + content)
}
const [inviter, inviteeStr] = [ found[1], found[2] ]
......@@ -280,7 +281,7 @@ function checkRoomLeave(content: string): string|null {
let found: string[]|null = []
reList.some(re => !!(found = content.match(re)))
if (!found || !found.length) {
return null
throw new Error('checkRoomLeave() no matched re for ' + content)
}
return found[1] // leaver
}
......@@ -288,11 +289,13 @@ function checkRoomLeave(content: string): string|null {
/**
* You removed "Bruce LEE" from the group chat
*/
async function fireRoomLeave(m: Message) {
async function fireRoomLeave(m: Message): Promise<void> {
log.verbose('PuppetWebFirer', 'fireRoomLeave(%s)', m.content())
const leaver = checkRoomLeave(m.content())
if (!leaver) {
let leaver
try {
leaver = checkRoomLeave(m.content())
} catch (e) {
return
}
log.silly('PuppetWebFirer', 'fireRoomLeave() got leaver: %s', leaver)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册