time-span-test.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. * time-span-test.js: Tests for the TimeSpan module.
  3. *
  4. * (C) Charlie Robbins
  5. * MIT LICENSE
  6. *
  7. */
  8. var vows = require('vows'),
  9. assert = require('assert'),
  10. timeSpan = require('../lib/time-span'),
  11. helpers = require('./helpers');
  12. vows.describe('time-span').addBatch({
  13. "When using the TimeSpan module": {
  14. "the parse() method": {
  15. "when passed a TimeSpan string with no days": {
  16. "should return a valid TimeSpan object": function () {
  17. var ts = timeSpan.parse("04:03:02.10");
  18. assert.equal(ts.hours, 4);
  19. assert.equal(ts.minutes, 3);
  20. assert.equal(ts.seconds, 2);
  21. assert.equal(ts.milliseconds, 100);
  22. }
  23. },
  24. "when passed a TimeSpan string with days": {
  25. "should return a valid TimeSpan object": function () {
  26. var ts = timeSpan.parse("01:04:03:02.10");
  27. assert.equal(ts.days, 1);
  28. assert.equal(ts.hours, 4);
  29. assert.equal(ts.minutes, 3);
  30. assert.equal(ts.seconds, 2);
  31. assert.equal(ts.milliseconds, 100);
  32. }
  33. }
  34. },
  35. "the test() method": {
  36. "when passed a TimeSpan string with no days": {
  37. "should return true": function () {
  38. assert.isTrue(timeSpan.test("04:03:02.10"));
  39. }
  40. },
  41. "when passed a TimeSpan string with days": {
  42. "should return true": function () {
  43. assert.isTrue(timeSpan.test("01:04:03:02.10"));
  44. }
  45. },
  46. "when passed an invalid TimeSpan string": {
  47. "should return false": function () {
  48. assert.isFalse(timeSpan.test('xx:00:invalid'));
  49. }
  50. }
  51. },
  52. "the fromDates() method": {
  53. "with two Date values": function () {
  54. var diff = 1000 * 60 * 60 * 12,
  55. end = new Date(),
  56. start = new Date(end.getTime() - diff);
  57. assert.equal(12, timeSpan.fromDates(start, end).hours);
  58. },
  59. "with two string values": function () {
  60. assert.equal(2, timeSpan.fromDates('NOW-4DAYS', 'NOW-2DAYS').days);
  61. }
  62. },
  63. "the factory methods": helpers.testFactories(10)
  64. }
  65. }).export(module);