package caesar import ( "bytes" "flag" "io" "io/ioutil" "os" "testing" ) var ( runCustom bool customCases string ) func TestMain(m *testing.M) { flag.BoolVar(&runCustom, "custom", false, "run custom tests") flag.StringVar(&customCases, "custom.cases", "", "input file for custom test cases") flag.Parse() os.Exit(m.Run()) } func TestCustom(t *testing.T) { if !runCustom { t.Skip("skipping custom testing") } f, err := os.Open(customCases) if err != nil { t.Fatalf("could not open custom test input: %v", err) } defer f.Close() data, err := ioutil.ReadAll(f) if err != nil { t.Fatalf("could not read custom test input: %v", err) } buf := bytes.NewBuffer(data) for { // read input inp, err := buf.ReadBytes('\t') if err == io.EOF { return } else if err != nil { t.Fatalf("read error: %v", err) } // read expected output exp, err := buf.ReadBytes('\n') if err == io.EOF { t.Fatalf("found input with no matching output") } else if err != nil { t.Fatalf("read error: %v", err) } if len(inp) < 2 || len(exp) < 2 { t.Fatalf("malformed input") } result := Encode(string(inp[:len(inp)-2])) if result != string(exp[:len(exp)-2]) { t.Logf("input: %s", inp) t.Logf("output: %s", result) t.Logf("expected: %s", exp) t.Fail() } } Encode(string(data)) }