acceptance.test.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. var assert = require('assert');
  2. var validate = require('../lib/validate');
  3. var validateStrict = require('../lib/validate-strict');
  4. var coerce = require('../lib/coerce');
  5. describe('Acceptance: simple coercion, validation, and strict validation', function() {
  6. describe('.coerce()', function() {
  7. describe('to string', function() {
  8. it('should coerce undefined to base type', function() {
  9. assert.strictEqual(coerce('string', undefined), '');
  10. });
  11. it('should coerce to base type when it gets: null', function (){
  12. assert.strictEqual(coerce('string', null), '');
  13. });
  14. it('should coerce to base type when it gets: NaN', function (){
  15. assert.strictEqual(coerce('string', NaN), '');
  16. });
  17. it('should coerce to base type when it gets: Infinity', function (){
  18. assert.strictEqual(coerce('string', Infinity), '');
  19. });
  20. it('should coerce to base type when it gets: -Infinity', function (){
  21. assert.strictEqual(coerce('string', -Infinity), '');
  22. });
  23. it('should not touch arbitrary string', function() {
  24. assert.strictEqual(coerce('string', 'foo'), 'foo');
  25. });
  26. it('should not touch empty string', function() {
  27. assert.strictEqual(coerce('string', ''), '');
  28. });
  29. it('should not touch integerish string', function() {
  30. assert.strictEqual(coerce('string', '2382'), '2382');
  31. });
  32. it('should not touch negative integerish string', function() {
  33. assert.strictEqual(coerce('string', '-2382'), '-2382');
  34. });
  35. it('should not touch negative zeroish string', function() {
  36. assert.strictEqual(coerce('string', '0'), '0');
  37. });
  38. it('should not touch decimalish string', function() {
  39. assert.strictEqual(coerce('string', '1.325'), '1.325');
  40. });
  41. it('should not touch negative decimalish string', function() {
  42. assert.strictEqual(coerce('string', '-1.325'), '-1.325');
  43. });
  44. it('should coerce numbers to strings', function() {
  45. assert.strictEqual(coerce('string', 2382), '2382');
  46. assert.strictEqual(coerce('string', -2382), '-2382');
  47. assert.strictEqual(coerce('string', 0), '0');
  48. assert.strictEqual(coerce('string', 1.325), '1.325');
  49. assert.strictEqual(coerce('string', -1.325), '-1.325');
  50. });
  51. });
  52. describe('to number', function (){
  53. it('should coerce undefined to base type', function() {
  54. assert.strictEqual(coerce('number', undefined), 0);
  55. });
  56. it('should coerce to base type when it gets: null', function (){
  57. assert.strictEqual(coerce('number', null), 0);
  58. });
  59. it('should coerce to base type when it gets: NaN', function (){
  60. assert.strictEqual(coerce('number', NaN), 0);
  61. });
  62. it('should coerce to base type when it gets: Infinity', function (){
  63. assert.strictEqual(coerce('number', Infinity), 0);
  64. });
  65. it('should coerce to base type when it gets: -Infinity', function (){
  66. assert.strictEqual(coerce('number', -Infinity), 0);
  67. });
  68. it('should not touch positive integer', function (){
  69. assert.strictEqual(coerce('number', 3), 3);
  70. });
  71. it('should not touch negative integer', function (){
  72. assert.strictEqual(coerce('number', -3), -3);
  73. });
  74. it('should not touch negative decimal', function (){
  75. assert.strictEqual(coerce('number', -3.2), -3.2);
  76. });
  77. it('should not touch zero', function (){
  78. assert.strictEqual(coerce('number', 0), 0);
  79. });
  80. it('should coerce "3.25" to 3.25', function() {
  81. assert.strictEqual(coerce('number', '3.25'), 3.25);
  82. });
  83. it('should coerce "-3.25" to -3.25', function() {
  84. assert.strictEqual(coerce('number', '-3.25'), -3.25);
  85. });
  86. it('should coerce "0" to 0', function() {
  87. assert.strictEqual(coerce('number', '0'), 0);
  88. });
  89. });
  90. describe('to boolean', function (){
  91. it('should coerce undefined to base type', function() {
  92. assert.strictEqual(coerce('boolean', undefined), false);
  93. });
  94. it('should coerce to base type when it gets: null', function (){
  95. assert.strictEqual(coerce('boolean', null), false);
  96. });
  97. it('should coerce to base type when it gets: NaN', function (){
  98. assert.strictEqual(coerce('boolean', NaN), false);
  99. });
  100. it('should coerce to base type when it gets: Infinity', function (){
  101. assert.strictEqual(coerce('boolean', Infinity), false);
  102. });
  103. it('should coerce to base type when it gets: -Infinity', function (){
  104. assert.strictEqual(coerce('boolean', Infinity), false);
  105. });
  106. it('should not touch true', function() {
  107. assert.strictEqual(coerce('boolean', true), true);
  108. });
  109. it('should not touch false', function() {
  110. assert.strictEqual(coerce('boolean', false), false);
  111. });
  112. it('should coerce "true" to true', function() {
  113. assert.strictEqual(coerce('boolean', 'true'), true);
  114. });
  115. it('should coerce "false" to false', function() {
  116. assert.strictEqual(coerce('boolean', 'false'), false);
  117. });
  118. });
  119. });
  120. describe('.validateStrict()', function() {
  121. describe('to string', function() {
  122. it('should fail on null', function (){
  123. assert.throws(function (){
  124. validateStrict('string', null);
  125. });
  126. });
  127. it('should fail on NaN', function (){
  128. assert.throws(function (){
  129. validateStrict('string', NaN);
  130. });
  131. });
  132. it('should fail on Infinity', function (){
  133. assert.throws(function (){
  134. validateStrict('string', Infinity);
  135. });
  136. });
  137. it('should fail on -Infinity', function (){
  138. assert.throws(function (){
  139. validateStrict('string', -Infinity);
  140. });
  141. });
  142. it('should choke on `undefined`', function() {
  143. assert.throws(function (){
  144. validateStrict('string', undefined);
  145. });
  146. });
  147. it('should not touch arbitrary string', function() {
  148. assert.strictEqual(validate('string', 'foo'), 'foo');
  149. });
  150. it('should not touch empty string', function() {
  151. assert.strictEqual(validate('string', ''), '');
  152. });
  153. it('should not touch integerish string', function() {
  154. assert.strictEqual(validate('string', '2382'), '2382');
  155. });
  156. it('should not touch negative integerish string', function() {
  157. assert.strictEqual(validate('string', '-2382'), '-2382');
  158. });
  159. it('should not touch negative zeroish string', function() {
  160. assert.strictEqual(validate('string', '0'), '0');
  161. });
  162. it('should not touch decimalish string', function() {
  163. assert.strictEqual(validate('string', '1.325'), '1.325');
  164. });
  165. it('should not touch negative decimalish string', function() {
  166. assert.strictEqual(validate('string', '-1.325'), '-1.325');
  167. });
  168. it('should choke on numbers', function() {
  169. assert.throws(function (){
  170. validateStrict('string', undefined);
  171. });
  172. assert.throws(function (){
  173. validateStrict('string', 2382);
  174. });
  175. assert.throws(function (){
  176. validateStrict('string', -2382);
  177. });
  178. assert.throws(function (){
  179. validateStrict('string', 0);
  180. });
  181. assert.throws(function (){
  182. validateStrict('string', 1.325);
  183. });
  184. assert.throws(function (){
  185. validateStrict('string', -1.325);
  186. });
  187. });
  188. });
  189. describe('to number', function (){
  190. it('should fail on null', function (){
  191. assert.throws(function (){
  192. validateStrict('number', null);
  193. });
  194. });
  195. it('should fail on NaN', function (){
  196. assert.throws(function (){
  197. validateStrict('number', NaN);
  198. });
  199. });
  200. it('should fail on Infinity', function (){
  201. assert.throws(function (){
  202. validateStrict('number', Infinity);
  203. });
  204. });
  205. it('should fail on -Infinity', function (){
  206. assert.throws(function (){
  207. validateStrict('number', -Infinity);
  208. });
  209. });
  210. it('should choke on `undefined`', function() {
  211. assert.throws(function (){
  212. validateStrict('number', undefined);
  213. });
  214. });
  215. it('should not touch positive integer', function (){
  216. assert.strictEqual(validate('number', 3), 3);
  217. });
  218. it('should not touch negative integer', function (){
  219. assert.strictEqual(validate('number', -3), -3);
  220. });
  221. it('should not touch negative decimal', function (){
  222. assert.strictEqual(validate('number', -3.2), -3.2);
  223. });
  224. it('should not touch zero', function (){
  225. assert.strictEqual(validate('number', 0), 0);
  226. });
  227. it('should choke on "3.25"', function() {
  228. assert.throws(function(){
  229. validateStrict('number', '3.25');
  230. });
  231. });
  232. it('should choke on "-3.25"', function() {
  233. assert.throws(function(){
  234. validateStrict('number', '-3.25');
  235. });
  236. });
  237. it('should choke on "0"', function() {
  238. assert.throws(function(){
  239. validateStrict('number', '0');
  240. });
  241. });
  242. });
  243. describe('to boolean', function (){
  244. it('should fail on null', function (){
  245. assert.throws(function (){
  246. validateStrict('boolean', null);
  247. });
  248. });
  249. it('should fail on NaN', function (){
  250. assert.throws(function (){
  251. validateStrict('boolean', NaN);
  252. });
  253. });
  254. it('should fail on Infinity', function (){
  255. assert.throws(function (){
  256. validateStrict('boolean', Infinity);
  257. });
  258. });
  259. it('should fail on -Infinity', function (){
  260. assert.throws(function (){
  261. validateStrict('boolean', -Infinity);
  262. });
  263. });
  264. it('should choke on `undefined`', function() {
  265. assert.throws(function (){
  266. validateStrict('boolean', undefined);
  267. });
  268. });
  269. it('should not touch true', function() {
  270. assert.strictEqual(validate('boolean', true), true);
  271. });
  272. it('should not touch false', function() {
  273. assert.strictEqual(validate('boolean', false), false);
  274. });
  275. it('should choke on "true"', function() {
  276. assert.throws(function (){
  277. validateStrict('boolean', 'true');
  278. });
  279. });
  280. it('should choke on "false"', function() {
  281. assert.throws(function (){
  282. validateStrict('boolean', 'false');
  283. });
  284. });
  285. });
  286. });
  287. });