sorted_collection.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Utilities: A classic collection of JavaScript utilities
  3. * Copyright 2112 Matthew Eernisse ([email protected])
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. var SortedCollection = require('../lib/sorted_collection').SortedCollection
  19. , assert = require('assert')
  20. , tests;
  21. tests = {
  22. 'test no default value': function () {
  23. // Set up a collection, no default value for new items
  24. var c = new SortedCollection();
  25. // Add some items
  26. c.addItem('testA', 'AAAA');
  27. c.addItem('testB', 'BBBB');
  28. c.addItem('testC', 'CCCC');
  29. // Test count
  30. assert.equal(3, c.count);
  31. // Test getItem by string key
  32. var item = c.getItem('testC');
  33. assert.equal('CCCC', item);
  34. // Test getItem by index number
  35. var item = c.getItem(1);
  36. assert.equal('BBBB', item);
  37. // Test setItem by string key
  38. c.setItem('testA', 'aaaa');
  39. var item = c.getItem('testA');
  40. assert.equal('aaaa', item);
  41. // Test setItem by index number
  42. c.setItem(2, 'cccc');
  43. var item = c.getItem(2);
  44. assert.equal('cccc', item);
  45. }
  46. , 'test default value': function () {
  47. // Set up a collection, default value for new items is 'foo'
  48. var c = new SortedCollection('foo');
  49. // Add an item with no value -- should get
  50. // default value
  51. c.addItem('testA');
  52. // Add some items with empty/falsey values --
  53. // should be set to desired values
  54. c.addItem('testB', null);
  55. c.addItem('testC', false);
  56. // Test getItem for default value
  57. var item = c.getItem('testA');
  58. assert.equal('foo', item);
  59. var item = c.getItem('testB');
  60. assert.equal(null, item);
  61. var item = c.getItem('testC');
  62. assert.equal(false, item);
  63. }
  64. , 'test each': function () {
  65. var c = new SortedCollection()
  66. , str = '';
  67. // Add an item with no value -- should get
  68. // default value
  69. c.addItem('a', 'A');
  70. c.addItem('b', 'B');
  71. c.addItem('c', 'C');
  72. c.addItem('d', 'D');
  73. c.each(function (val, key) {
  74. str += val + key;
  75. });
  76. assert.equal('AaBbCcDd', str);
  77. }
  78. , 'test removing an item': function () {
  79. var c = new SortedCollection()
  80. , str = '';
  81. // Add an item with no value -- should get
  82. // default value
  83. c.addItem('a', 'A');
  84. c.addItem('b', 'B');
  85. c.addItem('c', 'C');
  86. c.addItem('d', 'D');
  87. assert.equal(4, c.count);
  88. omg = c.removeItem('c');
  89. assert.equal(3, c.count);
  90. c.each(function (val, key) {
  91. str += val + key;
  92. });
  93. assert.equal('AaBbDd', str);
  94. }
  95. , 'test clone': function () {
  96. var c = new SortedCollection()
  97. , copy;
  98. c.addItem('a', 'A');
  99. c.addItem('b', 'B');
  100. copy = c.clone();
  101. assert.equal(2, copy.count);
  102. assert.equal('A', copy.getItem('a'));
  103. }
  104. };
  105. module.exports = tests;