assert-view 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,6 @@
1
+ pkg/*
2
+ .bundle
3
+ *.gem
4
+ *.log
5
+ .rvmrc
6
+ .rbenv-version
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify dependencies in assert-views.gemspec
4
+ gemspec
5
+
6
+ gem 'rake', '~>0.9.2'
@@ -0,0 +1,21 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ assert-view (0.1.0)
5
+
6
+ GEM
7
+ remote: http://rubygems.org/
8
+ specs:
9
+ ansi (1.3.0)
10
+ assert (0.3.0)
11
+ ansi (~> 1.3)
12
+ rake (0.9.2)
13
+
14
+ PLATFORMS
15
+ ruby
16
+
17
+ DEPENDENCIES
18
+ assert
19
+ assert-view!
20
+ bundler
21
+ rake (~> 0.9.2)
@@ -0,0 +1,112 @@
1
+ = Assert::View
2
+
3
+
4
+
5
+ == Description
6
+
7
+ Assert::View is a collection of view classes for displaying test results using the Assert testing framework (https://github.com/teaminsight/assert). It allows any number of views to be created and used when running tests with Assert.
8
+
9
+
10
+
11
+ == Installation and Usage
12
+ Assert::View is a dependency of Assert and will be automatically installed when you install Assert:
13
+
14
+ $ gem install assert # will install assert-view as a dependency
15
+
16
+ === Usage: override the default view with a different one
17
+ Assert uses the Assert::View::Terminal view outputting to $stdout by default (https://github.com/teaminsight/assert/blob/master/lib/assert/setup/view.rb). To override and use a different view, add the following to your ~/.assert/options.rb file:
18
+
19
+ require 'assert/view/different_view'
20
+
21
+ # Override the Assert view option and assign it an instance of the different view
22
+ # Setup the view passing it the suite accessor and the IO to output on
23
+ Assert.options.view Assert::View::DifferentView.new(Assert.suite, $stdout)
24
+
25
+ === Usage: define your own and override
26
+ So, ~/.assert/option.rb is just a ruby script that is required when Assert is setting itself up. You can use this file to define your own custom view class and then override the Assert view option like above:
27
+
28
+ # Say you wanted to tweak and make a better Terminal view
29
+ require 'assert/view/terminal'
30
+ module Assert::View
31
+ class MyBetterTerminal < Terminal
32
+ # override stuff and tweak it to your heart's content
33
+ end
34
+ end
35
+
36
+ # Now override the view option to use your better terminal
37
+ Assert.options.view Assert::View::MyBetterTerminal.new(Assert.suite, $stdout)
38
+
39
+
40
+
41
+ == Assert::View::Base class
42
+ All views need to subclass the Assert::View::Base class. This class implements a few core things that assert expects of its view classes:
43
+
44
+ === Initializer
45
+ All view initializers take two things at minimum:
46
+ * *output_io*: an IO stream to output to ($stdout, etc...)
47
+ * *suite*: (optional) an instance of the core suite class; the suite of tests to run/render (defaults to Assert.suite - you probably shouldn't change this default unless you know what you are doing)
48
+
49
+ === 'suite' reader
50
+ The suite reader provides access to the suite of tests that will be or has been run. Use this reader to do things like count tests or test results, iterate through the tests and display detailed results, etc. This reader provides all the model data needed to render your view.
51
+
52
+ === 'render' method
53
+ The render method, as its name suggests, handles the overall rendering of the view. All render methods should take the following arguments:
54
+ * *args*: not used right now - more for view backwards compatibility in the event that args are needed
55
+ * *runner*: runner is a block that is given to the render method by the Assert::Runner class in use. The view class should call this block (@runner.call@) when the view is ready to run the suite of tests. Output any view headers before calling; output any view summary/footer after calling.
56
+
57
+ Here is an excerpt from the Terminal class to illustrate how a render method could be implemented:
58
+
59
+ def render(*args, &runner)
60
+ self.io_puts(:load_stmt) # header info
61
+
62
+ if count(:tests) > 0
63
+ runner.call if runner # run the test suite
64
+ self.io_puts(:detailed_results) # show any result details
65
+ end
66
+
67
+ self.io_puts(:results_stmt) # summary/footer info
68
+ end
69
+
70
+ === 'handle_runtime_result' method
71
+ This method is called as the suite of tests is being run. It is a callback for when a new test result is added to a tests results and the result is passed as the only argument. Use of the method is totally optional. The Terminal view uses it to output result abbreviations while the test suite is running.
72
+
73
+ === Utilities
74
+ The base class provides a few utilities for rendering views:
75
+ * *io_puts*: puts output to the output IO. Pass it the string to output or a symbol of a method that returns the string to output.
76
+ * *io_print*: same as io_puts, printing the string instead of putting it
77
+ * *run_time*: get a string with the suite's run time in seconds
78
+ * *run_seed*: get the seed value used to run the test suite in random order
79
+ * *count*: helper method for counting stuff on the suite, ie: 'count(:tests)'
80
+
81
+
82
+
83
+ == Roll Your Own!
84
+
85
+ Assert::View is designed to be extended and customized. Using the Base class, create your own view classes and use them as you see fit. If you feel like sharing, fork the repo, and add a pull requests. Bonus points to anyone who rips off other testing frameworks views (LeftRight, Turn, etc.)
86
+
87
+
88
+
89
+ == License
90
+
91
+ Copyright (c) 2011 Kelly D. Redding and Team Insight
92
+
93
+ Permission is hereby granted, free of charge, to any person
94
+ obtaining a copy of this software and associated documentation
95
+ files (the "Software"), to deal in the Software without
96
+ restriction, including without limitation the rights to use,
97
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
98
+ copies of the Software, and to permit persons to whom the
99
+ Software is furnished to do so, subject to the following
100
+ conditions:
101
+
102
+ The above copyright notice and this permission notice shall be
103
+ included in all copies or substantial portions of the Software.
104
+
105
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
106
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
107
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
108
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
109
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
110
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
111
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
112
+ OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,7 @@
1
+ require 'assert/rake_tasks'
2
+ Assert::RakeTasks.for(:test)
3
+
4
+ require 'bundler'
5
+ Bundler::GemHelper.install_tasks
6
+
7
+ task :default => :build
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "assert/view/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "assert-view"
7
+ s.version = Assert::View::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Kelly Redding"]
10
+ s.email = ["kelly@kelredd.com"]
11
+ s.homepage = "http://github.com/teaminsight/assert-view"
12
+ s.summary = %q{A collection of views for use in the Assert testing framework}
13
+ s.description = %q{A collection of views for use in the Assert testing framework}
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
+ s.require_paths = ["lib"]
19
+
20
+ s.add_development_dependency("bundler")
21
+ s.add_development_dependency("assert")
22
+ end
@@ -0,0 +1,2 @@
1
+ module Assert; end
2
+ module Assert::Views; end
@@ -0,0 +1,63 @@
1
+ require 'assert/options'
2
+
3
+ module Assert::View
4
+
5
+ class Base
6
+ include Assert::Options
7
+ options do
8
+ default_passed_abbrev '.'
9
+ default_failed_abbrev 'F'
10
+ default_ignored_abbrev 'I'
11
+ default_skipped_abbrev 'S'
12
+ default_errored_abbrev 'E'
13
+ end
14
+
15
+ attr_reader :suite
16
+
17
+ def initialize(output_io, suite=Assert.suite)
18
+ @suite = suite
19
+ @out = output_io
20
+ end
21
+
22
+ # override this to define how a view calls the runner and renders its results
23
+ def render(*args, &runner)
24
+ end
25
+
26
+ def handle_runtime_result(result)
27
+ end
28
+
29
+ protected
30
+
31
+ def io_puts(msg, opts={})
32
+ @out.puts(io_msg(msg, opts={}))
33
+ end
34
+
35
+ def io_print(msg, opts={})
36
+ @out.print(io_msg(msg, opts={}))
37
+ end
38
+
39
+ def run_time(format='%.6f')
40
+ format % @suite.run_time
41
+ end
42
+
43
+ def runner_seed
44
+ @suite.runner_seed
45
+ end
46
+
47
+ def count(type)
48
+ @suite.count(type)
49
+ end
50
+
51
+ private
52
+
53
+ def io_msg(msg, opts={})
54
+ if msg.kind_of?(::Symbol) && self.respond_to?(msg)
55
+ self.send(msg).to_s
56
+ else
57
+ msg.to_s
58
+ end
59
+ end
60
+
61
+ end
62
+
63
+ end
@@ -0,0 +1,131 @@
1
+ require 'assert/view/base'
2
+ require 'assert/result'
3
+
4
+ require 'ansi/code'
5
+
6
+ module Assert::View
7
+
8
+ class Terminal < Base
9
+
10
+ options do
11
+ default_styled false
12
+ default_passed_styles :green
13
+ default_failed_styles :red, :bold
14
+ default_errored_styles :yellow, :bold
15
+ default_skipped_styles :cyan
16
+ default_ignored_styles :magenta
17
+ end
18
+
19
+ def render(*args, &runner)
20
+ self.io_puts(:load_stmt)
21
+
22
+ if count(:tests) > 0
23
+ self.io_puts(:run_stmt)
24
+ runner.call if runner
25
+ self.io_puts(:detailed_results)
26
+ end
27
+
28
+ self.io_puts(:results_stmt)
29
+ end
30
+
31
+ def handle_runtime_result(result)
32
+ sym = result.to_sym
33
+ self.io_print(result_io_msg(self.options.send("#{sym}_abbrev"), sym))
34
+ end
35
+
36
+ protected
37
+
38
+ def load_stmt
39
+ tplur = (tcount = count(:tests)) == 1 ? "test": "tests"
40
+ "\nLoaded suite (#{tcount} #{tplur})"
41
+ end
42
+
43
+ def run_stmt
44
+ "Running tests in random order, seed: '#{self.runner_seed}'"
45
+ end
46
+
47
+ def detailed_results
48
+ details = self.suite.ordered_tests.reverse.collect do |test|
49
+ test.results.collect do |result|
50
+ if show_result_details?(result)
51
+ [ result_io_msg(result.to_s, result.to_sym),
52
+ output_io_msg(test.output.to_s)
53
+ ].join("\n")
54
+ end
55
+ end
56
+ end.flatten.compact
57
+ "\n\n" + details.join("\n\n") if !details.empty?
58
+ end
59
+
60
+ def results_stmt
61
+ rplur = (rcount = count(:results)) == 1 ? "result" : "results"
62
+ [ "\n",
63
+ "#{rcount} test #{rplur}: ", results_breakdown, "\n\n",
64
+ "(#{self.run_time} seconds)"
65
+ ].join('')
66
+ end
67
+
68
+ def results_breakdown
69
+ if count(:passed) == count(:results)
70
+ stmnt = if count(:results) < 1
71
+ "uhh..."
72
+ elsif count(:results) == 1
73
+ "it passed"
74
+ else
75
+ "all passed"
76
+ end
77
+ result_io_msg(stmnt, :passed)
78
+ else
79
+ breakdowns = [:passed, :failed, :ignored, :skipped, :errored]
80
+ breakdowns = breakdowns.inject([]) do |results, result_sym|
81
+ results << (if count(result_sym) > 0
82
+ result_io_msg("#{count(result_sym)} #{result_sym}", result_sym)
83
+ end)
84
+ end.compact
85
+ if breakdowns.size < 2
86
+ breakdowns.join('')
87
+ elsif breakdowns.size == 2
88
+ breakdowns.join(" and ")
89
+ else
90
+ [breakdowns[0..-2].join(", "), breakdowns.last].join(", and ")
91
+ end
92
+ end
93
+ end
94
+
95
+ def result_io_msg(msg, result_sym)
96
+ term_styles = if self.options.styled
97
+ self.options.send("#{result_sym}_styles")
98
+ end
99
+ io_msg(msg, :term_styles => term_styles)
100
+ end
101
+
102
+ def output_io_msg(output)
103
+ if output && !output.empty?
104
+ [ "--- stdout ---",
105
+ io_msg(output),
106
+ "--------------"
107
+ ].collect{|i| i.strip}.join("\n")
108
+ end
109
+ end
110
+
111
+ def io_msg(msg, opts={})
112
+ val = super
113
+ if !(style = term_style(*opts[:term_styles])).empty?
114
+ val = style + val + ANSI.send(:reset)
115
+ else
116
+ val
117
+ end
118
+ end
119
+
120
+ def term_style(*ansi_codes)
121
+ ansi_codes.collect{|code| ANSI.send(code) rescue nil}.compact.join('')
122
+ end
123
+
124
+ def show_result_details?(result)
125
+ ([:failed, :errored].include?(result.to_sym)) ||
126
+ ([:skipped, :ignored].include?(result.to_sym) && result.message)
127
+ end
128
+
129
+ end
130
+
131
+ end
@@ -0,0 +1,5 @@
1
+ module Assert; end
2
+
3
+ module Assert::View
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,44 @@
1
+ require 'assert'
2
+ require 'assert/suite'
3
+
4
+ require 'assert/view/base'
5
+ require 'stringio'
6
+
7
+ module Assert::View
8
+
9
+ class BaseTest < Assert::Context
10
+ desc "the view base"
11
+ setup do
12
+ @view = Assert::View::Base.new(Assert::Suite.new, StringIO.new("", "w+"))
13
+ end
14
+ subject{ @view }
15
+
16
+ should have_reader :suite
17
+ should have_instance_methods :render, :handle_runtime_result, :options
18
+ should have_class_method :options
19
+
20
+ end
21
+
22
+ class BaseOptionsTest < Assert::Context
23
+ desc "options for the base view"
24
+ subject do
25
+ Assert::View::Base.options
26
+ end
27
+
28
+ should "be an Options::Base object" do
29
+ assert_kind_of Assert::Options::Base, subject
30
+ end
31
+
32
+ should "default its result abbreviations" do
33
+ assert_equal '.', subject.default_passed_abbrev
34
+ assert_equal 'F', subject.default_failed_abbrev
35
+ assert_equal 'I', subject.default_ignored_abbrev
36
+ assert_equal 'S', subject.default_skipped_abbrev
37
+ assert_equal 'E', subject.default_errored_abbrev
38
+ end
39
+
40
+ end
41
+
42
+
43
+
44
+ end
@@ -0,0 +1,5 @@
1
+ # this file is automatically required in when you require 'assert' in your tests
2
+ # put test helpers here
3
+
4
+ # add root dir to the load path
5
+ $LOAD_PATH.unshift(File.expand_path("../..", __FILE__))
@@ -0,0 +1,9 @@
1
+ require 'assert/setup'
2
+
3
+ # this file is required in when the 'irb' rake test is run.
4
+ # b/c 'assert/setup' is required above, the test helper will be
5
+ # required in as well.
6
+
7
+ # put any IRB setup code here
8
+
9
+ require 'assert/view'
@@ -0,0 +1,32 @@
1
+ require 'assert'
2
+ require 'assert/options'
3
+
4
+ require 'assert/view/terminal'
5
+
6
+ module Assert::View
7
+
8
+ class TerminalOptionsTest < Assert::Context
9
+ desc "options for the terminal view"
10
+ subject do
11
+ Assert::View::Terminal.options
12
+ end
13
+
14
+ should "be an Options::Base object" do
15
+ assert_kind_of Assert::Options::Base, subject
16
+ end
17
+
18
+ should "default the styled option" do
19
+ assert_equal false, subject.default_styled
20
+ end
21
+
22
+ should "default its result styles" do
23
+ assert_equal :green, subject.default_passed_styles
24
+ assert_equal [:red, :bold], subject.default_failed_styles
25
+ assert_equal :magenta, subject.default_ignored_styles
26
+ assert_equal :cyan, subject.default_skipped_styles
27
+ assert_equal [:yellow, :bold], subject.default_errored_styles
28
+ end
29
+
30
+ end
31
+
32
+ end
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: assert-view
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Kelly Redding
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-08-31 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ type: :development
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 0
31
+ version: "0"
32
+ version_requirements: *id001
33
+ name: bundler
34
+ - !ruby/object:Gem::Dependency
35
+ type: :development
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ hash: 3
43
+ segments:
44
+ - 0
45
+ version: "0"
46
+ version_requirements: *id002
47
+ name: assert
48
+ description: A collection of views for use in the Assert testing framework
49
+ email:
50
+ - kelly@kelredd.com
51
+ executables: []
52
+
53
+ extensions: []
54
+
55
+ extra_rdoc_files: []
56
+
57
+ files:
58
+ - .gitignore
59
+ - Gemfile
60
+ - Gemfile.lock
61
+ - README.rdoc
62
+ - Rakefile
63
+ - assert-view.gemspec
64
+ - lib/assert/view.rb
65
+ - lib/assert/view/base.rb
66
+ - lib/assert/view/terminal.rb
67
+ - lib/assert/view/version.rb
68
+ - test/base_test.rb
69
+ - test/helper.rb
70
+ - test/irb.rb
71
+ - test/terminal_test.rb
72
+ homepage: http://github.com/teaminsight/assert-view
73
+ licenses: []
74
+
75
+ post_install_message:
76
+ rdoc_options: []
77
+
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ hash: 3
86
+ segments:
87
+ - 0
88
+ version: "0"
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ none: false
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ hash: 3
95
+ segments:
96
+ - 0
97
+ version: "0"
98
+ requirements: []
99
+
100
+ rubyforge_project:
101
+ rubygems_version: 1.8.10
102
+ signing_key:
103
+ specification_version: 3
104
+ summary: A collection of views for use in the Assert testing framework
105
+ test_files:
106
+ - test/base_test.rb
107
+ - test/helper.rb
108
+ - test/irb.rb
109
+ - test/terminal_test.rb