doc_rspec 0.2.3 → 0.2.5

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: 8e2ea270de14b4a37fd648bde29d59a4f0a2244159d22a06e798c2387faa96f8
4
+ data.tar.gz: b3588a664c869b921e789c778be7508f8189628281d2c65796be3c3b31c1d060
5
5
  SHA512:
6
- metadata.gz: 15dfea09b23cf93667f6b7b2845a60b98e8346ae64b539040e4d17c4562c000035dc391e70258e2d39847e4f591c08cd724dcedd3b053aee38d49d6b37f9cb1d
7
- data.tar.gz: 51d041f6637b3aa8069511f08ff014fbf610e07c6678604c7e4b5324e6c63daf5d5161a5ffe54b00a1b9391c0123cb958df7cb61c320674f61cc3b0b3eb272e0
6
+ metadata.gz: 90a3129c0e172feaaae26d3d15cee20df6199d43d9d76676eab7b579038fbbd97c5f129fd4e72ac3ece19be2a4a01b260b60e255d765aa5f5b60478808ef2bfa
7
+ data.tar.gz: 31d6850d1534204007cf3953800c9d5a9f1cdf4e2f7e4450e93ca4dbfa44af7ea76fd8bf2e1fc03697f50a248c21eefe1f5d3d2693f76c30b9f564d18cbd59cc
@@ -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,23 +1,32 @@
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
14
-
16
+
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
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,23 @@ 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
+ return if COMMENT_LINE === code
66
+
67
+ compiled =
68
+ SHORTCUTS.find_value(default: code) do |shtct_def|
69
+ shtct_def => [rgx, to_or_not, rhs_template]
70
+ case code.split(rgx)
65
71
  in [lhs, rhs]
66
- ast.last.add_example_line("expect(#{lhs}).to be_#{rhs}")
72
+ "expect(#{lhs}).#{to_or_not} #{rhs_template.sub("%%%", rhs)}"
67
73
  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
74
+ nil
74
75
  end
75
76
  end
76
- end
77
+ ast.last.add_example_line(compiled)
77
78
  end
78
79
 
79
80
  def parse_line(line, lnb)
80
- p(state:, lnb:, line:) if ENV["DEBUG"]
81
+ p(state:, lnb:, line:) if debugging?
81
82
  case state
82
83
  when :outside
83
84
  parse_line_outside(line, lnb)
@@ -96,6 +97,10 @@ class DocRSpec
96
97
  add_example(lnb, Regexp.last_match)
97
98
  when EXAMPLE_LINE
98
99
  parse_example_line(Regexp.last_match)
100
+ when COMMENT_LINE
101
+ nil
102
+ else
103
+ @state = :outside
99
104
  end
100
105
  end
101
106
 
@@ -2,7 +2,7 @@
2
2
 
3
3
  class DocRSpec
4
4
  module Version
5
- VERSION = '0.2.3'
5
+ VERSION = '0.2.5'
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,22 @@ RSpec.configure { it.extend DocRSpec::RSpecExampleGroup }
92
93
  # require 'ostruct'
93
94
  # OpenStruct.new(ok?: false) not! ok
94
95
  #
96
+ #
97
+ # === Comments inside examples
98
+ #
99
+ # # example: comments are removed
100
+ #
101
+ # # so this works
102
+ # expect(1).to eq(1)
103
+ # # and this ==> too
104
+ #
105
+ # === End of Example
106
+ #
107
+ # the code does not continue after the rdoc comment
108
+
109
+ # # not a problem
110
+ # raise "This should never happen"
111
+ #
95
112
  class DocRSpec
96
113
 
97
114
  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.5
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