doc_rspec 0.2.2 → 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: 46bc118ccd32d52dd764381cf3d8c6273c31c448199fbd6776e78f4ece0b8504
4
- data.tar.gz: 606e445893abbc97ad32e506ce45ce23d49554a19d7e5342de84630ebd99467f
3
+ metadata.gz: 59e8d3ee3246fe4e1840227fee649981f2f57fb6be5ef1b9c02e2fb9a2ebb9ab
4
+ data.tar.gz: e470857001bff3657ae5ab05c269d00369f89111e292d5678de760dd53771265
5
5
  SHA512:
6
- metadata.gz: 8082a05554da0613a0e6566f8bbc60c515652037b95c1d273dd09a8955dfb625b7471af38a91a759bbc685c9bc0be136916deb4735bbb0e4231bcd987ac36378
7
- data.tar.gz: 877dda2bc5bd01c8acaf40a9ed521b3a2d72ba23a98e428e963b887c65a70119d26437fd4415686e82430f899af81662df5cc3275c6290bb1084fc79130166c7
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
@@ -15,8 +17,16 @@ class DocRSpec
15
17
  SHORT_EQUALS = %r{\s \=\=\> \s}x
16
18
  SHORT_MATCHES = %r{\s \~\> \s}x
17
19
  SHORT_PREDICATE = %r{\s is\! \s}x
20
+ SHORT_ANTIPREDICATE = %r{\s not\! \s}x
18
21
  START_EXAMPLE = %r{\A \s* \# \s{4,} \# \s example: \s (.*)}x
19
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
+
20
30
  attr_reader :ast, :lines, :state
21
31
 
22
32
  def parse
@@ -52,26 +62,21 @@ class DocRSpec
52
62
  code = match[1]
53
63
  return if code.empty?
54
64
 
55
- case code.split(SHORT_EQUALS)
56
- in [lhs, rhs]
57
- ast.last.add_example_line("expect(#{lhs}).to eq(#{rhs})")
58
- else
59
- case code.split(SHORT_MATCHES)
60
- in [lhs, rhs]
61
- ast.last.add_example_line("expect(#{lhs}).to match(#{rhs})")
62
- else
63
- 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)
64
69
  in [lhs, rhs]
65
- ast.last.add_example_line("expect(#{lhs}).to be_#{rhs}")
70
+ "expect(#{lhs}).#{to_or_not} #{rhs_template.sub("%%%", rhs)}"
66
71
  else
67
- ast.last.add_example_line(code)
72
+ nil
68
73
  end
69
74
  end
70
- end
75
+ ast.last.add_example_line(compiled)
71
76
  end
72
77
 
73
78
  def parse_line(line, lnb)
74
- p(state:, lnb:, line:) if ENV["DEBUG"]
79
+ p(state:, lnb:, line:) if debugging?
75
80
  case state
76
81
  when :outside
77
82
  parse_line_outside(line, lnb)
@@ -90,6 +95,10 @@ class DocRSpec
90
95
  add_example(lnb, Regexp.last_match)
91
96
  when EXAMPLE_LINE
92
97
  parse_example_line(Regexp.last_match)
98
+ when COMMENT_LINE
99
+ nil
100
+ else
101
+ @state = :outside
93
102
  end
94
103
  end
95
104
 
@@ -2,7 +2,7 @@
2
2
 
3
3
  class DocRSpec
4
4
  module Version
5
- VERSION = '0.2.2'
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'
@@ -85,6 +86,20 @@ RSpec.configure { it.extend DocRSpec::RSpecExampleGroup }
85
86
  # require 'ostruct'
86
87
  # OpenStruct.new(ok?: true) is! ok
87
88
  #
89
+ # === +not!+ for not be_
90
+ #
91
+ # # example: is! shorthand for be
92
+ #
93
+ # require 'ostruct'
94
+ # OpenStruct.new(ok?: false) not! ok
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
+ #
88
103
  class DocRSpec
89
104
 
90
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.2
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