add a test that checks that each errorcode has a string representation

This commit is contained in:
Lucas Clemente
2016-05-17 00:44:15 +02:00
parent dc2a14a5f7
commit 3cc7d7f289
3 changed files with 45 additions and 6 deletions

4
errorcodes/error_code.go Normal file
View File

@@ -0,0 +1,4 @@
package errorcodes
// An ErrorCode in QUIC
type ErrorCode uint32

View File

@@ -1,12 +1,7 @@
// Package errorcodes defines the error codes in QUIC.
//
package errorcodes
// An ErrorCode in QUIC
type ErrorCode uint32
// The error codes defined by QUIC
// Remeber to run go generate ./... whenever the error codes change.
// Remeber to run `go generate ./...` whenever the error codes change.
//go:generate stringer -type=ErrorCode
const (
InternalError ErrorCode = 1

View File

@@ -0,0 +1,40 @@
package errorcodes_test
import (
"go/ast"
"go/parser"
"go/token"
"os"
"strconv"
"github.com/lucas-clemente/quic-go/errorcodes"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"testing"
)
func TestErrorcodes(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Errorcodes Suite")
}
var _ = Describe("error codes", func() {
// If this test breaks, you should run `go generate ./...`
It("has a string representation for every error code", func() {
// We parse the error code file, extract all constants, and verify that
// each of them has a string version. Go FTW!
filename := os.Getenv("GOPATH") + "/src/github.com/lucas-clemente/quic-go/errorcodes/error_codes.go"
fileAst, err := parser.ParseFile(token.NewFileSet(), filename, nil, 0)
Expect(err).NotTo(HaveOccurred())
constSpecs := fileAst.Decls[0].(*ast.GenDecl).Specs
Expect(len(constSpecs)).To(BeNumerically(">", 4)) // at time of writing
for _, c := range constSpecs {
name := c.(*ast.ValueSpec).Names[0].Name
valString := c.(*ast.ValueSpec).Values[0].(*ast.BasicLit).Value
val, err := strconv.Atoi(valString)
Expect(err).NotTo(HaveOccurred())
Expect(errorcodes.ErrorCode(val).String()).To(Equal(name))
}
})
})