Skip to content

Commit

Permalink
fix: update unit tests with error captures instead of raises
Browse files Browse the repository at this point in the history
  • Loading branch information
jhonasiv committed Dec 6, 2024
1 parent 201ea4c commit d0b83eb
Showing 1 changed file with 114 additions and 96 deletions.
210 changes: 114 additions & 96 deletions test/test_exceptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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<id:ID>
no owners
arguments:
arg: 2,
test_dev: MasterDeviceInstance(test[D]_dev),
conf: default(["default"]),
read_only: default(false)
Chain 2:
T<id:ID>
no owners
arguments:
arg: 1,
test_dev: MasterDeviceInstance(test[D]_dev),
conf: default(["default"]),
read_only: default(false)
PP
assert_equal expected, formatted.gsub(/<id:\d+>/, "<id:ID>").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<id:ID>
no owners
arguments:
arg: 2,
test_dev: MasterDeviceInstance(test[D]_dev),
conf: default(["default"]),
read_only: default(false)
Chain 2:
T<id:ID>
no owners
arguments:
arg: 1,
test_dev: MasterDeviceInstance(test[D]_dev),
conf: default(["default"]),
read_only: default(false)
PP
assert_equal expected, formatted.gsub(/<id:\d+>/, "<id:ID>").chomp
end
end

it "displays definitions that depend on the conflicting tasks" do
Expand All @@ -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<id:ID>
no owners
arguments:
test_dev: MasterDeviceInstance(test[D]_dev),
arg: 2,
conf: ["default"],
read_only: false
Chain 2:
T<id:ID>
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(/<id:\d+>/, "<id:ID>").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<id:ID>
no owners
arguments:
test_dev: MasterDeviceInstance(test[D]_dev),
arg: 2,
conf: ["default"],
read_only: false
Chain 2:
T<id:ID>
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(/<id:\d+>/, "<id:ID>").chomp
end
end

it "displays merge chains to explain why devices are duplicated" do
Expand All @@ -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<id:ID>
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<id:ID>
no owners
arguments:
arg: 1,
conf: default(["default"]),
read_only: default(false)
Chain 2:
Driver<id:ID>
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<id:ID>
no owners
arguments:
arg: 2,
conf: default(["default"]),
read_only: default(false)
PP
assert_equal expected, formatted.gsub(/<id:\d+>/, "<id:ID>").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<id:ID>
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<id:ID>
no owners
arguments:
arg: 1,
conf: default(["default"]),
read_only: default(false)
Chain 2:
Driver<id:ID>
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<id:ID>
no owners
arguments:
arg: 2,
conf: default(["default"]),
read_only: default(false)
PP
assert_equal expected, formatted.gsub(/<id:\d+>/, "<id:ID>").chomp
end
end
end
end

0 comments on commit d0b83eb

Please sign in to comment.