parse-human.test.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. var assert = require('assert');
  2. var rttc = require('../');
  3. describe('.parseHuman()', function() {
  4. describe('with no type schema', function (){
  5. it('should understand strings', function() {
  6. assert.strictEqual( rttc.parseHuman('foo'), 'foo');
  7. assert.strictEqual( rttc.parseHuman(''), '');
  8. assert.strictEqual( rttc.parseHuman('stuff'), 'stuff');
  9. });
  10. it('should understand numbers', function() {
  11. assert.strictEqual( rttc.parseHuman('2'), 2);
  12. assert.strictEqual( rttc.parseHuman('-54'), -54);
  13. });
  14. it('should treat -0 and +0 as 0', function(){
  15. assert.strictEqual( rttc.parseHuman('0'), 0);
  16. assert.strictEqual( rttc.parseHuman('-0'), 0);
  17. assert.strictEqual( rttc.parseHuman('+0'), 0);
  18. });
  19. it('should leave null, NaN, Infinity, and -Infinity as strings', function(){
  20. assert.strictEqual( rttc.parseHuman('Infinity'), 'Infinity');
  21. assert.strictEqual( rttc.parseHuman('-Infinity'), '-Infinity');
  22. assert.strictEqual( rttc.parseHuman('NaN'), 'NaN');
  23. assert.strictEqual( rttc.parseHuman('null'), 'null');
  24. });
  25. it('should understand booleans', function() {
  26. assert.strictEqual( rttc.parseHuman('false'), false);
  27. assert.strictEqual( rttc.parseHuman('true'), true);
  28. });
  29. it('should leave JSON-encoded dictionaries and arrays as strings', function (){
  30. assert.strictEqual( rttc.parseHuman('{}'), '{}');
  31. assert.strictEqual( rttc.parseHuman('{"a":2}'), '{"a":2}');
  32. assert.strictEqual( rttc.parseHuman('[]'), '[]');
  33. assert.strictEqual( rttc.parseHuman('["a"]'), '["a"]');
  34. });
  35. });
  36. describe('with a type schema', function (){
  37. it('should perform loose validation on things, causing them to be potentially coerced such that they match the type schema', function() {
  38. assert.strictEqual( rttc.parseHuman('foo', 'string'), 'foo');
  39. assert.strictEqual( rttc.parseHuman('', 'string'), '');
  40. assert.strictEqual( rttc.parseHuman('4', 'number'), 4);
  41. assert.strictEqual( rttc.parseHuman('-99', 'number'), -99);
  42. assert.strictEqual( rttc.parseHuman('false', 'boolean'), false);
  43. assert.strictEqual( rttc.parseHuman('true', 'boolean'), true);
  44. assert.strictEqual( rttc.parseHuman('-0', 'number'), 0);
  45. assert.strictEqual( rttc.parseHuman('0', 'number'), 0);
  46. assert.strictEqual( rttc.parseHuman('', 'string'), '');
  47. });
  48. it('should perform loose validation on things, throwing an error if they cannot be coerced to match the type schema', function() {
  49. assert.throws(function (){ rttc.parseHuman('foo', 'number'); });
  50. assert.throws(function (){ rttc.parseHuman('foo', 'boolean'); });
  51. assert.throws(function (){ rttc.parseHuman('4', 'boolean'); });
  52. assert.throws(function (){ rttc.parseHuman('null', 'boolean'); });
  53. assert.throws(function (){ rttc.parseHuman('null', 'number'); });
  54. assert.throws(function (){ rttc.parseHuman('Infinity', 'number'); });
  55. assert.throws(function (){ rttc.parseHuman('-Infinity', 'number'); });
  56. assert.throws(function (){ rttc.parseHuman('', 'number'); });
  57. assert.throws(function (){ rttc.parseHuman('', 'boolean'); });
  58. });
  59. it('should hydrate functions in `unsafeMode`', function() {
  60. assert(
  61. rttc.isEqual(
  62. rttc.parseHuman('function foo (){}', 'lamda', true),
  63. function foo (){},
  64. 'lamda'
  65. )
  66. );
  67. });
  68. it('should leave functions as strings when NOT in `unsafeMode`', function() {
  69. assert(
  70. rttc.isEqual(
  71. rttc.parseHuman('function foo (){}', 'lamda'),
  72. 'function foo (){}',
  73. 'lamda'
  74. )
  75. );
  76. });
  77. });
  78. describe('edge-cases', function (){
  79. it('should refuse to work if provided with a non-string', function() {
  80. assert.throws(function (){
  81. rttc.parseHuman(4);
  82. });
  83. });
  84. it('should refuse to work in unsafeMode if no type schema is provided', function() {
  85. assert.throws(function (){
  86. rttc.parseHuman('whatever', undefined, true);
  87. });
  88. });
  89. });
  90. });