options.test.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. var options = require("..");
  2. exports.noneIsAnOption = function(test) {
  3. test.equal(true, options.isOption(options.none));
  4. test.done();
  5. };
  6. exports.someIsAnOption = function(test) {
  7. test.equal(true, options.isOption(options.some(4)));
  8. test.done();
  9. };
  10. exports.nullIsNotAnOption = function(test) {
  11. test.equal(false, options.isOption(null));
  12. test.done();
  13. };
  14. exports.stringIsNotAnOption = function(test) {
  15. test.equal(false, options.isOption("surrender"));
  16. test.done();
  17. };
  18. exports["none.isNone() returns true"] = function(test) {
  19. test.equal(true, options.none.isNone());
  20. test.done();
  21. };
  22. exports["none.isSome() returns false"] = function(test) {
  23. test.equal(false, options.none.isSome());
  24. test.done();
  25. };
  26. exports["some(_).isNone() returns false"] = function(test) {
  27. test.equal(false, options.some(1).isNone());
  28. test.done();
  29. };
  30. exports["some(_).isSome() returns true"] = function(test) {
  31. test.equal(true, options.some(1).isSome());
  32. test.done();
  33. };
  34. exports.fromNullableConvertsNullToNone = function(test) {
  35. test.deepEqual(options.none, options.fromNullable(null));
  36. test.done();
  37. };
  38. exports.fromNullableConvertsUndefinedToNone = function(test) {
  39. test.deepEqual(options.none, options.fromNullable(undefined));
  40. test.done();
  41. };
  42. exports.fromNullableConvertsNumberToSome = function(test) {
  43. test.deepEqual(options.some(5), options.fromNullable(5));
  44. test.done();
  45. };
  46. exports.mappingOverNoneHasNoEffect = function(test) {
  47. test.equal(options.none, options.none.map(function() { return true }));
  48. test.done();
  49. };
  50. exports.mappingOverSomeAppliesFunctionToValue = function(test) {
  51. test.deepEqual(options.some(4), options.some(2).map(function(value) {
  52. return value * value;
  53. }));
  54. test.done();
  55. };
  56. exports["none.flatMap(_) returns none"] = function(test) {
  57. test.deepEqual(options.none, options.none.flatMap(function(value) {
  58. return options.some("apple");
  59. }));
  60. test.done();
  61. };
  62. exports["when func(value) is none, then some(value).flatMap(func) returns none"] = function(test) {
  63. test.deepEqual(options.none, options.some(2).flatMap(function(value) {
  64. return options.none;
  65. }));
  66. test.done();
  67. };
  68. exports["when func(value) is some(result), then some(value).flatMap(func) returns some(result)"] = function(test) {
  69. test.deepEqual(options.some(4), options.some(2).flatMap(function(value) {
  70. return options.some(value * value);
  71. }));
  72. test.done();
  73. };
  74. exports.noneToArrayIsEmptyArray = function(test) {
  75. test.deepEqual([], options.none.toArray());
  76. test.done();
  77. };
  78. exports.someToArrayIsSingleElementArray = function(test) {
  79. test.deepEqual(["apple"], options.some("apple").toArray());
  80. test.done();
  81. };
  82. exports.noneValueOrElseCallsPassedValueIfItsAFunction = function(test) {
  83. test.deepEqual(4, options.none.valueOrElse(function() { return 4; }));
  84. test.done();
  85. };
  86. exports.noneValueOrElseReturnsArgumentIfItsNotAFunction = function(test) {
  87. test.deepEqual(4, options.none.valueOrElse(4));
  88. test.done();
  89. };
  90. exports.someValueOrElseReturnsWrappedValue = function(test) {
  91. test.deepEqual(1, options.some(1).valueOrElse(function() { return 4; }));
  92. test.done();
  93. };
  94. exports.noneOrElseReturnsArgumentIfItsNotAFunction = function(test) {
  95. test.deepEqual(options.some(4), options.none.orElse(options.some(4)));
  96. test.done();
  97. };
  98. exports.noneOrElseCallsArgumentIfItsAFunction = function(test) {
  99. test.deepEqual(options.some(4), options.none.orElse(function() { return options.some(4); }));
  100. test.done();
  101. };
  102. exports.someOrElseReturnsTheCurrentOption = function(test) {
  103. test.deepEqual(options.some(1), options.some(1).orElse(4));
  104. test.done();
  105. };
  106. exports.callingValueOnNoneRaisesError = function(test) {
  107. test.throws(function(){
  108. options.none.value();
  109. }, /Called value on none/);
  110. test.done();
  111. };
  112. exports.callingValueOnSomeReturnsValue = function(test) {
  113. test.deepEqual(4, options.some(4).value());
  114. test.done();
  115. };
  116. exports["none.filter returns none"] = function(test) {
  117. function equals3(x) {
  118. return x === 3;
  119. }
  120. test.deepEqual(options.none.filter(equals3), options.none);
  121. test.done();
  122. };
  123. exports["when predicate(value) is true, some(value).filter(predicate) returns some(value)"] = function(test) {
  124. var some3 = options.some(3);
  125. function equals3(x) {
  126. return x === 3;
  127. }
  128. test.deepEqual(some3.filter(equals3), some3);
  129. test.done();
  130. };
  131. exports["when predicate(value) is false, some(value).filter(predicate) returns none"] = function(test) {
  132. var some11 = options.some(11);
  133. function equals3(x) {
  134. return x === 3;
  135. }
  136. test.deepEqual(some11.filter(equals3), options.none);
  137. test.done();
  138. };