global.coffee 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. {
  2. _arrayCmp,
  3. getCloseMatches,
  4. _countLeading,
  5. IS_LINE_JUNK,
  6. IS_CHARACTER_JUNK,
  7. _formatRangeUnified,
  8. unifiedDiff,
  9. _formatRangeContext,
  10. contextDiff,
  11. ndiff,
  12. restore
  13. } = require '..'
  14. suite 'global'
  15. test '._arrayCmp', ->
  16. _arrayCmp([1, 2], [1, 2]).should.eql 0
  17. _arrayCmp([1, 2, 3], [1, 2, 4]).should.below 0
  18. _arrayCmp([1], [1, 2]).should.below 0
  19. _arrayCmp([2, 1], [1, 2]).should.above 0
  20. _arrayCmp([2, 0, 0], [2, 3]).should.below 0
  21. _arrayCmp([], [1]).should.below 0
  22. _arrayCmp([1], []).should.above 0
  23. _arrayCmp([], []).should.eql 0
  24. test '.getCloseMatches', ->
  25. getCloseMatches('appel', ['ape', 'apple', 'peach', 'puppy'])
  26. .should.eql ['apple', 'ape']
  27. KEYWORDS = require('coffee-script').RESERVED
  28. getCloseMatches('wheel', KEYWORDS).should.eql ['when', 'while']
  29. getCloseMatches('accost', KEYWORDS).should.eql ['const']
  30. test '._countLeading', ->
  31. _countLeading(' abc', ' ').should.eql 3
  32. test '.IS_LINE_JUNK', ->
  33. IS_LINE_JUNK('\n').should.be.true
  34. IS_LINE_JUNK(' # \n').should.be.true
  35. IS_LINE_JUNK('hello\n').should.be.false
  36. test '.IS_CHARACTER_JUNK', ->
  37. IS_CHARACTER_JUNK(' ').should.be.true
  38. IS_CHARACTER_JUNK('\t').should.be.true
  39. IS_CHARACTER_JUNK('\n').should.be.false
  40. IS_CHARACTER_JUNK('x').should.be.false
  41. test '._formatRangeUnified', ->
  42. _formatRangeUnified(1, 2).should.eql '2'
  43. _formatRangeUnified(1, 3).should.eql '2,2'
  44. _formatRangeUnified(1, 4).should.eql '2,3'
  45. test '.unifiedDiff', ->
  46. unifiedDiff('one two three four'.split(' '),
  47. 'zero one tree four'.split(' '), {
  48. fromfile: 'Original'
  49. tofile: 'Current',
  50. fromfiledate: '2005-01-26 23:30:50',
  51. tofiledate: '2010-04-02 10:20:52',
  52. lineterm: ''
  53. }).should.eql [
  54. '--- Original\t2005-01-26 23:30:50',
  55. '+++ Current\t2010-04-02 10:20:52',
  56. '@@ -1,4 +1,4 @@',
  57. '+zero',
  58. ' one',
  59. '-two',
  60. '-three',
  61. '+tree',
  62. ' four'
  63. ]
  64. test '._formatRangeContext', ->
  65. _formatRangeContext(1, 2).should.eql '2'
  66. _formatRangeContext(1, 3).should.eql '2,3'
  67. _formatRangeContext(1, 4).should.eql '2,4'
  68. test '.contextDiff', ->
  69. a = ['one\n', 'two\n', 'three\n', 'four\n']
  70. b = ['zero\n', 'one\n', 'tree\n', 'four\n']
  71. contextDiff(a, b, {fromfile: 'Original', tofile: 'Current'}).should.eql [
  72. '*** Original\n',
  73. '--- Current\n',
  74. '***************\n',
  75. '*** 1,4 ****\n',
  76. ' one\n',
  77. '! two\n',
  78. '! three\n',
  79. ' four\n',
  80. '--- 1,4 ----\n',
  81. '+ zero\n',
  82. ' one\n',
  83. '! tree\n',
  84. ' four\n'
  85. ]
  86. test 'ndiff', ->
  87. a = ['one\n', 'two\n', 'three\n']
  88. b = ['ore\n', 'tree\n', 'emu\n']
  89. ndiff(a, b).should.eql [
  90. '- one\n',
  91. '? ^\n',
  92. '+ ore\n',
  93. '? ^\n',
  94. '- two\n',
  95. '- three\n',
  96. '? -\n',
  97. '+ tree\n',
  98. '+ emu\n'
  99. ]
  100. test 'restore', ->
  101. a = ['one\n', 'two\n', 'three\n']
  102. b = ['ore\n', 'tree\n', 'emu\n']
  103. diff = ndiff(a, b)
  104. restore(diff, 1).should.eql [
  105. 'one\n',
  106. 'two\n',
  107. 'three\n'
  108. ]
  109. restore(diff, 2).should.eql [
  110. 'ore\n',
  111. 'tree\n',
  112. 'emu\n'
  113. ]
  114. (->
  115. restore(diff, 3)
  116. ).should.throw()