/
secret_test.ts
47 lines (42 loc) · 1.75 KB
/
secret_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
import { bold, red } from 'https://deno.land/std@v0.52.0/fmt/colors.ts';
import { Secret } from '../prompts/secret.ts';
import { assertEquals, assertThrowsAsync } from './lib/assert.ts';
Deno.test( 'prompt secret: value', async () => {
console.log();
Secret.inject( 'hallo' );
const result: string | undefined = await Secret.prompt( 'message' );
assertEquals( result, 'hallo' );
} );
Deno.test( 'prompt secret: validate option', async () => {
console.log();
Secret.inject( 'a'.repeat( 9 ) );
const result: string | undefined = await Secret.prompt( {
message: 'message',
validate: ( value: string ) => value.length < 10
} );
assertEquals( result, 'a'.repeat( 9 ) );
} );
Deno.test( 'prompt secret: empty value', async () => {
console.log();
await assertThrowsAsync( async () => {
Secret.inject( '' );
await Secret.prompt( 'message' );
}, Error, red( `${ Deno.build.os === 'windows' ? bold( ' × ' ) : bold( ' ✘ ' ) }Secret must be longer then 1 but has a length of 0.` ) );
} );
Deno.test( 'prompt secret: invalid value', async () => {
console.log();
await assertThrowsAsync( async () => {
Secret.inject( 'a'.repeat( 10 ) );
await Secret.prompt( {
message: 'message',
validate: ( value: string ) => value.length < 10
} );
}, Error, red( `${ Deno.build.os === 'windows' ? bold( ' × ' ) : bold( ' ✘ ' ) }Invalid answer.` ) );
} );
Deno.test( 'prompt secret: null value', async () => {
console.log();
await assertThrowsAsync( async () => {
Secret.inject( null as any );
await Secret.prompt( 'message' );
}, Error, red( `${ Deno.build.os === 'windows' ? bold( ' × ' ) : bold( ' ✘ ' ) }Invalid answer.` ) );
} );