File: treetop_spec.rb

package info (click to toggle)
ruby-parslet 1.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 908 kB
  • ctags: 473
  • sloc: ruby: 5,220; makefile: 2
file content (74 lines) | stat: -rw-r--r-- 1,841 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'

require 'parslet'

describe Parslet::Expression::Treetop do
  include Parslet

  describe "positive samples" do
    [ # pattern             # input
      "'abc'",              'abc', 
      "...",                'abc', 
      "[1-4]",              '3',
      
      "'abc'?",             'abc', 
      "'abc'?",             '', 
      
      "('abc')",            'abc', 
      
      "'a' 'b'",            'ab', 
      "'a' ('b')",          'ab', 
      
      "'a' / 'b'",          'a', 
      "'a' / 'b'",          'b', 
      
      "'a'*",               'aaa', 
      "'a'*",               '', 

      "'a'+",               'aa', 
      "'a'+",               'a', 
      
      "'a'{1,2}",           'a',
      "'a'{1,2}",           'aa',

      "'a'{1,}",            'a',
      "'a'{1,}",            'aa',

      "'a'{,2}",            '',
      "'a'{,2}",            'a',
      "'a'{,2}",            'aa',
    ].each_slice(2) do |pattern, input|
      context "exp(#{pattern.inspect})" do
        let(:parslet) { exp(pattern) }
        subject { parslet }
        it { should parse(input) }
        context "string representation" do
          subject { exp(parslet.to_s) }
          it { should parse(input, :trace => true) }
        end
      end
    end
  end
  describe "negative samples" do
    [ # pattern             # input
      "'abc'",              'cba', 
      "[1-4]",              '5',
      
      "'a' / 'b'",          'c', 
      
      "'a'+",               '',
      
      "'a'{1,2}",           '',
      "'a'{1,2}",           'aaa',
      
      "'a'{1,}",            '',

      "'a'{,2}",            'aaa',
    ].each_slice(2) do |pattern, input|
      context "exp(#{pattern.inspect})" do
        subject { exp(pattern) }
        it { should_not parse(input) }
      end
    end
  end
end