doc_rspec 0.2.3 → 0.2.4

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b3ea3a68b2a618eeac53b5988fcf7bef4c767b43899fa158ea5171af5e2b9866
4
- data.tar.gz: a95753a22dd8b00590b733b9774823a722b883dff39b7e79f60a018386278905
3
+ metadata.gz: 59e8d3ee3246fe4e1840227fee649981f2f57fb6be5ef1b9c02e2fb9a2ebb9ab
4
+ data.tar.gz: e470857001bff3657ae5ab05c269d00369f89111e292d5678de760dd53771265
5
5
  SHA512:
6
- metadata.gz: 15dfea09b23cf93667f6b7b2845a60b98e8346ae64b539040e4d17c4562c000035dc391e70258e2d39847e4f591c08cd724dcedd3b053aee38d49d6b37f9cb1d
7
- data.tar.gz: 51d041f6637b3aa8069511f08ff014fbf610e07c6678604c7e4b5324e6c63daf5d5161a5ffe54b00a1b9391c0123cb958df7cb61c320674f61cc3b0b3eb272e0
6
+ metadata.gz: 410226d89424038b7c58c1a971e20942af897472b6a9d4825e1b08aebe8e3a0f876bcec58c9205c0c3720006e63acd9a0654aaea8ba7693f84bbae3bacafae48
7
+ data.tar.gz: 9f73f46d97b292a6e7e8c0c198186b29021adeb50bcbfcb7c8e7901d8f7640ac360f1789c073c34e4f9e36d5acaafcc755579c5e7df3495ea0625f9be5aa63f1
@@ -4,6 +4,7 @@ class DocRSpec
4
4
  class Compiler
5
5
  class IncorrectSpec < RuntimeError; end
6
6
 
7
+ include DocRSpec::Debugging
7
8
  attr_reader :example_group, :path, :spec_definitions
8
9
 
9
10
  def compile
@@ -30,13 +31,22 @@ class DocRSpec
30
31
  def compile_example(example_spec)
31
32
  # See comment above for an explanation why we capture
32
33
  # this method as a function into a closure
33
- example_group.it example_spec.it_name(path) do
34
- example = self
35
- code = example_spec
36
- .lines
37
- .join("\n")
34
+ code = example_spec
35
+ .lines
36
+ .join("\n")
37
+ return if code.empty?
38
+
39
+ example_name = example_spec.it_name(path)
40
+ if debug_level > 1
41
+ puts "Example: #{example_name}"
42
+ puts example_spec.lines.map { "> #{it}" }
43
+ puts "=" * 72
44
+ end
45
+ return if debug_level > 2
38
46
 
39
- eval(code) unless code.empty?
47
+ example_group.it example_name do
48
+ example = self
49
+ eval(code)
40
50
  end
41
51
  end
42
52
 
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ class DocRSpec
4
+ module Debugging
5
+
6
+ DEBUG_ENV_VAR_NAME = 'DEBUG_DOC_RSPEC'
7
+ def debug_level
8
+ @__debug_level__ ||=
9
+ case ENV[DEBUG_ENV_VAR_NAME]
10
+ in nil
11
+ 0
12
+ in value
13
+ value.to_i.succ
14
+ end
15
+ end
16
+
17
+ def debugging? = debug_level.positive?
18
+
19
+ end
20
+ end
21
+ # SPDX-License-Identifier: AGPL-3.0-or-later
@@ -1,13 +1,15 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative 'context'
4
+ require_relative '../enumerable'
4
5
 
5
6
  class DocRSpec
6
7
  class Parser
7
8
  class InternalError < RuntimeError; end
8
9
  class SyntaxError < RuntimeError; end
10
+ include DocRSpec::Debugging
9
11
 
