docspec 0.0.1 → 0.0.2
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 +4 -4
- data/README.md +26 -2
- data/bin/docspec +0 -4
- data/lib/docspec/document.rb +1 -1
- data/lib/docspec/example.rb +36 -15
- data/lib/docspec/tester.rb +16 -10
- data/lib/docspec/version.rb +1 -1
- metadata +15 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 50f8d84c839b9b2c42002526d2a71b7ba77c3e4593e3bd11707b15437b8ff8fd
|
4
|
+
data.tar.gz: 03b17f9c88a24ac31c405954aadd080b65b8c288ccb3f8b92d25b3ec45d03017
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ad12b1aa05ab33e0fa3d506375c7f5de01edd4da59c3423d995af02b642ce56b6d08820fa0ea602998b6f895e4be8e2b0b2b5cbb4774f46cec4a1e19e7fb26ed
|
7
|
+
data.tar.gz: ed148aa6718aaa0b9c624e9af55b0029a115a6cb48e710d55acb1029ebce895001e5ff77ae9af8c0ee3cdac39cb4972a994a94a2490040dcf1322dce59fab18f
|
data/README.md
CHANGED
@@ -2,8 +2,11 @@ Docspec
|
|
2
2
|
==================================================
|
3
3
|
|
4
4
|
[](https://badge.fury.io/rb/docspec)
|
5
|
+
[](https://travis-ci.com/DannyBen/docspec)
|
6
|
+
[](https://codeclimate.com/github/DannyBen/docspec/maintainability)
|
5
7
|
|
6
|
-
|
8
|
+
Docspec lets you reuse your Markdown documents as the unit tests for your
|
9
|
+
Ruby library.
|
7
10
|
|
8
11
|
---
|
9
12
|
|
@@ -32,6 +35,9 @@ Rules
|
|
32
35
|
expected output.
|
33
36
|
- If a piece of code raises an error, the captured output will be the
|
34
37
|
`#inspect` string of that exception.
|
38
|
+
- If the first line of a code block includes the string `[:ignore_failure]`,
|
39
|
+
the example will not be considered an error if it fails.
|
40
|
+
|
35
41
|
|
36
42
|
To test the `README.md` in the current folder, just run:
|
37
43
|
|
@@ -82,11 +88,29 @@ puts 2 - 3
|
|
82
88
|
```
|
83
89
|
|
84
90
|
```ruby
|
85
|
-
# This example
|
91
|
+
# This example may fail [:ignore_failure]
|
92
|
+
# Due to the :ignore_failure flag, it will show the failure diff, but will
|
93
|
+
# not be considered a failure in the exit status.
|
86
94
|
puts 'hello world'.upcase
|
87
95
|
#=> hello world
|
88
96
|
```
|
89
97
|
|
98
|
+
```ruby
|
99
|
+
# Code that does not generate any output will be executed before each
|
100
|
+
# of the subsequent examples.
|
101
|
+
def create_caption(text)
|
102
|
+
[text.upcase, ("=" * text.length)].join "\n"
|
103
|
+
end
|
104
|
+
```
|
105
|
+
|
106
|
+
```ruby
|
107
|
+
# Example that builds upon code that was defined earlier
|
108
|
+
puts create_caption "tada!"
|
109
|
+
#=> TADA!
|
110
|
+
#=> =====
|
111
|
+
```
|
112
|
+
|
113
|
+
|
90
114
|
### Shell
|
91
115
|
|
92
116
|
```shell
|
data/bin/docspec
CHANGED
data/lib/docspec/document.rb
CHANGED
data/lib/docspec/example.rb
CHANGED
@@ -3,50 +3,71 @@ require 'diffy'
|
|
3
3
|
module Docspec
|
4
4
|
class Example
|
5
5
|
include OutputCapturer
|
6
|
-
|
7
|
-
attr_reader :code, :type
|
6
|
+
attr_reader :code, :type, :full_code
|
8
7
|
|
9
8
|
def initialize(code, type)
|
10
9
|
@code, @type = code, type
|
10
|
+
@full_code = @code
|
11
11
|
end
|
12
12
|
|
13
|
-
def
|
14
|
-
@
|
13
|
+
def actual
|
14
|
+
@actual ||= actual!
|
15
|
+
end
|
16
|
+
|
17
|
+
def diff
|
18
|
+
@diff ||= Diffy::Diff.new("#{expected}\n", "#{actual}\n", context: 2).to_s :color
|
19
|
+
end
|
20
|
+
|
21
|
+
def empty?
|
22
|
+
actual.empty?
|
15
23
|
end
|
16
24
|
|
17
25
|
def expected
|
18
26
|
@expected ||= code.scan(/#=>\s*(.*)/).map { |match| match.first.strip }.join "\n"
|
19
27
|
end
|
20
28
|
|
21
|
-
def
|
22
|
-
@
|
29
|
+
def first_line
|
30
|
+
@first_line ||= code.split("\n").first
|
23
31
|
end
|
24
32
|
|
25
|
-
def
|
26
|
-
|
33
|
+
def flags
|
34
|
+
@flags ||= first_line.scan(/\[:(.+?)\]/).map { |f| f.first.to_sym }
|
27
35
|
end
|
28
36
|
|
29
|
-
def
|
30
|
-
|
37
|
+
def ignore_failure?
|
38
|
+
flags.include? :ignore_failure
|
31
39
|
end
|
32
40
|
|
33
|
-
|
41
|
+
def label
|
42
|
+
@label ||= label!
|
43
|
+
end
|
34
44
|
|
35
|
-
def
|
36
|
-
|
37
|
-
|
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
|
+
def success?
|
52
|
+
actual == expected
|
38
53
|
end
|
39
54
|
|
55
|
+
protected
|
56
|
+
|
40
57
|
def actual!
|
41
58
|
capture_output do
|
42
59
|
case type
|
43
60
|
when 'ruby'
|
44
|
-
|
61
|
+
eval full_code
|
45
62
|
when 'shell'
|
46
63
|
puts `#{code}`
|
47
64
|
end
|
48
65
|
end.strip
|
49
66
|
end
|
50
67
|
|
68
|
+
def label!
|
69
|
+
first_line.gsub(/^#\s*/, '').strip
|
70
|
+
end
|
71
|
+
|
51
72
|
end
|
52
73
|
end
|
data/lib/docspec/tester.rb
CHANGED
@@ -3,24 +3,34 @@ require 'colsole'
|
|
3
3
|
module Docspec
|
4
4
|
class Tester
|
5
5
|
include Colsole
|
6
|
-
attr_reader :document, :errors
|
6
|
+
attr_reader :document, :errors, :total
|
7
7
|
|
8
8
|
def initialize(document)
|
9
9
|
document = Document.new document unless document.is_a? Document
|
10
10
|
@document = document
|
11
11
|
@errors = 0
|
12
|
+
@total = 0
|
12
13
|
end
|
13
14
|
|
14
|
-
def
|
15
|
-
@
|
15
|
+
def before_codes
|
16
|
+
@before_codes ||= []
|
16
17
|
end
|
17
18
|
|
18
19
|
def execute
|
19
20
|
document.examples.each do |example|
|
20
|
-
if example.
|
21
|
+
if example.empty?
|
22
|
+
before_codes << example.code
|
23
|
+
next
|
24
|
+
end
|
25
|
+
|
26
|
+
@total += 1
|
27
|
+
|
28
|
+
example.prepend before_codes
|
29
|
+
|
30
|
+
if example.success?
|
21
31
|
say "!txtgrn!PASS: #{example.label}"
|
22
32
|
else
|
23
|
-
@errors += 1
|
33
|
+
@errors += 1 unless example.ignore_failure?
|
24
34
|
say "!txtred!FAIL: #{example.label}"
|
25
35
|
say "---"
|
26
36
|
puts example.diff
|
@@ -28,11 +38,7 @@ module Docspec
|
|
28
38
|
end
|
29
39
|
end
|
30
40
|
|
31
|
-
errors ==
|
32
|
-
end
|
33
|
-
|
34
|
-
def expected_failures
|
35
|
-
ENV['DOCSPEC_EXPECTED_FAILURES']&.to_i || 0
|
41
|
+
errors == 0
|
36
42
|
end
|
37
43
|
end
|
38
44
|
end
|
data/lib/docspec/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: docspec
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Danny Ben Shitrit
|
@@ -38,6 +38,20 @@ dependencies:
|
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '3.3'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: simplecov
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0.17'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0.17'
|
41
55
|
description: Make your READMEs testable
|
42
56
|
email: db@dannyben.com
|
43
57
|
executables:
|