Add tests to traverseRegexMatchesInRange

This commit is contained in:
Corey Johnson 2012-03-29 11:01:26 -07:00
parent 51c4e012df
commit d739ebddbc
2 changed files with 36 additions and 1 deletions

View File

@ -243,6 +243,41 @@ describe 'Buffer', ->
expect(matches[2][1]).toBe 'rr'
expect(ranges[2]).toEqual [[6,34], [6,41]]
describe "when the last regex match exceeds the end of the range", ->
describe "when the portion of the match within the range also matches the regex", ->
it "calls the iterator with the truncated match", ->
matches = []
ranges = []
buffer.traverseRegexMatchesInRange /cu(r*)/g, [[4,0], [6,9]], (match, range) ->
matches.push(match)
ranges.push(range)
expect(matches.length).toBe 2
expect(ranges.length).toBe 2
expect(matches[0][0]).toBe 'curr'
expect(matches[0][1]).toBe 'rr'
expect(ranges[0]).toEqual [[5,6], [5,10]]
expect(matches[1][0]).toBe 'cur'
expect(matches[1][1]).toBe 'r'
expect(ranges[1]).toEqual [[6,6], [6,9]]
describe "when the portion of the match within the range does not matches the regex", ->
it "calls the iterator with the truncated match", ->
matches = []
ranges = []
buffer.traverseRegexMatchesInRange /cu(r*)e/g, [[4,0], [6,9]], (match, range) ->
matches.push(match)
ranges.push(range)
expect(matches.length).toBe 1
expect(ranges.length).toBe 1
expect(matches[0][0]).toBe 'curre'
expect(matches[0][1]).toBe 'rr'
expect(ranges[0]).toEqual [[5,6], [5,11]]
describe "when the iterator calls the 'replace' control function with a replacement string", ->
it "replaces each occurrence of the regex match with the string", ->
ranges = []

View File

@ -158,7 +158,7 @@ class Buffer
if matchEndIndex > endIndex
regex.lastIndex = 0
if matchStartIndex < endIndex and match = regex.exec(text[matchStartIndex..endIndex])
if matchStartIndex < endIndex and match = regex.exec(text[matchStartIndex...endIndex])
matchLength = match[0].length
matchEndIndex = matchStartIndex + matchLength
else