rspec-illustrate 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ .*.swp
2
+ /.bundle/
3
+ /.yardoc
4
+ /Gemfile.lock
5
+ /_yardoc/
6
+ /coverage/
7
+ /doc/
8
+ /pkg/
9
+ /spec/reports/
10
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rspec-illustrate.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Erik Schlyter
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,86 @@
1
+ # Rspec::Illustrate
2
+
3
+ This is an RSpec extension gem that allows you to define illustrative objects in
4
+ your examples that will be forwarded to the output formatter. This will allow
5
+ your output spec to become more readable, illustrative, and explanatory.
6
+
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'rspec-illustrate'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install rspec-illustrate
23
+
24
+ ## Usage
25
+
26
+ Within an example you use the `illustrate` statement to define an object that
27
+ will be passed to the output formatter. You can also provide additional options
28
+ if you want to set a label or define when the formatter should write the
29
+ illustration (e.g. omit if the example fails, etc.)
30
+
31
+ Given the file `./spec/array_spec.rb`:
32
+
33
+ ```ruby
34
+ require 'rspec/illustrate'
35
+
36
+ describe Array do
37
+ describe "#sort" do
38
+ it "should sort the array" do
39
+ given = [3, 1, 2]
40
+ expected = [1, 2, 3]
41
+ actual = given.sort
42
+
43
+ illustrate given.to_s, :label=>"Given the array"
44
+ illustrate expected.to_s, :label=>"After sort it looks like this"
45
+ illustrate actual.to_s, :show_when_passed=>false
46
+
47
+ expect(actual).to eq(expected)
48
+ end
49
+ end
50
+ end
51
+ ```
52
+
53
+ You execute RSpec with a formatter that takes the illustrations into
54
+ consideration, i.e.:
55
+
56
+ $ rspec -f RSpec::Formatters::IllustratedDocumentationFormatter
57
+
58
+ The output would be something like this:
59
+
60
+ ```
61
+ Array
62
+ #sort
63
+ should sort the array
64
+ --- Given the array ---
65
+ [3, 1, 2]
66
+ --- After sort it looks like this ---
67
+ [1, 2, 3]
68
+ ```
69
+
70
+
71
+ ## Development
72
+
73
+ After checking out the repo, run `bin/setup` to install dependencies.
74
+
75
+ To install this gem onto your local machine, run `bundle exec rake install`. To
76
+ release a new version, update the version number in `version.rb`, and then run
77
+ `bundle exec rake release` to create a git tag for the version, push git commits
78
+ and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
79
+
80
+ ## Contributing
81
+
82
+ 1. Fork it ( https://github.com/ErikSchlyter/rspec-illustrate/fork )
83
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
84
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
85
+ 4. Push to the branch (`git push origin my-new-feature`)
86
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ Bundler.setup
3
+ Bundler::GemHelper.install_tasks
4
+
5
+ require "rspec/core/rake_task"
6
+ RSpec::Core::RakeTask.new(:spec) do |t|
7
+ t.rspec_opts = "--format RSpec::Formatters::IllustratedDocumentationFormatter"
8
+ end
9
+
10
+ task :default => :spec
data/bin/setup ADDED
@@ -0,0 +1,5 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
@@ -0,0 +1,81 @@
1
+ RSpec::Support.require_rspec_core "formatters/documentation_formatter"
2
+ require 'rspec/formatters/illustration_formatter.rb'
3
+
4
+ RSpec.configure do |c|
5
+ c.add_setting(:illustration_passed_color, :default => :cyan)
6
+ c.add_setting(:illustration_failed_color, :default => :cyan)
7
+ c.add_setting(:illustration_pending_color, :default => :cyan)
8
+
9
+ color_map = RSpec::Core::Formatters::ConsoleCodes::CONFIG_COLORS_TO_METHODS
10
+ color_map[:illustration_passed] = :illustration_passed_color
11
+ color_map[:illustration_failed] = :illustration_failed_color
12
+ color_map[:illustration_pending] = :illustration_pending_color
13
+
14
+ c.add_setting(:illustration_formatter, :default =>
15
+ lambda {|illustration|
16
+ label = illustration.has_key?(:label) ?
17
+ "--- #{illustration[:label].to_s} ---\n" : ""
18
+
19
+ return label << illustration[:content].to_s
20
+ })
21
+ end
22
+
23
+ module RSpec
24
+ module Formatters
25
+ include RSpec::Core::Formatters
26
+
27
+ class IllustratedDocumentationFormatter < DocumentationFormatter
28
+ include RSpec::Formatters
29
+
30
+ # This registers the notifications this formatter supports, and tells
31
+ # us that this was written against the RSpec 3.x formatter API.
32
+ RSpec::Core::Formatters.register self, :example_passed,
33
+ :example_failed,
34
+ :example_pending
35
+
36
+ def initialize(output)
37
+ super(output)
38
+ end
39
+
40
+ def example_passed(passed)
41
+ super(passed)
42
+ write_illustrations(passed, :show_when_passed, :illustration_passed)
43
+ end
44
+
45
+ def example_failed(failure)
46
+ super(failure)
47
+ write_illustrations(failure, :show_when_failed, :illustration_failed)
48
+ end
49
+
50
+ def example_pending(pending)
51
+ super(pending)
52
+ write_illustrations(pending, :show_when_pending, :illustration_pending)
53
+ end
54
+
55
+ def write_illustrations(notification, filter_key, color_type)
56
+ illustrations = filter(illustrations_of(notification), filter_key)
57
+ return if illustrations.empty?
58
+
59
+ output.puts colored(indented(formatted(illustrations)), color_type)
60
+ end
61
+
62
+ def colored(text, color_type)
63
+ RSpec::Core::Formatters::ConsoleCodes.wrap(text, color_type)
64
+ end
65
+
66
+ def indented(text)
67
+ current_indentation << text.gsub(/\n/, "\n#{current_indentation}")
68
+ end
69
+
70
+ def formatted(illustrations)
71
+ formatter_proc = RSpec.configuration.illustration_formatter
72
+
73
+ illustrations.collect{|illustration|
74
+ formatter_proc.call(illustration)
75
+ }.join("\n")
76
+ end
77
+
78
+ end
79
+ end
80
+ end
81
+
@@ -0,0 +1,18 @@
1
+
2
+ module RSpec
3
+ module Formatters
4
+
5
+ def illustrations_of(notification)
6
+ notification.example.metadata[:illustrations]
7
+ end
8
+
9
+ module_function :illustrations_of
10
+
11
+ def filter(illustrations, filter_key)
12
+ illustrations.select{|illustration| illustration[filter_key]}
13
+ end
14
+
15
+ module_function :filter
16
+
17
+ end
18
+ end
@@ -0,0 +1,31 @@
1
+ require 'rspec/illustrate/version'
2
+ require 'rspec/core'
3
+
4
+ module RSpec
5
+ module Illustrate
6
+
7
+ def illustrate(content, *args)
8
+ illustration = { :content => content,
9
+ :show_when_passed => true,
10
+ :show_when_failed => true,
11
+ :show_when_pending => true }
12
+
13
+ args.each{|arg|
14
+ illustration[arg] = true if arg.is_a?(Symbol)
15
+ illustration.merge!(arg) if arg.kind_of?(Hash)
16
+ }
17
+
18
+ RSpec.current_example.metadata[:illustrations] << illustration
19
+ content
20
+ end
21
+ end
22
+ end
23
+
24
+ RSpec.configure do |c|
25
+ c.include RSpec::Illustrate
26
+ c.before do
27
+ RSpec.current_example.metadata[:illustrations] = []
28
+ end
29
+ end
30
+
31
+ RSpec::SharedContext.send(:include, RSpec::Illustrate)
@@ -0,0 +1,5 @@
1
+ module Rspec
2
+ module Illustrate
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rspec/illustrate/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rspec-illustrate"
8
+ spec.version = Rspec::Illustrate::VERSION
9
+ spec.authors = ["Erik Schlyter"]
10
+ spec.email = ["erik@erisc.se"]
11
+
12
+ spec.summary = %q{RSpec extension gem for including illustrative objects in your specs.}
13
+ spec.description = %q{A plugin to RSpec that allows you to define illustrative objects in your examples that will be forwarded to the output formatter. This will allow your output spec to become more readable, illustrative, and explanatory.}
14
+ spec.homepage = "https://github.com/ErikSchlyter/rspec-illustrate"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_dependency 'rspec-core', '~> 3.2.1'
21
+ spec.add_dependency 'rspec-expectations', '~> 3.2.0'
22
+ spec.add_development_dependency "bundler", "~> 1.8"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ end
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rspec-illustrate
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Erik Schlyter
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2015-03-16 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec-core
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 3.2.1
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 3.2.1
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec-expectations
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 3.2.0
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 3.2.0
46
+ - !ruby/object:Gem::Dependency
47
+ name: bundler
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: '1.8'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '1.8'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rake
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: '10.0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: '10.0'
78
+ description: A plugin to RSpec that allows you to define illustrative objects in your
79
+ examples that will be forwarded to the output formatter. This will allow your output
80
+ spec to become more readable, illustrative, and explanatory.
81
+ email:
82
+ - erik@erisc.se
83
+ executables: []
84
+ extensions: []
85
+ extra_rdoc_files: []
86
+ files:
87
+ - .gitignore
88
+ - Gemfile
89
+ - LICENSE.txt
90
+ - README.md
91
+ - Rakefile
92
+ - bin/setup
93
+ - lib/rspec/formatters/illustrated_documentation_formatter.rb
94
+ - lib/rspec/formatters/illustration_formatter.rb
95
+ - lib/rspec/illustrate.rb
96
+ - lib/rspec/illustrate/version.rb
97
+ - rspec-illustrate.gemspec
98
+ homepage: https://github.com/ErikSchlyter/rspec-illustrate
99
+ licenses:
100
+ - MIT
101
+ post_install_message:
102
+ rdoc_options: []
103
+ require_paths:
104
+ - lib
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ none: false
107
+ requirements:
108
+ - - ! '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ segments:
112
+ - 0
113
+ hash: 2309836766520635955
114
+ required_rubygems_version: !ruby/object:Gem::Requirement
115
+ none: false
116
+ requirements:
117
+ - - ! '>='
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ segments:
121
+ - 0
122
+ hash: 2309836766520635955
123
+ requirements: []
124
+ rubyforge_project:
125
+ rubygems_version: 1.8.23
126
+ signing_key:
127
+ specification_version: 3
128
+ summary: RSpec extension gem for including illustrative objects in your specs.
129
+ test_files: []