SequenceMatcher.coffee 2.9 KB

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