rspec-illustrate 0.1.1 → 0.1.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.
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # Rspec::Illustrate [![Gem Version](https://badge.fury.io/rb/rspec-illustrate.svg)](http://badge.fury.io/rb/rspec-illustrate)
1
+ # RSpec::Illustrate [![Gem Version](https://badge.fury.io/rb/rspec-illustrate.svg)](http://badge.fury.io/rb/rspec-illustrate)
2
2
 
3
3
 
4
4
  This is an RSpec extension gem that allows you to define illustrative objects in
data/Rakefile CHANGED
@@ -7,4 +7,8 @@ RSpec::Core::RakeTask.new(:spec) do |t|
7
7
  t.rspec_opts = "--format RSpec::Formatters::IllustratedDocumentationFormatter"
8
8
  end
9
9
 
10
+ RSpec::Core::RakeTask.new(:html) do |t|
11
+ t.rspec_opts = "--format RSpec::Formatters::IllustratedHtmlFormatter --out ./doc/specs.html"
12
+ end
13
+
10
14
  task :default => :spec
@@ -24,6 +24,12 @@ module RSpec
24
24
  module Formatters
25
25
  include RSpec::Core::Formatters
26
26
 
27
+ # An extension to the {RSpec::Core::Formatters::DocumentationFormatter} that
28
+ # renders the illustrations after each example. A title/label for each
29
+ # illustration can be set by setting the label option (@see
30
+ # RSpec::Illustrate#illustrate).
31
+ # If you want to filter the illustrations based on the test result, you can use
32
+ # the options show_when_passed, show_when_failed and show_when_pending.
27
33
  class IllustratedDocumentationFormatter < DocumentationFormatter
28
34
  include RSpec::Formatters
29
35
 
@@ -37,21 +43,35 @@ module RSpec
37
43
  super(output)
38
44
  end
39
45
 
46
+ # @see RSpec::Core::Formatters::DocumentationFormatter#example_passed
40
47
  def example_passed(passed)
41
48
  super(passed)
42
49
  write_illustrations(passed, :show_when_passed, :illustration_passed)
43
50
  end
44
51
 
52
+ # @see RSpec::Core::Formatters::DocumentationFormatter#example_failed
45
53
  def example_failed(failure)
46
54
  super(failure)
47
55
  write_illustrations(failure, :show_when_failed, :illustration_failed)
48
56
  end
49
57
 
58
+ # @see RSpec::Core::Formatters::DocumentationFormatter#example_pending
50
59
  def example_pending(pending)
51
60
  super(pending)
52
61
  write_illustrations(pending, :show_when_pending, :illustration_pending)
53
62
  end
54
63
 
64
+ # @private
65
+ # Writes the filtered illustrations of an example to the output stream.
66
+ #
67
+ # @param notification [RSpec::Core::Notifications::ExampleNotification]
68
+ # The example notificiation that contains the
69
+ # illustrations.
70
+ # @param filter_key [Symbol]
71
+ # The option that each illustration should have a truthy
72
+ # value of if they are to be written
73
+ # @param color_type [Symbol]
74
+ # The symbol that corresponds to a configurable color
55
75
  def write_illustrations(notification, filter_key, color_type)
56
76
  illustrations = filter(illustrations_of(notification), filter_key)
57
77
  return if illustrations.empty?
@@ -59,14 +79,26 @@ module RSpec
59
79
  output.puts colored(indented(formatted(illustrations)), color_type)
60
80
  end
61
81
 
82
+ # @private
83
+ # @param text [String] The text to be colored
84
+ # @param color_type [Symbol]
85
+ # @return [String] The text wrapped in color codes.
62
86
  def colored(text, color_type)
63
87
  RSpec::Core::Formatters::ConsoleCodes.wrap(text, color_type)
64
88
  end
65
89
 
90
+ # @private
91
+ # @param text [String]
92
+ # @return [String] The text where each newline is properly indented.
66
93
  def indented(text)
67
94
  current_indentation << text.gsub(/\n/, "\n#{current_indentation}")
68
95
  end
69
96
 
97
+ # @private
98
+ # Convert the illustrations to a string using the configurable illustration
99
+ # formatter {RSpec::Configuration#illustration_formatter}.
100
+ # @param illustrations [Array<Hash>] The illustrations
101
+ # @return [String] a text concatenation of the illustrations
70
102
  def formatted(illustrations)
71
103
  formatter_proc = RSpec.configuration.illustration_formatter
72
104
 
