github.com/TugasAkhir-QUIC/quic-go@v0.0.2-0.20240215011318-d20e25a9054c/http3/error_codes_test.go (about)

     1  package http3
     2  
     3  import (
     4  	"go/ast"
     5  	"go/parser"
     6  	"go/token"
     7  	"path"
     8  	"runtime"
     9  	"strconv"
    10  
    11  	. "github.com/onsi/ginkgo/v2"
    12  	. "github.com/onsi/gomega"
    13  )
    14  
    15  var _ = Describe("error codes", func() {
    16  	It("has a string representation for every error code", func() {
    17  		// We parse the error code file, extract all constants, and verify that
    18  		// each of them has a string version. Go FTW!
    19  		_, thisfile, _, ok := runtime.Caller(0)
    20  		if !ok {
    21  			panic("Failed to get current frame")
    22  		}
    23  		filename := path.Join(path.Dir(thisfile), "error_codes.go")
    24  		fileAst, err := parser.ParseFile(token.NewFileSet(), filename, nil, 0)
    25  		Expect(err).NotTo(HaveOccurred())
    26  		constSpecs := fileAst.Decls[2].(*ast.GenDecl).Specs
    27  		Expect(len(constSpecs)).To(BeNumerically(">", 4)) // at time of writing
    28  		for _, c := range constSpecs {
    29  			valString := c.(*ast.ValueSpec).Values[0].(*ast.BasicLit).Value
    30  			val, err := strconv.ParseInt(valString, 0, 64)
    31  			Expect(err).NotTo(HaveOccurred())
    32  			Expect(ErrCode(val).String()).ToNot(Equal("unknown error code"))
    33  		}
    34  	})
    35  
    36  	It("has a string representation for unknown error codes", func() {
    37  		Expect(ErrCode(0x1337).String()).To(Equal("unknown error code: 0x1337"))
    38  	})
    39  })