Top Level Namespace
Defined Under Namespace
Modules: Cybertrain, Gen Classes: Article, ArticleRelation, Comment, CommentRelation
Instance Method Summary collapse
-
#assert(condition, message = "expected condition to be truthy")
Passes when the condition is truthy.
-
#assert_equal(expected, actual)
Passes when
expected == actual; fails withexpected X, got Y. -
#assert_includes(haystack, needle)
Passes when a String contains
needle.to_sor an Array includesneedle. -
#assert_nil(actual)
Passes when
actualis nil. -
#assert_raises(class_name = "") { ... } ⇒ String
Passes when the block raises.
-
#assert_redirected_to(response, path)
Passes when the response is a 3xx whose Location is exactly
path. -
#assert_response(response, expected)
assert_response res, :ok / :not_found / 201 …; :redirect accepts any 3xx.
-
#flunk(message)
Fails the current case.
-
#refute(condition, message = "expected condition to be falsy")
Passes when the condition is false or nil.
-
#test(name, &block)
Registers a test case, run in order by Cybertrain::Test.run!.
Instance Method Details
#assert(condition, message = "expected condition to be truthy")
This method returns an undefined value.
Passes when the condition is truthy.
112 113 114 115 |
# File 'cybertrain/test.rb', line 112 def assert(condition, = "expected condition to be truthy") Cybertrain::Test.count_assertion flunk() unless condition end |
#assert_equal(expected, actual)
This method returns an undefined value.
Passes when expected == actual; fails with expected X, got Y.
132 133 134 135 136 137 |
# File 'cybertrain/test.rb', line 132 def assert_equal(expected, actual) Cybertrain::Test.count_assertion unless expected == actual flunk("expected #{expected.inspect}, got #{actual.inspect}") end end |
#assert_includes(haystack, needle)
This method returns an undefined value.
Passes when a String contains needle.to_s or an Array includes
needle. Any other haystack (a Hash) fails.
include? on a polymorphic receiver mis-dispatches under Spinel 2026.09.12
when the argument is also polymorphic (and String#include? does so as soon
as SafeString is in the program), so the receiver is narrowed first and the
String case goes through #index.
162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'cybertrain/test.rb', line 162 def assert_includes(haystack, needle) Cybertrain::Test.count_assertion found = false case haystack when String n = needle.to_s found = !haystack.index(n).nil? when Array found = haystack.include?(needle) end unless found flunk("expected #{haystack.inspect} to include #{needle.inspect}") end end |
#assert_nil(actual)
This method returns an undefined value.
Passes when actual is nil.
143 144 145 146 |
# File 'cybertrain/test.rb', line 143 def assert_nil(actual) Cybertrain::Test.count_assertion flunk("expected nil, got #{actual.inspect}") unless actual.nil? end |
#assert_raises(class_name = "") { ... } ⇒ String
Passes when the block raises. class_name (optional) must be a substring of
the raised exception’s class name; the raised message is returned so callers
can assert on it. A failing assertion inside the block is not “the expected
exception”: AssertionFailed is a StandardError, so without its own clause
(first, it is the more specific class) the rescue below would swallow it and
the test would pass vacuously. It is re-raised so the enclosing test fails,
unless the caller names it (assert_raises("AssertionFailed") { ... } is how
the assertion helpers themselves are tested).
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'cybertrain/test.rb', line 193 def assert_raises(class_name = "") Cybertrain::Test.count_assertion = "" raised = false begin yield rescue StandardError => e # One clause only (a second `=> e` of another type in this yielding # method is untested under Spinel, NOTES rule 32). `to_s`: Class#name is # nil for an anonymous class under CRuby (`Class.new(StandardError)`), and # a NoMethodError from inside this rescue clause would replace the very # exception being asserted on. name = e.class.name.to_s if name.include?("AssertionFailed") && !(class_name != "" && name.include?(class_name)) raise e end raised = true = e. if class_name != "" && !name.include?(class_name) flunk("expected #{class_name} to be raised, got #{name}: #{e.}") end end flunk("expected #{class_name == "" ? "an exception" : class_name} to be raised") unless raised end |
#assert_redirected_to(response, path)
This method returns an undefined value.
Passes when the response is a 3xx whose Location is exactly path.
233 234 235 236 237 238 239 |
# File 'cybertrain/test/client.rb', line 233 def assert_redirected_to(response, path) Cybertrain::Test.count_assertion status = response.status flunk("expected a redirect to #{path.inspect}, got #{status}") if status < 300 || status > 399 location = response.header("Location") flunk("expected a redirect to #{path.inspect}, got #{location.inspect}") unless location == path end |
#assert_response(response, expected)
This method returns an undefined value.
assert_response res, :ok / :not_found / 201 …; :redirect accepts any 3xx.
Symbols: :ok, :success (2xx), :created, :no_content, :redirect
(3xx), :see_other, :bad_request, :forbidden, :not_found,
:unprocessable_entity, :error (500). Needs cybertrain/test/client.
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'cybertrain/test/client.rb', line 209 def assert_response(response, expected) Cybertrain::Test.count_assertion status = response.status case expected when Integer flunk("expected response #{expected}, got #{status}") unless status == expected when Symbol range = Cybertrain::Test::Client.status_range(expected) flunk("unknown status symbol #{expected.inspect}") if range[0] == 0 unless status >= range[0] && status <= range[1] flunk("expected response #{expected.inspect}, got #{status}") end else flunk("expected an Integer or Symbol status, got #{expected.inspect}") end end |
#flunk(message)
This method returns an undefined value.
Fails the current case.
103 104 105 |
# File 'cybertrain/test.rb', line 103 def flunk() raise Cybertrain::Test::AssertionFailed, end |
#refute(condition, message = "expected condition to be falsy")
This method returns an undefined value.
Passes when the condition is false or nil.
122 123 124 125 |
# File 'cybertrain/test.rb', line 122 def refute(condition, = "expected condition to be falsy") Cybertrain::Test.count_assertion flunk() if condition end |
#test(name, &block)
This method returns an undefined value.
Registers a test case, run in order by Cybertrain::Test.run!.
94 95 96 |
# File 'cybertrain/test.rb', line 94 def test(name, &block) Cybertrain::Test.register(name, block) end |