test.text.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import test from 'ava'
  2. import Chance from '../chance.js'
  3. import _ from 'lodash'
  4. const chance = new Chance()
  5. // chance.sentence()
  6. test('sentence() returns a random sentence', t => {
  7. _.times(1000, () => {
  8. let sentence = chance.sentence()
  9. t.true(_.isString(sentence))
  10. let len = sentence.split(' ').length
  11. t.true(len > 11)
  12. t.true(len < 19)
  13. })
  14. })
  15. test('sentence() will obey bounds', t => {
  16. _.times(1000, () => {
  17. let sentence = chance.sentence({ words: 5 })
  18. t.true(_.isString(sentence))
  19. t.is(sentence.split(' ').length, 5)
  20. t.true(/[a-zA-Z]+ [a-zA-Z]+ [a-zA-Z]+ [a-zA-Z]+ [a-zA-Z]+.?/m.test(sentence))
  21. })
  22. })
  23. // chance.syllable()
  24. test('syllable() returns a random syllable', t => {
  25. _.times(1000, () => {
  26. let syllable = chance.syllable()
  27. t.true(_.isString(syllable))
  28. t.true(syllable.length > 1)
  29. t.true(syllable.length < 4)
  30. })
  31. })
  32. test('syllable() obeys the capitalize option', t => {
  33. _.times(1000, () => {
  34. let syllable = chance.syllable({ capitalize: true })
  35. t.true(_.isString(syllable))
  36. t.true(syllable.length > 1)
  37. t.true(syllable.length < 4)
  38. t.true(/[A-Z]+/.test(syllable))
  39. })
  40. })
  41. // chance.word()
  42. test('word() returns a random word', t => {
  43. _.times(1000, () => {
  44. let word = chance.word()
  45. t.true(_.isString(word))
  46. t.true(word.length > 1)
  47. t.true(word.length < 10)
  48. })
  49. })
  50. test('word() obeys the capitalize option', t => {
  51. _.times(1000, () => {
  52. let word = chance.word({ capitalize: true })
  53. t.true(_.isString(word))
  54. t.true(word.length > 1)
  55. t.true(word.length < 10)
  56. t.true(word.charAt(0) === word.charAt(0).toUpperCase())
  57. })
  58. })
  59. test('word() can have a set number of syllables', t => {
  60. _.times(1000, () => {
  61. let word = chance.word({ syllables: 3 })
  62. t.true(_.isString(word))
  63. t.true(word.length > 5)
  64. t.true(word.length < 10)
  65. })
  66. })
  67. test('word() can have a set length', t => {
  68. _.times(1000, () => {
  69. let word = chance.word({ length: 5 })
  70. t.true(_.isString(word))
  71. t.is(word.length, 5)
  72. })
  73. })
  74. test('word() throws if both syllables and length specified', t => {
  75. const fn = () => chance.word({ syllables: 3, length: 20 })
  76. t.throws(fn, 'Chance: Cannot specify both syllables AND length.')
  77. })
  78. // chance.paragraph()
  79. test('paragraph() returns a random paragraph', t => {
  80. _.times(100, () => {
  81. let paragraph = chance.paragraph()
  82. t.true(_.isString(paragraph))
  83. let len = paragraph.split('.').length
  84. t.true(len > 2)
  85. t.true(len < 9)
  86. })
  87. })
  88. test('paragraph() will obey bounds', t => {
  89. _.times(100, () => {
  90. let paragraph = chance.paragraph({ sentences: 5 })
  91. t.true(_.isString(paragraph))
  92. // Have to account for the fact that the period at the end will add
  93. // to the count of sentences. This is the fault of our hackish way
  94. // of detecting sentences -- by splitting on '.'
  95. let len = paragraph.split('.').length
  96. t.is(len, 6)
  97. })
  98. })