Use canonical table-driven test format

This commit is contained in:
Adam Schmideg 2020-03-17 14:07:51 +01:00
parent 3db06e4b20
commit 969c6712ce

View file

@ -17,6 +17,7 @@
package main package main
import ( import (
"fmt"
"io/ioutil" "io/ioutil"
"path/filepath" "path/filepath"
"runtime" "runtime"
@ -90,22 +91,25 @@ Path of the secret key file: .*UTC--.+--[0-9a-f]{40}
func TestAccountImport(t *testing.T) { func TestAccountImport(t *testing.T) {
bytes64 := "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" bytes64 := "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
success := `Address: {[0-9a-f]{40}}` success := "Address: {fcad0b19bb29d4674531d6f115237e16afce377c}\n"
failure := `Fatal: Failed to load the private key: expected 64 bytes, got \d+` failureTemplate := "Fatal: Failed to load the private key: expected 64 bytes, got %v\n"
keyToMsg := make(map[string]string) tests := []struct{
keyToMsg[bytes64] = success key string
keyToMsg[bytes64[:40]] = failure result string
keyToMsg[bytes64+"\n"] = success }{
keyToMsg[bytes64+"\r\n"] = success {key: bytes64, result: success},
keyToMsg[bytes64+"1"] = failure {key: bytes64 + "\n", result: success},
keyToMsg[bytes64+"x"] = failure {key: bytes64 + "\r\n", result: success},
keyToMsg[bytes64+bytes64] = failure {key: bytes64 + "1", result: fmt.Sprintf(failureTemplate, 65)},
for key, msg := range keyToMsg { {key: bytes64 + "x", result: fmt.Sprintf(failureTemplate, 65)},
importAccountWithExpect(t, key, msg) {key: bytes64 + "\n\n\n", result: fmt.Sprintf(failureTemplate, 67)},
}
for _, test := range tests {
importAccountWithExpect(t, test.key, test.result)
} }
} }
func importAccountWithExpect(t *testing.T, key string, expectedRegexp string) { func importAccountWithExpect(t *testing.T, key string, expected string) {
dir := tmpdir(t) dir := tmpdir(t)
keyfile := filepath.Join(dir, "key.prv") keyfile := filepath.Join(dir, "key.prv")
if err := ioutil.WriteFile(keyfile, []byte(key), 0644); err != nil { if err := ioutil.WriteFile(keyfile, []byte(key), 0644); err != nil {
@ -117,7 +121,7 @@ func importAccountWithExpect(t *testing.T, key string, expectedRegexp string) {
} }
geth := runGeth(t, "account", "import", keyfile, "-password", passwordFile) geth := runGeth(t, "account", "import", keyfile, "-password", passwordFile)
defer geth.ExpectExit() defer geth.ExpectExit()
geth.ExpectRegexp(expectedRegexp) geth.Expect(expected)
} }
func TestAccountNewBadRepeat(t *testing.T) { func TestAccountNewBadRepeat(t *testing.T) {