1 | import test from 'ava'
|
2 | import Chance from '../chance.js'
|
3 | import _ from 'lodash'
|
4 |
|
5 | const chance = new Chance()
|
6 |
|
7 | const fileExtensions = {
|
8 | "raster": [ "bmp", "gif", "gpl", "ico", "jpeg", "psd", "png", "psp", "raw",
|
9 | "tiff" ],
|
10 | "vector": [ "3dv", "amf", "awg", "ai", "cgm", "cdr", "cmx", "dxf", "e2d",
|
11 | "egt", "eps", "fs", "odg", "svg", "xar" ],
|
12 | "3d": [ "3dmf", "3dm", "3mf", "3ds", "an8", "aoi", "blend", "cal3d", "cob",
|
13 | "ctm", "iob", "jas", "max", "mb", "mdx", "obj", "x", "x3d" ],
|
14 | "document": [ "doc", "docx", "dot", "html", "xml", "odt", "odm", "ott", "csv",
|
15 | "rtf", "tex", "xhtml", "xps" ]
|
16 | }
|
17 |
|
18 |
|
19 | test('file() returns random file length with random extension', t => {
|
20 | _.times(1000, () => {
|
21 | let file = chance.file()
|
22 | t.true(_.isString(file))
|
23 | t.is(file.split('.').length, 2)
|
24 | })
|
25 | })
|
26 |
|
27 | test('file() returns error if wrong fileType provided', t => {
|
28 | _.times(1000, () => {
|
29 | const fn = () => chance.file({ fileType: 'not_specified' })
|
30 | t.throws(fn, 'Chance: Expect file type value to be \'raster\', \'vector\', \'3d\' or \'document\'')
|
31 | })
|
32 | })
|
33 |
|
34 | test('file() does not return error if legit fileType provided', t => {
|
35 | _.times(1000, () => {
|
36 | const fn = () => chance.file({ fileType: 'raster' })
|
37 | t.notThrows(fn)
|
38 | })
|
39 | })
|
40 |
|
41 | test('file() returns filename with specific extension type', t => {
|
42 | _.times(1000, () => {
|
43 | let file = chance.file({ fileType: 'raster' })
|
44 | t.true(_.isString(file))
|
45 | let extension = file.split('.')[1]
|
46 | t.true(fileExtensions['raster'].indexOf(extension) !== -1)
|
47 | })
|
48 | })
|
49 |
|
50 | test('file() returns filename with specific extension', t => {
|
51 | _.times(1000, () => {
|
52 | let file = chance.file({ extension: 'doc' })
|
53 | let extension = file.split('.')[1]
|
54 | t.is(extension, 'doc')
|
55 | })
|
56 | })
|
57 |
|
58 | test('file() can take a length and obey it', t => {
|
59 | _.times(1000, () => {
|
60 | let length = chance.d10()
|
61 | let file = chance.file({ length: length })
|
62 | let filename = file.split('.')[0]
|
63 | t.is(filename.length, length)
|
64 | })
|
65 | })
|
66 |
|
67 | test('file() can take a pool of extensions and obey them', t => {
|
68 | _.times(1000, () => {
|
69 | let extensions = [ 'bmp', '3dv', '3dmf', 'doc' ]
|
70 | let file = chance.file({ extensions: extensions })
|
71 | let extension = file.split('.')[1]
|
72 | t.true(extensions.indexOf(extension) !== -1)
|
73 | })
|
74 | })
|
75 |
|
76 | test('file() can take pool of extensions by object collection and obey them', t => {
|
77 | const objectExtensionCollection = {
|
78 | "one": [ "extension_one_1", "extension_one_2", "extension_one_3" ],
|
79 | "two": [ "extension_two_1", "extension_two_2", "extension_two_3" ],
|
80 | "three": [ "extension_three_1", "extension_three_2", "extension_three_3" ]
|
81 | }
|
82 |
|
83 | _.times(1000, () => {
|
84 | let file = chance.file({ extensions: objectExtensionCollection })
|
85 | let extension = file.split('.')[1]
|
86 | let extensionCount = 0
|
87 | for (let key in objectExtensionCollection) {
|
88 | let collection = objectExtensionCollection[key]
|
89 | collection.map((ext) => {
|
90 | if (ext === extension) {
|
91 | extensionCount++
|
92 | }
|
93 | })
|
94 | }
|
95 | t.is(extensionCount, 1)
|
96 | })
|
97 | })
|
98 |
|
99 | test('file() throws if bad extensions option provided', t => {
|
100 | const fn = () => chance.file({ extensions: 10 })
|
101 | t.throws(fn, 'Chance: Extensions must be an Array or Object')
|
102 | })
|
103 |
|
104 | test('file() does not throw if good extensions option provided as array', t => {
|
105 | _.times(1000, () => {
|
106 | const fn = () => chance.file({ extensions: fileExtensions.document })
|
107 | t.notThrows(fn)
|
108 | })
|
109 | })
|
110 |
|
111 | test('file() does not throw if good extensions option provided as object', t => {
|
112 | _.times(1000, () => {
|
113 | const fn = () => chance.file({ extensions: fileExtensions })
|
114 | t.notThrows(fn)
|
115 | })
|
116 | })
|