primitives.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. var test = require('tap').test;
  2. var {generateCreateBodyFunctionBody} = require('../lib/codeGenerators/generateCreateBody');
  3. function primitive(dimension) {
  4. let res = (new Function(generateCreateBodyFunctionBody(dimension)))();
  5. return res;
  6. }
  7. test('Body has properties force, pos and mass', function(t) {
  8. debugger;
  9. var body = new (primitive(2).Body)();
  10. t.ok(body.force, 'Force attribute is missing on body');
  11. t.ok(body.pos, 'Pos attribute is missing on body');
  12. t.ok(body.velocity, 'Velocity attribute is missing on body');
  13. t.ok(typeof body.mass === 'number' && body.mass !== 0, 'Body should have a mass');
  14. t.end();
  15. });
  16. test('Vector has x and y', function(t) {
  17. var vector = new (primitive(2).Vector)();
  18. t.ok(typeof vector.x === 'number', 'Vector has x coordinates');
  19. t.ok(typeof vector.y === 'number', 'Vector has y coordinates');
  20. var initialized = new (primitive(2).Vector)(1, 2);
  21. t.equal(initialized.x, 1, 'Vector initialized properly');
  22. t.equal(initialized.y, 2, 'Vector initialized properly');
  23. var badInput = new (primitive(2).Vector)('hello world');
  24. t.equal(badInput.x, 0, 'Vector should be resilient to bed input');
  25. t.equal(badInput.y, 0, 'Vector should be resilient to bed input');
  26. t.end();
  27. });
  28. test('Body3d has properties force, pos and mass', function(t) {
  29. var body = new (primitive(3).Body)();
  30. t.ok(body.force, 'Force attribute is missing on body');
  31. t.ok(body.pos, 'Pos attribute is missing on body');
  32. t.ok(body.velocity, 'Velocity attribute is missing on body');
  33. t.ok(typeof body.mass === 'number' && body.mass !== 0, 'Body should have a mass');
  34. t.end();
  35. });
  36. test('Vector3d has x and y and z', function(t) {
  37. var vector = new (primitive(3).Vector)();
  38. t.ok(typeof vector.x === 'number', 'Vector has x coordinates');
  39. t.ok(typeof vector.y === 'number', 'Vector has y coordinates');
  40. t.ok(typeof vector.z === 'number', 'Vector has z coordinates');
  41. var initialized = new (primitive(3).Vector)(1, 2, 3);
  42. t.equal(initialized.x, 1, 'Vector initialized properly');
  43. t.equal(initialized.y, 2, 'Vector initialized properly');
  44. t.equal(initialized.z, 3, 'Vector initialized properly');
  45. var badInput = new (primitive(3).Vector)('hello world');
  46. t.equal(badInput.x, 0, 'Vector should be resilient to bed input');
  47. t.equal(badInput.y, 0, 'Vector should be resilient to bed input');
  48. t.equal(badInput.z, 0, 'Vector should be resilient to bed input');
  49. t.end();
  50. });
  51. test('reset vector', function(t) {
  52. var v3 = new (primitive(3).Vector)(1, 2, 3);
  53. v3.reset();
  54. t.equal(v3.x, 0, 'Reset to 0');
  55. t.equal(v3.y, 0, 'Reset to 0');
  56. t.equal(v3.z, 0, 'Reset to 0');
  57. var v2 = new (primitive(2).Vector)(1, 2);
  58. v2.reset();
  59. t.equal(v2.x, 0, 'Reset to 0');
  60. t.equal(v2.y, 0, 'Reset to 0');
  61. t.end();
  62. });
  63. test('vector can use copy constructor', function(t) {
  64. var a = new (primitive(3).Vector)(1, 2, 3);
  65. var b = new (primitive(3).Vector)(a);
  66. t.equal(b.x, a.x, 'Value copied');
  67. t.equal(b.y, a.y, 'Value copied');
  68. t.equal(b.z, a.z, 'Value copied');
  69. t.end();
  70. });
  71. test('Body3d can set position', function(t) {
  72. var body = new (primitive(3).Body)();
  73. body.setPosition(10, 11, 12);
  74. t.equal(body.pos.x, 10, 'x is correct');
  75. t.equal(body.pos.y, 11, 'y is correct');
  76. t.equal(body.pos.z, 12, 'z is correct');
  77. t.end();
  78. });
  79. test('Body can set position', function(t) {
  80. var body = new (primitive(2).Body)();
  81. body.setPosition(10, 11);
  82. t.equal(body.pos.x, 10, 'x is correct');
  83. t.equal(body.pos.y, 11, 'y is correct');
  84. t.end();
  85. });