Modify the test for device list, drop noop/multiple contexts.

This commit is contained in:
Sebastian Zagrodzki
2017-03-10 07:08:35 -05:00
parent c918a7e503
commit a787d0a1d1

View File

@@ -13,52 +13,26 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
package usb_test package usb
import ( import (
"bytes" "bytes"
"log" "log"
"os" "os"
"testing" "testing"
. "github.com/kylelemons/gousb/usb"
"github.com/kylelemons/gousb/usbid"
) )
func TestNoop(t *testing.T) { func TestListDevices(t *testing.T) {
orig := libusb
defer func() { libusb = orig }()
libusb = newFakeLibusb()
c := NewContext() c := NewContext()
defer c.Close() defer c.Close()
c.Debug(0) c.Debug(0)
}
func TestEnum(t *testing.T) {
c := NewContext()
defer c.Close()
c.Debug(0)
logDevice := func(t *testing.T, desc *Descriptor) {
t.Logf("%03d.%03d %s", desc.Bus, desc.Address, usbid.Describe(desc))
t.Logf("- Protocol: %s", usbid.Classify(desc))
for _, cfg := range desc.Configs {
t.Logf("- %s:", cfg)
for _, alt := range cfg.Interfaces {
t.Logf(" --------------")
for _, iface := range alt.Setups {
t.Logf(" - %s", iface)
t.Logf(" - %s", usbid.Classify(iface))
for _, end := range iface.Endpoints {
t.Logf(" - %s (packet size: %d bytes)", end, end.MaxPacketSize)
}
}
}
t.Logf(" --------------")
}
}
descs := []*Descriptor{} descs := []*Descriptor{}
devs, err := c.ListDevices(func(desc *Descriptor) bool { devs, err := c.ListDevices(func(desc *Descriptor) bool {
logDevice(t, desc)
descs = append(descs, desc) descs = append(descs, desc)
return true return true
}) })
@@ -68,11 +42,14 @@ func TestEnum(t *testing.T) {
} }
}() }()
if err != nil { if err != nil {
t.Fatalf("list: %s", err) t.Fatalf("ListDevices(): %s", err)
} }
if got, want := len(devs), len(fakeDevices); got != want {
t.Fatalf("len(devs) = %d, want %d (based on num fake devs)", got, want)
}
if got, want := len(devs), len(descs); got != want { if got, want := len(devs), len(descs); got != want {
t.Fatalf("len(devs) = %d, want %d", got, want) t.Fatalf("len(devs) = %d, want %d (based on num opened devices)", got, want)
} }
for i := range devs { for i := range devs {
@@ -81,22 +58,3 @@ func TestEnum(t *testing.T) {
} }
} }
} }
func TestMultipleContexts(t *testing.T) {
var buf bytes.Buffer
log.SetOutput(&buf)
for i := 0; i < 2; i++ {
ctx := NewContext()
_, err := ctx.ListDevices(func(desc *Descriptor) bool {
return false
})
if err != nil {
t.Fatal(err)
}
ctx.Close()
}
log.SetOutput(os.Stderr)
if buf.Len() > 0 {
t.Errorf("Non zero output to log, while testing: %s", buf.String())
}
}