variable-spec.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. const Variable = require('../lib/variable');
  2. const {Point} = require('atom');
  3. describe('Variable', () => {
  4. let fakeCursor = {
  5. getCurrentWordBufferRange () { return true; },
  6. getBufferRow () { return 9; },
  7. };
  8. let fakeSelectionRange = {
  9. isEmpty: () => false
  10. };
  11. let fakeEditor = {
  12. getTitle () { return 'foo.rb'; },
  13. getPath () { return '/Users/pulsar/code/foo.rb'; },
  14. getTextInBufferRange (x) {
  15. return x === true ? 'word' : 'this text is selected';
  16. },
  17. lineTextForBufferRow () {
  18. return `this may be considered an entire line for the purposes of variable tests`;
  19. }
  20. };
  21. let fakeParams = {editor: fakeEditor, cursor: fakeCursor, selectionRange: fakeSelectionRange};
  22. it('resolves to the right value', () => {
  23. const expected = {
  24. 'TM_FILENAME': 'foo.rb',
  25. 'TM_FILENAME_BASE': 'foo',
  26. 'TM_CURRENT_LINE': `this may be considered an entire line for the purposes of variable tests`,
  27. 'TM_CURRENT_WORD': 'word',
  28. 'TM_LINE_INDEX': '9',
  29. 'TM_LINE_NUMBER': '10',
  30. 'TM_DIRECTORY': '/Users/pulsar/code',
  31. 'TM_SELECTED_TEXT': 'this text is selected'
  32. };
  33. for (let variable in expected) {
  34. let vrbl = new Variable({variable});
  35. expect(
  36. vrbl.resolve(fakeParams)
  37. ).toEqual(expected[variable]);
  38. }
  39. });
  40. it('transforms', () => {
  41. let vrbl = new Variable({
  42. variable: 'TM_FILENAME',
  43. substitution: {
  44. find: /(?:^|_)([A-Za-z0-9]+)(?:\.rb)?/g,
  45. replace: [
  46. {escape: 'u'},
  47. {backreference: 1}
  48. ]
  49. },
  50. point: new Point(0, 0),
  51. snippet: {}
  52. });
  53. expect(
  54. vrbl.resolve({editor: fakeEditor})
  55. ).toEqual('Foo');
  56. });
  57. });