test.util.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // requires local modules: util
  2. /* jshint expr: true */
  3. var assert = chai.assert;
  4. var expect = chai.expect;
  5. describe('Utils', function() {
  6. "use strict";
  7. describe('Array instance methods', function () {
  8. describe('push8', function () {
  9. it('should push a byte on to the array', function () {
  10. var arr = [1];
  11. arr.push8(128);
  12. expect(arr).to.deep.equal([1, 128]);
  13. });
  14. it('should only use the least significant byte of any number passed in', function () {
  15. var arr = [1];
  16. arr.push8(0xABCD);
  17. expect(arr).to.deep.equal([1, 0xCD]);
  18. });
  19. });
  20. describe('push16', function () {
  21. it('should push two bytes on to the array', function () {
  22. var arr = [1];
  23. arr.push16(0xABCD);
  24. expect(arr).to.deep.equal([1, 0xAB, 0xCD]);
  25. });
  26. it('should only use the two least significant bytes of any number passed in', function () {
  27. var arr = [1];
  28. arr.push16(0xABCDEF);
  29. expect(arr).to.deep.equal([1, 0xCD, 0xEF]);
  30. });
  31. });
  32. describe('push32', function () {
  33. it('should push four bytes on to the array', function () {
  34. var arr = [1];
  35. arr.push32(0xABCDEF12);
  36. expect(arr).to.deep.equal([1, 0xAB, 0xCD, 0xEF, 0x12]);
  37. });
  38. it('should only use the four least significant bytes of any number passed in', function () {
  39. var arr = [1];
  40. arr.push32(0xABCDEF1234);
  41. expect(arr).to.deep.equal([1, 0xCD, 0xEF, 0x12, 0x34]);
  42. });
  43. });
  44. });
  45. describe('logging functions', function () {
  46. beforeEach(function () {
  47. sinon.spy(console, 'log');
  48. sinon.spy(console, 'warn');
  49. sinon.spy(console, 'error');
  50. });
  51. afterEach(function () {
  52. console.log.restore();
  53. console.warn.restore();
  54. console.error.restore();
  55. });
  56. it('should use noop for levels lower than the min level', function () {
  57. Util.init_logging('warn');
  58. Util.Debug('hi');
  59. Util.Info('hello');
  60. expect(console.log).to.not.have.been.called;
  61. });
  62. it('should use console.log for Debug and Info', function () {
  63. Util.init_logging('debug');
  64. Util.Debug('dbg');
  65. Util.Info('inf');
  66. expect(console.log).to.have.been.calledWith('dbg');
  67. expect(console.log).to.have.been.calledWith('inf');
  68. });
  69. it('should use console.warn for Warn', function () {
  70. Util.init_logging('warn');
  71. Util.Warn('wrn');
  72. expect(console.warn).to.have.been.called;
  73. expect(console.warn).to.have.been.calledWith('wrn');
  74. });
  75. it('should use console.error for Error', function () {
  76. Util.init_logging('error');
  77. Util.Error('err');
  78. expect(console.error).to.have.been.called;
  79. expect(console.error).to.have.been.calledWith('err');
  80. });
  81. });
  82. // TODO(directxman12): test the conf_default and conf_defaults methods
  83. // TODO(directxman12): test decodeUTF8
  84. // TODO(directxman12): test the event methods (addEvent, removeEvent, stopEvent)
  85. // TODO(directxman12): figure out a good way to test getPosition and getEventPosition
  86. // TODO(directxman12): figure out how to test the browser detection functions properly
  87. // (we can't really test them against the browsers, except for Gecko
  88. // via PhantomJS, the default test driver)
  89. // TODO(directxman12): figure out how to test Util.Flash
  90. });