SequenceMatcher.coffee
  1  {SequenceMatcher} = require '..'
  2  
  3  suite 'SequenceMatcher'
  4  
  5  test '#setSeqs', ->
  6    s = new SequenceMatcher()
  7    s.setSeqs('abcd', 'bcde')
  8    s.ratio().should.eql 0.75
  9  
 10  test '#setSeq1', ->
 11    s = new SequenceMatcher(null, 'abcd', 'bcde')
 12    s.ratio().should.eql 0.75
 13    s.setSeq1('bcde')
 14    s.ratio().should.eql 1.0
 15  
 16  test '#setSeq2', ->
 17    s = new SequenceMatcher(null, 'abcd', 'bcde')
 18    s.ratio().should.eql 0.75
 19    s.setSeq2('abcd')
 20    s.ratio().should.eql 1.0
 21  
 22  test '#findLongestMatch', ->
 23    isjunk = (x) -> x is ' '
 24    s = new SequenceMatcher(isjunk, ' abcd', 'abcd abcd')
 25    m = s.findLongestMatch(0, 5, 0, 9)
 26    m.should.eql [1, 0, 4]
 27  
 28    s = new SequenceMatcher(null, 'ab', 'c')
 29    m = s.findLongestMatch(0, 2, 0, 1)
 30    m.should.eql [0, 0, 0]
 31  
 32  test '#getMatchingBlocks', ->
 33    s = new SequenceMatcher(null, 'abxcd', 'abcd')
 34    ms = s.getMatchingBlocks()
 35    ms.should.eql [[0, 0, 2], [3, 2, 2], [5, 4, 0]]
 36  
 37    isjunk = (x) -> x is ' '
 38    s = new SequenceMatcher(isjunk,
 39                            'private Thread currentThread;',
 40                            'private volatile Thread currentThread;')
 41    s.getMatchingBlocks().should.eql [ [0, 0, 8], [8, 17, 21], [29, 38, 0] ]
 42  
 43  test '#getOpcodes', ->
 44    s = new SequenceMatcher(null, 'qabxcd', 'abycdf')
 45    s.getOpcodes().should.eql [
 46       [ 'delete'  , 0 , 1 , 0 , 0 ] ,
 47       [ 'equal'   , 1 , 3 , 0 , 2 ] ,
 48       [ 'replace' , 3 , 4 , 2 , 3 ] ,
 49       [ 'equal'   , 4 , 6 , 3 , 5 ] ,
 50       [ 'insert'  , 6 , 6 , 5 , 6 ]
 51    ]
 52  
 53    isjunk = (x) -> x is ' '
 54    s = new SequenceMatcher(isjunk,
 55                            'private Thread currentThread;',
 56                            'private volatile Thread currentThread;')
 57  
 58    s.getOpcodes().should.eql [
 59      ['equal', 0, 8, 0, 8],
 60      ['insert', 8, 8, 8, 17],
 61      ['equal', 8, 29, 17, 38]
 62    ]
 63  
 64  test '#getGroupedOpcodes', ->
 65    a = [1...40].map(String)
 66    b = a.slice()
 67    b[8...8] = 'i'
 68    b[20] += 'x'
 69    b[23...28] = []
 70    b[30] += 'y'
 71    s = new SequenceMatcher(null, a, b)
 72    s.getGroupedOpcodes().should.eql [
 73      [
 74        [ 'equal'  , 5 , 8  , 5 , 8 ],
 75        [ 'insert' , 8 , 8  , 8 , 9 ],
 76        [ 'equal'  , 8 , 11 , 9 , 12 ]
 77      ],
 78      [
 79        [ 'equal'   , 16 , 19 , 17 , 20 ],
 80        [ 'replace' , 19 , 20 , 20 , 21 ],
 81        [ 'equal'   , 20 , 22 , 21 , 23 ],
 82        [ 'delete'  , 22 , 27 , 23 , 23 ],
 83        [ 'equal'   , 27 , 30 , 23 , 26 ]
 84      ],
 85      [
 86        [ 'equal'   , 31 , 34 , 27 , 30 ],
 87        [ 'replace' , 34 , 35 , 30 , 31 ],
 88        [ 'equal'   , 35 , 38 , 31 , 34 ]
 89      ]
 90    ]
 91  
 92  test '#ratio', ->
 93    s = new SequenceMatcher(null, 'abcd', 'bcde')
 94    s.ratio().should.equal 0.75
 95  
 96    isjunk = (x) -> x is ' '
 97    s = new SequenceMatcher(isjunk,
 98                            'private Thread currentThread;',
 99                            'private volatile Thread currentThread;')
100    s.ratio().toPrecision(3).should.eql '0.866'
101  
102  test '#quickRatio', ->
103    s = new SequenceMatcher(null, 'abcd', 'bcde')
104    s.quickRatio().should.equal 0.75
105  
106  test '#realQuickRatio', ->
107    s = new SequenceMatcher(null, 'abcd', 'bcde')
108    s.realQuickRatio().should.equal 1.0