File: contextual_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 (115 lines) | stat: -rw-r--r-- 3,421 bytes parent folder | download | duplicates (5)
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
require 'spec_helper'

describe Parslet::ErrorReporter::Contextual do

  let(:reporter) { described_class.new }
  let(:fake_source) { flexmock('source') }
  let(:fake_atom) { flexmock('atom') }
  let(:fake_cause) { flexmock('cause') }

  describe '#err' do
    before(:each) { fake_source.should_receive(
      :pos => 13,
      :line_and_column => [1,1]) }

    it "returns the deepest cause" do
      flexmock(reporter).
        should_receive(:deepest).and_return(:deepest)
      reporter.err('parslet', fake_source, 'message').
        should == :deepest
    end
  end
  describe '#err_at' do
    before(:each) { fake_source.should_receive(
      :pos => 13,
      :line_and_column => [1,1]) }

    it "returns the deepest cause" do
      flexmock(reporter).
        should_receive(:deepest).and_return(:deepest)
      reporter.err('parslet', fake_source, 'message', 13).
        should == :deepest
    end
  end
  describe '#deepest(cause)' do
    def fake_cause(pos=13, children=nil)
      flexmock('cause' + pos.to_s, :pos => pos, :children => children)
    end

    context "when there is no deepest cause yet" do
      let(:cause) { fake_cause }
      it "returns the given cause" do
        reporter.deepest(cause).should == cause
      end
    end
    context "when the previous cause is deeper (no relationship)" do
      let(:previous) { fake_cause }
      before(:each) {
        reporter.deepest(previous) }

      it "returns the previous cause" do
        reporter.deepest(fake_cause(12)).
          should == previous
      end
    end
    context "when the previous cause is deeper (child)" do
      let(:previous) { fake_cause }
      before(:each) {
        reporter.deepest(previous) }

      it "returns the given cause" do
        given = fake_cause(12, [previous])
        reporter.deepest(given).should == given
      end
    end
    context "when the previous cause is shallower" do
      before(:each) {
        reporter.deepest(fake_cause) }

      it "stores the cause as deepest" do
        deeper = fake_cause(14)
        reporter.deepest(deeper)
        reporter.deepest_cause.should == deeper
      end
    end
  end
  describe '#reset' do
    before(:each) { fake_source.should_receive(
      :pos => Parslet::Position.new('source', 13),
      :line_and_column => [1,1]) }

    it "resets deepest cause on success of sibling expression" do
      flexmock(reporter).
        should_receive(:deepest).and_return(:deepest)
      reporter.err('parslet', fake_source, 'message').
        should == :deepest
      flexmock(reporter).
        should_receive(:reset).once
      reporter.succ(fake_source)
    end
  end

  describe 'label' do
    before(:each) { fake_source.should_receive(
      :pos => Parslet::Position.new('source', 13),
      :line_and_column => [1,1]) }

    it "sets label if atom has one" do
      fake_atom.should_receive(:label).once.and_return('label')
      fake_cause.should_receive(:set_label).once
      flexmock(reporter).
        should_receive(:deepest).and_return(fake_cause)
      reporter.err(fake_atom, fake_source, 'message').
        should == fake_cause
    end

    it 'does not set label if atom does not have one' do
      flexmock(reporter).
        should_receive(:deepest).and_return(:deepest)
      fake_atom.should_receive(:update_label).never
      reporter.err(fake_atom, fake_source, 'message').
        should == :deepest
    end
  end

end