File: slice_spec.rb

package info (click to toggle)
ruby-parslet 1.8.2-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 808 kB
  • sloc: ruby: 5,406; sh: 4; makefile: 3
file content (151 lines) | stat: -rw-r--r-- 4,439 bytes parent folder | download | duplicates (2)
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
require 'spec_helper'

describe Parslet::Slice do
  def cslice string, offset, cache=nil
    described_class.new(
      Parslet::Position.new(string, offset), 
      string, cache)
  end

  describe "construction" do
    it "should construct from an offset and a string" do
      cslice('foobar', 40)
    end
  end
  context "('foobar', 40, 'foobar')" do
    let(:slice) { cslice('foobar', 40) }
    describe "comparison" do
      it "should be equal to other slices with the same attributes" do
        other = cslice('foobar', 40)
        slice.should == other
        other.should == slice
      end 
      it "should be equal to other slices (offset is irrelevant for comparison)" do
        other = cslice('foobar', 41)
        slice.should == other
        other.should == slice
      end 
      it "should be equal to a string with the same content" do
        slice.should == 'foobar'
      end
      it "should be equal to a string (inversed operands)" do
        'foobar'.should == slice
      end 
      it "should not be equal to a string" do
        slice.should_not equal('foobar')
      end 
      it "should not be eql to a string" do
        slice.should_not eql('foobar')
      end 
      it "should not hash to the same number" do
        slice.hash.should_not == 'foobar'.hash
      end 
    end
    describe "offset" do
      it "should return the associated offset" do
        slice.offset.should == 6
      end
      it "should fail to return a line and column" do
        lambda {
          slice.line_and_column
        }.should raise_error(ArgumentError)
      end 
      
      context "when constructed with a source" do
        let(:slice) { cslice(
          'foobar', 40,  
          flexmock(:cache, :line_and_column => [13, 14])) }
        it "should return proper line and column" do
          slice.line_and_column.should == [13, 14]
        end
      end
    end
    describe "string methods" do
      describe "matching" do
        it "should match as a string would" do
          slice.should match(/bar/)
          slice.should match(/foo/)

          md = slice.match(/f(o)o/)
          md.captures.first.should == 'o'
        end
      end
      describe "<- #size" do
        subject { slice.size }
        it { should == 6 } 
      end
      describe "<- #length" do
        subject { slice.length }
        it { should == 6 } 
      end
      describe "<- #+" do
        let(:other) { cslice('baz', 10) }
        subject { slice + other }
        
        it "should concat like string does" do
          subject.size.should == 9
          subject.should == 'foobarbaz'
          subject.offset.should == 6
        end 
      end
    end
    describe "conversion" do
      describe "<- #to_slice" do
        it "should return self" do
          slice.to_slice.should eq(slice)
        end 
      end
      describe "<- #to_sym" do
        it "should return :foobar" do
          slice.to_sym.should == :foobar
        end 
      end
      describe "cast to Float" do
        it "should return a float" do
          Float(cslice('1.345', 11)).should == 1.345
        end 
      end
      describe "cast to Integer" do
        it "should cast to integer as a string would" do
          s = cslice('1234', 40)
          Integer(s).should == 1234
          s.to_i.should == 1234
        end 
        it "should fail when Integer would fail on a string" do
           # Ruby 2.6 will call `#to_i` if `#to_int` fails
           if RUBY_VERSION.gsub(/[^\d]/, '').to_i >= 260
             Integer(slice).should == 0
           else
             lambda do
               Integer(slice)
             end.should raise_error(ArgumentError, /invalid value/)
           end
        end
        it "should turn into zero when a string would" do
          slice.to_i.should == 0
        end 
      end
    end
    describe "inspection and string conversion" do
      describe "#inspect" do
        subject { slice.inspect }
        it { should == '"foobar"@6' }
      end
      describe "#to_s" do
        subject { slice.to_s }
        it { should == 'foobar' }
      end
    end
    describe "serializability" do
      it "should serialize" do
        Marshal.dump(slice)
      end
      context "when storing a line cache" do
        let(:slice) { cslice('foobar', 40, Parslet::Source::LineCache.new()) }
        it "should serialize" do
          Marshal.dump(slice)
        end
      end
    end
  end
end