File: context_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 (56 lines) | stat: -rw-r--r-- 1,277 bytes parent folder | download | duplicates (4)
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
require 'spec_helper'

describe Parslet::Context do
  def context(*args)
    described_class.new(*args)
  end
  
  it "binds hash keys as variable like things" do
    context(:a => 'value').instance_eval { a }.
      should == 'value'
  end
  it "one contexts variables aren't the next ones" do
    ca = context(:a => 'b')
    cb = context(:b => 'c')

    ca.methods.should_not include(:b)
    cb.methods.should_not include(:a)
  end

  describe 'works as a Ruby object should' do
    let(:obj) { context(a: 1) }

    it 'responds_to? :a' do
      assert obj.respond_to?(:a)
    end
    it 'includes :a in #methods' do
      obj.methods.assert.include?(:a)
    end
    it 'allows inspection' do
      obj.inspect.assert.match(/@a=1/)
    end
    it 'allows conversion to string' do
      obj.to_s.assert.match(/Parslet::Context:0x/)
    end

    context 'when the context is enhanced' do
      before(:each) do
        class << obj
          def foo
            'foo'
          end
        end
      end

      it 'responds_to correctly' do
        assert obj.respond_to?(:foo)
      end
      it 'includes :foo also in methods' do
        obj.methods.assert.include?(:foo)
      end
      it 'allows calling #foo' do
        obj.foo.assert == 'foo'
      end
    end
  end
end