How to use end method of Test Package

Best Minitest_ruby code snippet using Test.end

test_minitest_unit.rb

Source:test_minitest_unit.rb Github

copy

Full Screen

1require 'pathname'2require 'minitest/metametameta'3module MyModule; end4class AnError < StandardError; include MyModule; end5class ImmutableString < String; def inspect; super.freeze; end; end6class TestMiniTestUnit < MetaMetaMetaTestCase7 parallelize_me!8 pwd = Pathname.new File.expand_path Dir.pwd9 basedir = Pathname.new(File.expand_path "lib/minitest") + 'mini'10 basedir = basedir.relative_path_from(pwd).to_s11 MINITEST_BASE_DIR = basedir[/\A\./] ? basedir : "./#{basedir}"12 BT_MIDDLE = ["#{MINITEST_BASE_DIR}/test.rb:161:in `each'",13 "#{MINITEST_BASE_DIR}/test.rb:158:in `each'",14 "#{MINITEST_BASE_DIR}/test.rb:139:in `run'",15 "#{MINITEST_BASE_DIR}/test.rb:106:in `run'"]16 def test_class_puke_with_assertion_failed17 exception = MiniTest::Assertion.new "Oh no!"18 exception.set_backtrace ["unhappy"]19 assert_equal 'F', @tu.puke('SomeClass', 'method_name', exception)20 assert_equal 1, @tu.failures21 assert_match(/^Failure.*Oh no!/m, @tu.report.first)22 assert_match("SomeClass#method_name [unhappy]", @tu.report.first)23 end24 def test_class_puke_with_assertion_failed_and_long_backtrace25 bt = (["test/test_some_class.rb:615:in `method_name'",26 "#{MINITEST_BASE_DIR}/unit.rb:140:in `assert_raises'",27 "test/test_some_class.rb:615:in `each'",28 "test/test_some_class.rb:614:in `test_method_name'",29 "#{MINITEST_BASE_DIR}/test.rb:165:in `__send__'"] +30 BT_MIDDLE +31 ["#{MINITEST_BASE_DIR}/test.rb:29"])32 bt = util_expand_bt bt33 ex_location = util_expand_bt(["test/test_some_class.rb:615"]).first34 exception = MiniTest::Assertion.new "Oh no!"35 exception.set_backtrace bt36 assert_equal 'F', @tu.puke('TestSomeClass', 'test_method_name', exception)37 assert_equal 1, @tu.failures38 assert_match(/^Failure.*Oh no!/m, @tu.report.first)39 assert_match("TestSomeClass#test_method_name [#{ex_location}]", @tu.report.first)40 end41 def test_class_puke_with_assertion_failed_and_user_defined_assertions42 bt = (["lib/test/my/util.rb:16:in `another_method_name'",43 "#{MINITEST_BASE_DIR}/unit.rb:140:in `assert_raises'",44 "lib/test/my/util.rb:15:in `block in assert_something'",45 "lib/test/my/util.rb:14:in `each'",46 "lib/test/my/util.rb:14:in `assert_something'",47 "test/test_some_class.rb:615:in `each'",48 "test/test_some_class.rb:614:in `test_method_name'",49 "#{MINITEST_BASE_DIR}/test.rb:165:in `__send__'"] +50 BT_MIDDLE +51 ["#{MINITEST_BASE_DIR}/test.rb:29"])52 bt = util_expand_bt bt53 ex_location = util_expand_bt(["test/test_some_class.rb:615"]).first54 exception = MiniTest::Assertion.new "Oh no!"55 exception.set_backtrace bt56 assert_equal 'F', @tu.puke('TestSomeClass', 'test_method_name', exception)57 assert_equal 1, @tu.failures58 assert_match(/^Failure.*Oh no!/m, @tu.report.first)59 assert_match("TestSomeClass#test_method_name [#{ex_location}]", @tu.report.first)60 end61 def test_class_puke_with_failure_and_flunk_in_backtrace62 exception = begin63 MiniTest::Unit::TestCase.new('fake tc').flunk64 rescue MiniTest::Assertion => failure65 failure66 end67 assert_equal 'F', @tu.puke('SomeClass', 'method_name', exception)68 refute @tu.report.any?{|line| line =~ /in .flunk/}69 end70 def test_class_puke_with_flunk_and_user_defined_assertions71 bt = (["lib/test/my/util.rb:16:in `flunk'",72 "#{MINITEST_BASE_DIR}/unit.rb:140:in `assert_raises'",73 "lib/test/my/util.rb:15:in `block in assert_something'",74 "lib/test/my/util.rb:14:in `each'",75 "lib/test/my/util.rb:14:in `assert_something'",76 "test/test_some_class.rb:615:in `each'",77 "test/test_some_class.rb:614:in `test_method_name'",78 "#{MINITEST_BASE_DIR}/test.rb:165:in `__send__'"] +79 BT_MIDDLE +80 ["#{MINITEST_BASE_DIR}/test.rb:29"])81 bt = util_expand_bt bt82 ex_location = util_expand_bt(["test/test_some_class.rb:615"]).first83 exception = MiniTest::Assertion.new "Oh no!"84 exception.set_backtrace bt85 assert_equal 'F', @tu.puke('TestSomeClass', 'test_method_name', exception)86 assert_equal 1, @tu.failures87 assert_match(/^Failure.*Oh no!/m, @tu.report.first)88 assert_match("TestSomeClass#test_method_name [#{ex_location}]", @tu.report.first)89 end90 def test_class_puke_with_non_failure_exception91 exception = Exception.new("Oh no again!")92 assert_equal 'E', @tu.puke('SomeClass', 'method_name', exception)93 assert_equal 1, @tu.errors94 assert_match(/^Exception.*Oh no again!/m, @tu.report.first)95 end96 def test_filter_backtrace97 # this is a semi-lame mix of relative paths.98 # I cheated by making the autotest parts not have ./99 bt = (["lib/autotest.rb:571:in `add_exception'",100 "test/test_autotest.rb:62:in `test_add_exception'",101 "#{MINITEST_BASE_DIR}/test.rb:165:in `__send__'"] +102 BT_MIDDLE +103 ["#{MINITEST_BASE_DIR}/test.rb:29",104 "test/test_autotest.rb:422"])105 bt = util_expand_bt bt106 ex = ["lib/autotest.rb:571:in `add_exception'",107 "test/test_autotest.rb:62:in `test_add_exception'"]108 ex = util_expand_bt ex109 fu = MiniTest::filter_backtrace(bt)110 assert_equal ex, fu111 end112 def test_filter_backtrace_all_unit113 bt = (["#{MINITEST_BASE_DIR}/test.rb:165:in `__send__'"] +114 BT_MIDDLE +115 ["#{MINITEST_BASE_DIR}/test.rb:29"])116 ex = bt.clone117 fu = MiniTest::filter_backtrace(bt)118 assert_equal ex, fu119 end120 def test_filter_backtrace_unit_starts121 bt = (["#{MINITEST_BASE_DIR}/test.rb:165:in `__send__'"] +122 BT_MIDDLE +123 ["#{MINITEST_BASE_DIR}/mini/test.rb:29",124 "-e:1"])125 bt = util_expand_bt bt126 ex = ["-e:1"]127 fu = MiniTest::filter_backtrace bt128 assert_equal ex, fu129 end130 def test_default_runner_is_minitest_unit131 assert_instance_of MiniTest::Unit, MiniTest::Unit.runner132 end133 def test_passed_eh_teardown_good134 test_class = Class.new MiniTest::Unit::TestCase do135 def teardown; assert true; end136 def test_omg; assert true; end137 end138 test = test_class.new :test_omg139 test.run @tu140 assert test.passed?141 end142 def test_passed_eh_teardown_skipped143 test_class = Class.new MiniTest::Unit::TestCase do144 def teardown; assert true; end145 def test_omg; skip "bork"; end146 end147 test = test_class.new :test_omg148 test.run @tu149 assert test.passed?150 end151 def test_passed_eh_teardown_flunked152 test_class = Class.new MiniTest::Unit::TestCase do153 def teardown; flunk; end154 def test_omg; assert true; end155 end156 test = test_class.new :test_omg157 test.run @tu158 refute test.passed?159 end160 def util_expand_bt bt161 if RUBY_VERSION >= '1.9.0' then162 bt.map { |f| (f =~ /^\./) ? File.expand_path(f) : f }163 else164 bt165 end166 end167end168class TestMiniTestUnitInherited < MetaMetaMetaTestCase169 def with_overridden_include170 Class.class_eval do171 def inherited_with_hacks klass172 throw :inherited_hook173 end174 alias inherited_without_hacks inherited175 alias inherited inherited_with_hacks176 alias IGNORE_ME! inherited # 1.8 bug. god I love venture bros177 end178 yield179 ensure180 Class.class_eval do181 alias inherited inherited_without_hacks182 undef_method :inherited_with_hacks183 undef_method :inherited_without_hacks184 end185 refute_respond_to Class, :inherited_with_hacks186 refute_respond_to Class, :inherited_without_hacks187 end188 def test_inherited_hook_plays_nice_with_others189 with_overridden_include do190 assert_throws :inherited_hook do191 Class.new MiniTest::Unit::TestCase192 end193 end194 end195end196class TestMiniTestRunner < MetaMetaMetaTestCase197 # do not parallelize this suite... it just can't handle it.198 def test_class_test_suites199 @assertion_count = 0200 tc = Class.new(MiniTest::Unit::TestCase)201 assert_equal 1, MiniTest::Unit::TestCase.test_suites.size202 assert_equal [tc], MiniTest::Unit::TestCase.test_suites203 end204 def test_run_test205 Class.new MiniTest::Unit::TestCase do206 attr_reader :foo207 def run_test name208 @foo = "hi mom!"209 super210 @foo = "okay"211 end212 def test_something213 assert_equal "hi mom!", foo214 end215 end216 expected = clean <<-EOM217 .218 Finished tests in 0.00219 1 tests, 1 assertions, 0 failures, 0 errors, 0 skips220 EOM221 assert_report expected222 end223 def test_run_error224 Class.new MiniTest::Unit::TestCase do225 def test_something226 assert true227 end228 def test_error229 raise "unhandled exception"230 end231 end232 expected = clean <<-EOM233 E.234 Finished tests in 0.00235 1) Error:236 #<Class:0xXXX>#test_error:237 RuntimeError: unhandled exception238 FILE:LINE:in \`test_error\'239 2 tests, 1 assertions, 0 failures, 1 errors, 0 skips240 EOM241 assert_report expected242 end243 def test_run_error_teardown244 Class.new MiniTest::Unit::TestCase do245 def test_something246 assert true247 end248 def teardown249 raise "unhandled exception"250 end251 end252 expected = clean <<-EOM253 E254 Finished tests in 0.00255 1) Error:256 #<Class:0xXXX>#test_something:257 RuntimeError: unhandled exception258 FILE:LINE:in \`teardown\'259 1 tests, 1 assertions, 0 failures, 1 errors, 0 skips260 EOM261 assert_report expected262 end263 def test_run_failing264 Class.new MiniTest::Unit::TestCase do265 def test_something266 assert true267 end268 def test_failure269 assert false270 end271 end272 expected = clean <<-EOM273 F.274 Finished tests in 0.00275 1) Failure:276 #<Class:0xXXX>#test_failure [FILE:LINE]:277 Failed assertion, no message given.278 2 tests, 2 assertions, 1 failures, 0 errors, 0 skips279 EOM280 assert_report expected281 end282 def test_run_failing_filtered283 Class.new MiniTest::Unit::TestCase do284 def test_something285 assert true286 end287 def test_failure288 assert false289 end290 end291 expected = clean <<-EOM292 .293 Finished tests in 0.00294 1 tests, 1 assertions, 0 failures, 0 errors, 0 skips295 EOM296 assert_report expected, %w[--name /some|thing/ --seed 42]297 end298 def assert_filtering name, expected, a = false299 args = %W[--name #{name} --seed 42]300 alpha = Class.new MiniTest::Unit::TestCase do301 define_method :test_something do302 assert a303 end304 end305 Object.const_set(:Alpha, alpha)306 beta = Class.new MiniTest::Unit::TestCase do307 define_method :test_something do308 assert true309 end310 end311 Object.const_set(:Beta, beta)312 assert_report expected, args313 ensure314 Object.send :remove_const, :Alpha315 Object.send :remove_const, :Beta316 end317 def test_run_filtered_including_suite_name318 expected = clean <<-EOM319 .320 Finished tests in 0.00321 1 tests, 1 assertions, 0 failures, 0 errors, 0 skips322 EOM323 assert_filtering "/Beta#test_something/", expected324 end325 def test_run_filtered_including_suite_name_string326 expected = clean <<-EOM327 .328 Finished tests in 0.00329 1 tests, 1 assertions, 0 failures, 0 errors, 0 skips330 EOM331 assert_filtering "Beta#test_something", expected332 end333 def test_run_filtered_string_method_only334 expected = clean <<-EOM335 ..336 Finished tests in 0.00337 2 tests, 2 assertions, 0 failures, 0 errors, 0 skips338 EOM339 assert_filtering "test_something", expected, :pass340 end341 def test_run_passing342 Class.new MiniTest::Unit::TestCase do343 def test_something344 assert true345 end346 end347 expected = clean <<-EOM348 .349 Finished tests in 0.00350 1 tests, 1 assertions, 0 failures, 0 errors, 0 skips351 EOM352 assert_report expected353 end354 def test_run_skip355 Class.new MiniTest::Unit::TestCase do356 def test_something357 assert true358 end359 def test_skip360 skip "not yet"361 end362 end363 expected = clean <<-EOM364 S.365 Finished tests in 0.00366 2 tests, 1 assertions, 0 failures, 0 errors, 1 skips367 EOM368 assert_report expected369 end370 def test_run_skip_verbose371 Class.new MiniTest::Unit::TestCase do372 def test_something373 assert true374 end375 def test_skip376 skip "not yet"377 end378 end379 expected = clean <<-EOM380 #<Class:0xXXX>#test_skip = 0.00 s = S381 #<Class:0xXXX>#test_something = 0.00 s = .382 Finished tests in 0.00383 1) Skipped:384 #<Class:0xXXX>#test_skip [FILE:LINE]:385 not yet386 2 tests, 1 assertions, 0 failures, 0 errors, 1 skips387 EOM388 assert_report expected, %w[--seed 42 --verbose]389 end390 def test_run_with_other_runner391 MiniTest::Unit.runner = Class.new MiniTest::Unit do392 def _run_suite suite, type393 suite.before_suite # Run once before each suite394 super suite, type395 end396 end.new397 Class.new MiniTest::Unit::TestCase do398 def self.name; "wacky!" end399 def self.before_suite400 MiniTest::Unit.output.puts "Running #{self.name} tests"401 @@foo = 1402 end403 def test_something404 assert_equal 1, @@foo405 end406 def test_something_else407 assert_equal 1, @@foo408 end409 end410 expected = clean <<-EOM411 Running wacky! tests412 ..413 Finished tests in 0.00414 2 tests, 2 assertions, 0 failures, 0 errors, 0 skips415 EOM416 assert_report expected417 end418 require 'monitor'419 class Latch420 def initialize count = 1421 @count = count422 @lock = Monitor.new423 @cv = @lock.new_cond424 end425 def release426 @lock.synchronize do427 @count -= 1 if @count > 0428 @cv.broadcast if @count == 0429 end430 end431 def await432 @lock.synchronize { @cv.wait_while { @count > 0 } }433 end434 end435 def test_parallel_each_size436 assert_equal 0, ParallelEach.new([]).size437 end438 def test_run_parallel439 skip "I don't have ParallelEach debugged yet" if maglev?440 test_count = 2441 test_latch = Latch.new test_count442 main_latch = Latch.new443 thread = Thread.new {444 Thread.current.abort_on_exception = true445 # This latch waits until both test latches have been released. Both446 # latches can't be released unless done in separate threads because447 # `main_latch` keeps the test method from finishing.448 test_latch.await449 main_latch.release450 }451 Class.new MiniTest::Unit::TestCase do452 parallelize_me!453 test_count.times do |i|454 define_method :"test_wait_on_main_thread_#{i}" do455 test_latch.release456 # This latch blocks until the "main thread" releases it. The main457 # thread can't release this latch until both test latches have458 # been released. This forces the latches to be released in separate459 # threads.460 main_latch.await461 assert true462 end463 end464 end465 expected = clean <<-EOM466 ..467 Finished tests in 0.00468 2 tests, 2 assertions, 0 failures, 0 errors, 0 skips469 EOM470 assert_report expected471 assert thread.join472 end473end474class TestMiniTestUnitOrder < MetaMetaMetaTestCase475 # do not parallelize this suite... it just can't handle it.476 def test_before_setup477 call_order = []478 Class.new MiniTest::Unit::TestCase do479 define_method :setup do480 super()481 call_order << :setup482 end483 define_method :before_setup do484 call_order << :before_setup485 end486 def test_omg; assert true; end487 end488 with_output do489 @tu.run %w[--seed 42]490 end491 expected = [:before_setup, :setup]492 assert_equal expected, call_order493 end494 def test_after_teardown495 call_order = []496 Class.new MiniTest::Unit::TestCase do497 define_method :teardown do498 super()499 call_order << :teardown500 end501 define_method :after_teardown do502 call_order << :after_teardown503 end504 def test_omg; assert true; end505 end506 with_output do507 @tu.run %w[--seed 42]508 end509 expected = [:teardown, :after_teardown]510 assert_equal expected, call_order511 end512 def test_all_teardowns_are_guaranteed_to_run513 call_order = []514 Class.new MiniTest::Unit::TestCase do515 define_method :after_teardown do516 super()517 call_order << :after_teardown518 raise519 end520 define_method :teardown do521 super()522 call_order << :teardown523 raise524 end525 define_method :before_teardown do526 super()527 call_order << :before_teardown528 raise529 end530 def test_omg; assert true; end531 end532 with_output do533 @tu.run %w[--seed 42]534 end535 expected = [:before_teardown, :teardown, :after_teardown]536 assert_equal expected, call_order537 end538 def test_setup_and_teardown_survive_inheritance539 call_order = []540 parent = Class.new MiniTest::Unit::TestCase do541 define_method :setup do542 call_order << :setup_method543 end544 define_method :teardown do545 call_order << :teardown_method546 end547 define_method :test_something do548 call_order << :test549 end550 end551 _ = Class.new parent552 with_output do553 @tu.run %w[--seed 42]554 end555 # Once for the parent class, once for the child556 expected = [:setup_method, :test, :teardown_method] * 2557 assert_equal expected, call_order558 end559end560class TestMiniTestUnitTestCase < MiniTest::Unit::TestCase561 # do not call parallelize_me! - teardown accesses @tc._assertions562 # which is not threadsafe. Nearly every method in here is an563 # assertion test so it isn't worth splitting it out further.564 RUBY18 = ! defined? Encoding565 def setup566 super567 MiniTest::Unit::TestCase.reset568 @tc = MiniTest::Unit::TestCase.new 'fake tc'569 @zomg = "zomg ponies!"570 @assertion_count = 1571 end572 def teardown573 assert_equal(@assertion_count, @tc._assertions,574 "expected #{@assertion_count} assertions to be fired during the test, not #{@tc._assertions}") if @tc.passed?575 end576 def non_verbose577 orig_verbose = $VERBOSE578 $VERBOSE = false579 yield580 ensure581 $VERBOSE = orig_verbose582 end583 def test_assert584 @assertion_count = 2585 @tc.assert_equal true, @tc.assert(true), "returns true on success"586 end587 def test_assert__triggered588 util_assert_triggered "Failed assertion, no message given." do589 @tc.assert false590 end591 end592 def test_assert__triggered_message593 util_assert_triggered @zomg do594 @tc.assert false, @zomg595 end596 end597 def test_assert_empty598 @assertion_count = 2599 @tc.assert_empty []600 end601 def test_assert_empty_triggered602 @assertion_count = 2603 util_assert_triggered "Expected [1] to be empty." do604 @tc.assert_empty [1]605 end606 end607 def test_assert_equal608 @tc.assert_equal 1, 1609 end610 def test_assert_equal_different_collection_array_hex_invisible611 object1 = Object.new612 object2 = Object.new613 msg = "No visible difference in the Array#inspect output.614 You should look at the implementation of #== on Array or its members.615 [#<Object:0xXXXXXX>]".gsub(/^ +/, "")616 util_assert_triggered msg do617 @tc.assert_equal [object1], [object2]618 end619 end620 def test_assert_equal_different_collection_hash_hex_invisible621 h1, h2 = {}, {}622 h1[1] = Object.new623 h2[1] = Object.new624 msg = "No visible difference in the Hash#inspect output.625 You should look at the implementation of #== on Hash or its members.626 {1=>#<Object:0xXXXXXX>}".gsub(/^ +/, "")627 util_assert_triggered msg do628 @tc.assert_equal h1, h2629 end630 end631 def test_assert_equal_different_diff_deactivated632 skip "https://github.com/MagLev/maglev/issues/209" if maglev?633 without_diff do634 util_assert_triggered util_msg("haha" * 10, "blah" * 10) do635 o1 = "haha" * 10636 o2 = "blah" * 10637 @tc.assert_equal o1, o2638 end639 end640 end641 def test_assert_equal_different_hex642 c = Class.new do643 def initialize s; @name = s; end644 end645 o1 = c.new "a"646 o2 = c.new "b"647 msg = "--- expected648 +++ actual649 @@ -1 +1 @@650 -#<#<Class:0xXXXXXX>:0xXXXXXX @name=\"a\">651 +#<#<Class:0xXXXXXX>:0xXXXXXX @name=\"b\">652 ".gsub(/^ +/, "")653 util_assert_triggered msg do654 @tc.assert_equal o1, o2655 end656 end657 def test_assert_equal_different_hex_invisible658 o1 = Object.new659 o2 = Object.new660 msg = "No visible difference in the Object#inspect output.661 You should look at the implementation of #== on Object or its members.662 #<Object:0xXXXXXX>".gsub(/^ +/, "")663 util_assert_triggered msg do664 @tc.assert_equal o1, o2665 end666 end667 def test_assert_equal_different_long668 msg = "--- expected669 +++ actual670 @@ -1 +1 @@671 -\"hahahahahahahahahahahahahahahahahahahaha\"672 +\"blahblahblahblahblahblahblahblahblahblah\"673 ".gsub(/^ +/, "")674 util_assert_triggered msg do675 o1 = "haha" * 10676 o2 = "blah" * 10677 @tc.assert_equal o1, o2678 end679 end680 def test_assert_equal_different_long_invisible681 msg = "No visible difference in the String#inspect output.682 You should look at the implementation of #== on String or its members.683 \"blahblahblahblahblahblahblahblahblahblah\"".gsub(/^ +/, "")684 util_assert_triggered msg do685 o1 = "blah" * 10686 o2 = "blah" * 10687 def o1.== o688 false689 end690 @tc.assert_equal o1, o2691 end692 end693 def test_assert_equal_different_long_msg694 msg = "message.695 --- expected696 +++ actual697 @@ -1 +1 @@698 -\"hahahahahahahahahahahahahahahahahahahaha\"699 +\"blahblahblahblahblahblahblahblahblahblah\"700 ".gsub(/^ +/, "")701 util_assert_triggered msg do702 o1 = "haha" * 10703 o2 = "blah" * 10704 @tc.assert_equal o1, o2, "message"705 end706 end707 def test_assert_equal_different_short708 util_assert_triggered util_msg(1, 2) do709 @tc.assert_equal 1, 2710 end711 end712 def test_assert_equal_different_short_msg713 util_assert_triggered util_msg(1, 2, "message") do714 @tc.assert_equal 1, 2, "message"715 end716 end717 def test_assert_equal_different_short_multiline718 msg = "--- expected\n+++ actual\n@@ -1,2 +1,2 @@\n \"a\n-b\"\n+c\"\n"719 util_assert_triggered msg do720 @tc.assert_equal "a\nb", "a\nc"721 end722 end723 def test_assert_in_delta724 @tc.assert_in_delta 0.0, 1.0 / 1000, 0.1725 end726 def test_delta_consistency727 @tc.assert_in_delta 0, 1, 1728 util_assert_triggered "Expected |0 - 1| (1) to not be <= 1." do729 @tc.refute_in_delta 0, 1, 1730 end731 end732 def test_assert_in_delta_triggered733 x = maglev? ? "9.999999xxxe-07" : "1.0e-06"734 util_assert_triggered "Expected |0.0 - 0.001| (0.001) to be <= #{x}." do735 @tc.assert_in_delta 0.0, 1.0 / 1000, 0.000001736 end737 end738 def test_assert_in_epsilon739 @assertion_count = 10740 @tc.assert_in_epsilon 10000, 9991741 @tc.assert_in_epsilon 9991, 10000742 @tc.assert_in_epsilon 1.0, 1.001743 @tc.assert_in_epsilon 1.001, 1.0744 @tc.assert_in_epsilon 10000, 9999.1, 0.0001745 @tc.assert_in_epsilon 9999.1, 10000, 0.0001746 @tc.assert_in_epsilon 1.0, 1.0001, 0.0001747 @tc.assert_in_epsilon 1.0001, 1.0, 0.0001748 @tc.assert_in_epsilon(-1, -1)749 @tc.assert_in_epsilon(-10000, -9991)750 end751 def test_epsilon_consistency752 @tc.assert_in_epsilon 1.0, 1.001753 msg = "Expected |1.0 - 1.001| (0.000999xxx) to not be <= 0.001."754 util_assert_triggered msg do755 @tc.refute_in_epsilon 1.0, 1.001756 end757 end758 def test_assert_in_epsilon_triggered759 util_assert_triggered 'Expected |10000 - 9990| (10) to be <= 9.99.' do760 @tc.assert_in_epsilon 10000, 9990761 end762 end763 def test_assert_in_epsilon_triggered_negative_case764 x = (RUBY18 and not maglev?) ? "0.1" : "0.100000xxx"765 y = maglev? ? "0.100000xxx" : "0.1"766 util_assert_triggered "Expected |-1.1 - -1| (#{x}) to be <= #{y}." do767 @tc.assert_in_epsilon(-1.1, -1, 0.1)768 end769 end770 def test_assert_includes771 @assertion_count = 2772 @tc.assert_includes [true], true773 end774 def test_assert_includes_triggered775 @assertion_count = 3776 e = @tc.assert_raises MiniTest::Assertion do777 @tc.assert_includes [true], false778 end779 expected = "Expected [true] to include false."780 assert_equal expected, e.message781 end782 def test_assert_instance_of783 @tc.assert_instance_of String, "blah"784 end785 def test_assert_instance_of_triggered786 util_assert_triggered 'Expected "blah" to be an instance of Array, not String.' do787 @tc.assert_instance_of Array, "blah"788 end789 end790 def test_assert_kind_of791 @tc.assert_kind_of String, "blah"792 end793 def test_assert_kind_of_triggered794 util_assert_triggered 'Expected "blah" to be a kind of Array, not String.' do795 @tc.assert_kind_of Array, "blah"796 end797 end798 def test_assert_match799 @assertion_count = 2800 @tc.assert_match(/\w+/, "blah blah blah")801 end802 def test_assert_match_matcher_object803 @assertion_count = 2804 pattern = Object.new805 def pattern.=~(other) true end806 @tc.assert_match pattern, 5807 end808 def test_assert_match_matchee_to_str809 @assertion_count = 2810 obj = Object.new811 def obj.to_str; "blah" end812 @tc.assert_match "blah", obj813 end814 def test_assert_match_object_triggered815 @assertion_count = 2816 pattern = Object.new817 def pattern.=~(other) false end818 def pattern.inspect; "[Object]" end819 util_assert_triggered 'Expected [Object] to match 5.' do820 @tc.assert_match pattern, 5821 end822 end823 def test_assert_match_triggered824 @assertion_count = 2825 util_assert_triggered 'Expected /\d+/ to match "blah blah blah".' do826 @tc.assert_match(/\d+/, "blah blah blah")827 end828 end829 def test_assert_nil830 @tc.assert_nil nil831 end832 def test_assert_nil_triggered833 util_assert_triggered 'Expected 42 to be nil.' do834 @tc.assert_nil 42835 end836 end837 def test_assert_operator838 @tc.assert_operator 2, :>, 1839 end840 def test_assert_operator_bad_object841 bad = Object.new842 def bad.==(other) true end843 @tc.assert_operator bad, :equal?, bad844 end845 def test_assert_operator_triggered846 util_assert_triggered "Expected 2 to be < 1." do847 @tc.assert_operator 2, :<, 1848 end849 end850 def test_assert_output_both851 @assertion_count = 2852 @tc.assert_output "yay", "blah" do853 print "yay"854 $stderr.print "blah"855 end856 end857 def test_assert_output_both_regexps858 @assertion_count = 4859 @tc.assert_output(/y.y/, /bl.h/) do860 print "yay"861 $stderr.print "blah"862 end863 end864 def test_assert_output_err865 @tc.assert_output nil, "blah" do866 $stderr.print "blah"867 end868 end869 def test_assert_output_neither870 @assertion_count = 0871 @tc.assert_output do872 # do nothing873 end874 end875 def test_assert_output_out876 @tc.assert_output "blah" do877 print "blah"878 end879 end880 def test_assert_output_triggered_both881 util_assert_triggered util_msg("blah", "blah blah", "In stderr") do882 @tc.assert_output "yay", "blah" do883 print "boo"884 $stderr.print "blah blah"885 end886 end887 end888 def test_assert_output_triggered_err889 util_assert_triggered util_msg("blah", "blah blah", "In stderr") do890 @tc.assert_output nil, "blah" do891 $stderr.print "blah blah"892 end893 end894 end895 def test_assert_output_triggered_out896 util_assert_triggered util_msg("blah", "blah blah", "In stdout") do897 @tc.assert_output "blah" do898 print "blah blah"899 end900 end901 end902 def test_assert_predicate903 @tc.assert_predicate "", :empty?904 end905 def test_assert_predicate_triggered906 util_assert_triggered 'Expected "blah" to be empty?.' do907 @tc.assert_predicate "blah", :empty?908 end909 end910 def test_assert_raises911 @tc.assert_raises RuntimeError do912 raise "blah"913 end914 end915 def test_assert_raises_module916 @tc.assert_raises MyModule do917 raise AnError918 end919 end920 ##921 # *sigh* This is quite an odd scenario, but it is from real (albeit922 # ugly) test code in ruby-core:923 #924 # http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=29259925 def test_assert_raises_skip926 @assertion_count = 0927 util_assert_triggered "skipped", MiniTest::Skip do928 @tc.assert_raises ArgumentError do929 begin930 raise "blah"931 rescue932 skip "skipped"933 end934 end935 end936 end937 def test_assert_raises_triggered_different938 e = assert_raises MiniTest::Assertion do939 @tc.assert_raises RuntimeError do940 raise SyntaxError, "icky"941 end942 end943 expected = clean <<-EOM.chomp944 [RuntimeError] exception expected, not945 Class: <SyntaxError>946 Message: <\"icky\">947 ---Backtrace---948 FILE:LINE:in \`test_assert_raises_triggered_different\'949 ---------------950 EOM951 actual = e.message.gsub(/^.+:\d+/, 'FILE:LINE')952 actual.gsub!(/block \(\d+ levels\) in /, '') if RUBY_VERSION >= '1.9.0'953 assert_equal expected, actual954 end955 def test_assert_raises_triggered_different_msg956 e = assert_raises MiniTest::Assertion do957 @tc.assert_raises RuntimeError, "XXX" do958 raise SyntaxError, "icky"959 end960 end961 expected = clean <<-EOM962 XXX.963 [RuntimeError] exception expected, not964 Class: <SyntaxError>965 Message: <\"icky\">966 ---Backtrace---967 FILE:LINE:in \`test_assert_raises_triggered_different_msg\'968 ---------------969 EOM970 actual = e.message.gsub(/^.+:\d+/, 'FILE:LINE')971 actual.gsub!(/block \(\d+ levels\) in /, '') if RUBY_VERSION >= '1.9.0'972 assert_equal expected.chomp, actual973 end974 def test_assert_raises_triggered_none975 e = assert_raises MiniTest::Assertion do976 @tc.assert_raises MiniTest::Assertion do977 # do nothing978 end979 end980 expected = "MiniTest::Assertion expected but nothing was raised."981 assert_equal expected, e.message982 end983 def test_assert_raises_triggered_none_msg984 e = assert_raises MiniTest::Assertion do985 @tc.assert_raises MiniTest::Assertion, "XXX" do986 # do nothing987 end988 end989 expected = "XXX.\nMiniTest::Assertion expected but nothing was raised."990 assert_equal expected, e.message991 end992 def test_assert_raises_triggered_subclass993 e = assert_raises MiniTest::Assertion do994 @tc.assert_raises StandardError do995 raise AnError996 end997 end998 expected = clean <<-EOM.chomp999 [StandardError] exception expected, not1000 Class: <AnError>1001 Message: <\"AnError\">1002 ---Backtrace---1003 FILE:LINE:in \`test_assert_raises_triggered_subclass\'1004 ---------------1005 EOM1006 actual = e.message.gsub(/^.+:\d+/, 'FILE:LINE')1007 actual.gsub!(/block \(\d+ levels\) in /, '') if RUBY_VERSION >= '1.9.0'1008 assert_equal expected, actual1009 end1010 def test_assert_respond_to1011 @tc.assert_respond_to "blah", :empty?1012 end1013 def test_assert_respond_to_triggered1014 util_assert_triggered 'Expected "blah" (String) to respond to #rawr!.' do1015 @tc.assert_respond_to "blah", :rawr!1016 end1017 end1018 def test_assert_same1019 @assertion_count = 31020 o = "blah"1021 @tc.assert_same 1, 11022 @tc.assert_same :blah, :blah1023 @tc.assert_same o, o1024 end1025 def test_assert_same_triggered1026 @assertion_count = 21027 util_assert_triggered 'Expected 2 (oid=N) to be the same as 1 (oid=N).' do1028 @tc.assert_same 1, 21029 end1030 s1 = "blah"1031 s2 = "blah"1032 util_assert_triggered 'Expected "blah" (oid=N) to be the same as "blah" (oid=N).' do1033 @tc.assert_same s1, s21034 end1035 end1036 def test_assert_send1037 @tc.assert_send [1, :<, 2]1038 end1039 def test_assert_send_bad1040 util_assert_triggered "Expected 1.>(*[2]) to return true." do1041 @tc.assert_send [1, :>, 2]1042 end1043 end1044 def test_assert_silent1045 @assertion_count = 21046 @tc.assert_silent do1047 # do nothing1048 end1049 end1050 def test_assert_silent_triggered_err1051 util_assert_triggered util_msg("", "blah blah", "In stderr") do1052 @tc.assert_silent do1053 $stderr.print "blah blah"1054 end1055 end1056 end1057 def test_assert_silent_triggered_out1058 @assertion_count = 21059 util_assert_triggered util_msg("", "blah blah", "In stdout") do1060 @tc.assert_silent do1061 print "blah blah"1062 end1063 end1064 end1065 def test_assert_throws1066 @tc.assert_throws :blah do1067 throw :blah1068 end1069 end1070 def test_assert_throws_different1071 util_assert_triggered 'Expected :blah to have been thrown, not :not_blah.' do1072 @tc.assert_throws :blah do1073 throw :not_blah1074 end1075 end1076 end1077 def test_assert_throws_unthrown1078 util_assert_triggered 'Expected :blah to have been thrown.' do1079 @tc.assert_throws :blah do1080 # do nothing1081 end1082 end1083 end1084 def test_capture_io1085 @assertion_count = 01086 non_verbose do1087 out, err = capture_io do1088 puts 'hi'1089 $stderr.puts 'bye!'1090 end1091 assert_equal "hi\n", out1092 assert_equal "bye!\n", err1093 end1094 end1095 def test_capture_subprocess_io1096 @assertion_count = 01097 skip "Dunno why but the parallel run of this fails"1098 non_verbose do1099 out, err = capture_subprocess_io do1100 system("echo 'hi'")1101 system("echo 'bye!' 1>&2")1102 end1103 assert_equal "hi\n", out1104 assert_equal "bye!\n", err1105 end1106 end1107 def test_class_asserts_match_refutes1108 @assertion_count = 01109 methods = MiniTest::Assertions.public_instance_methods1110 methods.map! { |m| m.to_s } if Symbol === methods.first1111 # These don't have corresponding refutes _on purpose_. They're1112 # useless and will never be added, so don't bother.1113 ignores = %w[assert_output assert_raises assert_send1114 assert_silent assert_throws]1115 # These are test/unit methods. I'm not actually sure why they're still here1116 ignores += %w[assert_no_match assert_not_equal assert_not_nil1117 assert_not_same assert_nothing_raised1118 assert_nothing_thrown assert_raise]1119 asserts = methods.grep(/^assert/).sort - ignores1120 refutes = methods.grep(/^refute/).sort - ignores1121 assert_empty refutes.map { |n| n.sub(/^refute/, 'assert') } - asserts1122 assert_empty asserts.map { |n| n.sub(/^assert/, 'refute') } - refutes1123 end1124 def test_flunk1125 util_assert_triggered 'Epic Fail!' do1126 @tc.flunk1127 end1128 end1129 def test_flunk_message1130 util_assert_triggered @zomg do1131 @tc.flunk @zomg1132 end1133 end1134 def test_message1135 @assertion_count = 01136 assert_equal "blah2.", @tc.message { "blah2" }.call1137 assert_equal "blah2.", @tc.message("") { "blah2" }.call1138 assert_equal "blah1.\nblah2.", @tc.message(:blah1) { "blah2" }.call1139 assert_equal "blah1.\nblah2.", @tc.message("blah1") { "blah2" }.call1140 message = proc { "blah1" }1141 assert_equal "blah1.\nblah2.", @tc.message(message) { "blah2" }.call1142 message = @tc.message { "blah1" }1143 assert_equal "blah1.\nblah2.", @tc.message(message) { "blah2" }.call1144 end1145 def test_message_message1146 util_assert_triggered "whoops.\nExpected: 1\n Actual: 2" do1147 @tc.assert_equal 1, 2, message { "whoops" }1148 end1149 end1150 def test_message_lambda1151 util_assert_triggered "whoops.\nExpected: 1\n Actual: 2" do1152 @tc.assert_equal 1, 2, lambda { "whoops" }1153 end1154 end1155 def test_message_deferred1156 @assertion_count, var = 0, nil1157 msg = message { var = "blah" }1158 assert_nil var1159 msg.call1160 assert_equal "blah", var1161 end1162 def test_pass1163 @tc.pass1164 end1165 def test_prints1166 printer = Class.new { extend MiniTest::Assertions }1167 @tc.assert_equal '"test"', printer.mu_pp(ImmutableString.new 'test')1168 end1169 def test_refute1170 @assertion_count = 21171 @tc.assert_equal false, @tc.refute(false), "returns false on success"1172 end1173 def test_refute_empty1174 @assertion_count = 21175 @tc.refute_empty [1]1176 end1177 def test_refute_empty_triggered1178 @assertion_count = 21179 util_assert_triggered "Expected [] to not be empty." do1180 @tc.refute_empty []1181 end1182 end1183 def test_refute_equal1184 @tc.refute_equal "blah", "yay"1185 end1186 def test_refute_equal_triggered1187 util_assert_triggered 'Expected "blah" to not be equal to "blah".' do1188 @tc.refute_equal "blah", "blah"1189 end1190 end1191 def test_refute_in_delta1192 @tc.refute_in_delta 0.0, 1.0 / 1000, 0.0000011193 end1194 def test_refute_in_delta_triggered1195 x = maglev? ? "0.100000xxx" : "0.1"1196 util_assert_triggered "Expected |0.0 - 0.001| (0.001) to not be <= #{x}." do1197 @tc.refute_in_delta 0.0, 1.0 / 1000, 0.11198 end1199 end1200 def test_refute_in_epsilon1201 @tc.refute_in_epsilon 10000, 9990-11202 end1203 def test_refute_in_epsilon_triggered1204 util_assert_triggered 'Expected |10000 - 9990| (10) to not be <= 10.0.' do1205 @tc.refute_in_epsilon 10000, 99901206 fail1207 end1208 end1209 def test_refute_includes1210 @assertion_count = 21211 @tc.refute_includes [true], false1212 end1213 def test_refute_includes_triggered1214 @assertion_count = 31215 e = @tc.assert_raises MiniTest::Assertion do1216 @tc.refute_includes [true], true1217 end1218 expected = "Expected [true] to not include true."1219 assert_equal expected, e.message1220 end1221 def test_refute_instance_of1222 @tc.refute_instance_of Array, "blah"1223 end1224 def test_refute_instance_of_triggered1225 util_assert_triggered 'Expected "blah" to not be an instance of String.' do1226 @tc.refute_instance_of String, "blah"1227 end1228 end1229 def test_refute_kind_of1230 @tc.refute_kind_of Array, "blah"1231 end1232 def test_refute_kind_of_triggered1233 util_assert_triggered 'Expected "blah" to not be a kind of String.' do1234 @tc.refute_kind_of String, "blah"1235 end1236 end1237 def test_refute_match1238 @assertion_count = 21239 @tc.refute_match(/\d+/, "blah blah blah")1240 end1241 def test_refute_match_matcher_object1242 @assertion_count = 21243 @tc.refute_match Object.new, 5 # default #=~ returns false1244 end1245 def test_refute_match_object_triggered1246 @assertion_count = 21247 pattern = Object.new1248 def pattern.=~(other) true end1249 def pattern.inspect; "[Object]" end1250 util_assert_triggered 'Expected [Object] to not match 5.' do1251 @tc.refute_match pattern, 51252 end1253 end1254 def test_refute_match_triggered1255 @assertion_count = 21256 util_assert_triggered 'Expected /\w+/ to not match "blah blah blah".' do1257 @tc.refute_match(/\w+/, "blah blah blah")1258 end1259 end1260 def test_refute_nil1261 @tc.refute_nil 421262 end1263 def test_refute_nil_triggered1264 util_assert_triggered 'Expected nil to not be nil.' do1265 @tc.refute_nil nil1266 end1267 end1268 def test_refute_predicate1269 @tc.refute_predicate "42", :empty?1270 end1271 def test_refute_predicate_triggered1272 util_assert_triggered 'Expected "" to not be empty?.' do1273 @tc.refute_predicate "", :empty?1274 end1275 end1276 def test_refute_operator1277 @tc.refute_operator 2, :<, 11278 end1279 def test_refute_operator_bad_object1280 bad = Object.new1281 def bad.==(other) true end1282 @tc.refute_operator true, :equal?, bad1283 end1284 def test_refute_operator_triggered1285 util_assert_triggered "Expected 2 to not be > 1." do1286 @tc.refute_operator 2, :>, 11287 end1288 end1289 def test_refute_respond_to1290 @tc.refute_respond_to "blah", :rawr!1291 end1292 def test_refute_respond_to_triggered1293 util_assert_triggered 'Expected "blah" to not respond to empty?.' do1294 @tc.refute_respond_to "blah", :empty?1295 end1296 end1297 def test_refute_same1298 @tc.refute_same 1, 21299 end1300 def test_refute_same_triggered1301 util_assert_triggered 'Expected 1 (oid=N) to not be the same as 1 (oid=N).' do1302 @tc.refute_same 1, 11303 end1304 end1305 def test_skip1306 @assertion_count = 01307 util_assert_triggered "haha!", MiniTest::Skip do1308 @tc.skip "haha!"1309 end1310 end1311 def test_test_methods_random1312 @assertion_count = 01313 sample_test_case = Class.new MiniTest::Unit::TestCase do1314 def self.test_order; :random; end1315 def test_test1; assert "does not matter" end1316 def test_test2; assert "does not matter" end1317 def test_test3; assert "does not matter" end1318 end1319 srand 421320 expected = case1321 when maglev? then1322 %w(test_test2 test_test3 test_test1)1323 else1324 %w(test_test2 test_test1 test_test3)1325 end1326 assert_equal expected, sample_test_case.test_methods1327 end1328 def test_test_methods_sorted1329 @assertion_count = 01330 sample_test_case = Class.new MiniTest::Unit::TestCase do1331 def self.test_order; :sorted end1332 def test_test3; assert "does not matter" end1333 def test_test2; assert "does not matter" end1334 def test_test1; assert "does not matter" end1335 end1336 expected = %w(test_test1 test_test2 test_test3)1337 assert_equal expected, sample_test_case.test_methods1338 end1339 def test_i_suck_and_my_tests_are_order_dependent_bang_sets_test_order_alpha1340 @assertion_count = 01341 shitty_test_case = Class.new MiniTest::Unit::TestCase1342 shitty_test_case.i_suck_and_my_tests_are_order_dependent!1343 assert_equal :alpha, shitty_test_case.test_order1344 end1345 def test_i_suck_and_my_tests_are_order_dependent_bang_does_not_warn1346 @assertion_count = 01347 shitty_test_case = Class.new MiniTest::Unit::TestCase1348 def shitty_test_case.test_order ; :lol end1349 assert_silent do1350 shitty_test_case.i_suck_and_my_tests_are_order_dependent!1351 end1352 end1353 def util_assert_triggered expected, klass = MiniTest::Assertion1354 e = assert_raises klass do1355 yield1356 end1357 msg = e.message.sub(/(---Backtrace---).*/m, '\1')1358 msg.gsub!(/\(oid=[-0-9]+\)/, '(oid=N)')1359 msg.gsub!(/(\d\.\d{6})\d+/, '\1xxx') # normalize: ruby version, impl, platform1360 assert_equal expected, msg1361 end1362 def util_msg exp, act, msg = nil1363 s = "Expected: #{exp.inspect}\n Actual: #{act.inspect}"1364 s = "#{msg}.\n#{s}" if msg1365 s1366 end1367 def without_diff1368 old_diff = MiniTest::Assertions.diff1369 MiniTest::Assertions.diff = nil1370 yield1371 ensure1372 MiniTest::Assertions.diff = old_diff1373 end1374end1375class TestMiniTestGuard < MiniTest::Unit::TestCase1376 parallelize_me!1377 def test_mri_eh1378 assert self.class.mri? "ruby blah"1379 assert self.mri? "ruby blah"1380 end1381 def test_jruby_eh1382 assert self.class.jruby? "java"1383 assert self.jruby? "java"1384 end1385 def test_rubinius_eh1386 assert self.class.rubinius? "rbx"1387 assert self.rubinius? "rbx"1388 end1389 def test_windows_eh1390 assert self.class.windows? "mswin"1391 assert self.windows? "mswin"1392 end1393end1394class TestMiniTestUnitRecording < MetaMetaMetaTestCase1395 # do not parallelize this suite... it just can't handle it.1396 def assert_run_record(*expected, &block)1397 def @tu.record suite, method, assertions, time, error1398 recording[method] << error1399 end1400 def @tu.recording1401 @recording ||= Hash.new { |h,k| h[k] = [] }1402 end1403 MiniTest::Unit.runner = @tu1404 Class.new MiniTest::Unit::TestCase, &block1405 with_output do1406 @tu.run1407 end1408 recorded = @tu.recording.fetch("test_method").map(&:class)1409 assert_equal expected, recorded1410 end1411 def test_record_passing1412 assert_run_record NilClass do1413 def test_method1414 assert true1415 end1416 end1417 end1418 def test_record_failing1419 assert_run_record MiniTest::Assertion do1420 def test_method1421 assert false1422 end1423 end1424 end1425 def test_record_error1426 assert_run_record RuntimeError do1427 def test_method1428 raise "unhandled exception"1429 end1430 end1431 end1432 def test_record_error_teardown1433 assert_run_record NilClass, RuntimeError do1434 def test_method1435 assert true1436 end1437 def teardown1438 raise "unhandled exception"1439 end1440 end1441 end1442 def test_record_error_in_test_and_teardown1443 assert_run_record AnError, RuntimeError do1444 def test_method1445 raise AnError1446 end1447 def teardown1448 raise "unhandled exception"1449 end1450 end1451 end1452 def test_record_skip1453 assert_run_record MiniTest::Skip do1454 def test_method1455 skip "not yet"1456 end1457 end1458 end1459end...

