Tests for OpenVidPid

This commit is contained in:
Sebastian Zagrodzki
2017-03-10 07:38:54 -05:00
parent 4b17678362
commit d392ad305d
2 changed files with 33 additions and 3 deletions

View File

@@ -80,12 +80,13 @@ func (c *Context) ListDevices(each func(desc *Descriptor) bool) ([]*Device, erro
// it will return a non-nil device and non-nil error. A Device.Close() must
// be called to release the device if the returned device wasn't nil.
func (c *Context) OpenDeviceWithVidPid(vid, pid int) (*Device, error) {
dev * Device
devs, err := ListDevices(func(desc *Descriptor) {
if dev != nil {
var found bool
devs, err := c.ListDevices(func(desc *Descriptor) bool {
if found {
return false
}
if desc.Vendor == ID(vid) && desc.Product == ID(pid) {
found = true
return true
}
return false

View File

@@ -53,3 +53,32 @@ func TestListDevices(t *testing.T) {
}
}
}
func TestOpenDeviceWithVidPid(t *testing.T) {
orig := libusb
defer func() { libusb = orig }()
libusb = newFakeLibusb()
for _, d := range []struct {
vid, pid int
exists bool
}{
{0x7777, 0x0003, false},
{0x8888, 0x0001, false},
{0x8888, 0x0002, true},
{0x9999, 0x0001, true},
{0x9999, 0x0002, false},
} {
c := NewContext()
dev, err := c.OpenDeviceWithVidPid(d.vid, d.pid)
if (dev != nil) != d.exists {
t.Errorf("OpenDeviceWithVidPid(%s/%s): device != nil is %v, want %v", ID(d.vid), ID(d.pid), dev != nil, d.exists)
}
if err != nil {
t.Errorf("OpenDeviceWithVidPid(%s/%s): got error %v, want nil", ID(d.vid), ID(d.pid), err)
}
if dev != nil {
dev.Close()
}
}
}