cofgratx 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/.gitignore +14 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +64 -0
- data/Rakefile +2 -0
- data/cofgratx.gemspec +24 -0
- data/lib/cofgratx.rb +10 -0
- data/lib/cofgratx/cfg/grammar.rb +46 -0
- data/lib/cofgratx/cfg/grammar_error.rb +2 -0
- data/lib/cofgratx/cfg/non_terminal.rb +54 -0
- data/lib/cofgratx/cfg/repetition.rb +9 -0
- data/lib/cofgratx/cfg/rule.rb +160 -0
- data/lib/cofgratx/cfg/rule_error.rb +2 -0
- data/lib/cofgratx/cfg/terminal.rb +25 -0
- data/lib/cofgratx/cfg/translation_repetition_set.rb +33 -0
- data/lib/cofgratx/cfg/translation_repetition_set_error.rb +2 -0
- data/lib/cofgratx/version.rb +3 -0
- data/spec/grammar_spec.rb +423 -0
- data/spec/non_terminal_spec.rb +126 -0
- data/spec/repetition_spec.rb +48 -0
- data/spec/rule_spec.rb +247 -0
- data/spec/spec_helper.rb +91 -0
- data/spec/terminal_spec.rb +60 -0
- data/spec/translation_repetition_set_spec.rb +68 -0
- metadata +119 -0
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'cofgratx/cfg/terminal'
|
3
|
+
|
4
|
+
describe Terminal do
|
5
|
+
context ".initialize" do
|
6
|
+
it "can initialize with a string" do
|
7
|
+
expect{ described_class.new("something") }.to_not raise_error
|
8
|
+
end
|
9
|
+
|
10
|
+
it "can initialize with a regular expression" do
|
11
|
+
expect{ described_class.new(/[a-z]/) }.to_not raise_error
|
12
|
+
end
|
13
|
+
|
14
|
+
it "raises an exception on bad initial objects" do
|
15
|
+
expect{ described_class.new(12345) }.to raise_error(ArgumentError, "expected Regular Expression or String; got #{12345.class.name}")
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
context ".match?" do
|
20
|
+
context "returns false when the terminal is not found at the strings beginning" do
|
21
|
+
it{ expect( described_class.new("test").match?("no match") ).to be_falsey }
|
22
|
+
it{ expect( described_class.new("test").match?("no test first") ).to be_falsey }
|
23
|
+
it{ expect( described_class.new(/tony/).match?("get some sandwiches") ).to be_falsey }
|
24
|
+
it{ expect( described_class.new(/burgers/).match?("bob's burgers are better") ).to be_falsey }
|
25
|
+
it{ expect( described_class.new(/b.*s/).match?("hamburgers") ).to be_falsey }
|
26
|
+
end
|
27
|
+
|
28
|
+
context "returns true when the terminal is found at the start of the string" do
|
29
|
+
it{ expect( described_class.new("test").match?("test") ).to be_truthy }
|
30
|
+
it{ expect( described_class.new("test").match?("test first") ).to be_truthy }
|
31
|
+
it{ expect( described_class.new(/tony/).match?("tony, get some sandwiches") ).to be_truthy }
|
32
|
+
it{ expect( described_class.new(/burgers/).match?("burgers by bob are better") ).to be_truthy }
|
33
|
+
it{ expect( described_class.new(/b.*s/).match?("burgers and hams") ).to be_truthy }
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
context ".extract" do
|
38
|
+
context "returns nil and the unmodified string when the terminal is not found at the strings beginning" do
|
39
|
+
it{ expect( described_class.new("test").extract("no match") ).to match_array( [nil, "no match"] ) }
|
40
|
+
it{ expect( described_class.new("test").extract("no test first") ).to match_array( [nil, "no test first"] ) }
|
41
|
+
it{ expect( described_class.new(/tony/).extract("get some sandwiches") ).to match_array( [nil, "get some sandwiches"] ) }
|
42
|
+
it{ expect( described_class.new(/burgers/).extract("bob's burgers are better") ).to match_array( [nil, "bob's burgers are better"] ) }
|
43
|
+
it{ expect( described_class.new(/b.*s/).extract("hamburgers") ).to match_array( [nil, "hamburgers"] ) }
|
44
|
+
end
|
45
|
+
|
46
|
+
context "returns the terminal match and remainder of string" do
|
47
|
+
it "does not mutate the input string" do
|
48
|
+
input_string = "Don't change me!"
|
49
|
+
expect( described_class.new("Don't change me!").extract(input_string) ).to match_array( ["Don't change me!", ""] )
|
50
|
+
expect( input_string ).to match "Don't change me!"
|
51
|
+
end
|
52
|
+
it{ expect( described_class.new("test").extract("test") ).to match_array( ["test", ""] ) }
|
53
|
+
it{ expect( described_class.new("test").extract("test first") ).to match_array( ["test", " first"] ) }
|
54
|
+
it{ expect( described_class.new(/tony/).extract("tony, get some sandwiches") ).to match_array( ["tony", ", get some sandwiches"] ) }
|
55
|
+
it{ expect( described_class.new(/burgers/).extract("burgers by bob are better") ).to match_array( ["burgers", " by bob are better"] ) }
|
56
|
+
it{ expect( described_class.new(/b.*?s/).extract("burgers and hams") ).to match_array( ["burgers", " and hams"] ) }
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'cofgratx/cfg/translation_repetition_set_error'
|
3
|
+
require 'cofgratx/cfg/translation_repetition_set'
|
4
|
+
|
5
|
+
describe TranslationRepetitionSet do
|
6
|
+
|
7
|
+
context '.offset=' do
|
8
|
+
before do
|
9
|
+
@set = described_class.new
|
10
|
+
end
|
11
|
+
|
12
|
+
it { expect{ @set.offset = 1 }.to_not raise_error }
|
13
|
+
it { expect{ @set.offset = 13 }.to_not raise_error }
|
14
|
+
|
15
|
+
[ "gibberish", nil, "", 1.234, "2", "three" ].each do |bad_input|
|
16
|
+
it { expect{ @set.offset = bad_input }.to raise_error(ArgumentError, "expected Fixnum; got '#{bad_input.class.name}'") }
|
17
|
+
end
|
18
|
+
|
19
|
+
[ 0, -3, -1 ].each do |bad_input|
|
20
|
+
it { expect{ @set.offset = bad_input }.to raise_error(ArgumentError, "expected positive Fixnum; got '#{bad_input}'") }
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
context '.translations=' do
|
25
|
+
before do
|
26
|
+
@set = described_class.new
|
27
|
+
end
|
28
|
+
|
29
|
+
it { expect{ @set.translations = 1 }.to_not raise_error }
|
30
|
+
it { expect{ @set.translations = [1] }.to_not raise_error }
|
31
|
+
it { expect{ @set.translations = 13, "one", 5 }.to_not raise_error }
|
32
|
+
it { expect{ @set.translations = ["one", "two", 3, 4, 5] }.to_not raise_error }
|
33
|
+
|
34
|
+
[ [0], [1, 0, -3, 2], -44 ].each do |bad_input|
|
35
|
+
it { expect{ @set.translations = bad_input }.to raise_error(TranslationRepetitionSetError, "subrule number cannot be less than 1") }
|
36
|
+
end
|
37
|
+
|
38
|
+
[ [/bad/], [1.231], [nil] ].each do |bad_input|
|
39
|
+
it { expect{ @set.translations = bad_input }.to raise_error(ArgumentError, "expected Fixnum or String; got #{bad_input.first.class.name}") }
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
context ".initialize" do
|
44
|
+
it { expect{ described_class.new }.to_not raise_error }
|
45
|
+
it { expect{ described_class.new(1) }.to_not raise_error }
|
46
|
+
it { expect{ described_class.new(1, "foo", "bar", 6) }.to_not raise_error }
|
47
|
+
it { expect{ described_class.new(1, ["foo", "bar", 6]) }.to_not raise_error }
|
48
|
+
|
49
|
+
it "sets the offset and translations by calling the appropriate methods" do
|
50
|
+
expect_any_instance_of(described_class).to receive(:offset=).with(1).and_call_original
|
51
|
+
expect_any_instance_of(described_class).to receive(:translations=).with(["foo",3]).and_call_original
|
52
|
+
|
53
|
+
set = described_class.new(1, "foo", 3)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
context ".offset" do
|
58
|
+
it "retrieves the correct offset" do
|
59
|
+
expect( described_class.new(3).offset ).to equal 3
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
context ".translations" do
|
64
|
+
it "retrieves the correct translations" do
|
65
|
+
expect( described_class.new(3, 5, 4, 3, "splat").translations ).to match_array [5, 4, 3, "splat"]
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
metadata
ADDED
@@ -0,0 +1,119 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cofgratx
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- callahat
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-03-19 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
description: The CFG class can be used to create a specification for a context free
|
56
|
+
grammar and define translations for it
|
57
|
+
email:
|
58
|
+
- tim.callahan25@yahoo.com
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- .gitignore
|
64
|
+
- .rspec
|
65
|
+
- Gemfile
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- cofgratx.gemspec
|
70
|
+
- lib/cofgratx.rb
|
71
|
+
- lib/cofgratx/cfg/grammar.rb
|
72
|
+
- lib/cofgratx/cfg/grammar_error.rb
|
73
|
+
- lib/cofgratx/cfg/non_terminal.rb
|
74
|
+
- lib/cofgratx/cfg/repetition.rb
|
75
|
+
- lib/cofgratx/cfg/rule.rb
|
76
|
+
- lib/cofgratx/cfg/rule_error.rb
|
77
|
+
- lib/cofgratx/cfg/terminal.rb
|
78
|
+
- lib/cofgratx/cfg/translation_repetition_set.rb
|
79
|
+
- lib/cofgratx/cfg/translation_repetition_set_error.rb
|
80
|
+
- lib/cofgratx/version.rb
|
81
|
+
- spec/grammar_spec.rb
|
82
|
+
- spec/non_terminal_spec.rb
|
83
|
+
- spec/repetition_spec.rb
|
84
|
+
- spec/rule_spec.rb
|
85
|
+
- spec/spec_helper.rb
|
86
|
+
- spec/terminal_spec.rb
|
87
|
+
- spec/translation_repetition_set_spec.rb
|
88
|
+
homepage: ''
|
89
|
+
licenses:
|
90
|
+
- MIT
|
91
|
+
metadata: {}
|
92
|
+
post_install_message:
|
93
|
+
rdoc_options: []
|
94
|
+
require_paths:
|
95
|
+
- lib
|
96
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
97
|
+
requirements:
|
98
|
+
- - ! '>='
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '0'
|
101
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
102
|
+
requirements:
|
103
|
+
- - ! '>='
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
106
|
+
requirements: []
|
107
|
+
rubyforge_project:
|
108
|
+
rubygems_version: 2.4.3
|
109
|
+
signing_key:
|
110
|
+
specification_version: 4
|
111
|
+
summary: A context free grammar validator and translator
|
112
|
+
test_files:
|
113
|
+
- spec/grammar_spec.rb
|
114
|
+
- spec/non_terminal_spec.rb
|
115
|
+
- spec/repetition_spec.rb
|
116
|
+
- spec/rule_spec.rb
|
117
|
+
- spec/spec_helper.rb
|
118
|
+
- spec/terminal_spec.rb
|
119
|
+
- spec/translation_repetition_set_spec.rb
|