Skip to content

Commit 6aeed4b

Browse files
committed
Add login test
1 parent b2f2b76 commit 6aeed4b

File tree

4 files changed

+133
-65
lines changed

4 files changed

+133
-65
lines changed

.gitignore

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*.~undo-tree
1212

1313
# ignore these dirty local tests.
14+
test-account
1415
test.el
15-
test-leetcode-account.el
16-
problem.graphql
16+
*.graphql
1717
*.http

leetcode-tests.el

-11
This file was deleted.

leetcode.el

+52-52
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,9 @@ When ACCOUNT or PASSWORD is empty string it will show a prompt."
169169
("login" . ("" :data ,account))
170170
("password" . ("" :data ,password)))
171171
:success
172-
(cl-function (lambda (&key data &allow-other-keys)
173-
(leetcode-global-loading-mode -1)))
172+
(cl-function
173+
(lambda (&key data &allow-other-keys)
174+
(leetcode-global-loading-mode -1)))
174175
:error
175176
(cl-function
176177
(lambda (&rest args &key error-thrown &allow-other-keys)
@@ -187,45 +188,44 @@ When ACCOUNT or PASSWORD is empty string it will show a prompt."
187188
(request-cookie-alist
188189
(concat "." leetcode--domain) "/" t)))))
189190

