From d739ebddbc8a5fb26882b9000e76e0fe403e57b6 Mon Sep 17 00:00:00 2001 From: Corey Johnson Date: Thu, 29 Mar 2012 11:01:26 -0700 Subject: [PATCH] Add tests to traverseRegexMatchesInRange --- spec/atom/buffer-spec.coffee | 35 +++++++++++++++++++++++++++++++++++ src/atom/buffer.coffee | 2 +- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/spec/atom/buffer-spec.coffee b/spec/atom/buffer-spec.coffee index 69d4e78a0..a14a08d6e 100644 --- a/spec/atom/buffer-spec.coffee +++ b/spec/atom/buffer-spec.coffee @@ -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 = [] diff --git a/src/atom/buffer.coffee b/src/atom/buffer.coffee index a4c619662..d8dc4a9f3 100644 --- a/src/atom/buffer.coffee +++ b/src/atom/buffer.coffee @@ -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