speculate_about 0.2.2 → 0.4.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/speculate_about.rb +12 -8
- data/lib/speculations/parser.rb +4 -3
- data/lib/speculations/parser/context.rb +23 -4
- data/lib/speculations/parser/state.rb +21 -1
- data/lib/speculations/parser/state/out.rb +44 -0
- data/lib/speculations/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1d08ccbaea3017274bdc86160387aeed915eba6aac5e5119cca48c2e8f4a53a3
|
4
|
+
data.tar.gz: dd53be1c870c773d263a59b03204b469c593e0244df48ea19aaac1be87a45b5d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 222b7eff1615cae7c4e3af5fbaffae7812e9e1144de7884b5a3b7d798e7647f9ea259dddf632e5262977d6a07da1234f22457c84eb656c225ba7745b1a317f49
|
7
|
+
data.tar.gz: 70c8e9c20f41acc002bfdda9ae3fa6f63561538730989c63e25e0460a65785b21d5835c9f16efc4f41ccaf22a51b179a27bc77c42a7585afa0474d2e95a9dcb8
|
data/lib/speculate_about.rb
CHANGED
@@ -3,11 +3,11 @@ require 'rspec'
|
|
3
3
|
require 'speculations/parser'
|
4
4
|
|
5
5
|
module SpeculateAbout
|
6
|
-
def speculate_about file
|
6
|
+
def speculate_about file, alternate_syntax: false
|
7
7
|
paths = _find_files(file, File.dirname( caller.first ))
|
8
8
|
raise ArgumentError, "no files found for pattern #{file}" if paths.empty?
|
9
9
|
paths.each do |path|
|
10
|
-
code = _compile path,
|
10
|
+
code = _compile path, _readable(path), alternate_syntax: alternate_syntax
|
11
11
|
ENV["SPECULATE_ABOUT_DEBUG"] ? _show(code, path) : instance_eval(code, path)
|
12
12
|
end
|
13
13
|
end
|
@@ -15,14 +15,13 @@ module SpeculateAbout
|
|
15
15
|
|
16
16
|
private
|
17
17
|
|
18
|
-
def _compile path, file
|
19
|
-
ast = Speculations::Parser.new.parse_from_file(path, file)
|
18
|
+
def _compile path, file, alternate_syntax: false
|
19
|
+
ast = Speculations::Parser.new.parse_from_file(path, file, alternate_syntax: alternate_syntax)
|
20
20
|
ast.to_code
|
21
21
|
end
|
22
|
-
def
|
23
|
-
|
24
|
-
|
25
|
-
puts code
|
22
|
+
def _readable(path)
|
23
|
+
d = File.dirname(path)
|
24
|
+
File.join(File.basename(d), File.basename(path))
|
26
25
|
end
|
27
26
|
def _find_files file, local_path
|
28
27
|
[Dir.pwd, local_path]
|
@@ -30,6 +29,11 @@ module SpeculateAbout
|
|
30
29
|
Dir.glob(File.join(dir, file))
|
31
30
|
end
|
32
31
|
end
|
32
|
+
def _show(code, path)
|
33
|
+
message = "Generated code for #{path}"
|
34
|
+
_underline(message)
|
35
|
+
puts code
|
36
|
+
end
|
33
37
|
def _underline(message, ul: "=")
|
34
38
|
puts message
|
35
39
|
puts message.gsub(/./, ul)
|
data/lib/speculations/parser.rb
CHANGED
@@ -3,7 +3,7 @@ class Speculations::Parser
|
|
3
3
|
require_relative './parser/context'
|
4
4
|
require_relative './parser/state'
|
5
5
|
|
6
|
-
attr_reader :filename, :input, :orig_filename, :root, :state
|
6
|
+
attr_reader :alternate_syntax, :filename, :input, :orig_filename, :root, :state
|
7
7
|
|
8
8
|
def self.parsers
|
9
9
|
@__parsers__ ||= {
|
@@ -14,7 +14,8 @@ class Speculations::Parser
|
|
14
14
|
}
|
15
15
|
end
|
16
16
|
|
17
|
-
def parse_from_file file, orig_filename = nil
|
17
|
+
def parse_from_file file, orig_filename = nil, alternate_syntax: false
|
18
|
+
@alternate_syntax = alternate_syntax
|
18
19
|
@filename = file
|
19
20
|
@orig_filename = orig_filename || file
|
20
21
|
@input = File
|
@@ -31,7 +32,7 @@ class Speculations::Parser
|
|
31
32
|
end
|
32
33
|
|
33
34
|
def parse!
|
34
|
-
root = node = Context.new(name: "Speculations from #{@filename}", lnb: 0, filename: @filename, orig_filename: orig_filename, parent: nil)
|
35
|
+
root = node = Context.new(alternate_syntax: alternate_syntax, name: "Speculations from #{@filename}", lnb: 0, filename: @filename, orig_filename: orig_filename, parent: nil)
|
35
36
|
input.each_with_index do |line, lnb|
|
36
37
|
@state, node = self.class.parsers.fetch(@state).parse(line, lnb.succ, node)
|
37
38
|
end
|
@@ -3,22 +3,23 @@ class Speculations::Parser::Context
|
|
3
3
|
require_relative './context/example'
|
4
4
|
require_relative './context/setup'
|
5
5
|
|
6
|
-
attr_reader :filename, :level, :lnb, :name, :orig_filename, :parent, :potential_name, :setup
|
6
|
+
attr_reader :alternate_syntax, :filename, :level, :lnb, :name, :orig_filename, :parent, :potential_name, :setup
|
7
7
|
|
8
8
|
def add_child(name:, lnb:)
|
9
9
|
raise "Illegal nesting" if parent
|
10
|
-
children << self.class.new(name: name, lnb: lnb, parent: self)
|
10
|
+
children << self.class.new(alternate_syntax: alternate_syntax, name: name, lnb: lnb, parent: self)
|
11
11
|
children.last
|
12
12
|
end
|
13
13
|
|
14
14
|
def add_example(lnb:, line:)
|
15
15
|
examples << Example.new(lnb: lnb, parent: self, line: line, name: potential_name)
|
16
|
-
|
16
|
+
reset_context
|
17
17
|
examples.last
|
18
18
|
end
|
19
19
|
|
20
20
|
def add_include(lnb:)
|
21
21
|
includes << Include.new(lnb: lnb, parent: self)
|
22
|
+
@given = false
|
22
23
|
includes.last
|
23
24
|
end
|
24
25
|
|
@@ -30,6 +31,10 @@ class Speculations::Parser::Context
|
|
30
31
|
@__examples__ ||= []
|
31
32
|
end
|
32
33
|
|
34
|
+
def given?
|
35
|
+
@given
|
36
|
+
end
|
37
|
+
|
33
38
|
def includes
|
34
39
|
@__includes__ ||= []
|
35
40
|
end
|
@@ -39,8 +44,20 @@ class Speculations::Parser::Context
|
|
39
44
|
lines.flatten.map{ |line| "#{prefix}#{line.strip}" }.join("\n")
|
40
45
|
end
|
41
46
|
|
47
|
+
def reset_context
|
48
|
+
@potential_name = nil
|
49
|
+
@given = false
|
50
|
+
self
|
51
|
+
end
|
52
|
+
|
53
|
+
def set_given given
|
54
|
+
@given = given
|
55
|
+
self
|
56
|
+
end
|
57
|
+
|
42
58
|
def set_name(potential_name)
|
43
59
|
@potential_name = potential_name
|
60
|
+
self
|
44
61
|
end
|
45
62
|
|
46
63
|
def set_setup(lnb:)
|
@@ -60,8 +77,10 @@ class Speculations::Parser::Context
|
|
60
77
|
|
61
78
|
private
|
62
79
|
|
63
|
-
def initialize(lnb:, name:, filename: nil, orig_filename: nil, parent: nil)
|
80
|
+
def initialize(alternate_syntax: false, lnb:, name:, filename: nil, orig_filename: nil, parent: nil)
|
64
81
|
_init_from_parent filename, orig_filename, parent
|
82
|
+
@alternate_syntax = alternate_syntax
|
83
|
+
@given = false
|
65
84
|
@level = parent ? parent.level.succ : 1
|
66
85
|
@lnb = lnb
|
67
86
|
@setup = nil
|
@@ -8,8 +8,12 @@ module Speculations::Parser::State extend self
|
|
8
8
|
CONTEXT_RGX = %r[\A\s{0,3}\#{1,7}\s+Context\s+(.*)]
|
9
9
|
EOBLOCK_RGX = %r[\A\s{0,3}```\s*\z]
|
10
10
|
EXAMPLE_RGX = %r[\A\s{0,3}```.*\s:example]
|
11
|
+
GIVEN_RGX = %r[\A\s{0,3}(?:Given|When)\b]
|
11
12
|
INCLUDE_RGX = %r[\A\s{0,3}```.*\s:include]
|
12
|
-
NAME_RGX = %r[\A\s{0,3}Example
|
13
|
+
NAME_RGX = %r[\A\s{0,3}Example:?\s+(.*)]i
|
14
|
+
RUBY_RGX = %r[\A\s{0,3}```ruby]
|
15
|
+
THEN_RGX = %r[\A\s{0,3}(?:Then|But|And)\s+(.*)]
|
16
|
+
WS_RGX = %r[\A\s*\z]
|
13
17
|
|
14
18
|
def before_match line
|
15
19
|
BEFORE_RGX =~ line
|
@@ -28,6 +32,10 @@ module Speculations::Parser::State extend self
|
|
28
32
|
def example_match line
|
29
33
|
EXAMPLE_RGX =~ line
|
30
34
|
end
|
35
|
+
|
36
|
+
def given_match line
|
37
|
+
GIVEN_RGX =~ line
|
38
|
+
end
|
31
39
|
|
32
40
|
def include_match line
|
33
41
|
INCLUDE_RGX =~ line
|
@@ -36,4 +44,16 @@ module Speculations::Parser::State extend self
|
|
36
44
|
def potential_name line
|
37
45
|
NAME_RGX.match(line)
|
38
46
|
end
|
47
|
+
|
48
|
+
def ruby_match line
|
49
|
+
RUBY_RGX =~ line
|
50
|
+
end
|
51
|
+
|
52
|
+
def then_match line
|
53
|
+
THEN_RGX.match(line)
|
54
|
+
end
|
55
|
+
|
56
|
+
def ws_match line
|
57
|
+
WS_RGX =~ line
|
58
|
+
end
|
39
59
|
end
|
@@ -4,6 +4,50 @@ module Speculations
|
|
4
4
|
module Out extend self
|
5
5
|
|
6
6
|
def parse line, lnb, node
|
7
|
+
if node.alternate_syntax
|
8
|
+
_parse_alternate line, lnb, node
|
9
|
+
else
|
10
|
+
_parse_classical line, lnb, node
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def _parse_alternate line, lnb, node
|
17
|
+
case
|
18
|
+
when name = State.context_match(line)
|
19
|
+
node = node.parent if node.parent
|
20
|
+
new_node = node.add_child(name: name, lnb: lnb)
|
21
|
+
[:out, new_node.reset_context]
|
22
|
+
when State.ws_match(line)
|
23
|
+
[:out, node]
|
24
|
+
when name = State.potential_name(line)
|
25
|
+
node.set_name(name[1])
|
26
|
+
node.set_given(false)
|
27
|
+
[:out, node]
|
28
|
+
when name = State.then_match(line)
|
29
|
+
node.set_name(name[1])
|
30
|
+
node.set_given(false)
|
31
|
+
[:out, node]
|
32
|
+
when State.given_match(line)
|
33
|
+
node.set_name(nil)
|
34
|
+
[:out, node.set_given(true)]
|
35
|
+
when State.ruby_match(line)
|
36
|
+
if node.potential_name
|
37
|
+
node = node.add_example(lnb: lnb, line: line)
|
38
|
+
[:exa, node]
|
39
|
+
elsif node.given?
|
40
|
+
node = node.add_include(lnb: lnb)
|
41
|
+
[:inc, node]
|
42
|
+
else
|
43
|
+
[:out, node]
|
44
|
+
end
|
45
|
+
else
|
46
|
+
[:out, node.reset_context]
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def _parse_classical line, lnb, node
|
7
51
|
case
|
8
52
|
when name = State.context_match(line)
|
9
53
|
node = node.parent if node.parent
|
data/lib/speculations/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: speculate_about
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Robert Dober
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-11-
|
11
|
+
date: 2020-11-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
@@ -63,7 +63,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
63
63
|
- !ruby/object:Gem::Version
|
64
64
|
version: '0'
|
65
65
|
requirements: []
|
66
|
-
rubygems_version: 3.
|
66
|
+
rubygems_version: 3.1.4
|
67
67
|
signing_key:
|
68
68
|
specification_version: 4
|
69
69
|
summary: Extract RSpecs from Markdown
|