assert 0.1.0

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.
Files changed (52) hide show
  1. data/.gitignore +5 -0
  2. data/Gemfile +6 -0
  3. data/Gemfile.lock +17 -0
  4. data/README.rdoc +77 -0
  5. data/Rakefile +7 -0
  6. data/assert.gemspec +21 -0
  7. data/examples/empty_test.rb +5 -0
  8. data/examples/results_test.rb +25 -0
  9. data/examples/single_test.rb +9 -0
  10. data/lib/assert.rb +8 -0
  11. data/lib/assert/assertions.rb +253 -0
  12. data/lib/assert/context.rb +196 -0
  13. data/lib/assert/options.rb +43 -0
  14. data/lib/assert/rake_tasks.rb +95 -0
  15. data/lib/assert/result.rb +164 -0
  16. data/lib/assert/result_set.rb +14 -0
  17. data/lib/assert/runner.rb +60 -0
  18. data/lib/assert/setup/autorun.rb +34 -0
  19. data/lib/assert/setup/helpers.rb +62 -0
  20. data/lib/assert/setup/suite.rb +12 -0
  21. data/lib/assert/setup/view.rb +11 -0
  22. data/lib/assert/suite.rb +128 -0
  23. data/lib/assert/test.rb +90 -0
  24. data/lib/assert/version.rb +3 -0
  25. data/lib/assert/view/base.rb +54 -0
  26. data/lib/assert/view/terminal.rb +138 -0
  27. data/test/assertions/assert_block_test.rb +39 -0
  28. data/test/assertions/assert_instance_of_test.rb +43 -0
  29. data/test/assertions/assert_kind_of_test.rb +43 -0
  30. data/test/assertions/assert_not_block_test.rb +39 -0
  31. data/test/assertions/assert_not_instance_of_test.rb +43 -0
  32. data/test/assertions/assert_not_kind_of_test.rb +43 -0
  33. data/test/assertions/assert_not_respond_to_test.rb +43 -0
  34. data/test/assertions/assert_nothing_raised_test.rb +46 -0
  35. data/test/assertions/assert_raises_test.rb +49 -0
  36. data/test/assertions/assert_respond_to_test.rb +43 -0
  37. data/test/assertions_test.rb +334 -0
  38. data/test/context/class_methods_test.rb +314 -0
  39. data/test/context_test.rb +288 -0
  40. data/test/fixtures/inherited_stuff.rb +36 -0
  41. data/test/fixtures/sample_context.rb +13 -0
  42. data/test/helper.rb +52 -0
  43. data/test/irb.rb +10 -0
  44. data/test/options_test.rb +78 -0
  45. data/test/result_set_test.rb +89 -0
  46. data/test/result_test.rb +255 -0
  47. data/test/runner_test.rb +33 -0
  48. data/test/suite_test.rb +200 -0
  49. data/test/test/running_test.rb +327 -0
  50. data/test/test_test.rb +184 -0
  51. data/test/view_test.rb +35 -0
  52. metadata +155 -0
