From d0b83eb1b3a3ffe17d422d63f0df9f479e325b67 Mon Sep 17 00:00:00 2001 From: Jhonas Date: Fri, 6 Dec 2024 14:05:23 -0300 Subject: [PATCH] fix: update unit tests with error captures instead of raises --- test/test_exceptions.rb | 210 ++++++++++++++++++++++------------------ 1 file changed, 114 insertions(+), 96 deletions(-) diff --git a/test/test_exceptions.rb b/test/test_exceptions.rb index af3b71b08..d5261de29 100644 --- a/test/test_exceptions.rb +++ b/test/test_exceptions.rb @@ -23,35 +23,39 @@ module Syskit plan.add(task1 = driver_m.new(arg: 1, test_dev: robot.test_dev)) plan.add(task2 = driver_m.new(arg: 2, test_dev: robot.test_dev)) - e = assert_raises(ConflictingDeviceAllocation) do - NetworkGeneration::SystemNetworkGenerator + errors = NetworkGeneration::SystemNetworkGenerator .new(plan).validate_generated_network - end - assert_equal Set[task1, task2], e.tasks.to_set - formatted = PP.pp(e, +"") - - expected = <<~PP.chomp - device 'test' of type D is assigned to two tasks that cannot be merged - Chain 1 cannot be merged in chain 2: - Chain 1: - T - no owners - arguments: - arg: 2, - test_dev: MasterDeviceInstance(test[D]_dev), - conf: default(["default"]), - read_only: default(false) - Chain 2: - T - no owners - arguments: - arg: 1, - test_dev: MasterDeviceInstance(test[D]_dev), - conf: default(["default"]), - read_only: default(false) - PP - assert_equal expected, formatted.gsub(//, "").chomp + expected_tasks = [task1, task2] + assert_equal 2, errors.size + errors.each_with_index do |e, i| + assert_kind_of ConflictingDeviceAllocation, + e.each_original_exception.first + assert_equal expected_tasks[i], e.failed_task + formatted = PP.pp(e, +"") + + expected = <<~PP.chomp + device 'test' of type D is assigned to two tasks that cannot be merged + Chain 1 cannot be merged in chain 2: + Chain 1: + T + no owners + arguments: + arg: 2, + test_dev: MasterDeviceInstance(test[D]_dev), + conf: default(["default"]), + read_only: default(false) + Chain 2: + T + no owners + arguments: + arg: 1, + test_dev: MasterDeviceInstance(test[D]_dev), + conf: default(["default"]), + read_only: default(false) + PP + assert_equal expected, formatted.gsub(//, "").chomp + end end it "displays definitions that depend on the conflicting tasks" do @@ -71,36 +75,44 @@ module Syskit .use("test" => robot.test_dev.with_arguments(arg: 2)) self.syskit_run_planner_validate_network = true - e = assert_raises(ConflictingDeviceAllocation) do - run_planners([profile.test1_def, profile.test2_def]) - end + errors = [] + flexmock(NetworkGeneration::SystemNetworkGenerator) + .new_instances + .should_receive(:validate_network) + .pass_thru { |e| errors = e } + run_planners([profile.test1_def, profile.test2_def]) - formatted = PP.pp(e, +"") - expected = <<~PP.chomp - device 'test' of type D is assigned to two tasks that cannot be merged - Chain 1 cannot be merged in chain 2: - Chain 1: - T - no owners - arguments: - test_dev: MasterDeviceInstance(test[D]_dev), - arg: 2, - conf: ["default"], - read_only: false - Chain 2: - T - no owners - arguments: - test_dev: MasterDeviceInstance(test[D]_dev), - arg: 1, - conf: ["default"], - read_only: false - Chain 1 is needed by the following definitions: - Test.test2_def - Chain 2 is needed by the following definitions: - Test.test1_def - PP - assert_equal expected, formatted.gsub(//, "").chomp + assert_equal 2, errors.size + errors.each do |e| + assert_kind_of ConflictingDeviceAllocation, + e.each_original_exception.first + formatted = PP.pp(e, +"") + expected = <<~PP.chomp + device 'test' of type D is assigned to two tasks that cannot be merged + Chain 1 cannot be merged in chain 2: + Chain 1: + T + no owners + arguments: + test_dev: MasterDeviceInstance(test[D]_dev), + arg: 2, + conf: ["default"], + read_only: false + Chain 2: + T + no owners + arguments: + test_dev: MasterDeviceInstance(test[D]_dev), + arg: 1, + conf: ["default"], + read_only: false + Chain 1 is needed by the following definitions: + Test.test2_def + Chain 2 is needed by the following definitions: + Test.test1_def + PP + assert_equal expected, formatted.gsub(//, "").chomp + end end it "displays merge chains to explain why devices are duplicated" do @@ -123,47 +135,53 @@ module Syskit plan.add(task2 = task_m.new(arg: 2)) task1.out_port.connect_to driver1.in_port task2.out_port.connect_to driver2.in_port - e = assert_raises(ConflictingDeviceAllocation) do - NetworkGeneration::SystemNetworkGenerator - .new(plan).validate_generated_network - end + errors = NetworkGeneration::SystemNetworkGenerator.new(plan) + .validate_generated_network + + expected_tasks = [driver1, driver2] + - assert_equal Set[driver1, driver2], e.tasks.to_set - formatted = PP.pp(e, +"") - - expected = <<~PP.chomp - device 'test' of type D is assigned to two tasks that cannot be merged - Chain 1 cannot be merged in chain 2: - Chain 1: - Driver - no owners - arguments: - test_dev: MasterDeviceInstance(test[D]_dev), - conf: default(["default"]), - read_only: default(false) - sink in_port connected via policy {} to source out_port of - Task - no owners - arguments: - arg: 1, - conf: default(["default"]), - read_only: default(false) - Chain 2: - Driver - no owners - arguments: - test_dev: MasterDeviceInstance(test[D]_dev), - conf: default(["default"]), - read_only: default(false) - sink in_port connected via policy {} to source out_port of - Task - no owners - arguments: - arg: 2, - conf: default(["default"]), - read_only: default(false) - PP - assert_equal expected, formatted.gsub(//, "").chomp + assert_equal 2, errors.size + errors.each_with_index do |e, i| + assert_kind_of ConflictingDeviceAllocation, + e.each_original_exception.first + assert_equal expected_tasks[i], e.failed_task + formatted = PP.pp(e, +"") + + expected = <<~PP.chomp + device 'test' of type D is assigned to two tasks that cannot be merged + Chain 1 cannot be merged in chain 2: + Chain 1: + Driver + no owners + arguments: + test_dev: MasterDeviceInstance(test[D]_dev), + conf: default(["default"]), + read_only: default(false) + sink in_port connected via policy {} to source out_port of + Task + no owners + arguments: + arg: 1, + conf: default(["default"]), + read_only: default(false) + Chain 2: + Driver + no owners + arguments: + test_dev: MasterDeviceInstance(test[D]_dev), + conf: default(["default"]), + read_only: default(false) + sink in_port connected via policy {} to source out_port of + Task + no owners + arguments: + arg: 2, + conf: default(["default"]), + read_only: default(false) + PP + assert_equal expected, formatted.gsub(//, "").chomp + end end end end