File: line_cache_spec.rb

package info (click to toggle)
ruby-parslet 2.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,260 kB
  • sloc: ruby: 6,157; sh: 8; javascript: 3; makefile: 3
file content (74 lines) | stat: -rw-r--r-- 2,320 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
require 'spec_helper'

describe Parslet::Source::RangeSearch do
  describe "<- #lbound" do
    context "for a simple array" do
      let(:ary) { [10, 20, 30, 40, 50] }
      before(:each) { ary.extend Parslet::Source::RangeSearch }

      it "should return correct answers for numbers not in the array" do
        ary.lbound(5).should == 0
        ary.lbound(15).should == 1
        ary.lbound(25).should == 2
        ary.lbound(35).should == 3
        ary.lbound(45).should == 4
      end
      it "should return correct answers for numbers in the array" do
        ary.lbound(10).should == 1
        ary.lbound(20).should == 2
        ary.lbound(30).should == 3
        ary.lbound(40).should == 4
      end
      it "should cover right edge case" do
        ary.lbound(50).should be_nil
        ary.lbound(51).should be_nil
      end 
      it "should cover left edge case" do
        ary.lbound(0).should == 0
      end
    end
    context "for an empty array" do
      let(:ary) { [] }
      before(:each) { ary.extend Parslet::Source::RangeSearch }

      it "should return nil" do
        ary.lbound(1).should be_nil
      end 
    end
  end
end

describe Parslet::Source::LineCache do
  describe "<- scan_for_line_endings" do
    context "calculating the line_and_columns" do
      let(:str) { "foo\nbar\nbazd" }

      it "should return the first line if we have no line ends" do
        subject.scan_for_line_endings(0, nil)
        subject.line_and_column(3).should == [1, 4]

        subject.scan_for_line_endings(0, "")
        subject.line_and_column(5).should == [1, 6]
      end

      it "should find the right line starting from pos 0" do
        subject.scan_for_line_endings(0, str)
        subject.line_and_column(5).should == [2, 2]
        subject.line_and_column(9).should == [3, 2]
      end

      it "should find the right line starting from pos 5" do
        subject.scan_for_line_endings(5, str)
        subject.line_and_column(11).should == [2, 3]
      end

      it "should find the right line if scannning the string multiple times" do
        subject.scan_for_line_endings(0, str)
        subject.scan_for_line_endings(0, "#{str}\nthe quick\nbrown fox")
        subject.line_and_column(10).should == [3,3]
        subject.line_and_column(24).should == [5,2]
      end
    end
  end
end