return true
}
+func endtest() {
+ if pclineTempDir != "" {
+ os.RemoveAll(pclineTempDir)
+ pclineTempDir = ""
+ pclinetestBinary = ""
+ }
+}
+
func getTable(t *testing.T) *Table {
f, tab := crack(os.Args[0], t)
f.Close()
if !dotest() {
return
}
+ defer endtest()
tab := getTable(t)
if !dotest() {
return
}
+ defer endtest()
tab := getTable(t)
if !dotest() {
return
}
- defer os.RemoveAll(pclineTempDir)
+ defer endtest()
f, tab := crack(pclinetestBinary, t)
text := f.Section(".text")
func TestConvertLarge(t *testing.T) {
for i, tt := range convLargeTests {
- e := &entry{elems: tt.in}
+ e := new(entry)
+ for _, ce := range tt.in {
+ e.elems = append(e.elems, makeRawCE(ce.w, ce.ccc))
+ }
elems, err := convertLargeWeights(e.elems)
if tt.err {
if err == nil {
}
func TestInt(t *testing.T) {
+ RemoveAll()
reqs := NewInt("requests")
if reqs.i != 0 {
t.Errorf("reqs.i = %v, want 0", reqs.i)
}
func TestFloat(t *testing.T) {
+ RemoveAll()
reqs := NewFloat("requests-float")
if reqs.f != 0.0 {
t.Errorf("reqs.f = %v, want 0", reqs.f)
}
func TestString(t *testing.T) {
+ RemoveAll()
name := NewString("my-name")
if name.s != "" {
t.Errorf("name.s = %q, want \"\"", name.s)
}
func TestMapCounter(t *testing.T) {
+ RemoveAll()
colors := NewMap("bike-shed-colors")
colors.Add("red", 1)
}
func TestFunc(t *testing.T) {
+ RemoveAll()
var x interface{} = []string{"a", "b"}
f := Func(func() interface{} { return x })
if s, exp := f.String(), `["a","b"]`; s != exp {
"time"
)
-var (
- test_bool = Bool("test_bool", false, "bool value")
- test_int = Int("test_int", 0, "int value")
- test_int64 = Int64("test_int64", 0, "int64 value")
- test_uint = Uint("test_uint", 0, "uint value")
- test_uint64 = Uint64("test_uint64", 0, "uint64 value")
- test_string = String("test_string", "0", "string value")
- test_float64 = Float64("test_float64", 0, "float64 value")
- test_duration = Duration("test_duration", 0, "time.Duration value")
-)
-
func boolString(s string) string {
if s == "0" {
return "false"
}
func TestEverything(t *testing.T) {
+ ResetForTesting(nil)
+ Bool("test_bool", false, "bool value")
+ Int("test_int", 0, "int value")
+ Int64("test_int64", 0, "int64 value")
+ Uint("test_uint", 0, "uint value")
+ Uint64("test_uint64", 0, "uint64 value")
+ String("test_string", "0", "string value")
+ Float64("test_float64", 0, "float64 value")
+ Duration("test_duration", 0, "time.Duration value")
+
m := make(map[string]*Flag)
desired := "0"
visitor := func(f *Flag) {
const testdata = "testdata"
+var fsetErrs *token.FileSet
+
// getFile assumes that each filename occurs at most once
func getFile(filename string) (file *token.File) {
- fset.Iterate(func(f *token.File) bool {
+ fsetErrs.Iterate(func(f *token.File) bool {
if f.Name() == filename {
if file != nil {
panic(filename + " used multiple times")
if len(expected) > 0 {
t.Errorf("%d errors not reported:", len(expected))
for pos, msg := range expected {
- t.Errorf("%s: %s\n", fset.Position(pos), msg)
+ t.Errorf("%s: %s\n", fsetErrs.Position(pos), msg)
}
}
}
return
}
- _, err = ParseFile(fset, filename, src, DeclarationErrors)
+ _, err = ParseFile(fsetErrs, filename, src, DeclarationErrors)
found, ok := err.(scanner.ErrorList)
if err != nil && !ok {
t.Error(err)
}
func TestErrors(t *testing.T) {
+ fsetErrs = token.NewFileSet()
list, err := ioutil.ReadDir(testdata)
if err != nil {
t.Fatal(err)
}
}
+var testBuiltinsDeclared = false
+
func TestCheck(t *testing.T) {
// Declare builtins for testing.
// Not done in an init func to avoid an init race with
// the construction of the Universe var.
- def(&Func{Name: "assert", Type: &builtin{_Assert, "assert", 1, false, true}})
- def(&Func{Name: "trace", Type: &builtin{_Trace, "trace", 0, true, true}})
+ if !testBuiltinsDeclared {
+ testBuiltinsDeclared = true
+ def(&Func{Name: "assert", Type: &builtin{_Assert, "assert", 1, false, true}})
+ def(&Func{Name: "trace", Type: &builtin{_Trace, "trace", 0, true, true}})
+ }
// For easy debugging w/o changing the testing code,
// if there is a local test file, only test that file.
Raw string
}
-var respWriteTests = []respWriteTest{
- // HTTP/1.0, identity coding; no trailer
- {
- Response{
- StatusCode: 503,
- ProtoMajor: 1,
- ProtoMinor: 0,
- Request: dummyReq("GET"),
- Header: Header{},
- Body: ioutil.NopCloser(bytes.NewBufferString("abcdef")),
- ContentLength: 6,
- },
+func TestResponseWrite(t *testing.T) {
+ respWriteTests := []respWriteTest{
+ // HTTP/1.0, identity coding; no trailer
+ {
+ Response{
+ StatusCode: 503,
+ ProtoMajor: 1,
+ ProtoMinor: 0,
+ Request: dummyReq("GET"),
+ Header: Header{},
+ Body: ioutil.NopCloser(bytes.NewBufferString("abcdef")),
+ ContentLength: 6,
+ },
- "HTTP/1.0 503 Service Unavailable\r\n" +
- "Content-Length: 6\r\n\r\n" +
- "abcdef",
- },
- // Unchunked response without Content-Length.
- {
- Response{
- StatusCode: 200,
- ProtoMajor: 1,
- ProtoMinor: 0,
- Request: dummyReq("GET"),
- Header: Header{},
- Body: ioutil.NopCloser(bytes.NewBufferString("abcdef")),
- ContentLength: -1,
+ "HTTP/1.0 503 Service Unavailable\r\n" +
+ "Content-Length: 6\r\n\r\n" +
+ "abcdef",
},
- "HTTP/1.0 200 OK\r\n" +
- "\r\n" +
- "abcdef",
- },
- // HTTP/1.1, chunked coding; empty trailer; close
- {
- Response{
- StatusCode: 200,
- ProtoMajor: 1,
- ProtoMinor: 1,
- Request: dummyReq("GET"),
- Header: Header{},
- Body: ioutil.NopCloser(bytes.NewBufferString("abcdef")),
- ContentLength: 6,
- TransferEncoding: []string{"chunked"},
- Close: true,
+ // Unchunked response without Content-Length.
+ {
+ Response{
+ StatusCode: 200,
+ ProtoMajor: 1,
+ ProtoMinor: 0,
+ Request: dummyReq("GET"),
+ Header: Header{},
+ Body: ioutil.NopCloser(bytes.NewBufferString("abcdef")),
+ ContentLength: -1,
+ },
+ "HTTP/1.0 200 OK\r\n" +
+ "\r\n" +
+ "abcdef",
},
+ // HTTP/1.1, chunked coding; empty trailer; close
+ {
+ Response{
+ StatusCode: 200,
+ ProtoMajor: 1,
+ ProtoMinor: 1,
+ Request: dummyReq("GET"),
+ Header: Header{},
+ Body: ioutil.NopCloser(bytes.NewBufferString("abcdef")),
+ ContentLength: 6,
+ TransferEncoding: []string{"chunked"},
+ Close: true,
+ },
- "HTTP/1.1 200 OK\r\n" +
- "Connection: close\r\n" +
- "Transfer-Encoding: chunked\r\n\r\n" +
- "6\r\nabcdef\r\n0\r\n\r\n",
- },
+ "HTTP/1.1 200 OK\r\n" +
+ "Connection: close\r\n" +
+ "Transfer-Encoding: chunked\r\n\r\n" +
+ "6\r\nabcdef\r\n0\r\n\r\n",
+ },
- // Header value with a newline character (Issue 914).
- // Also tests removal of leading and trailing whitespace.
- {
- Response{
- StatusCode: 204,
- ProtoMajor: 1,
- ProtoMinor: 1,
- Request: dummyReq("GET"),
- Header: Header{
- "Foo": []string{" Bar\nBaz "},
+ // Header value with a newline character (Issue 914).
+ // Also tests removal of leading and trailing whitespace.
+ {
+ Response{
+ StatusCode: 204,
+ ProtoMajor: 1,
+ ProtoMinor: 1,
+ Request: dummyReq("GET"),
+ Header: Header{
+ "Foo": []string{" Bar\nBaz "},
+ },
+ Body: nil,
+ ContentLength: 0,
+ TransferEncoding: []string{"chunked"},
+ Close: true,
},
- Body: nil,
- ContentLength: 0,
- TransferEncoding: []string{"chunked"},
- Close: true,
- },
- "HTTP/1.1 204 No Content\r\n" +
- "Connection: close\r\n" +
- "Foo: Bar Baz\r\n" +
- "\r\n",
- },
-}
+ "HTTP/1.1 204 No Content\r\n" +
+ "Connection: close\r\n" +
+ "Foo: Bar Baz\r\n" +
+ "\r\n",
+ },
+ }
-func TestResponseWrite(t *testing.T) {
for i := range respWriteTests {
tt := &respWriteTests[i]
var braw bytes.Buffer
}
func TestHostHandlers(t *testing.T) {
+ mux := NewServeMux()
for _, h := range handlers {
- Handle(h.pattern, stringHandler(h.msg))
+ mux.Handle(h.pattern, stringHandler(h.msg))
}
- ts := httptest.NewServer(nil)
+ ts := httptest.NewServer(mux)
defer ts.Close()
conn, err := net.Dial("tcp", ts.Listener.Addr().String())
check("Wait", err)
}
+var testedAlreadyLeaked = false
+
func TestExtraFiles(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("no operating system support; skipping")
// Ensure that file descriptors have not already been leaked into
// our environment.
- for fd := os.Stderr.Fd() + 1; fd <= 101; fd++ {
- err := os.NewFile(fd, "").Close()
- if err == nil {
- t.Logf("Something already leaked - closed fd %d", fd)
+ if !testedAlreadyLeaked {
+ testedAlreadyLeaked = true
+ for fd := os.Stderr.Fd() + 1; fd <= 101; fd++ {
+ err := os.NewFile(fd, "").Close()
+ if err == nil {
+ t.Logf("Something already leaked - closed fd %d", fd)
+ }
}
}