1
+ import { ALL_SAMPLES , FALSY_STRINGS , SAMPLE_STRING } from "@terminal-nerds/snippets-test/sample" ;
1
2
import { returns , throws } from "@terminal-nerds/snippets-test/unit" ;
2
3
import { describe , expect , it } from "vitest" ;
3
4
import { ZodError } from "zod" ;
4
5
5
- import { EMPTY_STRING_VALUES , SAMPLE_INPUT , testInvalidInput } from "../../tests/shared.js" ;
6
6
import {
7
7
CHAR_TYPES ,
8
8
type CharOptions ,
@@ -27,6 +27,19 @@ import {
27
27
validateSingleChar ,
28
28
} from "./char.js" ;
29
29
30
+ const EMPTY_STRINGS = FALSY_STRINGS ;
31
+ const NON_STRING_VALUES = ALL_SAMPLES . filter ( ( v ) => typeof v !== "string" ) ;
32
+
33
+ /* eslint-disable @typescript-eslint/no-explicit-any */
34
+ function testInvalidInput ( method : ( input : any , options ?: any ) => void , options ?: any ) : void {
35
+ it ( throws ( ZodError ) . on ( `passed non-string input` ) , ( ) => {
36
+ for ( const nonString of NON_STRING_VALUES ) {
37
+ expect ( ( ) => method ( nonString , options ) ) . toThrowError ( ZodError ) ;
38
+ }
39
+ } ) ;
40
+ }
41
+
42
+ // @ts -ignore FIXME: I have absolutely no clue how to fix this one, help!
30
43
const INPUT_WITH_ALL_CHARS = getJoinedChars ( SINGLE_CHARS ) ;
31
44
const INPUT_WITH_UPPER_CASED_LATIN_CHARS = getJoinedChars ( UPPER_CASED_LATIN_CHARS ) ;
32
45
const INPUT_WITH_LOWER_CASED_LATIN_CHARS = getJoinedChars ( LOWER_CASED_LATIN_CHARS ) ;
@@ -42,12 +55,12 @@ function testInvalidStringInput(method: Parameters<typeof testInvalidInput>[0]):
42
55
function testInvalidCharInput ( method : Parameters < typeof testInvalidInput > [ 0 ] ) : void {
43
56
testInvalidStringInput ( method ) ;
44
57
45
- it ( throws ( ZodError ) . on ( `input longer than 1 char` ) . sample ( SAMPLE_INPUT ) , ( ) => {
46
- expect ( ( ) => method ( SAMPLE_INPUT , { type : "latin" } ) ) . toThrowError ( ZodError ) ;
58
+ it ( throws ( ZodError ) . on ( `input longer than 1 char` ) . sample ( SAMPLE_STRING ) , ( ) => {
59
+ expect ( ( ) => method ( SAMPLE_STRING , { type : "latin" } ) ) . toThrowError ( ZodError ) ;
47
60
} ) ;
48
61
49
62
it ( throws ( ZodError ) . on ( `empty string input` ) , ( ) => {
50
- for ( const emptyString of EMPTY_STRING_VALUES ) {
63
+ for ( const emptyString of EMPTY_STRINGS ) {
51
64
expect ( ( ) => method ( emptyString , { type : "latin" } ) ) . toThrowError ( ZodError ) ;
52
65
}
53
66
} ) ;
@@ -58,7 +71,7 @@ function testInvalidCharTypeOption(method: (input: string, options: CharOptions<
58
71
59
72
it ( throws ( ZodError ) . on ( `passed unrecognized char type option` ) . with ( options ) , ( ) => {
60
73
// @ts -expect-error Testing
61
- expect ( ( ) => method ( SAMPLE_INPUT , options ) ) . toThrowError ( ZodError ) ;
74
+ expect ( ( ) => method ( SAMPLE_STRING , options ) ) . toThrowError ( ZodError ) ;
62
75
} ) ;
63
76
}
64
77
@@ -69,8 +82,8 @@ describe("isSingleChar(input)", () => {
69
82
expect ( isSingleChar ( "" ) ) . toBe ( false ) ;
70
83
} ) ;
71
84
72
- it ( returns ( false ) . on ( `input longer than 1 char` ) . sample ( SAMPLE_INPUT ) , ( ) => {
73
- expect ( isSingleChar ( SAMPLE_INPUT ) ) . toBe ( false ) ;
85
+ it ( returns ( false ) . on ( `input longer than 1 char` ) . sample ( SAMPLE_STRING ) , ( ) => {
86
+ expect ( isSingleChar ( SAMPLE_STRING ) ) . toBe ( false ) ;
74
87
} ) ;
75
88
76
89
it ( returns ( true ) . on ( `single chars input` ) . samples ( SINGLE_CHARS ) , ( ) => {
@@ -248,6 +261,16 @@ describe("hasChars(input, options?)", () => {
248
261
} ,
249
262
) ;
250
263
264
+ it (
265
+ returns ( true )
266
+ . on ( `input with lowercased latin chars` )
267
+ . sample ( INPUT_WITH_LOWER_CASED_LATIN_CHARS )
268
+ . with ( latinInsentitiveOptions ) ,
269
+ ( ) => {
270
+ expect ( hasChars ( INPUT_WITH_LOWER_CASED_LATIN_CHARS , latinInsentitiveOptions ) ) . toBe ( true ) ;
271
+ } ,
272
+ ) ;
273
+
251
274
it (
252
275
returns ( true )
253
276
. on ( `input with uppercased latin chars` )
@@ -291,33 +314,33 @@ describe("getChars(input, options?)", () => {
291
314
292
315
const latinOptions = { type : "latin" } as const ;
293
316
/* prettier-ignore */
294
- const expectedLatinChars = [ "X" , "E" , "H" , "O" , " t", "e" , "r" , "m" , "i" , "n" , "a" , "l" , "n" , "e" , "r" , "d" , "s" ] as const ;
317
+ const expectedLatinChars = [ "t" , "e" , "r" , "m" , "i" , "n" , "a" , "l" , "n" , "e" , "r" , "d" , "s" , "D" , "E" , "V "] as const ;
295
318
296
- it ( returns ( expectedLatinChars ) . on ( `sample input` ) . sample ( SAMPLE_INPUT ) . with ( latinOptions ) , ( ) => {
297
- expect ( getChars ( SAMPLE_INPUT , latinOptions ) ) . toStrictEqual ( expectedLatinChars ) ;
319
+ it ( returns ( expectedLatinChars ) . on ( `sample input` ) . sample ( SAMPLE_STRING ) . with ( latinOptions ) , ( ) => {
320
+ expect ( getChars ( SAMPLE_STRING , latinOptions ) ) . toStrictEqual ( expectedLatinChars ) ;
298
321
} ) ;
299
322
300
323
const latinInsentitiveOptions = { type : "latin" , caseInsensitive : false } as const ;
301
324
const expectedLowerCasedLatinChars = [ "t" , "e" , "r" , "m" , "i" , "n" , "a" , "l" , "n" , "e" , "r" , "d" , "s" ] as const ;
302
325
303
326
it (
304
- returns ( expectedLowerCasedLatinChars ) . on ( `sample input` ) . sample ( SAMPLE_INPUT ) . with ( latinInsentitiveOptions ) ,
327
+ returns ( expectedLowerCasedLatinChars ) . on ( `sample input` ) . sample ( SAMPLE_STRING ) . with ( latinInsentitiveOptions ) ,
305
328
( ) => {
306
- expect ( getChars ( SAMPLE_INPUT , latinInsentitiveOptions ) ) . toStrictEqual ( expectedLowerCasedLatinChars ) ;
329
+ expect ( getChars ( SAMPLE_STRING , latinInsentitiveOptions ) ) . toStrictEqual ( expectedLowerCasedLatinChars ) ;
307
330
} ,
308
331
) ;
309
332
310
333
const numberOptions = { type : "number" } as const ;
311
- const expectedNumberChars = [ "9 " , "1 " ] as const ;
334
+ const expectedNumberChars = [ "2 " , "0" , "2" , "3 "] as const ;
312
335
313
- it ( returns ( expectedNumberChars ) . on ( `sample input` ) . sample ( SAMPLE_INPUT ) . with ( numberOptions ) , ( ) => {
314
- expect ( getChars ( SAMPLE_INPUT , numberOptions ) ) . toStrictEqual ( expectedNumberChars ) ;
336
+ it ( returns ( expectedNumberChars ) . on ( `sample input` ) . sample ( SAMPLE_STRING ) . with ( numberOptions ) , ( ) => {
337
+ expect ( getChars ( SAMPLE_STRING , numberOptions ) ) . toStrictEqual ( expectedNumberChars ) ;
315
338
} ) ;
316
339
317
340
const specialOptions = { type : "special" } as const ;
318
- const expectedSpecialChars = [ "@ " , "- " ] as const ;
341
+ const expectedSpecialChars = [ "- " , "." , "@ "] as const ;
319
342
320
- it ( returns ( expectedSpecialChars ) . on ( `sample input` ) . sample ( SAMPLE_INPUT ) . with ( specialOptions ) , ( ) => {
321
- expect ( getChars ( SAMPLE_INPUT , specialOptions ) ) . toStrictEqual ( expectedSpecialChars ) ;
343
+ it ( returns ( expectedSpecialChars ) . on ( `sample input` ) . sample ( SAMPLE_STRING ) . with ( specialOptions ) , ( ) => {
344
+ expect ( getChars ( SAMPLE_STRING , specialOptions ) ) . toStrictEqual ( expectedSpecialChars ) ;
322
345
} ) ;
323
346
} ) ;
0 commit comments