summaryrefslogtreecommitdiff
path: root/src/node_modules/chance/test/test.file.js
blob: 4b0bf61cef5915dde10645e4c7296b5d8bcc02cd (plain)
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
import test from 'ava'
import Chance from '../chance.js'
import _ from 'lodash'

const chance = new Chance()

const fileExtensions = {
    "raster": [ "bmp", "gif", "gpl", "ico", "jpeg", "psd", "png", "psp", "raw",
                "tiff" ],
    "vector": [ "3dv", "amf", "awg", "ai", "cgm", "cdr", "cmx", "dxf", "e2d",
                "egt", "eps", "fs", "odg", "svg", "xar" ],
    "3d": [ "3dmf", "3dm", "3mf", "3ds", "an8", "aoi", "blend", "cal3d", "cob",
            "ctm", "iob", "jas", "max", "mb", "mdx", "obj", "x", "x3d" ],
    "document": [ "doc", "docx", "dot", "html", "xml", "odt", "odm", "ott", "csv",
                  "rtf", "tex", "xhtml", "xps" ]
}

// chance.file()
test('file() returns random file length with random extension', t => {
    _.times(1000, () => {
        let file = chance.file()
        t.true(_.isString(file))
        t.is(file.split('.').length, 2)
    })
})

test('file() returns error if wrong fileType provided', t => {
    _.times(1000, () => {
        const fn = () => chance.file({ fileType: 'not_specified' })
        t.throws(fn, 'Chance: Expect file type value to be \'raster\', \'vector\', \'3d\' or \'document\'')
    })
})

test('file() does not return error if legit fileType provided', t => {
    _.times(1000, () => {
        const fn = () => chance.file({ fileType: 'raster' })
        t.notThrows(fn)
    })
})

test('file() returns filename with specific extension type', t => {
    _.times(1000, () => {
        let file = chance.file({ fileType: 'raster' })
        t.true(_.isString(file))
        let extension = file.split('.')[1]
        t.true(fileExtensions['raster'].indexOf(extension) !== -1)
    })
})

test('file() returns filename with specific extension', t => {
    _.times(1000, () => {
        let file = chance.file({ extension: 'doc' })
        let extension = file.split('.')[1]
        t.is(extension, 'doc')
    })
})

test('file() can take a length and obey it', t => {
    _.times(1000, () => {
        let length = chance.d10()
        let file = chance.file({ length: length })
        let filename = file.split('.')[0]
        t.is(filename.length, length)
    })
})

test('file() can take a pool of extensions and obey them', t => {
    _.times(1000, () => {
        let extensions = [ 'bmp', '3dv', '3dmf', 'doc' ]
        let file = chance.file({ extensions: extensions })
        let extension = file.split('.')[1]
        t.true(extensions.indexOf(extension) !== -1)
    })
})

test('file() can take pool of extensions by object collection and obey them', t => {
    const objectExtensionCollection = {
        "one": [ "extension_one_1", "extension_one_2", "extension_one_3" ],
        "two": [ "extension_two_1", "extension_two_2", "extension_two_3" ],
        "three": [ "extension_three_1", "extension_three_2", "extension_three_3" ]
    }

    _.times(1000, () => {
        let file = chance.file({ extensions: objectExtensionCollection })
        let extension = file.split('.')[1]
        let extensionCount = 0
        for (let key in objectExtensionCollection) {
            let collection = objectExtensionCollection[key]
            collection.map((ext) => {
                if (ext === extension) {
                    extensionCount++
                }
            })
        }
        t.is(extensionCount, 1)
    })
})

test('file() throws if bad extensions option provided', t => {
    const fn = () => chance.file({ extensions: 10 })
    t.throws(fn, 'Chance: Extensions must be an Array or Object')
})

test('file() does not throw if good extensions option provided as array', t => {
    _.times(1000, () => {
        const fn = () => chance.file({ extensions: fileExtensions.document })
        t.notThrows(fn)
    })
})

test('file() does not throw if good extensions option provided as object', t => {
    _.times(1000, () => {
        const fn = () => chance.file({ extensions: fileExtensions })
        t.notThrows(fn)
    })
})