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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 285211d9493c575fb886a6e587e6163440017936c9171a015f323ee5b5e75f12
4
- data.tar.gz: 8481fd32a94801a88e995c82acee35cbf82f703f59212194e5b8da07d88c1607
3
+ metadata.gz: 50f8d84c839b9b2c42002526d2a71b7ba77c3e4593e3bd11707b15437b8ff8fd
4
+ data.tar.gz: 03b17f9c88a24ac31c405954aadd080b65b8c288ccb3f8b92d25b3ec45d03017
5
5
  SHA512:
6
- metadata.gz: acee792d5459ed127e6508feb898c3dc37f7cd0a705c3076ac0adbe457470de40b13282a465527a58275cdc7c21785a4e74cd8fd9d692bb3c24d1e026c6e0def
7
- data.tar.gz: c5e0e51cef54cf5080914695a1f6292bdd50bfbf113deb10d439e5b093d5265715d02f0172df6f4d551161ea002e5a9416ea00f8b51d30c607ee4f1f1b1a817d
6
+ metadata.gz: ad12b1aa05ab33e0fa3d506375c7f5de01edd4da59c3423d995af02b642ce56b6d08820fa0ea602998b6f895e4be8e2b0b2b5cbb4774f46cec4a1e19e7fb26ed
7
+ data.tar.gz: ed148aa6718aaa0b9c624e9af55b0029a115a6cb48e710d55acb1029ebce895001e5ff77ae9af8c0ee3cdac39cb4972a994a94a2490040dcf1322dce59fab18f
data/README.md CHANGED
@@ -2,8 +2,11 @@ Docspec
2
2
  ==================================================
3
3
 
4
4
  [![Gem Version](https://badge.fury.io/rb/docspec.svg)](https://badge.fury.io/rb/docspec)
5
+ [![Build Status](https://travis-ci.com/DannyBen/docspec.svg?branch=master)](https://travis-ci.com/DannyBen/docspec)
6
+ [![Maintainability](https://api.codeclimate.com/v1/badges/e0c15c1f33fa4aa45f70/maintainability)](https://codeclimate.com/github/DannyBen/docspec/maintainability)
5
7
 
6
- Inline tests in Markdown documents.
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 should fail
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
@@ -13,10 +13,6 @@ success = tester.execute
13
13
 
14
14
  say ''
15
15
 
16
- if tester.expected_failures != 0
17
- say "!txtblu!Expected failures: #{tester.expected_failures}"
18
- end
19
-
20
16
  if success
21
17
  say "!txtgrn!#{tester.total} tests, #{tester.errors} failed\n"
22
18
  exit 0
@@ -18,7 +18,7 @@ module Docspec
18
18
 
19
19
  def examples!
20
20
  result = []
21
- markdown.scan(/```(ruby|shell)\n(.*?)```/m) do |type, code|
21
+ markdown.scan(/```(ruby|shell)\s*\n(.*?)```/m) do |type, code|
22
22
  result << Example.new(code, type)
23
23
  end
24
24
  result
@@ -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 label
14
- @label ||= label!
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 actual
22
- @actual ||= actual!
29
+ def first_line
30
+ @first_line ||= code.split("\n").first
23
31
  end
24
32
 
25
- def passing?
26
- actual == expected
33
+ def flags
34
+ @flags ||= first_line.scan(/\[:(.+?)\]/).map { |f| f.first.to_sym }
27
35
  end
28
36
 
29
- def diff
30
- Diffy::Diff.new(expected, actual, context: 2).to_s :color
37
+ def ignore_failure?
38
+ flags.include? :ignore_failure
31
39
  end
32
40
 
33
- protected
41
+ def label
42
+ @label ||= label!
43
+ end
34
44
 
35
- def label!
36
- first_line = code.split("\n").first
37
- first_line.gsub(/^#\s*/, '').strip
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
- instance_eval(code)
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
@@ -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 total
15
- @total ||= document.examples.count
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.passing?
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 == expected_failures
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
@@ -1,3 +1,3 @@
1
1
  module Docspec
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
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.1
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: