execSync.test.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. var assert = require('assert');
  2. var Machine = require('../');
  3. describe('Machine.prototype.execSync()', function (){
  4. it('should throw when called on a machine that does not declare sync:true', function (){
  5. try {
  6. Machine.build(machineFixtures[0]).execSync();
  7. }
  8. catch (e) {
  9. assert.equal(e.code,'E_USAGE');
  10. return;
  11. }
  12. // Should never make it here
  13. assert(false, 'Should have thrown an error');
  14. });
  15. it('should return the expected value when called on a configured machine that triggers its success exit', function (){
  16. var result = Machine.build(machineFixtures[1]).execSync();
  17. assert.equal(result,'stuff');
  18. });
  19. it('should throw an Error when called on a configured machine that triggers any non-success exit and sends back an Error instance', function (){
  20. var m = Machine.build(machineFixtures[2]);
  21. m.configure({value: 'hi'});
  22. assert.throws(function (){
  23. m.execSync();
  24. });
  25. });
  26. it('should throw an Error when called on a configured machine that triggers any non-success exit and sends back a string', function (){
  27. var m = Machine.build(machineFixtures[2]);
  28. m.configure({value: 'hi'});
  29. assert.throws(function (){
  30. m.execSync();
  31. });
  32. });
  33. it('should throw an Error when called on a configured machine that triggers any non-success exit and sends back a boolean', function (){
  34. var m = Machine.build(machineFixtures[2]);
  35. m.configure({value: true});
  36. assert.throws(function (){
  37. m.execSync();
  38. });
  39. });
  40. it('should throw an Error when called on a configured machine that triggers any non-success exit and sends back a number', function (){
  41. var m = Machine.build(machineFixtures[2]);
  42. m.configure({value: -234.2});
  43. assert.throws(function (){
  44. m.execSync();
  45. });
  46. });
  47. it('should throw an Error when called on a configured machine that triggers any non-success exit and sends back an array', function (){
  48. var m = Machine.build(machineFixtures[2]);
  49. m.configure({value: ['stuff', 'and', 'things']});
  50. assert.throws(function (){
  51. m.execSync();
  52. });
  53. });
  54. it('should throw an Error when called on a configured machine that triggers any non-success exit and sends back an object', function (){
  55. var m = Machine.build(machineFixtures[2]);
  56. m.configure({value: {
  57. email: 'ricketorsomething@stark.io'
  58. }});
  59. assert.throws(function (){
  60. m.execSync();
  61. });
  62. });
  63. });
  64. var machineFixtures = [
  65. // (0)
  66. // Standard asynchronous machine
  67. {
  68. inputs: {
  69. foo: {
  70. example: 100
  71. },
  72. bar: {
  73. example: 'foobar'
  74. }
  75. },
  76. exits: {
  77. success: {
  78. example: 'hello'
  79. },
  80. error: {
  81. example: 'world'
  82. }
  83. },
  84. fn: function (inputs, exits) {
  85. exits.success('stuff');
  86. }
  87. },
  88. // (1)
  89. // Same machine as above, but with sync:true
  90. {
  91. sync: true,
  92. inputs: {
  93. foo: {
  94. example: 100
  95. },
  96. bar: {
  97. example: 'foobar'
  98. }
  99. },
  100. exits: {
  101. success: {
  102. example: 'hello'
  103. },
  104. error: {
  105. example: 'world'
  106. }
  107. },
  108. fn: function (inputs, exits) {
  109. exits.success('stuff');
  110. }
  111. },
  112. // (2)
  113. // Sync machine which calls a non-success exit with whatever
  114. // was passed into the `value` input.
  115. {
  116. sync: true,
  117. inputs: {
  118. value: {
  119. typeclass: '*'
  120. }
  121. },
  122. exits: {
  123. success: {
  124. example: 'hello'
  125. },
  126. someOtherExit: {
  127. getExample: function (inputs){
  128. return inputs.value;
  129. }
  130. },
  131. error: {
  132. example: 'world'
  133. }
  134. },
  135. fn: function (inputs, exits) {
  136. exits.someOtherExit(inputs.value);
  137. }
  138. },
  139. // (3)
  140. // Sync machine which calls a non-success exit with a new Error.
  141. {
  142. sync: true,
  143. inputs: {
  144. value: {
  145. typeclass: '*'
  146. }
  147. },
  148. exits: {
  149. success: {
  150. example: 'hello'
  151. },
  152. someOtherExit: {
  153. getExample: function (inputs){
  154. return inputs.value;
  155. }
  156. },
  157. error: {
  158. example: 'world'
  159. }
  160. },
  161. fn: function (inputs, exits) {
  162. exits.someOtherExit(new Error('deliberate error'));
  163. }
  164. }
  165. ];