10
-
12
+ COMMENT_LINE = %r{\A \s* \#}x
11
13
  CONTEXT_DEFINITION = %r{\A \s* \# \s ={1,7} \s (.*)}x
12
14
 
13
15
  EXAMPLE_LINE = %r{\A \s* \# \s{4,} (.*)}x
@@ -18,6 +20,13 @@ class DocRSpec
18
20
  SHORT_ANTIPREDICATE = %r{\s not\! \s}x
19
21
  START_EXAMPLE = %r{\A \s* \# \s{4,} \# \s example: \s (.*)}x
20
22
 
23
+ SHORTCUTS = [
24
+ [ SHORT_EQUALS, "to", "eq(%%%)" ],
25
+ [ SHORT_MATCHES, "to", "match(%%%)" ],
26
+ [ SHORT_PREDICATE, "to", "be_%%%" ],
27
+ [ SHORT_ANTIPREDICATE, "not_to", "be_%%%" ],
28
+ ]
29
+
21
30
  attr_reader :ast, :lines, :state
22
31
 
23
32
  def parse
@@ -53,31 +62,21 @@ class DocRSpec
53
62
  code = match[1]
54
63
  return if code.empty?
55
64
 
56
- case code.split(SHORT_EQUALS)
57
- in [lhs, rhs]
58
- ast.last.add_example_line("expect(#{lhs}).to eq(#{rhs})")
59
- else
60
- case code.split(SHORT_MATCHES)
61
- in [lhs, rhs]
62
- ast.last.add_example_line("expect(#{lhs}).to match(#{rhs})")
63
- else
64
- case code.split(SHORT_PREDICATE)
65
+ compiled =
66
+ SHORTCUTS.find_value(default: code) do |shtct_def|
67
+ shtct_def => [rgx, to_or_not, rhs_template]
68
+ case code.split(rgx)
65
69
  in [lhs, rhs]
66
- ast.last.add_example_line("expect(#{lhs}).to be_#{rhs}")
70
+ "expect(#{lhs}).#{to_or_not} #{rhs_template.sub("%%%", rhs)}"
67
71
  else
68
- case code.split(SHORT_ANTIPREDICATE)
69
- in [lhs, rhs]
70
- ast.last.add_example_line("expect(#{lhs}).not_to be_#{rhs}")
71
- else
72
- ast.last.add_example_line(code)
73
- end
72
+ nil
74
73
  end
75
74
  end
76
- end
75
+ ast.last.add_example_line(compiled)
77
76
  end
78
77
 
79
78
  def parse_line(line, lnb)
80
- p(state:, lnb:, line:) if ENV["DEBUG"]
79
+ p(state:, lnb:, line:) if debugging?
81
80
  case state
82
81
  when :outside
83
82
  parse_line_outside(line, lnb)
@@ -96,6 +95,10 @@ class DocRSpec
96
95
  add_example(lnb, Regexp.last_match)
97
96
  when EXAMPLE_LINE
98
97
  parse_example_line(Regexp.last_match)
98
+ when COMMENT_LINE
99
+ nil
100
+ else
101
+ @state = :outside
99
102
  end
100
103
  end
101
104
 
@@ -2,7 +2,7 @@
2
2
 
3
3
  class DocRSpec
4
4
  module Version
5
- VERSION = '0.2.3'
5
+ VERSION = '0.2.4'
6
6
  end
7
7
  end
8
8
  # SPDX-License-Identifier: AGPL-3.0-or-later
data/lib/doc_rspec.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'rspec'
4
+ require_relative 'doc_rspec/debugging'
4
5
  require_relative 'doc_rspec/compiler'
5
6
  require_relative 'doc_rspec/rspec_example_group'
6
7
  require_relative 'doc_rspec/parser'
@@ -92,6 +93,13 @@ RSpec.configure { it.extend DocRSpec::RSpecExampleGroup }
92
93
  # require 'ostruct'
93
94
  # OpenStruct.new(ok?: false) not! ok
94
95
  #
96
+ # === End of Example
97
+ #
98
+ # the code does not continue after the rdoc comment
99
+
100
+ # # not a problem
101
+ # raise "This should never happen"
102
+ #
95
103
  class DocRSpec
96
104
 
97
105
  attr_reader :example_group, :lines, :path
data/lib/enumerable.rb ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Enumerable
4
+ def find_value(default: nil, &blk)
5
+ each do |ele|
6
+ result = blk.(ele)
7
+ return result if result
8
+ end
9
+ default
10
+ end
11
+ end
12
+ # SPDX-License-Identifier: AGPL-3.0-or-later
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: doc_rspec
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Dober
@@ -49,9 +49,11 @@ files:
49
49
  - lib/doc_rspec/compiler.rb
50
50
  - lib/doc_rspec/context.rb
51
51
  - lib/doc_rspec/context/example.rb
52
+ - lib/doc_rspec/debugging.rb
52
53
  - lib/doc_rspec/parser.rb
53
54
  - lib/doc_rspec/rspec_example_group.rb
54
55
  - lib/doc_rspec/version.rb
56
+ - lib/enumerable.rb
55
57
  homepage: https://codeberg.org/lab419/doc_rspec
56
58
  licenses:
57
59
  - AGPL-3.0-or-later