@@ -0,0 +1,138 @@
1
+ require 'assert/view/base'
2
+ require 'assert/result'
3
+
4
+ module Assert::View
5
+
6
+ class Terminal < Base
7
+
8
+ TERM_STYLES = {
9
+ :reset => 0,
10
+ :bold => 1,
11
+ :underline => 4,
12
+ :black => 30,
13
+ :red => 31,
14
+ :green => 32,
15
+ :yellow => 33,
16
+ :blue => 34,
17
+ :magenta => 35,
18
+ :cyan => 36,
19
+ :white => 37,
20
+ :bgblack => 40,
21
+ :bgred => 41,
22
+ :bggreen => 42,
23
+ :bgyellow => 43,
24
+ :bgblue => 44,
25
+ :bgmagenta => 45,
26
+ :bgcyan => 46,
27
+ :bgwhite => 47
28
+ }
29
+
30
+ options do
31
+ default_styled false
32
+ default_passed_abbrev '.'
33
+ default_failed_abbrev 'F'
34
+ default_ignored_abbrev 'I'
35
+ default_skipped_abbrev 'S'
36
+ default_errored_abbrev 'E'
37
+ default_passed_styles :green
38
+ default_failed_styles :red, :bold
39
+ default_errored_styles :yellow, :bold
40
+ default_skipped_styles :cyan
41
+ default_ignored_styles :magenta
42
+ end
43
+
44
+ def render(*args, &block)
45
+ self.io_print(:load_stmt)
46
+
47
+ if count(:tests) > 0
48
+ block.call if block
49
+ self.io_puts(:detailed_results)
50
+ end
51
+
52
+ self.io_puts(:results_stmt)
53
+ end
54
+
55
+ def print_runtime_result(result)
56
+ sym = result.to_sym
57
+ self.io_print(result_io_msg(self.options.send("#{sym}_abbrev"), sym))
58
+ end
59
+
60
+ protected
61
+
62
+ def load_stmt
63
+ tplur = (tcount = count(:tests)) == 1 ? "test": "tests"
64
+ "\nLoaded suite (#{tcount} #{tplur}) "
65
+ end
66
+
67
+ def detailed_results
68
+ details = self.suite.ordered_results.reverse.collect do |result|
69
+ result_io_msg(result.to_s, result.to_sym) if show_result_details?(result)
70
+ end.compact
71
+ "\n\n" + details.join("\n\n") if !details.empty?
72
+ end
73
+
74
+ def results_stmt
75
+ rplur = (rcount = count(:results)) == 1 ? "result" : "results"
76
+ [ "\n",
77
+ "#{rcount} test #{rplur}: ", results_breakdown, "\n\n",
78
+ "(#{run_time} seconds)"
79
+ ].join('')
80
+ end
81
+
82
+ def results_breakdown
83
+ if count(:passed) == count(:results)
84
+ stmnt = if count(:results) < 1
85
+ "uhh..."
86
+ elsif count(:results) == 1
87
+ "it passed"
88
+ else
89
+ "all passed"
90
+ end
91
+ result_io_msg(stmnt, :passed)
92
+ else
93
+ breakdowns = [:passed, :failed, :ignored, :skipped, :errored]
94
+ breakdowns = breakdowns.inject([]) do |results, result_sym|
95
+ results << (if count(result_sym) > 0
96
+ result_io_msg("#{count(result_sym)} #{result_sym}", result_sym)
97
+ end)
98
+ end.compact
99
+ if breakdowns.size < 2
100
+ breakdowns.join('')
101
+ elsif breakdowns.size == 2
102
+ breakdowns.join(" and ")
103
+ else
104
+ [breakdowns[0..-2].join(", "), breakdowns.last].join(", and ")
105
+ end
106
+ end
107
+ end
108
+
109
+ def result_io_msg(msg, result_sym)
110
+ term_styles = if self.options.styled
111
+ self.options.send("#{result_sym}_styles")
112
+ end
113
+ io_msg(msg, :term_styles => term_styles)
114
+ end
115
+
116
+ def io_msg(msg, opts={})
117
+ val = super
118
+ if !(style = term_style(*opts[:term_styles])).empty?
119
+ val = style + val + term_style(:reset)
120
+ else
121
+ val
122
+ end
123
+ end
124
+
125
+ def term_style(*styles)
126
+ styles.collect do |style|
127
+ TERM_STYLES.include?(style) ? "\e[#{TERM_STYLES[style]}m" : nil
128
+ end.join('')
129
+ end
130
+
131
+ def show_result_details?(result)
132
+ ([:failed, :errored].include?(result.to_sym)) ||
133
+ ([:skipped, :ignored].include?(result.to_sym) && result.message)
134
+ end
135
+
136
+ end
137
+
138
+ end
@@ -0,0 +1,39 @@
1
+ require 'assert'
2
+
3
+ class Assert::Assertions::AssertBlockTest < Assert::Context
4
+ desc "the assert_block helper run in a test"
5
+ setup do
6
+ fail_desc = @fail_desc = "assert block fail desc"
7
+ @test = Factory.test do
8
+ assert_block{ true }
9
+ assert_block(fail_desc){ false }
10
+ end
11
+ @test.run
12
+ end
13
+ subject{ @test }
14
+
15
+ should "have 2 total results" do
16
+ assert_equal 2, subject.result_count
17
+ end
18
+ should "have 1 pass result" do
19
+ assert_equal 1, subject.result_count(:pass)
20
+ end
21
+ should "have 1 fail result" do
22
+ assert_equal 1, subject.result_count(:fail)
23
+ end
24
+
25
+ class FailMessageTest < AssertBlockTest
26
+ desc "with a failed result"
27
+ setup do
28
+ @expected = [ "Expected block to return true value.", @fail_desc ].join("\n")
29
+ @fail_message = @test.fail_results.first.message
30
+ end
31
+ subject{ @fail_message }
32
+
33
+ should "have a fail message with an explanation of what failed and my fail description" do
34
+ assert_equal @expected, subject
35
+ end
36
+
37
+ end
38
+
39
+ end
@@ -0,0 +1,43 @@
1
+ require 'assert'
2
+
3
+ class Assert::Assertions::AssertInstanceOfTest < Assert::Context
4
+ desc "the assert_instance_of helper run in a test"
5
+ setup do
6
+ fail_desc = @fail_desc = "assert instance of fail desc"
7
+ fail_args = @fail_args = [ Array, "object", fail_desc ]
8
+ @test = Factory.test do
9
+ assert_instance_of(String, "object") # pass
10
+ assert_instance_of(*fail_args) # fail
11
+ end
12
+ @test.run
13
+ end
14
+ subject{ @test }
15
+
16
+ should "have 2 total results" do
17
+ assert_equal 2, subject.result_count
18
+ end
19
+ should "have 1 pass result" do
20
+ assert_equal 1, subject.result_count(:pass)
21
+ end
22
+ should "have 1 fail result" do
23
+ assert_equal 1, subject.result_count(:fail)
24
+ end
25
+
26
+ class FailMessageTest < AssertInstanceOfTest
27
+ desc "with a failed result"
28
+ setup do
29
+ @expected = [
30
+ "Expected #{@fail_args[1].inspect} to be an instance of #{@fail_args[0]},",
31
+ "not #{@fail_args[1].class}.\n#{@fail_args[2]}"
32
+ ].join(" ")
33
+ @fail_message = @test.fail_results.first.message
34
+ end
35
+ subject{ @fail_message }
36
+
37
+ should "have a fail message with an explanation of what failed and my fail description" do
38
+ assert_equal @expected, subject
39
+ end
40
+
41
+ end
42
+
43
+ end
@@ -0,0 +1,43 @@
1
+ require 'assert'
2
+
3
+ class Assert::Assertions::AssertKindOfTest < Assert::Context
4
+ desc "the assert_kind_of helper run in a test"
5
+ setup do
6
+ fail_desc = @fail_desc = "assert kind of fail desc"
7
+ fail_args = @fail_args = [ Array, "object", fail_desc ]
8
+ @test = Factory.test do
9
+ assert_kind_of(String, "object") # pass
10
+ assert_kind_of(*fail_args) # fail
11
+ end
12
+ @test.run
13
+ end
14
+ subject{ @test }
15
+
16
+ should "have 2 total results" do
17
+ assert_equal 2, subject.result_count
18
+ end
19
+ should "have 1 pass result" do
20
+ assert_equal 1, subject.result_count(:pass)
21
+ end
22
+ should "have 1 fail result" do
23
+ assert_equal 1, subject.result_count(:fail)
24
+ end
25
+
26
+ class FailMessageTest < AssertKindOfTest
27
+ desc "with a failed result"
28
+ setup do
29
+ @expected = [
30
+ "Expected #{@fail_args[1].inspect} to be a kind of #{@fail_args[0]},",
31
+ "not #{@fail_args[1].class}.\n#{@fail_args[2]}"
32
+ ].join(" ")
33
+ @fail_message = @test.fail_results.first.message
34
+ end
35
+ subject{ @fail_message }
36
+
37
+ should "have a fail message with an explanation of what failed and my fail description" do
38
+ assert_equal @expected, subject
39
+ end
40
+
41
+ end
42
+
43
+ end
@@ -0,0 +1,39 @@
1
+ require 'assert'
2
+
3
+ class Assert::Assertions::AssertNotBlockTest < Assert::Context
4
+ desc "the assert_not_block helper run in a test"
5
+ setup do
6
+ fail_desc = @fail_desc = "assert not block fail desc"
7
+ @test = Factory.test do
8
+ assert_not_block(fail_desc){ true }
9
+ assert_not_block{ false }
10
+ end
11
+ @test.run
12
+ end
13
+ subject{ @test }
14
+
15
+ should "have 2 total results" do
16
+ assert_equal 2, subject.result_count
17
+ end
18
+ should "have 1 pass result" do
19
+ assert_equal 1, subject.result_count(:pass)
20
+ end
21
+ should "have 1 fail result" do
22
+ assert_equal 1, subject.result_count(:fail)
23
+ end
24
+
25
+ class FailMessageTest < AssertNotBlockTest
26
+ desc "with a failed result"
27
+ setup do
28
+ @expected = [ "Expected block to return false value.", @fail_desc ].join("\n")
29
+ @fail_message = @test.fail_results.first.message
30
+ end
31
+ subject{ @fail_message }
32
+
33
+ should "have a fail message with an explanation of what failed and my fail description" do
34
+ assert_equal @expected, subject
35
+ end
36
+
37
+ end
38
+
39
+ end
@@ -0,0 +1,43 @@
1
+ require 'assert'
2
+
3
+ class Assert::Assertions::AssertNotInstanceOfTest < Assert::Context
4
+ desc "the assert_not_instance_of helper run in a test"
5
+ setup do
6
+ fail_desc = @fail_desc = "assert not instance of fail desc"
7
+ fail_args = @fail_args = [ String, "object", fail_desc ]
8
+ @test = Factory.test do
9
+ assert_not_instance_of(*fail_args) # fail
10
+ assert_not_instance_of(Array, "object") # pass
11
+ end
12
+ @test.run
13
+ end
14
+ subject{ @test }
15
+
16
+ should "have 2 total results" do
17
+ assert_equal 2, subject.result_count
18
+ end
19
+ should "have 1 pass result" do
20
+ assert_equal 1, subject.result_count(:pass)
21
+ end
22
+ should "have 1 fail result" do
23
+ assert_equal 1, subject.result_count(:fail)
24
+ end
25
+
26
+ class FailMessageTest < AssertNotInstanceOfTest
27
+ desc "with a failed result"
28
+ setup do
29
+ @expected = [
30
+ "#{@fail_args[1].inspect} was not expected to be an instance of #{@fail_args[0]}.",
31
+ "\n#{@fail_args[2]}"
32
+ ].join
33
+ @fail_message = @test.fail_results.first.message
34
+ end
35
+ subject{ @fail_message }
36
+
37
+ should "have a fail message with an explanation of what failed and my fail description" do
38
+ assert_equal @expected, subject
39
+ end
40
+
41
+ end
42
+
43
+ end
@@ -0,0 +1,43 @@
1
+ require 'assert'
2
+
3
+ class Assert::Assertions::AssertNotKindOfTest < Assert::Context
4
+ desc "the assert_not_kind_of helper run in a test"
5
+ setup do
6
+ fail_desc = @fail_desc = "assert not kind of fail desc"
7
+ fail_args = @fail_args = [ String, "object", fail_desc ]
8
+ @test = Factory.test do
9
+ assert_not_kind_of(*fail_args) # fail
10
+ assert_not_kind_of(Array, "object") # pass
11
+ end
12
+ @test.run
13
+ end
14
+ subject{ @test }
15
+
16
+ should "have 2 total results" do
17
+ assert_equal 2, subject.result_count
18
+ end
19
+ should "have 1 pass result" do
20
+ assert_equal 1, subject.result_count(:pass)
21
+ end
22
+ should "have 1 fail result" do
23
+ assert_equal 1, subject.result_count(:fail)
24
+ end
25
+
26
+ class FailMessageTest < AssertNotKindOfTest
27
+ desc "with a failed result"
28
+ setup do
29
+ @expected = [
30
+ "#{@fail_args[1].inspect} was not expected to be a kind of #{@fail_args[0]}.",
31
+ "\n#{@fail_args[2]}"
32
+ ].join
33
+ @fail_message = @test.fail_results.first.message
34
+ end
35
+ subject{ @fail_message }
36
+
37
+ should "have a fail message with an explanation of what failed and my fail description" do
38
+ assert_equal @expected, subject
39
+ end
40
+
41
+ end
42
+
43
+ end
@@ -0,0 +1,43 @@
1
+ require 'assert'
2
+
3
+ class Assert::Assertions::AssertNotRespondToTest < Assert::Context
4
+ desc "the assert_not_respond_to helper run in a test"
5
+ setup do
6
+ fail_desc = @fail_desc = "assert not respond to fail desc"
7
+ fail_args = @fail_args = [ 1, :abs, fail_desc ]
8
+ @test = Factory.test do
9
+ assert_not_respond_to(*fail_args) # fail
10
+ assert_not_respond_to("1", :abs) # pass
11
+ end
12
+ @test.run
13
+ end
14
+ subject{ @test }
15
+
16
+ should "have 2 total results" do
17
+ assert_equal 2, subject.result_count
18
+ end
19
+ should "have 1 pass result" do
20
+ assert_equal 1, subject.result_count(:pass)
21
+ end
22
+ should "have 1 fail result" do
23
+ assert_equal 1, subject.result_count(:fail)
24
+ end
25
+
26
+ class FailMessageTest < AssertNotRespondToTest
27
+ desc "with a failed result"
28
+ setup do
29
+ @expected = [
30
+ "#{@fail_args[0].inspect} (#{@fail_args[0].class}) not expected to",
31
+ "respond to ##{@fail_args[1]}.\n#{@fail_args[2]}"
32
+ ].join(" ")
33
+ @fail_message = @test.fail_results.first.message
34
+ end
35
+ subject{ @fail_message }
36
+
37
+ should "have a fail message with an explanation of what failed and my fail description" do
38
+ assert_equal @expected, subject
39
+ end
40
+
41
+ end
42
+
43
+ end
@@ -0,0 +1,46 @@
1
+ require 'assert'
2
+
3
+ class Assert::Assertions::AssertNothingRaisedTest < Assert::Context
4
+ desc "the assert_nothing_raised helper run in a test"
5
+ setup do
6
+ fail_desc = @fail_desc = "assert nothing raised fail desc"
7
+ @test = Factory.test do
8
+ assert_nothing_raised(StandardError, RuntimeError, fail_desc){ raise(StandardError) } # fail
9
+ assert_nothing_raised(RuntimeError){ raise(StandardError) } # pass
10
+ assert_nothing_raised(fail_desc){ raise(RuntimeError) } # fail
11
+ assert_nothing_raised{ true } # pass
12
+ end
13
+ @test.run
14
+ end
15
+ subject{ @test }
16
+
17
+ should "have 4 total results" do
18
+ assert_equal 4, subject.result_count
19
+ end
20
+ should "have 2 pass result" do
21
+ assert_equal 2, subject.result_count(:pass)
22
+ end
23
+ should "have 2 fail result" do
24
+ assert_equal 2, subject.result_count(:fail)
25
+ end
26
+
27
+ class FailMessageTest < AssertNothingRaisedTest
28
+ desc "with a failed result"
29
+ setup do
30
+ @expected = [
31
+ "#{@fail_desc}\nStandardError or RuntimeError exception was not expected, but was raised:",
32
+ "#{@fail_desc}\nAn exception was not expected, but was raised:"
33
+ ]
34
+ @fail_messages = @test.fail_results.collect(&:message)
35
+ end
36
+ subject{ @fail_messages }
37
+
38
+ should "have a fail message with an explanation of what failed and my fail description" do
39
+ subject.each_with_index do |message, n|
40
+ assert_match /^#{@expected[n]}/, message
41
+ end
42
+ end
43
+
44
+ end
45
+
46
+ end