From 59b4222e7d85ac86aae824b2c33dfa201722ac2a Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Fri, 1 Jan 2016 18:26:21 +0100 Subject: [PATCH] Refactoring tests Tests are now moved in the source folder. Simplified travis-ci script. Added some examples (that are checked for build, but not executed) --- .travis.yml | 18 +++++------- test/serial_test.go => example_test.go | 40 +++++++++++++++++++++++--- travis/build_check.go | 27 ----------------- 3 files changed, 43 insertions(+), 42 deletions(-) rename test/serial_test.go => example_test.go (55%) delete mode 100644 travis/build_check.go diff --git a/.travis.yml b/.travis.yml index 8348269..2bbb46c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,9 @@ language: go go: - - 1.4 + - 1.4.2 + +go_import_path: go.bug.st/serial env: - TEST_OS=linux TEST_ARCH=386 @@ -20,18 +22,12 @@ matrix: - env: TEST_OS=dragonfly TEST_ARCH=amd64 before_install: - - cd $GOROOT/src - - GOOS=$TEST_OS GOARCH=$TEST_ARCH ./make.bash - - mkdir -p $HOME/gopath/src/go.bug.st - - mv $TRAVIS_BUILD_DIR $HOME/gopath/src/go.bug.st/serial - - export TRAVIS_BUILD_DIR=$HOME/gopath/src/go.bug.st/serial - - cd $TRAVIS_BUILD_DIR - -install: - - export PATH="$HOME/gopath/bin:$PATH" +# bootstrap go tools for the specific OS/Arch + - ( cd $GOROOT/src; GOOS=$TEST_OS GOARCH=$TEST_ARCH ./make.bash ) script: - - GOARM=5 GO386=387 GOOS=$TEST_OS GOARCH=$TEST_ARCH go build travis/build_check.go + - GOARM=5 GO386=387 GOOS=$TEST_OS GOARCH=$TEST_ARCH go build -v ./... + - GOARM=5 GO386=387 GOOS=$TEST_OS GOARCH=$TEST_ARCH go test -c -v ./... notifications: email: diff --git a/test/serial_test.go b/example_test.go similarity index 55% rename from test/serial_test.go rename to example_test.go index 9fe9647..12bec45 100644 --- a/test/serial_test.go +++ b/example_test.go @@ -1,16 +1,47 @@ // -// Copyright 2014 Cristian Maglie. All rights reserved. +// Copyright 2015 Cristian Maglie. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // -package serial +package serial_test -import "go.bug.st/serial" import "fmt" import "log" +import "go.bug.st/serial" -func ExampleCommunication() { +func ExampleGetPortsList() { + ports, err := serial.GetPortsList() + if err != nil { + log.Fatal(err) + } + if len(ports) == 0 { + fmt.Println("No serial ports found!") + } else { + for _, port := range ports { + fmt.Printf("Found port: %v\n", port) + } + } +} + +func ExampleSetMode() { + port, err := serial.OpenPort("/dev/ttyACM0", &serial.Mode{}) + if err != nil { + log.Fatal(err) + } + mode := &serial.Mode{ + BaudRate: 9600, + Parity: serial.PARITY_NONE, + DataBits: 8, + StopBits: serial.STOPBITS_ONE, + } + if err := port.SetMode(mode); err != nil { + log.Fatal(err) + } + fmt.Println("Port set to 9600 N81") +} + +func ExampleFullCommunication() { ports, err := serial.GetPortsList() if err != nil { log.Fatal(err) @@ -41,6 +72,7 @@ func ExampleCommunication() { buff := make([]byte, 100) for { + // Reads up to 100 bytes n, err := port.Read(buff) if err != nil { log.Fatal(err) diff --git a/travis/build_check.go b/travis/build_check.go deleted file mode 100644 index 890102e..0000000 --- a/travis/build_check.go +++ /dev/null @@ -1,27 +0,0 @@ -// -// Copyright 2014 Cristian Maglie. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. -// - -package test - -import "go.bug.st/serial" - -func BuildTest() { - serial.GetPortsList() - mode := &serial.Mode{ - BaudRate: 9600, - Parity: serial.PARITY_NONE, - DataBits: 8, - StopBits: serial.STOPBITS_ONE, - } - port, _ := serial.OpenPort("", mode) - port.SetMode(mode) - buff := make([]byte, 100) - port.Write(buff) - port.Read(buff) - port.Close() -} - -// vi:ts=2