-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtio.test.ts
More file actions
152 lines (124 loc) · 3.4 KB
/
tio.test.ts
File metadata and controls
152 lines (124 loc) · 3.4 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import { deepStrictEqual, rejects, throws, strictEqual } from 'node:assert'
import { it } from 'node:test'
import tio from './src/index'
function testTioAttribute<T>(
key: string,
defaultVariation: T,
newVariation: T
) {
deepStrictEqual((tio as any)[key], defaultVariation)
;(tio as any)[key] = newVariation
deepStrictEqual((tio as any)[key], newVariation)
;(tio as any)[key] = defaultVariation
}
it('works with the timeout option', async () => {
testTioAttribute('defaultTimeout', Infinity, 2000)
const invalidValues = [NaN, 0, -5, 'test', null, undefined]
for (const invalidValue of invalidValues) {
await rejects(
() =>
tio("console.log('Hello, World!');", {
timeout: invalidValue as any
}),
{ name: 'TioError' }
)
throws(
() => {
tio.defaultTimeout = invalidValue as any
},
{ name: 'TioError' }
)
}
})
it('works with the language option', async () => {
testTioAttribute('defaultLanguage', 'javascript-node', 'python3')
const invalidValues = [0, 'test', null, undefined]
for (const invalidValue of invalidValues) {
await rejects(
() =>
tio("console.log('Hello, World!');", {
language: invalidValue as any
}),
{ name: 'TioError' }
)
throws(
() => {
tio.defaultLanguage = invalidValue as any
},
{ name: 'TioError' }
)
}
})
it('works with the cflags and argv options', async () => {
const invalidValues = [
0,
'test',
null,
undefined,
{},
[''],
['hello', '', 'world'],
['hello', 2, 'world'],
[undefined, null, 'hello']
]
const options = ['cflags', 'argv']
for (const option of options) {
const defaultOptionKey = `default${option.replace(/\b\w/g, char => char.toUpperCase())}`
testTioAttribute(defaultOptionKey, [], ['test', 'test 2'])
for (const invalidValue of invalidValues) {
await rejects(
() => tio("console.log('Hello, World!');", { [option]: invalidValue }),
{ name: 'TioError' }
)
throws(
() => {
;(tio as any)[defaultOptionKey] = invalidValue
},
{ name: 'TioError' }
)
}
}
})
it('outputs "Hello, World!" in JavaScript', async () => {
const { output } = await tio("console.log('Hello, World!');")
strictEqual(output, 'Hello, World!\n')
})
it('outputs "Hello, World!" in Python 3', async () => {
const { output } = await tio("print('Hello, World!')", {
language: 'python3'
})
strictEqual(output, 'Hello, World!\n')
})
it('surpresses an infinite loop', async () => {
const { output, timedOut } = await tio('for (;;);', {
timeout: 2000
})
strictEqual(timedOut, true)
strictEqual(output, 'Request timed out after 2000ms')
})
it('works with custom compiler flags', async () => {
const code = `
fn main() {
#[cfg(feature = "something")]
println!("this will be printed");
}
`
const { output } = await tio(code, {
language: 'rust',
cflags: ['--cfg', 'feature="something"']
})
strictEqual(output, 'this will be printed\n')
})
it('works with custom command-line arguments', async () => {
const { output } = await tio(
'console.log(process.argv.slice(2).join(", "))',
{
argv: ['Hello', 'World!']
}
)
strictEqual(output, 'Hello, World!\n')
})
it('works with empty outputs', async () => {
const { output } = await tio('')
strictEqual(output, '\n')
})