UNPKG

4.75 kBJavaScriptView Raw
1import test from 'ava'
2import Chance from '../chance.js'
3import _ from 'lodash'
4
5const chance = new Chance()
6
7// chance.sentence()
8test('sentence() returns a random sentence', t => {
9 _.times(1000, () => {
10 let sentence = chance.sentence()
11 t.true(_.isString(sentence))
12 let len = sentence.split(' ').length
13 t.true(len > 11)
14 t.true(len < 19)
15 })
16})
17
18test('sentence() will obey bounds', t => {
19 _.times(1000, () => {
20 let sentence = chance.sentence({ words: 5 })
21 t.true(_.isString(sentence))
22 t.is(sentence.split(' ').length, 5)
23 t.true(/[a-zA-Z]+ [a-zA-Z]+ [a-zA-Z]+ [a-zA-Z]+ [a-zA-Z]+.?/m.test(sentence))
24 })
25})
26
27// chance.syllable()
28test('syllable() returns a random syllable', t => {
29 _.times(1000, () => {
30 let syllable = chance.syllable()
31 t.true(_.isString(syllable))
32 t.true(syllable.length > 1)
33 t.true(syllable.length < 4)
34 })
35})
36
37test('syllable() obeys the capitalize option', t => {
38 _.times(1000, () => {
39 let syllable = chance.syllable({ capitalize: true })
40 t.true(_.isString(syllable))
41 t.true(syllable.length > 1)
42 t.true(syllable.length < 4)
43 t.true(/[A-Z]+/.test(syllable))
44 })
45})
46
47// chance.word()
48test('word() returns a random word', t => {
49 _.times(1000, () => {
50 let word = chance.word()
51 t.true(_.isString(word))
52 t.true(word.length > 1)
53 t.true(word.length < 10)
54 })
55})
56
57test('word() obeys the capitalize option', t => {
58 _.times(1000, () => {
59 let word = chance.word({ capitalize: true })
60 t.true(_.isString(word))
61 t.true(word.length > 1)
62 t.true(word.length < 10)
63 t.true(word.charAt(0) === word.charAt(0).toUpperCase())
64 })
65})
66
67test('word() can have a set number of syllables', t => {
68 _.times(1000, () => {
69 let word = chance.word({ syllables: 3 })
70 t.true(_.isString(word))
71 t.true(word.length > 5)
72 t.true(word.length < 10)
73 })
74})
75
76test('word() can have a set length', t => {
77 _.times(1000, () => {
78 let word = chance.word({ length: 5 })
79 t.true(_.isString(word))
80 t.is(word.length, 5)
81 })
82})
83
84test('word() throws if both syllables and length specified', t => {
85 const fn = () => chance.word({ syllables: 3, length: 20 })
86 t.throws(fn, 'Chance: Cannot specify both syllables AND length.')
87})
88
89// chance.paragraph()
90test('paragraph() returns a random paragraph', t => {
91 _.times(100, () => {
92 let paragraph = chance.paragraph()
93 t.true(_.isString(paragraph))
94
95 let len = paragraph.split('.').length
96 t.true(len > 2)
97 t.true(len < 9)
98 })
99})
100
101test('paragraph() will obey bounds', t => {
102 _.times(100, () => {
103 let paragraph = chance.paragraph({ sentences: 5 })
104 t.true(_.isString(paragraph))
105
106 // Have to account for the fact that the period at the end will add
107 // to the count of sentences. This is the fault of our hackish way
108 // of detecting sentences -- by splitting on '.'
109 let len = paragraph.split('.').length
110 t.is(len, 6)
111 })
112})
113
114test('paragraph) will obey line breaks', t => {
115 _.times(100, () => {
116 let rand = _.random(1, 50);
117 let paragraph = chance.paragraph({ sentences: rand, linebreak: true })
118 t.true(_.isString(paragraph))
119
120 let len = paragraph.split('\n').length
121 t.is(len, rand);
122 })
123})
124
125test('emoji() will return a single emoji by default', t => {
126 let emoji = chance.emoji();
127
128 t.true(_.isString(emoji));
129 t.is([...emoji].length, 1);
130})
131
132test('emoji() will return as many emojis as you tell it to', t => {
133 _.times(100, () => {
134 let rand = _.random(1, 50);
135 let emoji = chance.emoji({ length: rand });
136
137 t.true(_.isString(emoji));
138 t.is([...emoji].length, rand);
139 })
140})
141
142test('emoji() will throw an error when category is unrecognised', t => {
143 let fn = () => chance.emoji({ category: 'something-incorrect' });
144 t.throws(fn, "Chance: Unrecognised emoji category: [something-incorrect].");
145})
146
147test('emoji() will throw an error when length is 0', t => {
148 let fn = () => chance.emoji({ length: 0 });
149 t.throws(fn, "Chance: length must be between 1 and 9007199254740992");
150})
151
152test('emoji() will throw an error when length is negative', t => {
153 let fn = () => chance.emoji({ length: -1 });
154 t.throws(fn, "Chance: length must be between 1 and 9007199254740992");
155})
156
157test('emoji() will throw an error when length is greater than 9007199254740992', t => {
158 let fn = () => chance.emoji({ length: BigInt('9007199254740993') });
159 t.throws(fn, "Chance: length must be between 1 and 9007199254740992");
160})
\No newline at end of file