speculate_about 0.1.0 → 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/speculate_about.rb +22 -10
- data/lib/speculations/parser.rb +4 -3
- data/lib/speculations/parser/context.rb +21 -11
- data/lib/speculations/parser/context/example.rb +16 -2
- data/lib/speculations/parser/state.rb +5 -0
- data/lib/speculations/parser/state/out.rb +4 -1
- data/lib/speculations/version.rb +3 -0
- metadata +8 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5abf0f1d0b408b81d4b2b9644eeaaca4137bcf0180f298bb94145b275a847ecd
|
4
|
+
data.tar.gz: a040f692852c90de48d3d1a758ecda31ce8a3d1f36920ce44145d396f7666aca
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d6d2d63515ade99e8eaac787572359c3b707301263d4ebbee6ce3df03546426262202e092eef337239cf9a82e92096e8fa7318756b405f46900c0514fd1d1d4c
|
7
|
+
data.tar.gz: 5ca6224cf00ab3ee4cd13d8ba2df5cfc83bcc7d516a018435f097ed509f94a27cc2ec9df054a2cc43b2215a3399a0b79c4279c00b01b9fd27181e0c0ac905597
|
data/lib/speculate_about.rb
CHANGED
@@ -4,23 +4,35 @@ require 'speculations/parser'
|
|
4
4
|
|
5
5
|
module SpeculateAbout
|
6
6
|
def speculate_about file
|
7
|
-
|
8
|
-
|
9
|
-
|
7
|
+
paths = _find_files(file, File.dirname( caller.first ))
|
8
|
+
raise ArgumentError, "no files found for pattern #{file}" if paths.empty?
|
9
|
+
paths.each do |path|
|
10
|
+
code = _compile path, file
|
11
|
+
ENV["SPECULATE_ABOUT_DEBUG"] ? _show(code, path) : instance_eval(code, path)
|
12
|
+
end
|
10
13
|
end
|
11
14
|
|
12
15
|
|
13
16
|
private
|
14
17
|
|
15
|
-
def _compile path
|
16
|
-
ast = Speculations::Parser.new.parse_from_file(path)
|
18
|
+
def _compile path, file
|
19
|
+
ast = Speculations::Parser.new.parse_from_file(path, file)
|
17
20
|
ast.to_code
|
18
21
|
end
|
19
|
-
def
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
22
|
+
def _show(code, path)
|
23
|
+
message = "Generated code for #{path}"
|
24
|
+
_underline(message)
|
25
|
+
puts code
|
26
|
+
end
|
27
|
+
def _find_files file, local_path
|
28
|
+
[Dir.pwd, local_path]
|
29
|
+
.flat_map do |dir|
|
30
|
+
Dir.glob(File.join(dir, file))
|
31
|
+
end
|
32
|
+
end
|
33
|
+
def _underline(message, ul: "=")
|
34
|
+
puts message
|
35
|
+
puts message.gsub(/./, ul)
|
24
36
|
end
|
25
37
|
end
|
26
38
|
|
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, :root, :state
|
6
|
+
attr_reader :filename, :input, :orig_filename, :root, :state
|
7
7
|
|
8
8
|
def self.parsers
|
9
9
|
@__parsers__ ||= {
|
@@ -14,8 +14,9 @@ class Speculations::Parser
|
|
14
14
|
}
|
15
15
|
end
|
16
16
|
|
17
|
-
def parse_from_file file
|
17
|
+
def parse_from_file file, orig_filename = nil
|
18
18
|
@filename = file
|
19
|
+
@orig_filename = orig_filename || file
|
19
20
|
@input = File
|
20
21
|
.new(file)
|
21
22
|
.each_line(chomp: true)
|
@@ -30,7 +31,7 @@ class Speculations::Parser
|
|
30
31
|
end
|
31
32
|
|
32
33
|
def parse!
|
33
|
-
root = node = Context.new(name: "Speculations from #{@filename}", lnb: 0, filename: @filename, parent: nil)
|
34
|
+
root = node = Context.new(name: "Speculations from #{@filename}", lnb: 0, filename: @filename, orig_filename: orig_filename, parent: nil)
|
34
35
|
input.each_with_index do |line, lnb|
|
35
36
|
@state, node = self.class.parsers.fetch(@state).parse(line, lnb.succ, node)
|
36
37
|
end
|
@@ -3,7 +3,7 @@ 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, :parent, :setup
|
6
|
+
attr_reader :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
|
@@ -11,8 +11,9 @@ class Speculations::Parser::Context
|
|
11
11
|
children.last
|
12
12
|
end
|
13
13
|
|
14
|
-
def add_example(lnb:)
|
15
|
-
examples << Example.new(lnb: lnb, parent: self)
|
14
|
+
def add_example(lnb:, line:)
|
15
|
+
examples << Example.new(lnb: lnb, parent: self, line: line, name: potential_name)
|
16
|
+
@potential_name = nil
|
16
17
|
examples.last
|
17
18
|
end
|
18
19
|
|
@@ -38,6 +39,10 @@ class Speculations::Parser::Context
|
|
38
39
|
lines.flatten.map{ |line| "#{prefix}#{line.strip}" }.join("\n")
|
39
40
|
end
|
40
41
|
|
42
|
+
def set_name(potential_name)
|
43
|
+
@potential_name = potential_name
|
44
|
+
end
|
45
|
+
|
41
46
|
def set_setup(lnb:)
|
42
47
|
@setup = Setup.new(lnb: lnb, parent: self)
|
43
48
|
end
|
@@ -55,20 +60,25 @@ class Speculations::Parser::Context
|
|
55
60
|
|
56
61
|
private
|
57
62
|
|
58
|
-
def initialize(lnb:, name:, filename: nil, parent: nil)
|
59
|
-
_init_from_parent filename, parent
|
60
|
-
@level
|
61
|
-
@lnb
|
62
|
-
@setup
|
63
|
-
@name
|
64
|
-
@parent
|
63
|
+
def initialize(lnb:, name:, filename: nil, orig_filename: nil, parent: nil)
|
64
|
+
_init_from_parent filename, orig_filename, parent
|
65
|
+
@level = parent ? parent.level.succ : 1
|
66
|
+
@lnb = lnb
|
67
|
+
@setup = nil
|
68
|
+
@name = name
|
69
|
+
@parent = parent
|
65
70
|
end
|
66
71
|
|
67
72
|
def _header
|
68
73
|
map_lines(%{context "#{name}" do}, indent: -1)
|
69
74
|
end
|
70
75
|
|
71
|
-
def _init_from_parent filename, parent
|
76
|
+
def _init_from_parent filename, orig_filename, parent
|
77
|
+
_set_filename filename, orig_filename, parent
|
78
|
+
@orig_filename = parent ? parent.orig_filename : orig_filename
|
79
|
+
end
|
80
|
+
|
81
|
+
def _set_filename filename, orig_filename, parent
|
72
82
|
@filename = parent ? parent.filename : filename
|
73
83
|
raise ArgumentError, "no filename given in root context" unless @filename
|
74
84
|
end
|
@@ -2,6 +2,8 @@ class Speculations::Parser::Context::Example
|
|
2
2
|
|
3
3
|
attr_reader :lnb, :name, :parent
|
4
4
|
|
5
|
+
NAMED_EXAMPLE = %r{\A[`~]{3,}ruby\s+:example\s+(.*)}
|
6
|
+
|
5
7
|
def add_line line
|
6
8
|
lines << line
|
7
9
|
self
|
@@ -22,10 +24,22 @@ class Speculations::Parser::Context::Example
|
|
22
24
|
|
23
25
|
private
|
24
26
|
|
25
|
-
def initialize(lnb:, parent:)
|
27
|
+
def initialize(lnb:, line:, parent:, name: nil)
|
26
28
|
@lnb = lnb
|
27
|
-
@name = "Example from #{parent.filename}:#{lnb.succ}"
|
28
29
|
@parent = parent
|
30
|
+
@name = _compute_name(lnb: lnb, line: line, parent: parent, name: name)
|
31
|
+
end
|
32
|
+
|
33
|
+
def _compute_name(lnb:, parent:, line:, name:)
|
34
|
+
_, name1 = NAMED_EXAMPLE.match(line).to_a
|
35
|
+
|
36
|
+
if name1&.empty? == false # SIC
|
37
|
+
"#{name1} (#{parent.orig_filename}:#{lnb.succ})"
|
38
|
+
elsif name
|
39
|
+
"#{name} (#{parent.orig_filename}:#{lnb.succ})"
|
40
|
+
else
|
41
|
+
"Example from #{parent.orig_filename}:#{lnb.succ}"
|
42
|
+
end
|
29
43
|
end
|
30
44
|
|
31
45
|
def _example_head
|
@@ -9,6 +9,7 @@ module Speculations::Parser::State extend self
|
|
9
9
|
EOBLOCK_RGX = %r[\A\s{0,3}```\s*\z]
|
10
10
|
EXAMPLE_RGX = %r[\A\s{0,3}```.*\s:example]
|
11
11
|
INCLUDE_RGX = %r[\A\s{0,3}```.*\s:include]
|
12
|
+
NAME_RGX = %r[\A\s{0,3}Example:\s+(.*)]
|
12
13
|
|
13
14
|
def before_match line
|
14
15
|
BEFORE_RGX =~ line
|
@@ -31,4 +32,8 @@ module Speculations::Parser::State extend self
|
|
31
32
|
def include_match line
|
32
33
|
INCLUDE_RGX =~ line
|
33
34
|
end
|
35
|
+
|
36
|
+
def potential_name line
|
37
|
+
NAME_RGX.match(line)
|
38
|
+
end
|
34
39
|
end
|
@@ -13,11 +13,14 @@ module Speculations
|
|
13
13
|
node = node.set_setup(lnb: lnb)
|
14
14
|
[:bef, node]
|
15
15
|
when State.example_match(line)
|
16
|
-
node = node.add_example(lnb: lnb)
|
16
|
+
node = node.add_example(lnb: lnb, line: line)
|
17
17
|
[:exa, node]
|
18
18
|
when State.include_match(line)
|
19
19
|
node = node.add_include(lnb: lnb)
|
20
20
|
[:inc, node]
|
21
|
+
when name = State.potential_name(line)
|
22
|
+
node.set_name(name[1])
|
23
|
+
[:out, node]
|
21
24
|
else
|
22
25
|
[:out, node]
|
23
26
|
end
|
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.2.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Robert Dober
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-11-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
@@ -24,7 +24,7 @@ dependencies:
|
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '3.9'
|
27
|
-
description: Allows Markdown or other text files to be used as literal specs,
|
27
|
+
description: Allows Markdown or other text files to be used as literal specs, à la
|
28
28
|
Elixr doctest, but from any file.
|
29
29
|
email: robert.dober@gmail.com
|
30
30
|
executables: []
|
@@ -43,11 +43,12 @@ files:
|
|
43
43
|
- lib/speculations/parser/state/exa.rb
|
44
44
|
- lib/speculations/parser/state/inc.rb
|
45
45
|
- lib/speculations/parser/state/out.rb
|
46
|
+
- lib/speculations/version.rb
|
46
47
|
homepage: https://github.com/robertdober/speculate
|
47
48
|
licenses:
|
48
49
|
- Apache-2.0
|
49
50
|
metadata: {}
|
50
|
-
post_install_message:
|
51
|
+
post_install_message:
|
51
52
|
rdoc_options: []
|
52
53
|
require_paths:
|
53
54
|
- lib
|
@@ -62,8 +63,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
62
63
|
- !ruby/object:Gem::Version
|
63
64
|
version: '0'
|
64
65
|
requirements: []
|
65
|
-
rubygems_version: 3.1
|
66
|
-
signing_key:
|
66
|
+
rubygems_version: 3.2.0.rc.1
|
67
|
+
signing_key:
|
67
68
|
specification_version: 4
|
68
69
|
summary: Extract RSpecs from Markdown
|
69
70
|
test_files: []
|