docspec 0.0.3 → 0.0.4

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: ad249396cfc15361e8f19e3932b5c75ab565b5042ce3431204daf0a39d4f0a14
4
- data.tar.gz: a0772e8fd3692466756215f0786709fae4c0772248d1715a0503810a7e0af719
3
+ metadata.gz: 3084cbb5bf7c1a0d99ec73d0e3a505b0690d44f28a5ec8e8cdcd8b74ab325bdf
4
+ data.tar.gz: 4f3dc9b69fb2ed4a0a32d52f4aeb2e25f3245c0734fa78362fd0f97279369884
5
5
  SHA512:
6
- metadata.gz: 4985a1fcb629b102bd560fc4a33bb24d18b0ef91368619cfbcc1f053ac2ac3fffa15426e81f1826de87bebeb5233ad4d416551f8eaad9c52f2513cd273d0b04f
7
- data.tar.gz: cae6a31e132105853a6015d53d39a3d0ad13bbd99b94fd03e10bedada81e635d7409a2495940d98d9447ff51e9f8249de719e5bd8dfa1aec8a1c4af8dd2a52cc
6
+ metadata.gz: ce61cf2c337223fcbaff45694209c9259922f3148f06dac163e647a8ac559fb597dc48096cc9c6c6f789491aca35087cf676b37af1d7f317e87720d2c761296a
7
+ data.tar.gz: 1044d67ab45478ca2e3bbe632bbc220e475a99b1269278f9364c2e4acd4183b59cd7a53e87fdfc86bdd1b6df94eae522b72e6fa96d54e8d052096d472dbc602a
data/README.md CHANGED
@@ -26,8 +26,8 @@ test.
26
26
  Code snippets should be enclosed in a `ruby` or `shell` code fence:
27
27
 
28
28
  ```ruby
29
- code_to_test = 'here'
30
- #=> expected output
29
+ puts "code to test (should output something)"
30
+ #=> code to test (should output something)
31
31
  ```
32
32
 
33
33
  This document itself serves as the test suite for this gem, so you can take a
@@ -75,8 +75,8 @@ string of that exception:
75
75
 
76
76
  ```ruby
77
77
  # Exceptions are captured
78
- raise ArgumentError, "Testing error raising"
79
- #=> #<ArgumentError: Testing error raising>
78
+ puts "hello".camel_case
79
+ #=> #<NoMethodError: undefined method `camel_case' for "hello":String>
80
80
  ```
81
81
 
82
82
  Your code and expected output can contain multiple lines of code:
@@ -103,6 +103,13 @@ puts 2 - 3
103
103
  #=> -1
104
104
  ```
105
105
 
106
+ or have the expected output in the same line as its code:
107
+
108
+ ```ruby
109
+ puts 2 * 3 #=> 6
110
+ puts 'works' #=> works
111
+ ```
112
+
106
113
  The first line of the example may contain specially formatted flags. Flags
107
114
  are always formatted like this: `[:flag_name]`.
108
115
 
@@ -128,27 +135,29 @@ this will not be executed
128
135
 
129
136
  Sometimes it is useful to build the example over several different code
130
137
  blocks. To help achieve this, docspec will treat any example that does not
131
- output anything as a code that needs to be executed before all subsequent
132
- examples:
138
+ expect any output (no `#=> markers`) as a code that needs to be executed
139
+ before all subsequent examples:
133
140
 
134
141
  ```ruby
135
- # Define a function for later use
142
+ # Define functions or variables for later use
136
143
  def create_caption(text)
137
144
  [text.upcase, ("=" * text.length)].join "\n"
138
145
  end
146
+
147
+ message = 'tada!'
139
148
  ```
140
149
 
141
150
  All the examples below this line, will have the above function available:
142
151
 
143
152
  ```ruby
144
- # Use a previously defined function
145
- puts create_caption "tada!"
153
+ # Use a previously defined function or variable
154
+ puts create_caption message
146
155
  #=> TADA!
147
156
  #=> =====
148
157
  ```
149
158
 
150
159
 
151
- Finally, examples marked with a `shell` code fence will be executed by the
160
+ Examples marked with a `shell` code fence will be executed by the
152
161
  shell, and not by ruby:
153
162
 
154
163
  ```shell
@@ -157,4 +166,16 @@ echo hello world
157
166
  #=> hello world
158
167
  ```
159
168
 
169
+ and they also support chaining of examples:
170
+
171
+ ```shell
172
+ # Prepend shell example to all subsequent shell examples
173
+ # (since this example does not define an expected output)
174
+ SOME_ENV_VAR=yes
175
+ ```
176
+
177
+ ```shell
178
+ echo $SOME_ENV_VAR
179
+ #=> yes
180
+ ```
160
181
 
data/bin/docspec CHANGED
@@ -1,5 +1,8 @@
1
1
  #!/usr/bin/env ruby
2
2
  require 'simplecov'
3
+ SimpleCov.configure do
4
+ command_name 'Docspec'
5
+ end
3
6
  SimpleCov.start
4
7
 
5
8
  require 'docspec'
@@ -14,10 +17,10 @@ document.test
14
17
  say ''
15
18
 
16
19
  if document.success?
17
- say "!txtgrn!#{document.total_examples} tests, #{document.failed_examples} failed\n"
20
+ say "!txtgrn!#{document.examples.count} tests, #{document.failed_examples.count} failed\n"
18
21
  exit 0