Full Screen

Full Screen

callback_spec.rb

Source:callback_spec.rb Github

copy

Full Screen

...4#5require File.expand_path(File.join(File.dirname(__FILE__), "spec_helper"))6describe "Callback" do7# module LibC8# extend FFI::Library9# callback :qsort_cmp, [ :pointer, :pointer ], :int10# attach_function :qsort, [ :pointer, :int, :int, :qsort_cmp ], :int11# end12# it "arguments get passed correctly" do13# p = MemoryPointer.new(:int, 2)14# p.put_array_of_int32(0, [ 1 , 2 ])15# args = []16# cmp = proc do |p1, p2| args.push(p1.get_int(0)); args.push(p2.get_int(0)); 0; end17# # this is a bit dodgey, as it relies on qsort passing the args in order18# LibC.qsort(p, 2, 4, cmp)19# args.should == [ 1, 2 ]20# end21#22# it "Block can be substituted for Callback as last argument" do23# p = MemoryPointer.new(:int, 2)24# p.put_array_of_int32(0, [ 1 , 2 ])25# args = []26# # this is a bit dodgey, as it relies on qsort passing the args in order27# LibC.qsort(p, 2, 4) do |p1, p2|28# args.push(p1.get_int(0))29# args.push(p2.get_int(0))30# 031# end32# args.should == [ 1, 2 ]33# end34 module LibTest35 extend FFI::Library36 ffi_lib TestLibrary::PATH37 class S8F32S32 < FFI::Struct38 layout :s8, :char, :f32, :float, :s32, :int39 end40 callback :cbVrS8, [ ], :char41 callback :cbVrU8, [ ], :uchar42 callback :cbVrS16, [ ], :short43 callback :cbVrU16, [ ], :ushort44 callback :cbVrS32, [ ], :int45 callback :cbVrU32, [ ], :uint46 callback :cbVrL, [ ], :long47 callback :cbVrUL, [ ], :ulong48 callback :cbVrS64, [ ], :long_long49 callback :cbVrU64, [ ], :ulong_long50 callback :cbVrP, [], :pointer51 callback :cbVrZ, [], :bool52 callback :cbCrV, [ :char ], :void53 callback :cbSrV, [ :short ], :void54 callback :cbIrV, [ :int ], :void55 callback :cbLrV, [ :long ], :void56 callback :cbULrV, [ :ulong ], :void57 callback :cbLrV, [ :long_long ], :void58 callback :cbVrT, [ ], S8F32S32.by_value59 callback :cbTrV, [ S8F32S32.by_value ], :void60 callback :cbYrV, [ S8F32S32.ptr ], :void61 callback :cbVrY, [ ], S8F32S32.ptr62 attach_function :testCallbackVrS8, :testClosureVrB, [ :cbVrS8 ], :char63 attach_function :testCallbackVrU8, :testClosureVrB, [ :cbVrU8 ], :uchar64 attach_function :testCallbackVrS16, :testClosureVrS, [ :cbVrS16 ], :short65 attach_function :testCallbackVrU16, :testClosureVrS, [ :cbVrU16 ], :ushort66 attach_function :testCallbackVrS32, :testClosureVrI, [ :cbVrS32 ], :int67 attach_function :testCallbackVrU32, :testClosureVrI, [ :cbVrU32 ], :uint68 attach_function :testCallbackVrL, :testClosureVrL, [ :cbVrL ], :long69 attach_function :testCallbackVrZ, :testClosureVrZ, [ :cbVrZ ], :bool70 attach_function :testCallbackVrUL, :testClosureVrL, [ :cbVrUL ], :ulong71 attach_function :testCallbackVrS64, :testClosureVrLL, [ :cbVrS64 ], :long_long72 attach_function :testCallbackVrU64, :testClosureVrLL, [ :cbVrU64 ], :ulong_long73 attach_function :testCallbackVrP, :testClosureVrP, [ :cbVrP ], :pointer74 attach_function :testCallbackVrY, :testClosureVrP, [ :cbVrY ], S8F32S32.ptr75 attach_function :testCallbackVrT, :testClosureVrT, [ :cbVrT ], S8F32S32.by_value76 attach_function :testCallbackTrV, :testClosureTrV, [ :cbTrV, S8F32S32.ptr ], :void77 attach_variable :cbVrS8, :gvar_pointer, :cbVrS878 attach_variable :pVrS8, :gvar_pointer, :pointer79 attach_function :testGVarCallbackVrS8, :testClosureVrB, [ :pointer ], :char80 attach_function :testOptionalCallbackCrV, :testOptionalClosureBrV, [ :cbCrV, :char ], :void81 end82 it "returning :char (0)" do83 LibTest.testCallbackVrS8 { 0 }.should == 084 end85 it "returning :char (127)" do86 LibTest.testCallbackVrS8 { 127 }.should == 12787 end88 it "returning :char (-128)" do89 LibTest.testCallbackVrS8 { -128 }.should == -12890 end91 # test wrap around92 it "returning :char (128)" do93 LibTest.testCallbackVrS8 { 128 }.should == -12894 end95 it "returning :char (255)" do96 LibTest.testCallbackVrS8 { 0xff }.should == -197 end98 it "returning :uchar (0)" do99 LibTest.testCallbackVrU8 { 0 }.should == 0100 end101 it "returning :uchar (0xff)" do102 LibTest.testCallbackVrU8 { 0xff }.should == 0xff103 end104 it "returning :uchar (-1)" do105 LibTest.testCallbackVrU8 { -1 }.should == 0xff106 end107 it "returning :uchar (128)" do108 LibTest.testCallbackVrU8 { 128 }.should == 128109 end110 it "returning :uchar (-128)" do111 LibTest.testCallbackVrU8 { -128 }.should == 128112 end113 it "returning :short (0)" do114 LibTest.testCallbackVrS16 { 0 }.should == 0115 end116 it "returning :short (0x7fff)" do117 LibTest.testCallbackVrS16 { 0x7fff }.should == 0x7fff118 end119 # test wrap around120 it "returning :short (0x8000)" do121 LibTest.testCallbackVrS16 { 0x8000 }.should == -0x8000122 end123 it "returning :short (0xffff)" do124 LibTest.testCallbackVrS16 { 0xffff }.should == -1125 end126 it "returning :ushort (0)" do127 LibTest.testCallbackVrU16 { 0 }.should == 0128 end129 it "returning :ushort (0x7fff)" do130 LibTest.testCallbackVrU16 { 0x7fff }.should == 0x7fff131 end132 it "returning :ushort (0x8000)" do133 LibTest.testCallbackVrU16 { 0x8000 }.should == 0x8000134 end135 it "returning :ushort (0xffff)" do136 LibTest.testCallbackVrU16 { 0xffff }.should == 0xffff137 end138 it "returning :ushort (-1)" do139 LibTest.testCallbackVrU16 { -1 }.should == 0xffff140 end141 it "returning :int (0)" do142 LibTest.testCallbackVrS32 { 0 }.should == 0143 end144 it "returning :int (0x7fffffff)" do145 LibTest.testCallbackVrS32 { 0x7fffffff }.should == 0x7fffffff146 end147 # test wrap around148 it "returning :int (-0x80000000)" do149 LibTest.testCallbackVrS32 { -0x80000000 }.should == -0x80000000150 end151 it "returning :int (-1)" do152 LibTest.testCallbackVrS32 { -1 }.should == -1153 end154 it "returning :uint (0)" do155 LibTest.testCallbackVrU32 { 0 }.should == 0156 end157 it "returning :uint (0x7fffffff)" do158 LibTest.testCallbackVrU32 { 0x7fffffff }.should == 0x7fffffff159 end160 # test wrap around161 it "returning :uint (0x80000000)" do162 LibTest.testCallbackVrU32 { 0x80000000 }.should == 0x80000000163 end164 it "returning :uint (0xffffffff)" do165 LibTest.testCallbackVrU32 { 0xffffffff }.should == 0xffffffff166 end167 it "returning :uint (-1)" do168 LibTest.testCallbackVrU32 { -1 }.should == 0xffffffff169 end170 it "returning :long (0)" do171 LibTest.testCallbackVrL { 0 }.should == 0172 end173 it "returning :long (0x7fffffff)" do174 LibTest.testCallbackVrL { 0x7fffffff }.should == 0x7fffffff175 end176 # test wrap around177 it "returning :long (-0x80000000)" do178 LibTest.testCallbackVrL { -0x80000000 }.should == -0x80000000179 end180 it "returning :long (-1)" do181 LibTest.testCallbackVrL { -1 }.should == -1182 end183 it "returning :ulong (0)" do184 LibTest.testCallbackVrUL { 0 }.should == 0185 end186 it "returning :ulong (0x7fffffff)" do187 LibTest.testCallbackVrUL { 0x7fffffff }.should == 0x7fffffff188 end189 # test wrap around190 it "returning :ulong (0x80000000)" do191 LibTest.testCallbackVrUL { 0x80000000 }.should == 0x80000000192 end193 it "returning :ulong (0xffffffff)" do194 LibTest.testCallbackVrUL { 0xffffffff }.should == 0xffffffff195 end196 it "Callback returning :ulong (-1)" do197 if FFI::Platform::LONG_SIZE == 32198 LibTest.testCallbackVrUL { -1 }.should == 0xffffffff199 else200 LibTest.testCallbackVrUL { -1 }.should == 0xffffffffffffffff201 end202 end203 it "returning :long_long (0)" do204 LibTest.testCallbackVrS64 { 0 }.should == 0205 end206 it "returning :long_long (0x7fffffffffffffff)" do207 LibTest.testCallbackVrS64 { 0x7fffffffffffffff }.should == 0x7fffffffffffffff208 end209 # test wrap around210 it "returning :long_long (-0x8000000000000000)" do211 LibTest.testCallbackVrS64 { -0x8000000000000000 }.should == -0x8000000000000000212 end213 it "returning :long_long (-1)" do214 LibTest.testCallbackVrS64 { -1 }.should == -1215 end216 it "returning bool" do217 LibTest.testCallbackVrZ { true }.should be_true218 end219 it "returning :pointer (nil)" do220 LibTest.testCallbackVrP { nil }.null?.should be_true221 end222 it "returning :pointer (MemoryPointer)" do223 p = FFI::MemoryPointer.new :long224 LibTest.testCallbackVrP { p }.should == p225 end226 it "returning struct by value" do227 s = LibTest::S8F32S32.new228 s[:s8] = 0x12229 s[:s32] = 0x1eefbeef230 s[:f32] = 1.234567231 ret = LibTest.testCallbackVrT { s }232 ret[:s8].should == s[:s8]233 ret[:f32].should == s[:f32]234 ret[:s32].should == s[:s32]235 end236 it "struct by value parameter" do237 s = LibTest::S8F32S32.new238 s[:s8] = 0x12239 s[:s32] = 0x1eefbeef240 s[:f32] = 1.234567241 s2 = LibTest::S8F32S32.new242 LibTest.testCallbackTrV(s) do |struct|243 s2[:s8] = struct[:s8]244 s2[:f32] = struct[:f32]245 s2[:s32] = struct[:s32]246 end247 end248 249 it "global variable" do250 proc = Proc.new { 0x1e }251 LibTest.cbVrS8 = proc252 LibTest.testGVarCallbackVrS8(LibTest.pVrS8).should == 0x1e253 end254 describe "When the callback is considered optional by the underlying library" do255 it "should handle receiving 'nil' in place of the closure" do256 LibTest.testOptionalCallbackCrV(nil, 13)257 end258 end259 describe 'when inlined' do260 it 'could be anonymous' do261 module LibTest262 extend FFI::Library263 ffi_lib TestLibrary::PATH264 attach_function :testAnonymousCallbackVrS8, :testClosureVrB, [ callback([ ], :char) ], :char265 end266 LibTest.testAnonymousCallbackVrS8 { 0 }.should == 0267 end268 end269 describe "as return value" do270 it "should not blow up when a callback is defined that returns a callback" do271 module LibTest272 extend FFI::Library273 ffi_lib TestLibrary::PATH274 callback :cb_return_type_1, [ :short ], :short275 callback :cb_lookup_1, [ :short ], :cb_return_type_1276 attach_function :testReturnsCallback_1, :testReturnsClosure, [ :cb_lookup_1, :short ], :cb_return_type_1277 end 278 end279 it "should return a callback" do280 module LibTest281 extend FFI::Library282 ffi_lib TestLibrary::PATH283 callback :cb_return_type, [ :int ], :int284 callback :cb_lookup, [ ], :cb_return_type285 attach_function :testReturnsCallback, :testReturnsClosure, [ :cb_lookup, :int ], :int286 end 287 lookup_proc_called = false288 return_proc_called = false289 return_proc = Proc.new do |a|290 return_proc_called = true291 a * 2292 end293 lookup_proc = Proc.new do294 lookup_proc_called = true295 return_proc296 end297 val = LibTest.testReturnsCallback(lookup_proc, 0x1234)298 val.should == 0x1234 * 2299 lookup_proc_called.should be_true300 return_proc_called.should be_true301 end302 it "should return a method callback" do303 module LibTest304 extend FFI::Library305 ffi_lib TestLibrary::PATH306 callback :cb_return_type, [ :int ], :int307 callback :cb_lookup, [ ], :cb_return_type308 attach_function :testReturnsCallback_2, :testReturnsClosure, [ :cb_lookup, :int ], :int309 end310 module MethodCallback311 def self.lookup312 method(:perform)313 end314 def self.perform num315 num * 2316 end317 end318 LibTest.testReturnsCallback_2(MethodCallback.method(:lookup), 0x1234).should == 0x2468319 end320 it 'should not blow up when a callback takes a callback as argument' do321 module LibTest322 extend FFI::Library323 ffi_lib TestLibrary::PATH324 callback :cb_argument, [ :int ], :int325 callback :cb_with_cb_argument, [ :cb_argument, :int ], :int326 attach_function :testCallbackAsArgument_2, :testArgumentClosure, [ :cb_with_cb_argument, :int ], :int327 end 328 end329 it 'should be able to use the callback argument' do330 module LibTest331 extend FFI::Library332 ffi_lib TestLibrary::PATH333 callback :cb_argument, [ :int ], :int334 callback :cb_with_cb_argument, [ :cb_argument, :int ], :int335 attach_function :testCallbackAsArgument, :testArgumentClosure, [ :cb_with_cb_argument, :cb_argument, :int ], :int336 end 337 callback_arg_called = false338 callback_with_callback_arg_called = false 339 callback_arg = Proc.new do |val|340 callback_arg_called = true341 val * 2342 end343 callback_with_callback_arg = Proc.new do |cb, val|344 callback_with_callback_arg_called = true345 cb.call(val)346 end347 val = LibTest.testCallbackAsArgument(callback_with_callback_arg, callback_arg, 0xff1)348 val.should == 0xff1 * 2349 callback_arg_called.should be_true350 callback_with_callback_arg_called.should be_true 351 end352 it 'function returns callable object' do353 module LibTest354 extend FFI::Library355 ffi_lib TestLibrary::PATH356 callback :funcptr, [ :int ], :int357 attach_function :testReturnsFunctionPointer, [ ], :funcptr358 end359 f = LibTest.testReturnsFunctionPointer360 f.call(3).should == 6361 end362 end363end364describe "Callback with " do365 #366 # Test callbacks that take an argument, returning void367 #368 module LibTest369 extend FFI::Library370 ffi_lib TestLibrary::PATH371 class S8F32S32 < FFI::Struct372 layout :s8, :char, :f32, :float, :s32, :int373 end374 callback :cbS8rV, [ :char ], :void375 callback :cbU8rV, [ :uchar ], :void376 callback :cbS16rV, [ :short ], :void377 callback :cbU16rV, [ :ushort ], :void378 callback :cbZrV, [ :bool ], :void379 callback :cbS32rV, [ :int ], :void380 callback :cbU32rV, [ :uint ], :void381 callback :cbLrV, [ :long ], :void382 callback :cbULrV, [ :ulong ], :void383 callback :cbArV, [ :string ], :void384 callback :cbPrV, [ :pointer], :void385 callback :cbYrV, [ S8F32S32.ptr ], :void386 callback :cbS64rV, [ :long_long ], :void387 attach_function :testCallbackCrV, :testClosureBrV, [ :cbS8rV, :char ], :void388 attach_function :testCallbackU8rV, :testClosureBrV, [ :cbU8rV, :uchar ], :void389 attach_function :testCallbackSrV, :testClosureSrV, [ :cbS16rV, :short ], :void390 attach_function :testCallbackU16rV, :testClosureSrV, [ :cbU16rV, :ushort ], :void391 attach_function :testCallbackZrV, :testClosureZrV, [ :cbZrV, :bool ], :void392 attach_function :testCallbackIrV, :testClosureIrV, [ :cbS32rV, :int ], :void393 attach_function :testCallbackU32rV, :testClosureIrV, [ :cbU32rV, :uint ], :void394 attach_function :testCallbackLrV, :testClosureLrV, [ :cbLrV, :long ], :void395 attach_function :testCallbackULrV, :testClosureULrV, [ :cbULrV, :ulong ], :void396 attach_function :testCallbackLLrV, :testClosureLLrV, [ :cbS64rV, :long_long ], :void397 attach_function :testCallbackArV, :testClosurePrV, [ :cbArV, :string ], :void398 attach_function :testCallbackPrV, :testClosurePrV, [ :cbPrV, :pointer], :void399 attach_function :testCallbackYrV, :testClosurePrV, [ :cbYrV, S8F32S32.in ], :void400 end401 it "function with Callback plus another arg should raise error if no arg given" do402 lambda { LibTest.testCallbackCrV { |*a| }}.should raise_error403 end404 it ":char (0) argument" do405 v = 0xdeadbeef406 LibTest.testCallbackCrV(0) { |i| v = i }407 v.should == 0408 end409 it ":char (127) argument" do410 v = 0xdeadbeef411 LibTest.testCallbackCrV(127) { |i| v = i }412 v.should == 127413 end414 it ":char (-128) argument" do415 v = 0xdeadbeef416 LibTest.testCallbackCrV(-128) { |i| v = i }417 v.should == -128418 end419 it ":char (-1) argument" do420 v = 0xdeadbeef421 LibTest.testCallbackCrV(-1) { |i| v = i }422 v.should == -1423 end424 it ":uchar (0) argument" do425 v = 0xdeadbeef426 LibTest.testCallbackU8rV(0) { |i| v = i }427 v.should == 0428 end429 it ":uchar (127) argument" do430 v = 0xdeadbeef431 LibTest.testCallbackU8rV(127) { |i| v = i }432 v.should == 127433 end434 it ":uchar (128) argument" do435 v = 0xdeadbeef436 LibTest.testCallbackU8rV(128) { |i| v = i }437 v.should == 128438 end439 it ":uchar (255) argument" do440 v = 0xdeadbeef441 LibTest.testCallbackU8rV(255) { |i| v = i }442 v.should == 255443 end444 it ":short (0) argument" do445 v = 0xdeadbeef446 LibTest.testCallbackSrV(0) { |i| v = i }447 v.should == 0448 end449 it ":short (0x7fff) argument" do450 v = 0xdeadbeef451 LibTest.testCallbackSrV(0x7fff) { |i| v = i }452 v.should == 0x7fff453 end454 it ":short (-0x8000) argument" do455 v = 0xdeadbeef456 LibTest.testCallbackSrV(-0x8000) { |i| v = i }457 v.should == -0x8000458 end459 it ":short (-1) argument" do460 v = 0xdeadbeef461 LibTest.testCallbackSrV(-1) { |i| v = i }462 v.should == -1463 end464 it ":ushort (0) argument" do465 v = 0xdeadbeef466 LibTest.testCallbackU16rV(0) { |i| v = i }467 v.should == 0468 end469 it ":ushort (0x7fff) argument" do470 v = 0xdeadbeef471 LibTest.testCallbackU16rV(0x7fff) { |i| v = i }472 v.should == 0x7fff473 end474 it ":ushort (0x8000) argument" do475 v = 0xdeadbeef476 LibTest.testCallbackU16rV(0x8000) { |i| v = i }477 v.should == 0x8000478 end479 it ":ushort (0xffff) argument" do480 v = 0xdeadbeef481 LibTest.testCallbackU16rV(0xffff) { |i| v = i }482 v.should == 0xffff483 end484 it ":bool (true) argument" do485 v = false486 LibTest.testCallbackZrV(true) { |i| v = i }487 v.should be_true488 end489 it ":int (0) argument" do490 v = 0xdeadbeef491 LibTest.testCallbackIrV(0) { |i| v = i }492 v.should == 0493 end494 it ":int (0x7fffffff) argument" do495 v = 0xdeadbeef496 LibTest.testCallbackIrV(0x7fffffff) { |i| v = i }497 v.should == 0x7fffffff498 end499 it ":int (-0x80000000) argument" do500 v = 0xdeadbeef501 LibTest.testCallbackIrV(-0x80000000) { |i| v = i }502 v.should == -0x80000000503 end504 it ":int (-1) argument" do505 v = 0xdeadbeef506 LibTest.testCallbackIrV(-1) { |i| v = i }507 v.should == -1508 end509 it ":uint (0) argument" do510 v = 0xdeadbeef511 LibTest.testCallbackU32rV(0) { |i| v = i }512 v.should == 0513 end514 it ":uint (0x7fffffff) argument" do515 v = 0xdeadbeef516 LibTest.testCallbackU32rV(0x7fffffff) { |i| v = i }517 v.should == 0x7fffffff518 end519 it ":uint (0x80000000) argument" do520 v = 0xdeadbeef521 LibTest.testCallbackU32rV(0x80000000) { |i| v = i }522 v.should == 0x80000000523 end524 it ":uint (0xffffffff) argument" do525 v = 0xdeadbeef526 LibTest.testCallbackU32rV(0xffffffff) { |i| v = i }527 v.should == 0xffffffff528 end529 it ":long (0) argument" do530 v = 0xdeadbeef531 LibTest.testCallbackLrV(0) { |i| v = i }532 v.should == 0533 end534 it ":long (0x7fffffff) argument" do535 v = 0xdeadbeef536 LibTest.testCallbackLrV(0x7fffffff) { |i| v = i }537 v.should == 0x7fffffff538 end539 it ":long (-0x80000000) argument" do540 v = 0xdeadbeef541 LibTest.testCallbackLrV(-0x80000000) { |i| v = i }542 v.should == -0x80000000543 end544 it ":long (-1) argument" do545 v = 0xdeadbeef546 LibTest.testCallbackLrV(-1) { |i| v = i }547 v.should == -1548 end549 it ":ulong (0) argument" do550 v = 0xdeadbeef551 LibTest.testCallbackULrV(0) { |i| v = i }552 v.should == 0553 end554 it ":ulong (0x7fffffff) argument" do555 v = 0xdeadbeef556 LibTest.testCallbackULrV(0x7fffffff) { |i| v = i }557 v.should == 0x7fffffff558 end559 it ":ulong (0x80000000) argument" do560 v = 0xdeadbeef561 LibTest.testCallbackULrV(0x80000000) { |i| v = i }562 v.should == 0x80000000563 end564 it ":ulong (0xffffffff) argument" do565 v = 0xdeadbeef566 LibTest.testCallbackULrV(0xffffffff) { |i| v = i }567 v.should == 0xffffffff568 end569 it ":long_long (0) argument" do570 v = 0xdeadbeef571 LibTest.testCallbackLLrV(0) { |i| v = i }572 v.should == 0573 end574 it ":long_long (0x7fffffffffffffff) argument" do575 v = 0xdeadbeef576 LibTest.testCallbackLLrV(0x7fffffffffffffff) { |i| v = i }577 v.should == 0x7fffffffffffffff578 end579 it ":long_long (-0x8000000000000000) argument" do580 v = 0xdeadbeef581 LibTest.testCallbackLLrV(-0x8000000000000000) { |i| v = i }582 v.should == -0x8000000000000000583 end584 it ":long_long (-1) argument" do585 v = 0xdeadbeef586 LibTest.testCallbackLLrV(-1) { |i| v = i }587 v.should == -1588 end589 it ":string argument" do590 v = nil591 LibTest.testCallbackArV("Hello, World") { |i| v = i }592 v.should == "Hello, World"593 end594 it ":string (nil) argument" do595 v = "Hello, World"596 LibTest.testCallbackArV(nil) { |i| v = i }597 v.should be_nil598 end599 it ":pointer argument" do600 v = nil601 magic = FFI::Pointer.new(0xdeadbeef)602 LibTest.testCallbackPrV(magic) { |i| v = i }603 v.should == magic604 end605 it ":pointer (nil) argument" do606 v = "Hello, World"607 LibTest.testCallbackPrV(nil) { |i| v = i }608 v.should == FFI::Pointer::NULL609 end610 it "struct by reference argument" do611 v = nil612 magic = LibTest::S8F32S32.new613 LibTest.testCallbackYrV(magic) { |i| v = i }614 v.class.should == magic.class615 v.pointer == magic.pointer616 end617 it "struct by reference argument with nil value" do618 v = LibTest::S8F32S32.new619 LibTest.testCallbackYrV(nil) { |i| v = i }620 v.is_a?(FFI::Struct).should be_true621 v.pointer.should == FFI::Pointer::NULL622 end623 it "varargs parameters are rejected" do624 lambda {625 Module.new do626 extend FFI::Library627 ffi_lib TestLibrary::PATH628 callback :cbVrL, [ :varargs ], :long629 end630 }.should raise_error(ArgumentError)631 end632end...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Minitest_ruby automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful