speculate_about 0.1.1 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 01f8a6b1bd4d875d642678f3587c58494d61280f9064debbac19b5b60c3da4ee
4
- data.tar.gz: 409f1b1d636138e95180144267eab84a81eee537734e0bc417ee4e0e7af579f0
3
+ metadata.gz: a3d2e27062babfecbf03e34c59b76b495cbb1a9f27d3cbd0f4b23501d3be9181
4
+ data.tar.gz: bdde751f7fa2c532dc95551f06ecc22f44fba32ad0f70103371e1743f62ca347
5
5
  SHA512:
6
- metadata.gz: ea0a3e7ec17457a6de3bbefe6cc01c4a99be7626ad87ff3c766a04e62795f0648048e5fea50a6d85f31be9b321a73b7296ffa1ff65bfb4dbb38a4670216d13ad
7
- data.tar.gz: 455a502c19ca76b5aa5ebf5ddabec69d435f4af28a00b3f96bc048c98b50b44d6b72646e2e2f39ca62f2d486b6b9b2b57880eed493d43cc43338a7ec19930469
6
+ metadata.gz: 95b2fd95e73b0b95d8c9237200bdb86c198f8584987d1f0a54968b742b46c1a06ffff29eb6352962a14e54c9d9d7e047a02f98c57b78a8894fc5c14a4f941d8c
7
+ data.tar.gz: bc56ce52b6e4bc87fbff62c5fb5583ad6fe683b4dd78e18bc8b6d6da28bfbdedf46d3397e297c70878dff8633e3c8c0aa15cb89c871d01ff3c84a8c32c3ccf12
@@ -4,23 +4,39 @@ require 'speculations/parser'
4
4
 
5
5
  module SpeculateAbout
6
6
  def speculate_about file
7
- path = _find_file(file, File.dirname( caller.first ))
8
- code = _compile path
9
- ENV["SPECULATE_ABOUT_DEBUG"] ? puts(code) : instance_eval(code)
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, _readable(path)
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 _find_file file, local_path
20
- return file if File.readable? file
21
- local_file = File.join(local_path, file)
22
- return local_file if File.readable? local_file
23
- raise ArgumentError, "file #{file.inspect} not found"
22
+ def _readable(path)
23
+ d = File.dirname(path)
24
+ File.join(File.basename(d), File.basename(path))
25
+ end
26
+ def _find_files file, local_path
27
+ [Dir.pwd, local_path]
28
+ .flat_map do |dir|
29
+ Dir.glob(File.join(dir, file))
30
+ end
31
+ end
32
+ def _show(code, path)
33
+ message = "Generated code for #{path}"
34
+ _underline(message)
35
+ puts code
36
+ end
37
+ def _underline(message, ul: "=")
38
+ puts message
39
+ puts message.gsub(/./, ul)
24
40
  end
25
41
  end
26
42
 
@@ -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
@@ -12,7 +12,8 @@ class Speculations::Parser::Context
12
12
  end
13
13
 
14
14
  def add_example(lnb:, line:)
15
- examples << Example.new(lnb: lnb, parent: self, line: 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 = parent ? parent.level.succ : 1
61
- @lnb = lnb
62
- @setup = nil
63
- @name = name
64
- @parent = 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
@@ -24,19 +24,21 @@ class Speculations::Parser::Context::Example
24
24
 
25
25
  private
26
26
 
27
- def initialize(lnb:, line:, parent:)
27
+ def initialize(lnb:, line:, parent:, name: nil)
28
28
  @lnb = lnb
29
29
  @parent = parent
30
- @name = _compute_name(lnb: lnb, line: line, parent: parent)
30
+ @name = _compute_name(lnb: lnb, line: line, parent: parent, name: name)
31
31
  end
32
32
 
33
- def _compute_name(lnb:, parent:, line:)
34
- _, name = NAMED_EXAMPLE.match(line).to_a
33
+ def _compute_name(lnb:, parent:, line:, name:)
34
+ _, name1 = NAMED_EXAMPLE.match(line).to_a
35
35
 
36
- if name&.empty? == false # SIC
37
- "#{name} (#{File.basename(parent.filename)}:#{lnb.succ})"
36
+ if name1&.empty? == false # SIC
37
+ "#{name1} (#{parent.orig_filename}:#{lnb.succ})"
38
+ elsif name
39
+ "#{name} (#{parent.orig_filename}:#{lnb.succ})"
38
40
  else
39
- "Example from #{parent.filename}:#{lnb.succ}"
41
+ "Example from #{parent.orig_filename}:#{lnb.succ}"
40
42
  end
41
43
  end
42
44
 
@@ -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
@@ -18,6 +18,9 @@ module Speculations
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
@@ -0,0 +1,3 @@
1
+ module SpeculateAbout
2
+ VERSION = "0.2.3"
3
+ 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.1.1
4
+ version: 0.2.3
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-02-02 00:00:00.000000000 Z
11
+ date: 2020-11-12 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, a la
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
@@ -63,7 +64,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
63
64
  version: '0'
64
65
  requirements: []
65
66
  rubygems_version: 3.1.2
66
- signing_key:
67
+ signing_key:
67
68
  specification_version: 4
68
69
  summary: Extract RSpecs from Markdown
69
70
  test_files: []