From c5bcc85d9cb79c080846cb13979938c8be7c0cba Mon Sep 17 00:00:00 2001 From: Artur Malabarba Date: Thu, 18 Dec 2014 18:49:38 -0200 Subject: Test sx-assoc-let not let-alist Since let-alist is no longer defined here. It makes no sense to test its contents. --- test/tests.el | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) (limited to 'test') diff --git a/test/tests.el b/test/tests.el index 8969c37..66d8d88 100644 --- a/test/tests.el +++ b/test/tests.el @@ -123,20 +123,14 @@ (should (equal '(progn (require 'let-alist) (sx--ensure-site data) - (let ((.test (cdr (assq 'test data)))) - .test)) - (macroexpand-all - '(sx-assoc-let data - .test)))) + (let-alist data .test)) + (macroexpand '(sx-assoc-let data .test)))) (should (equal '(progn (require 'let-alist) (sx--ensure-site data) - (let ((.test-one (cdr (assq 'test-one data))) - (.test-two (cdr (assq 'test-two data)))) - (cons .test-one .test-two))) - (macroexpand-all - '(sx-assoc-let data - (cons .test-one .test-two)))))) + (let-alist data (cons .test-one .test-two))) + (macroexpand + '(sx-assoc-let data (cons .test-one .test-two)))))) (ert-deftest sx--user-@name () "Tests macro expansion for `sx-assoc-let'" -- cgit v1.2.3 From 04ffa4b891161ce98fa203812768f06808417e0d Mon Sep 17 00:00:00 2001 From: Artur Malabarba Date: Sun, 21 Dec 2014 10:34:21 -0200 Subject: Data sample --- test/data-samples/inbox-item.el | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 test/data-samples/inbox-item.el (limited to 'test') diff --git a/test/data-samples/inbox-item.el b/test/data-samples/inbox-item.el new file mode 100644 index 0000000..faeba12 --- /dev/null +++ b/test/data-samples/inbox-item.el @@ -0,0 +1,13 @@ +((title . "Can I mark inbox items as read in api v2.2?") + (link . "http://stackapps.com/posts/comments/12080?noredirect=1") + (item_type . "comment") + (question_id . 5059) + (comment_id . 12080) + (creation_date . 1419153905) + (is_unread . :json-false) + (site (site_type . "main_site") + (name . "Stack Apps") + (api_site_parameter . "stackapps") + (site_url . "http://stackapps.com") + (favicon_url . "http://cdn.sstatic.net/stackapps/img/favicon.ico") + (styling (link_color . "#0077DD") (tag_foreground_color . "#555555") (tag_background_color . "#E7ECEC")))) -- cgit v1.2.3 From b4ac5e67f55147db98e17f9e2df8d7e044b6bcdf Mon Sep 17 00:00:00 2001 From: Sean Allred Date: Thu, 1 Jan 2015 17:07:38 -0500 Subject: Remove redundant consing --- test/tests.el | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'test') diff --git a/test/tests.el b/test/tests.el index d709600..53e053f 100644 --- a/test/tests.el +++ b/test/tests.el @@ -55,7 +55,7 @@ (defun sx-test-message (message &rest args) (when sx-test-enable-messages - (apply #'message (cons message args)))) + (apply #'message message args))) (mapc #'sx-load-test '(api macros printing util)) -- cgit v1.2.3 From bf4f193a76100917764f249023f4844a2ca15b2c Mon Sep 17 00:00:00 2001 From: Sean Allred Date: Thu, 1 Jan 2015 19:01:36 -0500 Subject: Fix docstring --- test/test-printing.el | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'test') diff --git a/test/test-printing.el b/test/test-printing.el index 6225bf6..2857cb7 100644 --- a/test/test-printing.el +++ b/test/test-printing.el @@ -61,7 +61,7 @@ after being run through `sx-question--tag-format'." 2 1 "autocomplete" "performance" "ctags")))) (ert-deftest sx--user-@name () - "Test macro expansion for `sx-assoc-let'" + "Test `sx--user-@name' character substitution" (should (string= (sx--user-@name '((display_name . "ĥÞßđłřğĝýÿñńśşšŝżźžçćčĉùúûüŭůòóôõöøőðìíîïıèéêëęàåáâäãåąĵ★"))) -- cgit v1.2.3 From 8b5e430d385db04b21b917bb62935e8266819e38 Mon Sep 17 00:00:00 2001 From: Sean Allred Date: Thu, 1 Jan 2015 23:13:17 -0500 Subject: Add tests for search functionality --- test/test-search.el | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ test/tests.el | 2 +- 2 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 test/test-search.el (limited to 'test') diff --git a/test/test-search.el b/test/test-search.el new file mode 100644 index 0000000..72dbcdc --- /dev/null +++ b/test/test-search.el @@ -0,0 +1,53 @@ +(defmacro test-with-bogus-string (cell &rest body) + "Let-bind a bogus string to CELL and execute BODY." + (declare (indent 1)) + `(let ((,cell "E7631BCF-A94B-4507-8F0C-02CFB3207F55")) + ,@body)) + + +(ert-deftest test-search-basic () + "Test basic search functionality" + (should + (sx-search-get-questions + "emacs" 1 "emacs"))) + +(ert-deftest test-search-empty () + "Test bogus search returns empty vector" + (test-with-bogus-string query + (should + (equal + [] + (sx-search-get-questions "emacs" 1 query))))) + +(ert-deftest test-search-invalid () + "Test invalid search" + (should-error + ;; @todo: test the interactive call + (sx-search + "emacs" nil nil ["emacs"]))) + +(ert-deftest test-search-full-page () + "Test retrieval of the full search page" + (should + (= 30 (length (sx-search-get-questions + "stackoverflow" 1 "jquery"))))) + +(ert-deftest test-search-exclude-tags () + "Test excluding tags from a search" + (should + (cl-every + (lambda (p) + (sx-assoc-let p + (not (member "org-export" .tags)))) + (sx-search-get-questions + "emacs" 1 nil "org-mode" "org-export"))) + (should + (cl-every + (lambda (p) + (sx-assoc-let p + (not (or (member "org-export" .tags) + (member "org-agenda" .tags))))) + (sx-search-get-questions + "emacs" 1 nil "org-mode" + ["org-export" "org-agenda"])))) + diff --git a/test/tests.el b/test/tests.el index 53e053f..d06c0ff 100644 --- a/test/tests.el +++ b/test/tests.el @@ -58,4 +58,4 @@ (apply #'message message args))) (mapc #'sx-load-test - '(api macros printing util)) + '(api macros printing util search)) -- cgit v1.2.3 From 27eb38cfc4bba9013e8454bbe81ce497bf224474 Mon Sep 17 00:00:00 2001 From: Sean Allred Date: Fri, 2 Jan 2015 00:59:59 -0500 Subject: Introduce `sx-request-all-items' This function repeatedly makes API requests until a condition is satisfied (such as 'no more items'). First and foremost, this will allow us to retrieve all tags for a site. --- sx-request.el | 32 ++++++++++++++++++++++++++++++++ test/test-api.el | 5 +++++ 2 files changed, 37 insertions(+) (limited to 'test') diff --git a/sx-request.el b/sx-request.el index f1d20af..387bde5 100644 --- a/sx-request.el +++ b/sx-request.el @@ -94,6 +94,35 @@ number of requests left every time it finishes a call." ;;; Making Requests +(defun sx-request-all-items (method &optional args request-method + stop-when process-function) + "Call METHOD with ARGS until there are no more items. +STOP-WHEN is a function that takes the entire response and +returns non-nil if the process should stop. + +All other arguments are identical to `sx-request-make', but +PROCESS-FUNCTION is given the default value of `identity' (rather +than `sx-request-response-get-items') to allow STOP-WHEN to +access the response wrapper." + ;; @TODO: Refactor. This is the product of a late-night jam + ;; session... it is not intended to be model code. + (let* ((return-value []) + (current-page 1) + (stop-when (or stop-when #'sx-request-all-stop-when-no-more)) + (process-function (or process-function #'identity)) + (response + (sx-request-make method `((page . ,current-page) ,@args) + request-method process-function))) + (while (not (funcall stop-when response)) + (setq return-value + (vconcat return-value + (cdr (assoc 'items response)))) + (setq current-page (1+ current-page) + response + (sx-request-make method `((page . ,current-page) ,@args) + request-method process-function))) + (vconcat return-value + (cdr (assoc 'items response))))) (defun sx-request-make (method &optional args request-method process-function) "Make a request to the API, executing METHOD with ARGS. @@ -213,6 +242,9 @@ false, use the symbol `false'. Each element is processed with (sx-assoc-let response (sx-encoding-clean-content-deep .items))) +(defun sx-request-all-stop-when-no-more (response) + (or (not response) + (equal :json-false (cdr (assoc 'has_more response))))) (provide 'sx-request) ;;; sx-request.el ends here diff --git a/test/test-api.el b/test/test-api.el index ca775ff..b99ec7a 100644 --- a/test/test-api.el +++ b/test/test-api.el @@ -11,3 +11,8 @@ (should-error (sx-request-make "questions" '(())))) +(ert-deftest test-request-all () + "Test request all items" + (should + (< 250 + (length (sx-request-all-items "sites"))))) -- cgit v1.2.3 From a11be51a7aa5ea26e3d55383d328a0624b924125 Mon Sep 17 00:00:00 2001 From: Sean Allred Date: Fri, 2 Jan 2015 12:02:27 -0500 Subject: Use .site_par in tests In accordance with d51791fb6ae69e3b426f7d598920b4349858908f --- test/test-macros.el | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'test') diff --git a/test/test-macros.el b/test/test-macros.el index 2169088..b6bf20b 100644 --- a/test/test-macros.el +++ b/test/test-macros.el @@ -9,11 +9,11 @@ "Test `sx-assoc-let'" (sx-test-with-json-data data (should - (null (let-alist data .site)))) + (null (let-alist data .site_par)))) (sx-test-with-json-data data (should - (equal (sx-assoc-let data .site) + (equal (sx-assoc-let data .site_par) "meta.emacs"))) (sx-test-with-json-data data -- cgit v1.2.3 From dea60197cf658925827ccf3d10080dc4c0a22c40 Mon Sep 17 00:00:00 2001 From: Sean Allred Date: Fri, 2 Jan 2015 19:35:33 -0500 Subject: Test new sx-method functionality --- test/test-api.el | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'test') diff --git a/test/test-api.el b/test/test-api.el index b99ec7a..8e71eb2 100644 --- a/test/test-api.el +++ b/test/test-api.el @@ -16,3 +16,7 @@ (should (< 250 (length (sx-request-all-items "sites"))))) + +(ert-deftest test-method-get-all () + "Tests sx-method interface to `sx-request-all-items'" + (should (< 250 (sx-method-call 'sites :get-all t)))) -- cgit v1.2.3 From 63dcca1acd08c0d103196a5d35a581d5380d3717 Mon Sep 17 00:00:00 2001 From: Sean Allred Date: Fri, 2 Jan 2015 19:37:07 -0500 Subject: Fix test Dumb mistake -- I wasn't taking the length of the request before comparing it. --- test/test-api.el | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'test') diff --git a/test/test-api.el b/test/test-api.el index 8e71eb2..91a8adb 100644 --- a/test/test-api.el +++ b/test/test-api.el @@ -19,4 +19,4 @@ (ert-deftest test-method-get-all () "Tests sx-method interface to `sx-request-all-items'" - (should (< 250 (sx-method-call 'sites :get-all t)))) + (should (< 250 (length (sx-method-call 'sites :get-all t))))) -- cgit v1.2.3 From 53fdb88732568fb9c9e5d682f88371e602064d42 Mon Sep 17 00:00:00 2001 From: Sean Allred Date: Fri, 2 Jan 2015 20:07:34 -0500 Subject: Add test file for state changes --- test/test-state.el | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 test/test-state.el (limited to 'test') diff --git a/test/test-state.el b/test/test-state.el new file mode 100644 index 0000000..32f52b2 --- /dev/null +++ b/test/test-state.el @@ -0,0 +1,12 @@ +(defmacro with-question-data (cell id &rest body) + (declare (indent 2)) + `(let ((,cell '((question_id . ,id) + (site_par . "emacs") + (last_activity_date . 1234123456)))) + ,@body)) + +(ert-deftest test-question-mark-read () + "00ccd139248e782cd8316eff65c26aed838c7e46" + (should + (with-question-data q nil + (sx-question--mark-read q)))) -- cgit v1.2.3 From b5e4da2a4b6f59e28b1aca11f7dad9323df1a8fe Mon Sep 17 00:00:00 2001 From: Sean Allred Date: Fri, 2 Jan 2015 22:14:37 -0500 Subject: Don't display status messages from url during test --- test/tests.el | 1 + 1 file changed, 1 insertion(+) (limited to 'test') diff --git a/test/tests.el b/test/tests.el index 53e053f..bb2f9f4 100644 --- a/test/tests.el +++ b/test/tests.el @@ -11,6 +11,7 @@ sx-initialized t sx-request-remaining-api-requests-message-threshold 50000 debug-on-error t + url-show-status nil user-emacs-directory "." sx-test-base-dir (file-name-directory (or load-file-name "./"))) -- cgit v1.2.3 From aea08be5b6bf4286dacc8ae12f912f36223352f8 Mon Sep 17 00:00:00 2001 From: Sean Allred Date: Sat, 3 Jan 2015 23:16:05 -0500 Subject: Change test to reflect new default page size See eeb327e3af94e1118218c129916b8f7165e835ef. --- test/test-search.el | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'test') diff --git a/test/test-search.el b/test/test-search.el index 72dbcdc..72f0846 100644 --- a/test/test-search.el +++ b/test/test-search.el @@ -29,8 +29,8 @@ (ert-deftest test-search-full-page () "Test retrieval of the full search page" (should - (= 30 (length (sx-search-get-questions - "stackoverflow" 1 "jquery"))))) + (= 100 (length (sx-search-get-questions + "stackoverflow" 1 "jquery"))))) (ert-deftest test-search-exclude-tags () "Test excluding tags from a search" -- cgit v1.2.3 From ea2e7287b1c4965ab30da749624a06d5f4e5a9e3 Mon Sep 17 00:00:00 2001 From: Sean Allred Date: Sat, 3 Jan 2015 23:16:58 -0500 Subject: Remove unneeded test This test is largely unneeded and (currently) almost doubles the test time. --- test/test-api.el | 6 ------ 1 file changed, 6 deletions(-) (limited to 'test') diff --git a/test/test-api.el b/test/test-api.el index 91a8adb..b7d5dbb 100644 --- a/test/test-api.el +++ b/test/test-api.el @@ -11,12 +11,6 @@ (should-error (sx-request-make "questions" '(())))) -(ert-deftest test-request-all () - "Test request all items" - (should - (< 250 - (length (sx-request-all-items "sites"))))) - (ert-deftest test-method-get-all () "Tests sx-method interface to `sx-request-all-items'" (should (< 250 (length (sx-method-call 'sites :get-all t))))) -- cgit v1.2.3 From 5332946380610d5166c2d7b517a817e021130edd Mon Sep 17 00:00:00 2001 From: Artur Malabarba Date: Sun, 4 Jan 2015 11:30:04 -0200 Subject: More tests --- test/test-state.el | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'test') diff --git a/test/test-state.el b/test/test-state.el index 32f52b2..7af4a64 100644 --- a/test/test-state.el +++ b/test/test-state.el @@ -7,6 +7,16 @@ (ert-deftest test-question-mark-read () "00ccd139248e782cd8316eff65c26aed838c7e46" + (with-question-data q 10 + ;; Check basic logic. + (should (sx-question--mark-read q)) + (should (sx-question--read-p q)) + (should (not (setcdr (assq 10 (cdr (assoc "emacs" sx-question--user-read-list))) nil))) + ;; Don't freak out because the cdr was nil. + (should (not (sx-question--read-p q))) + (should (sx-question--mark-read q))) (should (with-question-data q nil + ;; Don't freak out because question_id was nil. (sx-question--mark-read q)))) + -- cgit v1.2.3 From 59327f6f9c6ba419543c47670a62af0aa41271ca Mon Sep 17 00:00:00 2001 From: Sean Allred Date: Sun, 4 Jan 2015 11:41:30 -0500 Subject: Tree-style specification for filter-from-nil Instead of saying (field-a field-b object-a.subfield field-c object-b.subfield-a object-b.subfield-b) you can rather say (field-a field-b (object-a subfield) field-c (object-b subfield-a subfield-b)) To avoid a dependency loop, sx-browse-filter has been moved to sx-filter.el. --- sx-filter.el | 87 +++++++++++++++++++++++++++++++++++------------------ sx-networks.el | 26 ++++++++-------- sx-site.el | 12 ++++---- sx.el | 61 ++++++++++++------------------------- test/test-macros.el | 22 ++++++++++++++ test/test-util.el | 14 +++++++++ 6 files changed, 132 insertions(+), 90 deletions(-) (limited to 'test') diff --git a/sx-filter.el b/sx-filter.el index ad37e67..d484f43 100644 --- a/sx-filter.el +++ b/sx-filter.el @@ -42,39 +42,26 @@ Structure: ;;; Creation - (defmacro sx-filter-from-nil (included) "Creates a filter data structure with INCLUDED fields. All wrapper fields are included by default." - ;; @OTODO: it would be neat to have syntax like - ;; - ;; (field-a - ;; field-b - ;; (object-a subfield) - ;; field-c - ;; (object-b subfield-a subfield-b)) - ;; - ;; expand into - ;; - ;; (field-a - ;; field-b - ;; object-a.subfield - ;; field-c - ;; object-b.subfield-a object-b.subfield-b) - `(quote ((,@included - .backoff - .error_id - .error_message - .error_name - .has_more - .items - .page - .page_size - .quota_max - .quota_remaining - .total - .type) - nil none))) + `(quote + ((,@(sx--tree-expand + (lambda (path) + (intern (mapconcat #'symbol-name path "."))) + included) + .backoff + .error_id + .error_message + .error_name + .has_more + .items + .page + .page_size + .quota_max + .quota_remaining + .total) + nil none))) ;;; @TODO allow BASE to be a precompiled filter name (defun sx-filter-compile (&optional include exclude base) @@ -114,6 +101,46 @@ return the compiled filter." (sx-cache-set 'filter sx--filter-alist) filter)))) + +;;; Browsing filter +(defvar sx-browse-filter + (sx-filter-from-nil + ((question body_markdown + comments + answers + last_editor + last_activity_date + accepted_answer_id + link + upvoted + downvoted + question_id + share_link) + (user display_name) + (comment owner + body_markdown + body + link + edited + creation_date + upvoted + score + post_type + post_id + comment_id) + (answer answer_id + last_editor + last_activity_date + link + share_link + owner + body_markdown + upvoted + downvoted + comments))) + "The filter applied when retrieving question data. +See `sx-question-get-questions' and `sx-question-get-question'.") + (provide 'sx-filter) ;;; sx-filter.el ends here diff --git a/sx-networks.el b/sx-networks.el index 20ac65c..58ebff5 100644 --- a/sx-networks.el +++ b/sx-networks.el @@ -28,19 +28,19 @@ (defvar sx-network--user-filter (sx-filter-from-nil - (badge_count.bronze - badge_count.silver - badge_count.gold - network_user.account_id - network_user.answer_count - network_user.badge_counts - network_user.creation_date - network_user.last_access_date - network_user.reputation - network_user.site_name - network_user.site_url - network_user.user_id - network_user.user_type))) + ((badge_count bronze + silver + gold) + (network_user account_id + answer_count + badge_counts + creation_date + last_access_date + reputation + site_name + site_url + user_id + user_type)))) (defun sx-network--get-associated () "Retrieve cached information for network user. diff --git a/sx-site.el b/sx-site.el index 1bc86a6..2f0a31d 100644 --- a/sx-site.el +++ b/sx-site.el @@ -27,12 +27,12 @@ (defvar sx-site-browse-filter (sx-filter-from-nil - (site.site_type - site.name - site.api_site_parameter - site.related_sites - related_site.api_site_parameter - related_site.relation)) + ((site site_type + name + api_site_parameter + related_sites) + (related_site api_site_parameter + relation))) "Filter for browsing sites.") (defun sx-site--get-site-list () diff --git a/sx.el b/sx.el index 3aa87e7..f77b313 100644 --- a/sx.el +++ b/sx.el @@ -136,6 +136,26 @@ with a `link' property)." result)) result)) +(defun sx--tree-paths (tree) + "Return a list of all paths in TREE. +Adapted from http://stackoverflow.com/q/3019250." + (if (atom tree) + (list (list tree)) + (apply #'append + (mapcar (lambda (node) + (mapcar (lambda (path) + (cons (car tree) path)) + (sx--tree-paths node))) + (cdr tree))))) + +(defun sx--tree-expand (path-func tree) + "Apply PATH-FUNC to every path in TREE. +Return the result. See `sx--tree-paths'." + (mapcar path-func + (apply #'append + (mapcar #'sx--tree-paths + tree)))) + (defmacro sx-assoc-let (alist &rest body) "Use ALIST with `let-alist' to execute BODY. `.site_par' has a special meaning, thanks to `sx--ensure-site'. @@ -148,47 +168,6 @@ If ALIST doesn't have a `site' property, one is created using the ,(macroexpand `(let-alist ,alist ,@body)))) - -;;; Browsing filter -(defvar sx-browse-filter - ;; @TODO: Use `sx-filter-from-nil' - '((question.body_markdown - question.comments - question.answers - question.last_editor - question.last_activity_date - question.accepted_answer_id - question.link - question.upvoted - question.downvoted - question.question_id - question.share_link - user.display_name - comment.owner - comment.body_markdown - comment.body - comment.link - comment.edited - comment.creation_date - comment.upvoted - comment.score - comment.post_type - comment.post_id - comment.comment_id - answer.answer_id - answer.last_editor - answer.last_activity_date - answer.link - answer.share_link - answer.owner - answer.body_markdown - answer.upvoted - answer.downvoted - answer.comments) - (user.profile_image shallow_user.profile_image)) - "The filter applied when retrieving question data. -See `sx-question-get-questions' and `sx-question-get-question'.") - ;;; Utility Functions (defun sx-completing-read (&rest args) diff --git a/test/test-macros.el b/test/test-macros.el index b6bf20b..1634603 100644 --- a/test/test-macros.el +++ b/test/test-macros.el @@ -20,3 +20,25 @@ (should (equal (sx-assoc-let data (cons .test-one .test-two)) '(1 . 2))))) + +(ert-deftest macro-test--sx-filter-from-nil () + "Test `sx-filter-from-nil'" + (should + (equal + (sx-filter-from-nil + (one two (three four five) (six seven) + (a b c d e))) + '((one two three.four three.five six.seven + a.b a.c a.d a.e + .backoff + .error_id + .error_message + .error_name + .has_more + .items + .page + .page_size + .quota_max + .quota_remaining + .total) + nil none)))) diff --git a/test/test-util.el b/test/test-util.el index 5db1691..1e3dc2b 100644 --- a/test/test-util.el +++ b/test/test-util.el @@ -29,3 +29,17 @@ (string= (sx--thing-as-string 'test& nil t) "test%26"))) + +(ert-deftest tree () + (should + (equal + (sx--tree-expand + (lambda (path) (mapconcat #'symbol-name path ".")) + '(a b (c d (e f g) h i (j k) l) m (n o) p)) + '("a" "b" "c.d" "c.e.f" "c.e.g" "c.h" "c.i" "c.j.k" "c.l" "m" "n.o" "p"))) + (should + (equal + (sx--tree-expand + (lambda (path) (intern (mapconcat #'symbol-name path "/"))) + '(a b (c d (e f g) h i (j k) l) m (n o) p)) + '(a b c/d c/e/f c/e/g c/h c/i c/j/k c/l m n/o p)))) -- cgit v1.2.3