diff options
-rw-r--r-- | .travis.yml | 2 | ||||
-rw-r--r-- | stack-core.el | 32 | ||||
-rw-r--r-- | test/tests.el | 68 | ||||
-rw-r--r-- | test/util.el | 12 | ||||
-rw-r--r-- | tests.el | 29 |
5 files changed, 78 insertions, 65 deletions
diff --git a/.travis.yml b/.travis.yml index b81771d..63f0cb6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -21,7 +21,7 @@ before_install: - cask --version script: - - emacs -batch -L . -l ert -l tests.el -f ert-run-tests-batch-and-exit + - emacs --batch -L . -l ert -l test/tests.el -f ert-run-tests-batch-and-exit notifications: webhooks: diff --git a/stack-core.el b/stack-core.el index d48c873..9a535cd 100644 --- a/stack-core.el +++ b/stack-core.el @@ -92,6 +92,10 @@ recent call. Set by `stack-core-make-request'.") number, `stack-core-make-request' will begin printing out the number of requests left every time it finishes a call.") +(defcustom stack-core-silent-requests + t + "When `t', requests default to being silent.") + ;;; Keyword Arguments @@ -151,6 +155,7 @@ optional KEYWORD-ARGUMENTS. If no KEYWORD-ARGUMENTS are given, entire response as a complex alist." (let ((url-automatic-caching stack-core-cache-requests) (url-inhibit-uncompression t) + (silent (or silent stack-core-silent-requests)) (call (stack-core-build-request method @@ -209,5 +214,32 @@ entire response as a complex alist." stack-core-remaining-api-requests)) (cdr (assoc 'items response)))))))) +(defun stack-core-filter-data (data desired-tree) + "Filters DATA and returns the DESIRED-TREE" + (if (vectorp data) + (apply #'vector + (mapcar (lambda (entry) + (stack-core-filter-data + entry desired-tree)) + data)) + (delq + nil + (mapcar (lambda (cons-cell) + ;; TODO the resolution of `f' is O(2n) in the worst + ;; case. It may be faster to implement the same + ;; functionality as a `while' loop to stop looking the + ;; list once it has found a match. Do speed tests. + ;; See edfab4443ec3d376c31a38bef12d305838d3fa2e. + (let ((f (or (memq (car cons-cell) desired-tree) + (assoc (car cons-cell) desired-tree)))) + (when f + (if (and (sequencep (cdr cons-cell)) + (sequencep (elt (cdr cons-cell) 0))) + (cons (car cons-cell) + (stack-core-filter-data + (cdr cons-cell) (cdr f))) + cons-cell)))) + data)))) + (provide 'stack-core) ;;; stack-core.el ends here diff --git a/test/tests.el b/test/tests.el index e8452af..44b0de0 100644 --- a/test/tests.el +++ b/test/tests.el @@ -3,7 +3,22 @@ (mapatoms (lambda (symbol) (if (string-prefix-p "stack-" (symbol-name symbol)) - (unintern symbol))))) + (unintern symbol))))) + +(defun stack-test-sample-data (method &optional directory) + (with-current-buffer + (find-file-noselect + (concat "data-samples/" + (when directory (concat directory "/")) + method ".el")) + (eval (read (if (string-equal "" (buffer-string)) + "'no-value" + (buffer-string)))))) + +(setq stack-test-data-questions + (stack-test-sample-data "questions") + stack-test-data-sites + (stack-test-sample-data "sites")) ;;; Tests @@ -26,30 +41,37 @@ (should-error (stack-core-make-request "questions" '(())))) -(ert-deftest test-data-filter-1 () - "Test the meta-convenience function -- flat structure" +(ert-deftest test-tree-filter () + "`stack-core-filter-data'" + ;; flat (should (equal '((1 . t) (2 . [1 2]) (3)) - (stack-core-filter-data '((0 . 3) - (1 . t) - (a . five) - (2 . [1 2]) - ("5" . bop) - (3) - (p . 4)) - '(1 2 3))))) - -(ert-deftest test-data-filter-2 () - "Test the meta-convenience function -- complex structure" + (stack-core-filter-data '((0 . 3) (1 . t) (a . five) (2 . [1 2]) + ("5" . bop) (3) (p . 4)) + '(1 2 3)))) + ;; complex + (should + (equal + '((1 . [a b c]) + (2 . [((a . 1) (c . 3)) + ((a . 4) (c . 6))]) + (3 . peach)) + (stack-core-filter-data '((1 . [a b c]) + (2 . [((a . 1) (b . 2) (c . 3)) + ((a . 4) (b . 5) (c . 6))]) + (3 . peach) + (4 . banana)) + '(1 (2 a c) 3)))) + + ;; vector (should (equal - '([()]) - (stack-core-filter-data '((0 . 3) - (1 . t) - (a . five) - (2 . [1 2]) - ("5" . bop) - (3) - (p . 4)) - '(1 2 3))))) + [((1 . 2) (2 . 3) (3 . 4)) + ((1 . a) (2 . b) (3 . c)) + nil ((1 . alpha) (2 . beta))] + (stack-core-filter-data [((1 . 2) (2 . 3) (3 . 4)) + ((1 . a) (2 . b) (3 . c) (5 . seven)) + ((should-not-go)) + ((1 . alpha) (2 . beta))] + '(1 2 3))))) diff --git a/test/util.el b/test/util.el deleted file mode 100644 index 7d5937f..0000000 --- a/test/util.el +++ /dev/null @@ -1,12 +0,0 @@ -(defmacro stack-test-get-sample-data (method &optional directory) - (with-current-buffer - (find-file-noselect - (concat "data-samples/" - (when directory (concat directory "/")) - method ".el")) - (eval (read (buffer-string))))) - -(setq stack-test-data-questions - (stack-test-get-sample-data "questions") - stack-test-data-sites - (stack-test-get-sample-data "sites")) diff --git a/tests.el b/tests.el deleted file mode 100644 index 379c903..0000000 --- a/tests.el +++ /dev/null @@ -1,29 +0,0 @@ -(defun -stack--nuke () - (interactive) - (mapatoms - (lambda (symbol) - (if (string-prefix-p "stack-" (symbol-name symbol)) - (unintern symbol))))) - -;;; Tests - -(setq stack-core-remaining-api-requests-message-threshold 1000000000) -(setq debug-on-error t) - -(require 'stack-core) -(require 'stack-question) - -(setq stack-core-remaining-api-requests-message-threshold 50000) - -(ert-deftest test-basic-request () - "Test basic request functionality" - (should (stack-core-make-request "sites"))) - -(ert-deftest test-question-retrieve () - "Test the ability to receive a list of questions." - (should (stack-question-get-questions 'emacs))) - -(ert-deftest test-bad-request () - "Test a method given a bad set of keywords" - (should-error - (stack-core-make-request "questions" '(())))) |