Skip to content

Commit

Permalink
fix(extron\usb_extender_plus\virtual_switcher): add spec
Browse files Browse the repository at this point in the history
  • Loading branch information
stakach committed Feb 12, 2024
1 parent 454b17b commit 0303f6c
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 2 deletions.
5 changes: 3 additions & 2 deletions drivers/extron/usb_extender_plus/virtual_switcher.cr
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ class Extron::UsbExtenderPlus::VirtualSwitcher < PlaceOS::Driver
generic_name :USB_Switcher
descriptive_name "Extron USB Extender Plus Switcher"

accessor hosts : Array(USB_Host), implementing: InputSelection
accessor devices : Array(USB_Device), implementing: InputSelection
accessor hosts : Array(USB_Host)
accessor devices : Array(USB_Device)

getter host_macs : Hash(String, Int32) do
hash = {} of String => Int32
Expand Down Expand Up @@ -46,6 +46,7 @@ class Extron::UsbExtenderPlus::VirtualSwitcher < PlaceOS::Driver
host_mac = host.status(String, :mac_address)

unjoin_all_devices
unjoin_all_hosts
devices.each { |device| perform_join(host, device) }
end
end
Expand Down
76 changes: 76 additions & 0 deletions drivers/extron/usb_extender_plus/virtual_switcher_spec.cr
Original file line number Diff line number Diff line change
@@ -1,4 +1,80 @@
require "placeos-driver/spec"
require "random"

DriverSpecs.mock_driver "Extron::UsbExtenderPlus::VirtualSwitcher" do
system({
USB_Host: {EndpointMock, EndpointMock},
USB_Device: {EndpointMock, EndpointMock},
})

exec(:switch_to, 1).get

host1_mac = system(:USB_Host_1)["mac_address"].as_s
host2_mac = system(:USB_Host_2)["mac_address"].as_s
dev1_mac = system(:USB_Device_1)["mac_address"].as_s
dev2_mac = system(:USB_Device_2)["mac_address"].as_s

system(:USB_Device_1)["joined_to"].should eq [host1_mac]
system(:USB_Device_2)["joined_to"].should eq [host1_mac]
system(:USB_Host_1)["joined_to"].should eq [dev1_mac, dev2_mac]
system(:USB_Host_2)["joined_to"].should eq [] of String

exec(:switch_to, 2).get
system(:USB_Device_1)["joined_to"].should eq [host2_mac]
system(:USB_Device_2)["joined_to"].should eq [host2_mac]
system(:USB_Host_1)["joined_to"].should eq [] of String
system(:USB_Host_2)["joined_to"].should eq [dev1_mac, dev2_mac]

exec(:switch, {1 => [2]}).get
sleep 0.1
system(:USB_Device_1)["joined_to"].should eq [host2_mac]
system(:USB_Device_2)["joined_to"].should eq [host1_mac]
system(:USB_Host_1)["joined_to"].should eq [dev2_mac]
system(:USB_Host_2)["joined_to"].should eq [dev1_mac]

exec(:switch, {1 => [1], 2 => [2]}).get
sleep 0.1
system(:USB_Device_1)["joined_to"].should eq [host1_mac]
system(:USB_Device_2)["joined_to"].should eq [host2_mac]
system(:USB_Host_1)["joined_to"].should eq [dev1_mac]
system(:USB_Host_2)["joined_to"].should eq [dev2_mac]
end

# :nodoc:
class EndpointMock < DriverSpecs::MockDriver
@joined_to : Array(String) = [] of String

def on_load
self[:mac_address] = Random::Secure.hex(6).downcase
self[:joined_to] = [] of String
end

def query_joins
@joined_to
end

def unjoin_all
self[:joined_to] = @joined_to = [] of String
end

def unjoin(from : String | Int32)
mac = case from
in Int32
@joined_to[from]
in String
formatted = from.gsub(/\-|\:/, "").downcase
formatted if @joined_to.includes? formatted
end

if mac
@joined_to.delete(mac)
self[:joined_to] = @joined_to
end
end

def join(mac : String)
mac = mac.gsub(/\-|\:/, "").downcase
@joined_to << mac
self[:joined_to] = @joined_to
end
end

0 comments on commit 0303f6c

Please sign in to comment.