// Chapter 34 example: tests for both the good and the bad cases. package percent import ( "errors" "testing" ) func TestParseGood(t *testing.T) { tests := []struct { text string want int }{ {"0", 0}, {"85", 85}, {"85%", 85}, {" 100 ", 100}, } for _, tc := range tests { t.Run(tc.text, func(t *testing.T) { got, err := Parse(tc.text) if err != nil { t.Fatalf("Parse(%q) gave the error %v", tc.text, err) } if got != tc.want { t.Errorf("Parse(%q) = %d, want %d", tc.text, got, tc.want) } }) } } func TestParseNotANumber(t *testing.T) { _, err := Parse("abc") if err == nil { t.Fatal("Parse(\"abc\") gave no error") } } func TestParseOutOfRange(t *testing.T) { for _, text := range []string{"-1", "101", "150%"} { t.Run(text, func(t *testing.T) { _, err := Parse(text) if !errors.Is(err, ErrOutOfRange) { t.Errorf("Parse(%q) error = %v, want ErrOutOfRange", text, err) } }) } }