@@ -0,0 +1,97 @@
1
+ RSpec::Support.require_rspec_core "formatters/html_formatter"
2
+ require 'rspec/formatters/illustration_formatter.rb'
3
+
4
+ RSpec.configure do |c|
5
+ c.add_setting(:illustration_html_formatter, :default =>
6
+ lambda {|illustration|
7
+ html = "<dd>"
8
+
9
+ if illustration.has_key?(:label) then
10
+ html << "<span>#{illustration[:label].to_s}</span>"
11
+ end
12
+
13
+ content = illustration[:content]
14
+ if content.respond_to?(:to_html) then
15
+ html << content.to_html
16
+ else
17
+ html << "<pre>#{content.to_s}</pre>"
18
+ end
19
+
20
+ return html << "</dd>"
21
+ })
22
+ end
23
+
24
+ module RSpec
25
+ module Formatters
26
+ include RSpec::Core::Formatters
27
+
28
+ # An extension to the {RSpec::Core::Formatters::HtmlFormatter} that
29
+ # renders the illustrations after each example. A title/label for each
30
+ # illustration can be set by setting the label option (@see
31
+ # RSpec::Illustrate#illustrate).
32
+ # If you want to filter the illustrations based on the test result, you can use
33
+ # the options show_when_passed, show_when_failed and show_when_pending.
34
+ class IllustratedHtmlFormatter < HtmlFormatter
35
+ include RSpec::Formatters
36
+
37
+ # This registers the notifications this formatter supports, and tells
38
+ # us that this was written against the RSpec 3.x formatter API.
39
+ RSpec::Core::Formatters.register self, :example_passed,
40
+ :example_failed,
41
+ :example_pending
42
+
43
+ def initialize(output)
44
+ super(output)
45
+ end
46
+
47
+ # @see RSpec::Core::Formatters::HtmlFormatter#example_passed
48
+ def example_passed(passed)
49
+ super(passed)
50
+ write_illustrations(passed, :show_when_passed)
51
+ end
52
+
53
+ # @see RSpec::Core::Formatters::HtmlFormatter#example_failed
54
+ def example_failed(failure)
55
+ super(failure)
56
+ write_illustrations(failure, :show_when_failed)
57
+ end
58
+
59
+ # @see RSpec::Core::Formatters::HtmlFormatter#example_pending
60
+ def example_pending(pending)
61
+ super(pending)
62
+ write_illustrations(pending, :show_when_pending)
63
+ end
64
+
65
+ # @private
66
+ # Writes the filtered illustrations of an example to the output stream.
67
+ #
68
+ # @param notification [RSpec::Core::Notifications::ExampleNotification]
69
+ # The example notificiation that contains the
70
+ # illustrations.
71
+ # @param filter_key [Symbol]
72
+ # The option that each illustration should have a truthy
73
+ # value of if they are to be written
74
+ def write_illustrations(notification, filter_key)
75
+ illustrations = filter(illustrations_of(notification), filter_key)
76
+ return if illustrations.empty?
77
+
78
+ output.puts formatted(illustrations)
79
+ end
80
+
81
+ # @private
82
+ # Convert the illustrations to a string using the configurable illustration
83
+ # html formatter {RSpec::Configuration#illustration_html_formatter}.
84
+ # @param illustrations [Array<Hash>] The illustrations
85
+ # @return [String] a html concatenation of the illustrations
86
+ def formatted(illustrations)
87
+ formatter_proc = RSpec.configuration.illustration_html_formatter
88
+
89
+ illustrations.collect{|illustration|
90
+ formatter_proc.call(illustration)
91
+ }.join("\n")
92
+ end
93
+
94
+ end
95
+ end
96
+ end
97
+
@@ -2,12 +2,22 @@
2
2
  module RSpec
3
3
  module Formatters
4
4
 
5
+ # @private
6
+ # @param notification [RSpec::Core::Notifications::ExampleNotification]
7
+ # The example notification to be formatted.
8
+ # @return [Array<Hash>] The list of illustrations in the example, where each
9
+ # illustration is represented by a { Hash } containing its properties.
5
10
  def illustrations_of(notification)
6
- notification.example.metadata[:illustrations]
11
+ notification.example.metadata[:illustrations] || []
7
12
  end
8
13
 
9
14
  module_function :illustrations_of
10
15
 
16
+ # @private
17
+ # @param illustrations [Array<Hash>] A list of illustrations ({ Hash })
18
+ # @param filter_key [Symbol] The property that each illustration must have.
19
+ # @return [Array<Hash>] The illustrations that have truthy property values for
20
+ # filter_key.
11
21
  def filter(illustrations, filter_key)
12
22
  illustrations.select{|illustration| illustration[filter_key]}
13
23
  end
@@ -2,8 +2,15 @@ require 'rspec/illustrate/version'
2
2
  require 'rspec/core'
3
3
 
4
4
  module RSpec
5
+ # Extension module to RSpec that allows you to define illustrations in the
6
+ # examples that will be forwarded to the output formatter.
5
7
  module Illustrate
6
8
 
9
+ # Stores an object in the surrounding example's metadata, which can be used
10
+ # by the output formatter as an illustration for the example.
11
+ # @param content The object that will act as an illustration.
12
+ # @param args [Array<Hash, Symbol>] Additional options.
13
+ # @return The given illustration object.
7
14
  def illustrate(content, *args)
8
15
  illustration = { :content => content,
9
16
  :show_when_passed => true,
@@ -1,5 +1,6 @@
1
- module Rspec
1
+ module RSpec
2
2
  module Illustrate
3
- VERSION = "0.1.1"
3
+ # The version used by gemspec
4
+ VERSION = "0.1.2"
4
5
  end
5
6
  end
@@ -5,7 +5,7 @@ require 'rspec/illustrate/version'
5
5
 
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = "rspec-illustrate"
8
- spec.version = Rspec::Illustrate::VERSION
8
+ spec.version = RSpec::Illustrate::VERSION
9
9
  spec.authors = ["Erik Schlyter"]
10
10
  spec.email = ["erik@erisc.se"]
11
11
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rspec-illustrate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-03-16 00:00:00.000000000 Z
12
+ date: 2015-03-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec-core
@@ -91,6 +91,7 @@ files:
91
91
  - Rakefile
92
92
  - bin/setup
93
93
  - lib/rspec/formatters/illustrated_documentation_formatter.rb
94
+ - lib/rspec/formatters/illustrated_html_formatter.rb
94
95
  - lib/rspec/formatters/illustration_formatter.rb
95
96
  - lib/rspec/illustrate.rb
96
97
  - lib/rspec/illustrate/version.rb
@@ -116,7 +117,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
116
117
  version: '0'
117
118
  segments:
118
119
  - 0
119
- hash: 807417647878713660
120
+ hash: 2396350347062895824
120
121
  requirements: []
121
122
  rubyforge_project:
122
123
  rubygems_version: 1.8.23