From dd405348e9a5dce3bd5579df1cb273485f7308ae Mon Sep 17 00:00:00 2001 From: Sebastian Zagrodzki Date: Sun, 9 Apr 2017 20:55:37 +0200 Subject: [PATCH] move tests around --- usb/config_test.go | 45 ------------------- usb/device_test.go | 43 ++++++++++++++++++ usb/endpoint_info_test.go | 48 --------------------- usb/endpoint_test.go | 91 ++++++++++++++++++++++++++++++--------- 4 files changed, 114 insertions(+), 113 deletions(-) delete mode 100644 usb/config_test.go create mode 100644 usb/device_test.go delete mode 100644 usb/endpoint_info_test.go diff --git a/usb/config_test.go b/usb/config_test.go deleted file mode 100644 index 7035e8a..0000000 --- a/usb/config_test.go +++ /dev/null @@ -1,45 +0,0 @@ -package usb - -import "testing" - -func TestEndpointInfo(t *testing.T) { - for _, tc := range []struct { - ep EndpointInfo - want string - }{ - { - ep: EndpointInfo{ - Number: 6, - Direction: EndpointDirectionIn, - TransferType: TransferTypeBulk, - MaxPacketSize: 512, - }, - want: "Endpoint #6 IN (address 0x86) bulk [512 bytes]", - }, - { - ep: EndpointInfo{ - Number: 2, - Direction: EndpointDirectionOut, - TransferType: TransferTypeIsochronous, - MaxPacketSize: 512, - IsoSyncType: IsoSyncTypeAsync, - UsageType: IsoUsageTypeData, - }, - want: "Endpoint #2 OUT (address 0x02) isochronous - asynchronous data [512 bytes]", - }, - { - ep: EndpointInfo{ - Number: 3, - Direction: EndpointDirectionIn, - TransferType: TransferTypeInterrupt, - MaxPacketSize: 16, - UsageType: InterruptUsageTypePeriodic, - }, - want: "Endpoint #3 IN (address 0x83) interrupt - periodic [16 bytes]", - }, - } { - if got := tc.ep.String(); got != tc.want { - t.Errorf("%#v.String(): got %q, want %q", tc.ep, got, tc.want) - } - } -} diff --git a/usb/device_test.go b/usb/device_test.go new file mode 100644 index 0000000..9b0d4eb --- /dev/null +++ b/usb/device_test.go @@ -0,0 +1,43 @@ +// Copyright 2017 the gousb Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package usb + +import ( + "reflect" + "testing" +) + +func TestOpenEndpoint(t *testing.T) { + origLib := libusb + defer func() { libusb = origLib }() + libusb = newFakeLibusb() + + c := NewContext() + dev, err := c.OpenDeviceWithVidPid(0x8888, 0x0002) + if dev == nil { + t.Fatal("OpenDeviceWithVidPid(0x8888, 0x0002): got nil device, need non-nil") + } + defer dev.Close() + if err != nil { + t.Fatalf("OpenDeviceWithVidPid(0x8888, 0x0002): got error %v, want nil", err) + } + got, err := dev.OpenEndpoint(0x86, 1, 1, 2) + if err != nil { + t.Fatalf("OpenEndpoint(cfg=1, if=1, alt=2, ep=0x86): got error %v, want nil", err) + } + if want := fakeDevices[1].Configs[0].Interfaces[1].AltSettings[2].Endpoints[1]; !reflect.DeepEqual(got.Info, want) { + t.Errorf("OpenEndpoint(cfg=1, if=1, alt=2, ep=0x86): got %+v, want %+v", got, want) + } +} diff --git a/usb/endpoint_info_test.go b/usb/endpoint_info_test.go deleted file mode 100644 index a44ec18..0000000 --- a/usb/endpoint_info_test.go +++ /dev/null @@ -1,48 +0,0 @@ -// Copyright 2017 the gousb Authors. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package usb - -import "time" - -// IN bulk endpoint -var testBulkInEP = EndpointInfo{ - Number: 2, - Direction: EndpointDirectionIn, - MaxPacketSize: 512, - TransferType: TransferTypeBulk, -} - -var testBulkInSetting = InterfaceSetting{ - Number: 0, - Alternate: 0, - Class: ClassVendorSpec, - Endpoints: []EndpointInfo{testBulkInEP}, -} - -// OUT iso endpoint -var testIsoOutEP = EndpointInfo{ - Number: 6, - MaxPacketSize: 3 * 1024, - TransferType: TransferTypeIsochronous, - PollInterval: 125 * time.Microsecond, - UsageType: IsoUsageTypeData, -} - -var testIsoOutSetting = InterfaceSetting{ - Number: 0, - Alternate: 0, - Class: ClassVendorSpec, - Endpoints: []EndpointInfo{testIsoOutEP}, -} diff --git a/usb/endpoint_test.go b/usb/endpoint_test.go index 0c216cd..eee2896 100644 --- a/usb/endpoint_test.go +++ b/usb/endpoint_test.go @@ -20,8 +20,40 @@ import ( "time" ) +// IN bulk endpoint +var testBulkInEP = EndpointInfo{ + Number: 2, + Direction: EndpointDirectionIn, + MaxPacketSize: 512, + TransferType: TransferTypeBulk, +} + +var testBulkInSetting = InterfaceSetting{ + Number: 0, + Alternate: 0, + Class: ClassVendorSpec, + Endpoints: []EndpointInfo{testBulkInEP}, +} + +// OUT iso endpoint +var testIsoOutEP = EndpointInfo{ + Number: 6, + MaxPacketSize: 3 * 1024, + TransferType: TransferTypeIsochronous, + PollInterval: 125 * time.Microsecond, + UsageType: IsoUsageTypeData, +} + +var testIsoOutSetting = InterfaceSetting{ + Number: 0, + Alternate: 0, + Class: ClassVendorSpec, + Endpoints: []EndpointInfo{testIsoOutEP}, +} + func TestEndpoint(t *testing.T) { defer func(i libusbIntf) { libusb = i }(libusb) + for _, epCfg := range []struct { method string InterfaceSetting @@ -106,25 +138,44 @@ func TestEndpointWrongDirection(t *testing.T) { } } -func TestOpenEndpoint(t *testing.T) { - origLib := libusb - defer func() { libusb = origLib }() - libusb = newFakeLibusb() - - c := NewContext() - dev, err := c.OpenDeviceWithVidPid(0x8888, 0x0002) - if dev == nil { - t.Fatal("OpenDeviceWithVidPid(0x8888, 0x0002): got nil device, need non-nil") - } - defer dev.Close() - if err != nil { - t.Fatalf("OpenDeviceWithVidPid(0x8888, 0x0002): got error %v, want nil", err) - } - got, err := dev.OpenEndpoint(0x86, 1, 1, 2) - if err != nil { - t.Fatalf("OpenEndpoint(cfg=1, if=1, alt=2, ep=0x86): got error %v, want nil", err) - } - if want := fakeDevices[1].Configs[0].Interfaces[1].AltSettings[2].Endpoints[1]; !reflect.DeepEqual(got.Info, want) { - t.Errorf("OpenEndpoint(cfg=1, if=1, alt=2, ep=0x86): got %+v, want %+v", got, want) +func TestEndpointInfo(t *testing.T) { + for _, tc := range []struct { + ep EndpointInfo + want string + }{ + { + ep: EndpointInfo{ + Number: 6, + Direction: EndpointDirectionIn, + TransferType: TransferTypeBulk, + MaxPacketSize: 512, + }, + want: "Endpoint #6 IN (address 0x86) bulk [512 bytes]", + }, + { + ep: EndpointInfo{ + Number: 2, + Direction: EndpointDirectionOut, + TransferType: TransferTypeIsochronous, + MaxPacketSize: 512, + IsoSyncType: IsoSyncTypeAsync, + UsageType: IsoUsageTypeData, + }, + want: "Endpoint #2 OUT (address 0x02) isochronous - asynchronous data [512 bytes]", + }, + { + ep: EndpointInfo{ + Number: 3, + Direction: EndpointDirectionIn, + TransferType: TransferTypeInterrupt, + MaxPacketSize: 16, + UsageType: InterruptUsageTypePeriodic, + }, + want: "Endpoint #3 IN (address 0x83) interrupt - periodic [16 bytes]", + }, + } { + if got := tc.ep.String(); got != tc.want { + t.Errorf("%#v.String(): got %q, want %q", tc.ep, got, tc.want) + } } }