Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore question if it is available only for premium plan #13

Merged
merged 2 commits into from
Oct 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"time"
)

var PaidOnlyError = fmt.Errorf("not support premium question for now")

type LeetCode struct {
BaseURL string
session, token string
Expand Down Expand Up @@ -71,6 +73,9 @@ func (lc *LeetCode) GetQuestionByFrontendID(ctx context.Context, id int) (*Quest
if pair == nil {
return nil, fmt.Errorf("cannot find problem")
}
if pair.PaidOnly {
return nil, PaidOnlyError
}

q, err := lc.GetQuestion(ctx, pair.Stat)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions core.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ var CmdMap = map[CmdName]Cmd{
HELP: &HelpCmd{},
}

func buildPath(id, slug string) string {
func buildPath(id int, slug string) string {
// TODO: changeable directory
format := "%s.%s.go"
format := "%d.%s.go"
return fmt.Sprintf(format, id, slug)
}
2 changes: 1 addition & 1 deletion exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (c *ExecCmd) Run(ctx context.Context, out io.Writer, args []string) error {
if err != nil {
return err
}
fp := buildPath(q.QuestionID, q.Slug)
fp := buildPath(q.FrontendQuestionID, q.Slug)
code, err := ioutil.ReadFile(fp)
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (g *GenerateCmd) Run(ctx context.Context, out io.Writer, args []string) err
log.Printf("%s", buf.String())

// TODO: どうやってファイル保存とテストしやすさを分けようか?
path := buildPath(q.QuestionID, q.Slug)
path := buildPath(q.FrontendQuestionID, q.Slug)
fmt.Fprintf(out, "save at %q\n", path)
if err := ioutil.WriteFile(path, buf.Bytes(), 0644); err != nil {
return err
Expand Down
21 changes: 20 additions & 1 deletion generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func TestGenerateCmd(t *testing.T) {
name string
args []string
}{
{name: "SuccessGenerateFiles", args: []string{"1"}},
{name: "SuccessGenerateFiles", args: []string{"2"}},
}
for _, tt := range tests {
tt := tt
Expand All @@ -24,3 +24,22 @@ func TestGenerateCmd(t *testing.T) {
})
}
}

func TestGenerateCmd_Error(t *testing.T) {
tests := [...]struct {
name string
args []string
want error
}{
{name: "FailedPaidOnlyProblem", args: []string{"1602"}, want: PaidOnlyError},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
cmd := &GenerateCmd{}
if err := cmd.Run(context.TODO(), ioutil.Discard, tt.args); err != tt.want {
t.Errorf("GenerateCmd() error = %v", err)
}
})
}
}
2 changes: 1 addition & 1 deletion test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (c *TestCmd) Run(ctx context.Context, out io.Writer, args []string) error {
if err != nil {
return err
}
fp := buildPath(q.QuestionID, q.Slug)
fp := buildPath(q.FrontendQuestionID, q.Slug)
code, err := ioutil.ReadFile(fp)
if err != nil {
return err
Expand Down