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