locale.spec.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. var assert = require('assert');
  2. var Locale = require('../lib/locale');
  3. var KeyCombo = require('../lib/key-combo');
  4. describe('Locale', function() {
  5. var locale;
  6. beforeEach(function() {
  7. locale = new Locale('test');
  8. });
  9. describe('#bindKeyCode', function() {
  10. it('binds a key code to a key name', function() {
  11. locale.bindKeyCode(0, 'a');
  12. assert.equal(locale._keyMap[0][0], 'a');
  13. });
  14. it('binds a key code to a set of key names', function() {
  15. locale.bindKeyCode(0, ['a', 'b', 'c']);
  16. assert.equal(locale._keyMap[0][0], 'a');
  17. assert.equal(locale._keyMap[0][1], 'b');
  18. assert.equal(locale._keyMap[0][2], 'c');
  19. });
  20. });
  21. describe('#bindMacro', function() {
  22. it('binds a key combo to a key name', function() {
  23. locale.bindMacro('a', 'b');
  24. assert.equal(locale._macros[0].keyCombo.sourceStr, 'a');
  25. assert.equal(locale._macros[0].keyNames[0], 'b');
  26. });
  27. it('binds a key combo to a set of key names', function() {
  28. locale.bindMacro('a', ['b', 'c', 'd']);
  29. assert.equal(locale._macros[0].keyCombo.sourceStr, 'a');
  30. assert.equal(locale._macros[0].keyNames[0], 'b');
  31. assert.equal(locale._macros[0].keyNames[1], 'c');
  32. assert.equal(locale._macros[0].keyNames[2], 'd');
  33. });
  34. it('binds a key combo to a macro handler', function() {
  35. var macroHandler = function() {};
  36. locale.bindMacro('a', macroHandler);
  37. assert.equal(locale._macros[0].keyCombo.sourceStr, 'a');
  38. assert.equal(locale._macros[0].handler, macroHandler);
  39. });
  40. });
  41. describe('#getKeyCodes', function() {
  42. it('gets all key codes associated with a key name', function() {
  43. locale._keyMap[0] = ['a'];
  44. locale._keyMap[1] = ['b'];
  45. locale._keyMap[2] = ['a'];
  46. var keyCodes = locale.getKeyCodes('a');
  47. assert.equal(keyCodes[0], 0);
  48. assert.equal(keyCodes[1], 2);
  49. });
  50. });
  51. describe('#getKeyNames', function() {
  52. it('gets all key names associated with a key code', function() {
  53. locale._keyMap[0] = ['a', 'b'];
  54. var keyNames = locale.getKeyNames(0);
  55. assert.equal(keyNames[0], 'a');
  56. assert.equal(keyNames[1], 'b');
  57. });
  58. });
  59. describe('#setKillKey', function() {
  60. it('marks a key code as a kill key', function() {
  61. locale.setKillKey(0);
  62. assert.equal(locale._killKeyCodes[0], 0);
  63. });
  64. it('marks all key codes matching a key name as a kill key', function() {
  65. locale._keyMap[0] = ['a'];
  66. locale._keyMap[1] = ['a'];
  67. locale.setKillKey('a');
  68. assert.equal(locale._killKeyCodes[0], 0);
  69. assert.equal(locale._killKeyCodes[1], 1);
  70. });
  71. });
  72. describe('#pressKey', function() {
  73. beforeEach(function() {
  74. locale._keyMap = {
  75. 0: ['a', 'b'],
  76. 1: ['a', 'c'],
  77. };
  78. });
  79. it('adds all key names associated with a given key code to pressedKeys', function() {
  80. locale.pressKey(0);
  81. assert.equal(locale.pressedKeys[0], 'a');
  82. assert.equal(locale.pressedKeys[1], 'b');
  83. });
  84. it('adds all other key names associated with the same key code of a given key name to pressedKeys', function() {
  85. locale.pressKey('a');
  86. assert.equal(locale.pressedKeys[0], 'a');
  87. assert.equal(locale.pressedKeys[1], 'b');
  88. assert.equal(locale.pressedKeys[2], 'c');
  89. });
  90. it('applies macros with combos that match the pressed keys', function() {
  91. locale._keyMap = {
  92. 0: ['a'],
  93. 1: ['b']
  94. };
  95. locale._macros = [
  96. {
  97. keyCombo : new KeyCombo('a + b'),
  98. keyNames : ['c', 'd'],
  99. handler : null
  100. }, {
  101. keyCombo : new KeyCombo('a + b'),
  102. handler : function(pressedKeys) {
  103. assert.equal(pressedKeys, locale.pressedKeys);
  104. return ['e', 'f'];
  105. },
  106. keyNames : null
  107. }
  108. ];
  109. locale.pressKey('a');
  110. locale.pressKey('b');
  111. assert.equal(locale.pressedKeys[0], 'a');
  112. assert.equal(locale.pressedKeys[1], 'b');
  113. assert.equal(locale.pressedKeys[2], 'c');
  114. assert.equal(locale.pressedKeys[3], 'd');
  115. assert.equal(locale.pressedKeys[4], 'e');
  116. assert.equal(locale.pressedKeys[5], 'f');
  117. assert.equal(locale._appliedMacros[0], locale._macros[0]);
  118. assert.equal(locale._appliedMacros[1], locale._macros[1]);
  119. assert.equal(locale._appliedMacros[1].keyNames[0], 'e');
  120. assert.equal(locale._appliedMacros[1].keyNames[1], 'f');
  121. });
  122. });
  123. describe('#releaseKey', function() {
  124. beforeEach(function() {
  125. locale._keyMap = {
  126. 0: ['a', 'b'],
  127. 1: ['a', 'c'],
  128. };
  129. locale.pressedKeys = ['a', 'b', 'c'];
  130. });
  131. it('removes all key names associated with a given key code from pressedKeys', function() {
  132. locale.releaseKey(0);
  133. assert.equal(locale.pressedKeys[0], 'c');
  134. });
  135. it('removes all other key names associated with the same key code of a given key name from pressedKeys', function() {
  136. locale.releaseKey('a');
  137. assert.equal(locale.pressedKeys.length, 0);
  138. });
  139. it('clears applied macros with combos that no longer match the pressed keys', function() {
  140. locale._keyMap = {
  141. 0: ['a'],
  142. 1: ['b'],
  143. };
  144. locale._macros = [
  145. {
  146. keyCombo : new KeyCombo('a + b'),
  147. keyNames : ['c', 'd'],
  148. handler : null
  149. }, {
  150. keyCombo : new KeyCombo('a + b'),
  151. keyNames : ['e', 'f'],
  152. handler : function(pressedKeys) {
  153. assert.equal(pressedKeys, locale.pressedKeys);
  154. return ['e', 'f'];
  155. }
  156. }
  157. ];
  158. locale.pressedKeys = ['a', 'b', 'c', 'd', 'e', 'f'];
  159. locale._appliedMacros = locale._macros.slice(0);
  160. locale.releaseKey('a');
  161. assert.equal(locale.pressedKeys[0], 'b');
  162. assert.equal(locale.pressedKeys.length, 1);
  163. assert.equal(locale._appliedMacros.length, 0);
  164. });
  165. });
  166. });