UNPKG

3.16 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})