solea 0.9.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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/solea.rb +162 -0
  3. metadata +43 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e8892aed1a89ac7902cd0354accfb10e6c6d69c0
4
+ data.tar.gz: 907bd4155ed61518e04f5e2298f396e5c9756f81
5
+ SHA512:
6
+ metadata.gz: a820bdddf834207afafb11a29fca10a7d0e597ee62a758b018b88126cd1bd7d0c887427a9f271ce2e497763a9d6c0c707c5b475a9a59edd8397da98f0b058313
7
+ data.tar.gz: 04d3c9988dba0e5c4dbf59ed34cac7f0d40d45e5995bad5160c00c5b60b21c117e43a16f04c3678ee0b33647e2de5a867c426801c170ab5ea02f35448259ef4b
data/lib/solea.rb ADDED
@@ -0,0 +1,162 @@
1
+ class ClassTest
2
+ def initialize(class_name, &block)
3
+ @class_name = class_name
4
+ @tests = []
5
+ block.call(self)
6
+ print report
7
+ end
8
+
9
+ def test_method(method_name, &block)
10
+ method_test = MethodTest.new(method_name)
11
+ block.call(method_test)
12
+ @tests << method_test.tests
13
+ end
14
+
15
+ def report
16
+ @tests = @tests.flatten
17
+ @passing_tests = @tests.select{|test| test.pass}
18
+ @failing_tests = @tests.select{|test| !test.pass}
19
+ text_color = (@failing_tests.any? ? "37;41;1" : "37;47;1")
20
+ message = "\n\e[#{text_color}m" + "#{@passing_tests.count} of #{@tests.count} tests passed.".center(145) + "\e[0m\n\n" +
21
+ "| lineno: pass/fail |".center(145, "-") + "\n\n|"
22
+ @tests.each_with_index do |test, i|
23
+ text_color = (test.pass ? "0" : "37;41;1")
24
+ status = (test.pass ? "pass" : "fail")
25
+ message += "\e[#{text_color}m #{"%3d" % test.state[:hypothesis_caller].lineno}: #{status} \e[0m|"
26
+ message += "\n|" if (i + 1) % 12 == 0
27
+ end
28
+ message += "\n\nFor detailed information on any test, try \"m.print{|t| t.report}\" after the test's expect/assert statement."
29
+ "#{message}\n\n"
30
+ end
31
+ end
32
+
33
+ class MethodTest
34
+ attr_accessor :method_name, :pass, :tests
35
+
36
+ def initialize(method_name)
37
+ @method_name = method_name
38
+ @tests = []
39
+ @exceptions = []
40
+ @notes = []
41
+ end
42
+
43
+ def before(&block)
44
+ @before = yield
45
+ end
46
+
47
+ def describe(&block)
48
+ @description = yield
49
+ @notes = []
50
+ end
51
+
52
+ def note(&block)
53
+ @notes << yield
54
+ end
55
+
56
+ def example(&block)
57
+ @example_caller = caller_locations(1,1)[0]
58
+ @exceptions = []
59
+ begin
60
+ @example = block.call(@before)
61
+ rescue Exception => e
62
+ @example = block
63
+ @exceptions << [e, @example_caller]
64
+ end
65
+ end
66
+
67
+ def assert(&block)
68
+ @hypothesis_caller = caller_locations(1,1)[0]
69
+ begin
70
+ @pass = block.call(@example)
71
+ rescue Exception => e
72
+ @pass = false
73
+ @exceptions << [e, @hypothesis_caller]
74
+ end
75
+ @tests << self.dup
76
+ end
77
+
78
+ def assert!(&block)
79
+ @hypothesis_caller = caller_locations(1,1)[0]
80
+ begin
81
+ @pass = !block.call(@example)
82
+ rescue Exception => e
83
+ @pass = false
84
+ @exceptions << [e, @hypothesis_caller]
85
+ end
86
+ @tests << self.dup
87
+ end
88
+
89
+ def expect(exception_expected = nil, &block)
90
+ @hypothesis_caller = caller_locations(1,1)[0]
91
+ if exception_expected
92
+ test_for_exception(exception_expected, block)
93
+ else
94
+ begin
95
+ @pass = yield == @example
96
+ rescue Exception => e
97
+ @pass = false
98
+ @exceptions << [e, @hypothesis_caller]
99
+ end
100
+ end
101
+ @tests << self.dup
102
+ end
103
+
104
+ def expect!(&block)
105
+ @hypothesis_caller = caller_locations(1,1)[0]
106
+ begin
107
+ @pass = yield != @example
108
+ rescue Exception => e
109
+ @pass = false
110
+ @exceptions << [e, @hypothesis_caller]
111
+ end
112
+ @tests << self.dup
113
+ end
114
+
115
+ def test_for_exception(exception_expected, block)
116
+ begin
117
+ block_code = block.call(@example)
118
+ rescue Exception => e
119
+ block_code = block
120
+ @exceptions << [e, @hypothesis_caller]
121
+ end
122
+ if block_code.nil?
123
+ test = @example.is_a?(Proc)
124
+ else
125
+ test = block_code.is_a?(Proc)
126
+ end
127
+ @pass = test if exception_expected == :fail
128
+ @pass = !test if exception_expected == :pass
129
+ end
130
+
131
+ def state
132
+ { :before => @before,
133
+ :description => @description,
134
+ :example => @example,
135
+ :example_caller => @example_caller,
136
+ :exceptions => @exceptions,
137
+ :hypothesis_caller => @hypothesis_caller,
138
+ :notes => @notes,
139
+ :pass => @pass
140
+ }
141
+ end
142
+
143
+ def print(&block)
144
+ puts block.call(self)
145
+ end
146
+
147
+ def report
148
+ report_caller = caller_locations(1,1)[0]
149
+ report = "\n" + "Report requested on line #{report_caller.lineno}".center(145, "-") + "\n\n" +
150
+ "-Except for \"method name\", each of the terms in the lefthand column below is a key in the #state hash.\n" +
151
+ "-Thus, for example, the @exceptions array can be accessed for more information with \"m.print{|t| t.state[:exceptions]}\".\n" +
152
+ "-Arrays and hashes are abbreviated below; the number of dots represents their length.\n\n" +
153
+ "method name: #{" " * ("hypothesis_caller".length - "Method name".length)}\"#{@method_name}\"\n"
154
+ state.each do |k, v|
155
+ spaces = " " * ("hypothesis_caller".length - k.length)
156
+ v = "\"#{v}\"" if v.is_a?(String)
157
+ v = "[#{"." * v.length}]" if v.is_a?(Array) || v.is_a?(Hash)
158
+ report += "#{k}: #{spaces}#{v}\n"
159
+ end
160
+ report
161
+ end
162
+ end
metadata ADDED
@@ -0,0 +1,43 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: solea
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.2
5
+ platform: ruby
6
+ authors:
7
+ - Joseph Swetnam
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-08-15 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A simple, streamlined unit testing gem.
14
+ email: jbswetnam@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/solea.rb
20
+ homepage: https://github.com/crownyx/solea
21
+ licenses: []
22
+ metadata: {}
23
+ post_install_message:
24
+ rdoc_options: []
25
+ require_paths:
26
+ - lib
27
+ required_ruby_version: !ruby/object:Gem::Requirement
28
+ requirements:
29
+ - - '>='
30
+ - !ruby/object:Gem::Version
31
+ version: '0'
32
+ required_rubygems_version: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - '>='
35
+ - !ruby/object:Gem::Version
36
+ version: '0'
37
+ requirements: []
38
+ rubyforge_project:
39
+ rubygems_version: 2.0.5
40
+ signing_key:
41
+ specification_version: 4
42
+ summary: Solea
43
+ test_files: []