-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.test.ts
135 lines (121 loc) · 3.57 KB
/
mod.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import { assertEquals, assertThrows } from 'jsr:@std/testing/asserts'
import parse, { isCommand, OptionType, parseCommand } from './mod.ts'
Deno.test('isCommand == true', () => {
assertEquals(isCommand('/hello this is a command'), true)
})
Deno.test('isCommand == false', () => {
assertEquals(isCommand('hello this is not a command'), false)
})
Deno.test('parseCommand', () => {
assertEquals(parseCommand('/hello this is a slack-style command'), {
command: 'hello',
text: 'this is a slack-style command',
})
})
Deno.test('command without options', () => {
assertEquals(parse('/hello'), {
text: '',
command: 'hello',
options: {},
subCommands: [],
})
})
Deno.test('command with named options', () => {
assertEquals(
parse('/hello message: hello this is my message'),
{
command: 'hello',
text: 'message: hello this is my message',
options: { message: 'hello this is my message' },
subCommands: [],
},
)
})
Deno.test('command with multiple named options', () => {
assertEquals(
parse('/hello message: hello this is my message custom: -O.O-'),
{
command: 'hello',
text: 'message: hello this is my message custom: -O.O-',
options: { message: 'hello this is my message', custom: '-O.O-' },
subCommands: [],
},
)
})
Deno.test('command with subcommand', () => {
assertEquals(parse('/todos add name: My Todo Name'), {
text: 'add name: My Todo Name',
command: 'todos',
options: { name: 'My Todo Name' },
subCommands: ['add'],
})
})
Deno.test('command with multiple subcommands', () => {
assertEquals(parse('/todos add shopping veggie: lettuce'), {
text: 'add shopping veggie: lettuce',
command: 'todos',
options: { veggie: 'lettuce' },
subCommands: ['add', 'shopping'],
})
})
Deno.test('command with option definition', () => {
const interaction = parse(
'/todos add item: lettuce howmany: 2 complete: false',
[
{ name: 'item', type: OptionType.string },
{ name: 'howmany', type: OptionType.integer },
{ name: 'complete', type: OptionType.boolean },
],
)
assertEquals(interaction, {
text: 'add item: lettuce howmany: 2 complete: false',
command: 'todos',
options: { item: 'lettuce', howmany: 2, complete: false },
subCommands: ['add'],
})
})
Deno.test('command with nested option definition', () => {
const interaction = parse(
'/todos add item: lettuce howmany: 2 complete: false',
[
{
name: 'item',
type: OptionType.string,
options: [
{ name: 'howmany', type: OptionType.integer },
{ name: 'complete', type: OptionType.boolean },
],
},
],
)
assertEquals(interaction, {
text: 'add item: lettuce howmany: 2 complete: false',
command: 'todos',
options: { item: 'lettuce', howmany: 2, complete: false },
subCommands: ['add'],
})
})
Deno.test('command with single option definition', () => {
const interaction = parse(
'/todos add item: lettuce',
{ name: 'item', type: OptionType.string },
)
assertEquals(interaction, {
text: 'add item: lettuce',
command: 'todos',
options: { item: 'lettuce' },
subCommands: ['add'],
})
})
Deno.test('Error: no content', () => {
const fn = () => parse('')
assertThrows(fn, Error, 'no content')
})
Deno.test('Error: no prefix', () => {
const fn = () => parse('message without a command')
assertThrows(fn, Error, 'no prefix (not a command)')
})
Deno.test('Error: no body after prefix', () => {
const fn = () => parse('/')
assertThrows(fn, Error, 'no body after prefix')
})