19
22
  else
20
- say "!txtred!#{document.total_examples} tests, #{document.failed_examples} failed\n"
23
+ say "!txtred!#{document.examples.count} tests, #{document.failed_examples.count} failed\n"
21
24
  exit 1
22
25
  end
23
26
 
@@ -15,13 +15,23 @@ module Docspec
15
15
  @examples ||= examples!
16
16
  end
17
17
 
18
+ def before
19
+ @before ||= {}
20
+ end
21
+
18
22
  protected
19
23
 
20
24
  def examples!
21
25
  result = []
22
26
  markdown.scan(/```(ruby|shell)\s*\n(.*?)```/m) do |type, code|
23
- example = Example.new(code, type)
24
- result << example unless example.skip?
27
+ example = Example.new(type: type, code: code, before: before[type])
28
+
29
+ next if example.skip?
30
+
31
+ before[type] ||= []
32
+ before[type] << example.code if example.empty?
33
+
34
+ result << example
25
35
  end
26
36
  result
27
37
  end
@@ -3,29 +3,36 @@ require 'diffy'
3
3
  module Docspec
4
4
  class Example
5
5
  include OutputCapturer
6
- attr_reader :code, :type, :full_code
6
+ attr_reader :code, :type, :before
7
7
 
8
- def initialize(code, type)
9
- @code, @type = code, type
10
- @full_code = @code
8
+ def initialize(type:, code:, before: nil)
9
+ @code, @type, @before = code, type, before
11
10
  end
12
11
 
13
12
  def actual
14
13
  @actual ||= actual!
15
14
  end
16
15
 
16
+ def consider_failed?
17
+ failed? and !ignore_failure?
18
+ end
19
+
17
20
  def diff
18
21
  @diff ||= Diffy::Diff.new("#{expected}\n", "#{actual}\n", context: 2).to_s :color
19
22
  end
20
23
 
21
24
  def empty?
22
- actual.empty?
25
+ expected.empty?
23
26
  end
24
27
 
25
28
  def expected
26
29
  @expected ||= code.scan(/#=>\s*(.*)/).map { |match| match.first.strip }.join "\n"
27
30
  end
28
31
 
32
+ def failed?
33
+ !success?
34
+ end
35
+
29
36
  def first_line
30
37
  @first_line ||= code.split("\n").first
31
38
  end
@@ -34,6 +41,10 @@ module Docspec
34
41
  @flags ||= first_line.scan(/\[:(.+?)\]/).map { |f| f.first.to_sym }
35
42
  end
36
43
 
44
+ def full_code
45
+ @full_code ||= full_code!
46
+ end
47
+
37
48
  def ignore_failure?
38
49
  flags.include? :ignore_failure
39
50
  end
@@ -42,12 +53,6 @@ module Docspec
42
53
  @label ||= label!
43
54
  end
44
55
 
45
- def prepend(codes)
46
- codes = [codes] unless codes.is_a? Array
47
- codes = codes.join "\n\n"
48
- @full_code = "#{codes}\n#{@full_code}"
49
- end
50
-
51
56
  def skip?
52
57
  flags.include? :skip
53
58
  end
@@ -64,11 +69,16 @@ module Docspec
64
69
  when 'ruby'
65
70
  eval full_code
66
71
  when 'shell'
67
- puts `#{code}`
72
+ puts `#{full_code}`
68
73
  end
69
74
  end.strip
70
75
  end
71
76
 
77
+ def full_code!
78
+ return code unless before
79
+ [before.join("\n\n"), code].join "\n"
80
+ end
81
+
72
82
  def label!
73
83
  first_line.gsub(/^#\s*/, '').strip
74
84
  end
@@ -3,34 +3,17 @@ require 'colsole'
3
3
  module Docspec
4
4
  module Testable
5
5
  include Colsole
6
- attr_reader :failed_examples, :total_examples
7
-
8
- def before_codes
9
- @before_codes ||= []
10
- end
11
6
 
12
7
  def success?
13
- failed_examples == 0
8
+ failed_examples.count == 0
14
9
  end
15
10
 
16
- def each_example
17
- reset_counters
18
-
19
- examples.each do |example|
20
- if example.empty?
21
- before_codes << example.code
22
- else
23
- @total_examples += 1
24
- example.prepend before_codes
25
- @failed_examples += 1 unless example.success? or example.ignore_failure?
26
- end
27
-
28
- yield example
29
- end
11
+ def failed_examples
12
+ @failed_examples ||= examples.select(&:consider_failed?)
30
13
  end
31
14
 
32
15
  def test
33
- each_example do |example|
16
+ examples.each do |example|
34
17
  if example.empty?
35
18
  say "!txtpur!void :!txtrst! #{example.label}"
36
19
  elsif example.success?
@@ -44,14 +27,6 @@ module Docspec
44
27
  end
45
28
  end
46
29
 
47
- protected
48
-
49
- def reset_counters
50
- @before_codes = nil
51
- @failed_examples = 0
52
- @total_examples = 0
53
- end
54
-
55
30
  end
56
31
  end
57
32
 
@@ -1,3 +1,3 @@
1
1
  module Docspec
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: docspec
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Danny Ben Shitrit
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-10-13 00:00:00.000000000 Z
11
+ date: 2019-10-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: colsole