blackstart 0.2.0 → 0.8.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.
data/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
data/README ADDED
@@ -0,0 +1,191 @@
1
+ Blackstart
2
+
3
+ Blackstart is a small, subdued library for automated testing in Ruby. It
4
+ doesn't depend on anything beyond the Ruby platform and doesn't modify the
5
+ environment beyond its conventional namespace. The program that tests it is
6
+ written in primitive Ruby, with no other testing library, so it can be verified
7
+ straightforwardly.
8
+
9
+ The blackstart is a small, subdued bird that eats bugs. A black start is when
10
+ an inactive power plant restarts by means of an independent power source rather
11
+ than the electrical grid.
12
+
13
+ Here's an example of a test program that uses Blackstart:
14
+
15
+ require "blackstart"
16
+
17
+ exit Blackstart.run [
18
+ lambda {
19
+ unless "Hello, World!" == ["He", "", "o, Wor", "d!"].join("l")
20
+ raise "join did not work as expected"
21
+ end
22
+ },
23
+
24
+ lambda {
25
+ unless "sample" == "simple".gsub(/i/, "a")
26
+ raise "gsub did not work as expected"
27
+ end
28
+ }
29
+ ]
30
+
31
+ Blackstart.run runs a sequence of tests and reports information about any that
32
+ fail. It returns false if there were failures, true otherwise.
33
+
34
+ An array of procs is an easy way to represent a sequence of tests. In general,
35
+ the sequence can be any object that responds to an each message in the
36
+ conventional way and each test object in the sequence can be any object that
37
+ (1) is a proc or converts to one via to_proc and (2) converts to a string via
38
+ to_s.
39
+
40
+ Each test is run by converting the test object to a proc if necessary and then
41
+ calling it in the context of a new instance of Blackstart::Scratchpad; if and
42
+ only if this raises an error (any exception that's a kind of StandardError),
43
+ Blackstart.run interprets it as a failed test.
44
+
45
+ After each test failure, Blackstart.run sends a puts message with failure
46
+ information to its second parameter, which is $stdout by default. The failure
47
+ information includes a description of the test (the test object converted to a
48
+ string) and a description of the error raised (its class, string
49
+ representation, and backtrace).
50
+
51
+ If a test raises a non-StandardError exception, Blackstart.run interprets it as
52
+ a system-level event or fundamental problem: it immediately raises the
53
+ exception and doesn't run any more tests.
54
+
55
+ The library provides little beyond this, but it supports more sophisticated
56
+ uses. Some examples follow.
57
+
58
+
59
+ - Exiting with the appropriate status
60
+
61
+ You may want your test program to exit with a successful status only if there
62
+ were no failures, perhaps so it can work as part of a testing script. You can
63
+ do this by using the object returned by Blackstart.run as the argument to
64
+ Kernel#exit. Blackstart.run returns false if there were failures, true
65
+ otherwise.
66
+
67
+
68
+ - Defining helpers
69
+
70
+ You may want to enable all your tests to assert something, generate test data,
71
+ or perform some other task by sending a message. You could implement this
72
+ statelessly in a singleton object or all instances of Object, for example, but
73
+ there's another option that may be more convenient. When Blackstart.run calls a
74
+ test proc, it sets the self object to a new instance of Blackstart::Scratchpad.
75
+ You can define instance methods in that class. For example:
76
+
77
+ class Blackstart::Scratchpad
78
+ def assert boolean
79
+ raise "assertion failed" unless boolean
80
+ end
81
+
82
+ def make_products
83
+ @cabbage = { :description => "head of cabbage", :price => 125 }
84
+ @orange = { :description => "Cara Cara navel orange", :price => 100 }
85
+ nil
86
+ end
87
+ end
88
+
89
+ All your test procs can use them by sending messages to self. These methods,
90
+ like the test proc itself, can see and modify instance variables and other
91
+ elements of the scratchpad's state. The scratchpad is disposable:
92
+ Blackstart.run discards it after the test is complete.
93
+
94
+
95
+ - Running code before and after each test
96
+
97
+ You may want to run code before and after each test -- for example, to create
98
+ objects needed in tests or clean up resources without requiring every test to
99
+ do these explicitly. You can do this by defining a custom
100
+ Blackstart::Scratchpad#instance_exec. For example:
101
+
102
+ class Blackstart::Scratchpad
103
+ def instance_exec(*)
104
+ puts "before test"
105
+ @variable = "example"
106
+ super
107
+ ensure
108
+ puts "after test"
109
+ end
110
+ end
111
+
112
+ Use this technique with care because the test proc could send instance_exec
113
+ messages to self.
114
+
115
+
116
+ - Building a test collection in stages
117
+
118
+ You may want to build a test collection in stages rather than all at once. This
119
+ can be done straightforwardly:
120
+
121
+ require "blackstart"
122
+
123
+ TESTS = []
124
+
125
+ TESTS.concat [
126
+ lambda {
127
+ unless "Hello, World!" == ["He", "", "o, Wor", "d!"].join("l")
128
+ raise "join did not work as expected"
129
+ end
130
+ }
131
+ ]
132
+
133
+ TESTS.concat [
134
+ lambda {
135
+ unless "sample" == "simple".gsub(/i/, "a")
136
+ raise "gsub did not work as expected"
137
+ end
138
+ }
139
+ ]
140
+
141
+ exit Blackstart.run TESTS
142
+
143
+ This pattern is useful if you want to define your tests in multiple files: you
144
+ create a collection with an agreed-upon name, load multiple files, each of
145
+ which adds test objects to that collection, and then run all the tests.
146
+
147
+
148
+ - Running tests in random order
149
+
150
+ To check if you have any tests that depend on other tests having run, or not
151
+ having run, earlier, you may want to run your tests in random order.
152
+ Blackstart.run runs a sequence of tests in order, but you can pass it a
153
+ randomly-ordered sequence. Array#shuffle may be helpful for this. If you use
154
+ Array#shuffle or something similar, you may also want to print the random seed
155
+ just before the tests are shuffled so you can rerun your tests in the same
156
+ order by setting the random seed.
157
+
158
+
159
+ - Reporting detailed test descriptions
160
+
161
+ After a test fails, Blackstart.run writes a string describing the test to the
162
+ output stream. It gets this string by sending a to_s message to the test
163
+ object. When the test object is an instance of Proc, the returned string
164
+ typically includes the file path and line number where it was defined: helpful,
165
+ but it won't be immediately clear what was being tested.
166
+
167
+ You can improve this by designing your own test objects. Blackstart.run does
168
+ not strictly need a sequence of procs; it also works with a sequence of objects
169
+ that convert to procs in response to to_proc messages. Your custom test objects
170
+ can respond to to_s with detailed descriptions instead of mere file paths and
171
+ line numbers.
172
+
173
+
174
+ - Handling failures differently
175
+
176
+ Blackstart.run handles each failure by sending a puts message to the output
177
+ stream, one of its parameters, with failure information. By default, the output
178
+ stream is $stdout, so the default behavior is to print unadorned failure
179
+ information to standard output. But you can specify any object as the output
180
+ stream -- even if it's not really a stream -- allowing you to handle failure
181
+ information however you want.
182
+
183
+
184
+ - Running multiple tests at the same time
185
+
186
+ Although Blackstart.run is written in a sequential style, it can run multiple
187
+ tests at the same time. Blackstart.run sends an each message with a block to
188
+ your test collection, expecting the block to be executed once for each test;
189
+ your test collection can execute the block in multiple threads concurrently.
190
+ For your program to work well in this mode, your tests and the output stream
191
+ must work correctly when used from multiple threads.
data/lib/blackstart.rb CHANGED
@@ -1,92 +1,76 @@
1
1
  ##
2
- # This module provides facilities for defining and running automated tests.
2
+ # This module provides facilities for running automated tests.
3
3
 
4
4
  module Blackstart
5
5
 
6
6
  ##
7
- # Calls the block and returns a string describing the error raised, if any.
7
+ # Yields to the block and returns a string describing the error raised, if
8
+ # any.
8
9
  #
9
- # In detail: This method sends call with no arguments to the object
10
- # representing the block. If that raises an error -- that is, an exception
11
- # whose class is StandardError or a descendent of StandardError -- this
12
- # method creates a string describing that error and returns it; the
13
- # description includes the error's class, message, and backtrace (if any). If
14
- # sending the message raises any other exception, this method raises that
15
- # exception. If sending the message does not raise an exception, this method
16
- # returns nil.
10
+ # In detail: This method yields to the block with no arguments. If that
11
+ # raises an error -- meaning an exception whose class is StandardError or a
12
+ # descendant of StandardError -- this method attempts to make and return a
13
+ # string describing that error; the description consists of the error's
14
+ # class, string representation, and backtrace (if any). If an exception is
15
+ # raised while making an error description, this method raises that
16
+ # exception. If yielding to the block raises a non-error exception, this
17
+ # method raises that exception. If yielding to the block does not raise an
18
+ # exception, this method returns nil.
17
19
 
18
- def self.vet &prc
19
- prc.call
20
+ def self.vet
21
+ yield
20
22
  nil
21
- rescue ::StandardError
23
+ rescue ::StandardError => e
22
24
  # Like IO#puts, separate lines with a line feed character rather than $\.
23
- ["#{$!.class}: #{$!.message}"].concat($!.backtrace || []).join "\n"
25
+ ["#{e.class}: #{e}"].concat(e.backtrace || []).join "\n"
24
26
  end
25
27
 
26
28
  ##
27
- # Allows the block to add procs to a collection, which it returns.
28
- #
29
- # In detail: This method sends call with one positional argument, an adder
30
- # object, to the object representing the block, director. This block can send
31
- # call messages to the adder to add objects representing the respective
32
- # blocks to the collection, sink. With each sending of call, the object to be
33
- # added will be either a proc (if call is sent with a block) or nil (if call
34
- # is sent without a block). If any non-block arguments are sent with the call
35
- # message, an exception is raised. To add the object, the adder sends a <<
36
- # message to sink with the object as the positional argument and no other
37
- # arguments. The adder returns nil in response to a call message if it
38
- # returns at all. This method returns sink.
39
- #
40
- # By default, sink is a new empty array.
41
-
42
- def self.add sink = [], &director
43
- director.call ::Kernel.lambda { |&prc|
44
- sink << prc
45
- nil
46
- }
47
- sink
48
- end
49
-
50
- ##
51
- # A class for the contexts (the self objects) of tests.
29
+ # A class for tests' disposable helper objects.
52
30
 
53
- class Context
31
+ class Scratchpad
54
32
  end
55
33
 
56
34
  ##
57
- # Runs the tests, writes any failure information to the output stream, and
35
+ # Runs tests, writes any failure information to the output stream, and
58
36
  # returns a boolean indicating whether there were no failures.
59
37
  #
60
- # In detail: This method expects the sequence of test objects, source, to
61
- # respond to an each message by yielding successive test objects to its
62
- # block. Each test is run by converting the test object to a proc and calling
63
- # it in the context of a new instance of Blackstart::Context. A failure is
64
- # when the test raises an error (that is, an exception whose class is
65
- # StandardError or a descendent of StandardError). After a failure, this
66
- # method sends a puts message to the output stream, ostream, with these
67
- # arguments (positional, in order): a string "FAILED TEST:", the test object
68
- # converted to a string via to_s, a string "...ERROR:", a string describing
69
- # the error, and an empty string. If a test raises any other exception, this
70
- # method immediately raises that exception. After it has run all the tests
71
- # and handled any failures, this method returns false if there were failures
72
- # or true otherwise.
38
+ # In detail: This method sends an each message with a block to the sequence
39
+ # of test objects, source, expecting it to yield to the block once for each
40
+ # test object in the sequence with that test object as the first argument.
41
+ # Each test involves creating a new scratchpad -- an instance of
42
+ # Blackstart::Scratchpad -- and sending an instance_exec message to it with
43
+ # the test object as the block, which converts the test object to a proc via
44
+ # to_proc if it's not already a proc. The resulting proc is called with no
45
+ # arguments and no block; the self object is set to the scratchpad. A failure
46
+ # is when the test (which includes any initial conversion to a proc) raises
47
+ # an error: an exception whose class is StandardError or a descendant of
48
+ # StandardError. After a failure, this method sends a puts message to the
49
+ # output stream, ostream, with no block and these arguments (positional, in
50
+ # order): a string "FAILED TEST:"; the test object converted to a string via
51
+ # to_s; a string "...ERROR:"; a string combining the class, string
52
+ # representation, and backtrace of the error; and an empty string. If a test
53
+ # raises a non-error exception, this method immediately raises that
54
+ # exception. After it has run all the tests and handled any failures, this
55
+ # method returns false if there were failures, true otherwise. This method
56
+ # immediately raises any exception raised outside of a test.
73
57
  #
74
58
  # By default, ostream is $stdout.
75
59
 
76
60
  def self.run source, ostream = $stdout
77
- success = true
78
- for tst in source
79
- # Convert to a proc and create a context outside the vet block so that
80
- # this method will raise any exception that results instead of
81
- # potentially treating it as a test failure.
82
- prc = ::Proc.new(&tst)
83
- context = Context.new
84
- if err_desc = vet { context.instance_exec(&prc) }
85
- success = false
86
- ostream.puts "FAILED TEST:", tst.to_s, "...ERROR:", err_desc.to_s, ""
61
+ no_failures = true
62
+ source.each do |tst|
63
+ # Create the scratchpad outside the vet block so that this method will
64
+ # raise any exception that results instead of potentially treating it as
65
+ # a test failure.
66
+ scratchpad = ::Blackstart::Scratchpad.new
67
+ if err_desc = vet { scratchpad.instance_exec(&tst) }
68
+ no_failures = false
69
+ ostream.puts "FAILED TEST:", tst.to_s, "...ERROR:", err_desc, ""
87
70
  end
71
+ nil
88
72
  end
89
- success
73
+ no_failures
90
74
  end
91
75
 
92
76
  end
@@ -0,0 +1,18 @@
1
+ module Blackstart
2
+ def self.vet: () ?{ () -> void } -> ::String?
3
+
4
+ class Scratchpad
5
+ end
6
+
7
+ type test_proc = ^() [self: Scratchpad] -> void
8
+ interface _ConvertsToTestProc
9
+ def to_proc: () -> test_proc
10
+ end
11
+ type test = (test_proc | _ConvertsToTestProc) & ::_ToS
12
+
13
+ interface _OutputStream
14
+ def puts: (::String, ::String, ::String, ::String, ::String) -> void
15
+ end
16
+
17
+ def self.run: (::_Each[test], ?_OutputStream) -> bool
18
+ end