Skip to content

Commit

Permalink
fix(core): fix interpolate issue, close #190
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Apr 1, 2021
1 parent 0654893 commit ed1e2d0
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 146 deletions.
9 changes: 1 addition & 8 deletions .mocharc.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,7 @@ for (const name of readdirSync(__dirname + '/packages')) {
}

const specs = [
// 'packages/koishi-core/tests/*.spec.ts',
'packages/koishi-core/tests/command.spec.ts',
'packages/koishi-core/tests/context.spec.ts',
'packages/koishi-core/tests/help.spec.ts',
'packages/koishi-core/tests/hook.spec.ts',
'packages/koishi-core/tests/runtime.spec.ts',
'packages/koishi-core/tests/session.spec.ts',
'packages/koishi-core/tests/parser.spec.ts',
'packages/koishi-core/tests/*.spec.ts',
'packages/koishi-utils/tests/*.spec.ts',
'packages/koishi-test-utils/tests/*.spec.ts',
'packages/plugin-common/tests/*.spec.ts',
Expand Down
13 changes: 6 additions & 7 deletions packages/koishi-core/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -533,10 +533,14 @@ export namespace Argv {
}

stringify(argv: Argv) {
return argv.tokens.reduce((prev, token) => {
const output = argv.tokens.reduce((prev, token) => {
if (token.quoted) prev += leftQuotes[rightQuotes.indexOf(token.terminator[0])]
return prev + token.content + token.terminator
}, '')
if (argv.rest && !rightQuotes.includes(output[output.length - 1]) || argv.initiator) {
return output.slice(0, -1)
}
return output
}
}

Expand All @@ -547,12 +551,7 @@ export namespace Argv {
}

export function stringify(argv: Argv) {
const source = defaultTokenizer.stringify(argv)
if (argv.rest) {
const { terminator } = argv.tokens[argv.tokens.length - 1]
if (!leftQuotes.includes(terminator[0])) return source.slice(0, -terminator.length)
}
return source
return defaultTokenizer.stringify(argv)
}

export function revert(token: Token) {
Expand Down
130 changes: 0 additions & 130 deletions packages/koishi-core/tests/server.spec.ts

This file was deleted.

26 changes: 25 additions & 1 deletion packages/koishi-core/tests/session.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,31 @@ import { App } from 'koishi-test-utils'
import { sleep } from 'koishi-utils'

describe('Session API', () => {
describe('Command Suggestions', () => {
describe('Command Execution', () => {
const app = new App()
const sess = app.session('456')
app.command('echo [content:text]').action((_, text) => text)
app.command('exec [command:text]').action(({ session }, text) => session.execute(text))

it('basic support', async () => {
await sess.shouldReply('echo 0', '0')
await sess.shouldReply('exec echo 0', '0')
})

it('interpolate 1', async () => {
await sess.shouldReply('echo $(echo 0)', '0')
await sess.shouldReply('echo $(exec echo 0)', '0')
await sess.shouldReply('echo 1$(echo 0)2', '102')
await sess.shouldReply('echo 1 $(echo 0) 2', '1 0 2')
})

it('interpolate 2', async () => {
await sess.shouldReply('echo $(echo $(echo 0))', '0')
await sess.shouldReply('echo 1 $(echo $(echo 0))2', '1 02')
})
})

describe('Command Suggestion', () => {
const app = new App({ prefix: '/' })
const session1 = app.session('456')
const session2 = app.session('789', '987')
Expand Down

0 comments on commit ed1e2d0

Please sign in to comment.