190-
(defun leetcode--set-user-and-problems (response)
191+
(defun leetcode--set-user-and-problems (res)
191192
"Set `leetcode--user' and `leetcode--problems'.
192-
If user isn't login, only `leetcode--problems' will be set.
193-
RESPONSE is a request.el response."
194-
(let ((data (request-response-data response)))
195-
;; user
196-
(setq leetcode--user (plist-put leetcode--user :username (alist-get 'user_name data)))
197-
(setq leetcode--user (plist-put leetcode--user :solved (alist-get 'num_solved data)))
198-
(setq leetcode--user (plist-put leetcode--user :easy (alist-get 'ac_easy data)))
199-
(setq leetcode--user (plist-put leetcode--user :medium (alist-get 'ac_medium data)))
200-
(setq leetcode--user (plist-put leetcode--user :hard (alist-get 'ac_hard data)))
201-
;; problem list
202-
(setq leetcode--problems (plist-put leetcode--problems :num (alist-get 'num_total data)))
203-
(setq leetcode--problems (plist-put leetcode--problems :tag "all"))
204-
(setq leetcode--problems
205-
(plist-put leetcode--problems :problems
206-
(let ((raw-vec (alist-get 'stat_status_pairs data))
207-
(len (plist-get leetcode--problems :num))
208-
problems)
209-
(dolist (i (number-sequence 0 (1- len)))
210-
(let* ((cur (aref raw-vec i))
211-
(stat (alist-get 'stat cur))
212-
(status (alist-get 'status cur))
213-
(difficulty (alist-get 'level (alist-get 'difficulty cur)))
214-
(paid-only (eq (alist-get 'paid_only cur) t))
215-
(question-id (alist-get 'question_id stat))
216-
(total-submitted (alist-get 'total_submitted stat))
217-
(total-acs (alist-get 'total_acs stat)))
218-
(push
219-
(list
220-
:status status
221-
:id question-id
222-
:pos (- len i)
223-
:title (alist-get 'question__title stat)
224-
:acceptance (format "%.1f%%" (* 100 (/ (float total-acs) total-submitted)))
225-
:difficulty difficulty
226-
:paid-only paid-only)
227-
problems)))
228-
problems)))))
193+
If user isn't login, only `leetcode--problems' will be set. RES
194+
is an alist comes from `leetcode--api-all-problems'."
195+
;; user
196+
(setq leetcode--user (plist-put leetcode--user :username (alist-get 'user_name res)))
197+
(setq leetcode--user (plist-put leetcode--user :solved (alist-get 'num_solved res)))
198+
(setq leetcode--user (plist-put leetcode--user :easy (alist-get 'ac_easy res)))
199+
(setq leetcode--user (plist-put leetcode--user :medium (alist-get 'ac_medium res)))
200+
(setq leetcode--user (plist-put leetcode--user :hard (alist-get 'ac_hard res)))
201+
;; problem list
202+
(setq leetcode--problems (plist-put leetcode--problems :num (alist-get 'num_total res)))
203+
(setq leetcode--problems (plist-put leetcode--problems :tag "all"))
204+
(setq leetcode--problems
205+
(plist-put leetcode--problems :problems
206+
(let ((raw-vec (alist-get 'stat_status_pairs res))
207+
(len (plist-get leetcode--problems :num))
208+
problems)
209+
(dolist (i (number-sequence 0 (1- len)))
210+
(let* ((cur (aref raw-vec i))
211+
(status (alist-get 'status cur))
212+
(stat (alist-get 'stat cur))
213+
(question-id (alist-get 'question_id stat))
214+
(total-submitted (alist-get 'total_submitted stat))
215+
(total-acs (alist-get 'total_acs stat))
216+
(difficulty (alist-get 'level (alist-get 'difficulty cur)))
217+
(paid-only (eq (alist-get 'paid_only cur) t)))
218+
(push
219+
(list
220+
:status status
221+
:id question-id
222+
:pos (- len i)
223+
:title (alist-get 'question__title stat)
224+
:acceptance (format "%.1f%%" (* 100 (/ (float total-acs) total-submitted)))
225+
:difficulty difficulty
226+
:paid-only paid-only)
227+
problems)))
228+
problems))))
229229

230230
(defun leetcode--slugify-title (title)
231231
"Make TITLE a slug title.
@@ -305,7 +305,7 @@ row."
305305
(defun leetcode--problems-rows ()
306306
"Generate tabulated list rows from `leetcode--problems'.
307307
Return a list of rows, each row is a vector:
308-
([<checkmark> <position> <acceptance> <difficulty>] ...)"
308+
\([<checkmark> <position> <title> <acceptance> <difficulty>] ...)"
309309
(let ((problems (reverse (plist-get leetcode--problems :problems)))
310310
(easy-tag "easy")
311311
(medium-tag "medium")
@@ -355,7 +355,8 @@ Return a list of rows, each row is a vector:
355355
,(leetcode--referer leetcode--url-login))
356356
:parser 'json-read)
357357
(deferred:nextc it
358-
(lambda (response) (leetcode--set-user-and-problems response)))
358+
(lambda (response)
359+
(leetcode--set-user-and-problems (request-response-data response))))
359360
(deferred:nextc it
360361
(lambda ()
361362
(let* ((header-names '(" " "#" "Problem" "Acceptance" "Difficulty"))
@@ -379,18 +380,17 @@ Return a list of rows, each row is a vector:
379380
(interactive)
380381
(if (get-buffer leetcode--buffer-name)
381382
(switch-to-buffer leetcode--buffer-name)
382-
(if (leetcode--login-p)
383-
(deferred:$
383+
(deferred:$
384+
(if (leetcode--login-p)
385+
(deferred:next
386+
(lambda ()
387+
(message "User have been login in.")))
388+
(leetcode--login leetcode-account leetcode-password))
389+
(deferred:nextc it
390+
(lambda ()
384391
(deferred:nextc (leetcode-problems-refresh)
385392
(lambda ()
386-
(switch-to-buffer leetcode--buffer-name))))
387-
(deferred:$
388-
(leetcode--login leetcode-account leetcode-password)
389-
(deferred:nextc it
390-
(lambda ()
391-
(deferred:nextc (leetcode-problems-refresh)
392-
(lambda ()
393-
(switch-to-buffer leetcode--buffer-name)))))))))
393+
(switch-to-buffer leetcode--buffer-name))))))))
394394

395395
(defun leetcode--buffer-content (buf)
396396
"Get content without text properties of BUF."

test/leetcode-tests.el

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
;;; leetcode-tests.el --- Test for leetcode.el -*- lexical-binding: t; -*-
2+
3+
;; Copyright (C) 2019 Wang Kai
4+
5+
;; Author: Wang Kai <[email protected]>
6+
;; Keywords: extensions
7+
8+
;; This program is free software; you can redistribute it and/or modify
9+
;; it under the terms of the GNU General Public License as published by
10+
;; the Free Software Foundation, either version 3 of the License, or
11+
;; (at your option) any later version.
12+
13+
;; This program is distributed in the hope that it will be useful,
14+
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
;; GNU General Public License for more details.
17+
18+
;; You should have received a copy of the GNU General Public License
19+
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
20+
21+
;;; Commentary:
22+
;; Test for leetcode.el
23+
;;; Code:
24+
25+
(require 'leetcode)
26+
27+
(ert-deftest leetcode-test-slugify-title ()
28+
(should (equal (leetcode--slugify-title "Two Sum") "two-sum"))
29+
(should (equal (leetcode--slugify-title "Reverse Nodes in k-Group") "reverse-nodes-in-k-group"))
30+
(should (equal (leetcode--slugify-title "Pow(x, n)") "powx-n"))
31+
(should (equal (leetcode--slugify-title "Implement strStr()") "implement-strstr"))
32+
(should (equal (leetcode--slugify-title "String to Integer (atoi)") "string-to-integer-atoi")))
33+
34+
(ert-deftest leetcode-test-fetch-csrftoken ()
35+
(should (equal (leetcode--csrf-token) (leetcode--csrf-token))))
36+
37+
(ert-deftest leetcode-test-login ()
38+
(let ((leetcode-account
39+
(with-temp-buffer
40+
(insert-file-contents "test-account")
41+
(car (split-string (string-trim (buffer-string)) "\n"))))
42+
(leetcode-password
43+
(with-temp-buffer
44+
(insert-file-contents "test-account")
45+
(cadr (split-string (string-trim (buffer-string)) "\n")))))
46+
(setq leetcode--user nil)
47+
(setq leetcode--problems nil)
48+
(should (equal (leetcode--login-p) nil))
49+
(deferred:sync! (leetcode--login leetcode-account leetcode-password))
50+
(deferred:sync! (leetcode-problems-refresh))
51+
(should (equal (not (leetcode--login-p)) nil))
52+
(setq leetcode--user nil)
53+
(setq leetcode--problems nil)
54+
(kill-buffer (get-buffer leetcode--buffer-name))))
55+
56+
(ert-deftest leetcode-test-make-tabulated-headers ()
57+
(should (equal (leetcode--make-tabulated-headers
58+
'(" " "#" "Problem" "Acceptance" "Difficulty")
59+
'(["" "1" " Two Sum" "43.3%" "easy"]
60+
["" "2" " Add Two Numbers" "30.9%" "medium"]
61+
["" "3" "Longest Substring Without Repeating Characters" "28.2%" "medium"]
62+
["" "4" "Median of Two Sorted Arrays" "26.0%" "hard"]
63+
["" "5" "Longest Palindromic Substring" "27.0%" "medium"]
64+
[" " "6" "ZigZag Conversion" "31.3%" "medium"]))
65+
[(" " 1 nil) ("#" 1 nil) ("Problem" 46 nil) ("Acceptance" 10 nil) ("Difficulty" 10 nil)])))
66+
67+
(ert-deftest leetcode-test-get-code-buffer-name ()
68+
(let ((leetcode-prefer-language "c"))
69+
(should (equal (leetcode--get-code-buffer-name "String to Integer (atoi)")
70+
"string-to-integer-atoi.c")))
71+
(let ((leetcode-prefer-language "java"))
72+
(should (equal (leetcode--get-code-buffer-name "Pow(x, n)")
73+
"powx-n.java")))
74+
(let ((leetcode-prefer-language "python3"))
75+
(should (equal (leetcode--get-code-buffer-name "Reverse Nodes in k-Group")
76+
"reverse-nodes-in-k-group.py"))))
77+
78+
(provide 'leetcode-tests)
79+
;;; leetcode-tests.el ends here

0 commit comments

Comments
 (0)