Skip to content

Commit

Permalink
Added repositories descriptions
Browse files Browse the repository at this point in the history
Changed cache file format, file now contains printed hash-table.
Helm candidates now contains repository's descriptions.
  • Loading branch information
Sliim committed Nov 23, 2013
1 parent 99abd04 commit 66e4bf3
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 26 deletions.
16 changes: 10 additions & 6 deletions helm-github-stars.el
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@
(insert (s-join "\n" (hgs/get-github-stars))))))
(candidates-in-buffer)
(action . (lambda (candidate)
(browse-url (concat hgs/github-url candidate)))))
(let ((repo (substring candidate 0 (string-match " - " candidate))))
(browse-url (concat hgs/github-url repo))))))
"Helm source definition.")

(defvar hgs/helm-c-source-repos
Expand All @@ -83,7 +84,8 @@
(insert (s-join "\n" (hgs/get-github-repos))))))
(candidates-in-buffer)
(action . (lambda (candidate)
(browse-url (concat hgs/github-url candidate)))))
(let ((repo (substring candidate 0 (string-match " - " candidate))))
(browse-url (concat hgs/github-url repo))))))
"Helm source definition.")

(defvar hgs/helm-c-source-search
Expand All @@ -96,14 +98,13 @@
"Read cache file and return list of starred repositories."
(with-temp-buffer
(insert-file-contents helm-github-stars-cache-file)
(let ((json-object-type 'hash-table))
(json-read-from-string (buffer-string)))))
(read (current-buffer))))

(defun hgs/write-cache-file (hash)
"Write HASH of repositories in cache file."
(with-temp-buffer
(let ((file helm-github-stars-cache-file))
(insert (json-encode hash))
(print hash (current-buffer))
(when (file-writable-p file)
(write-region (point-min) (point-max) file)))))

Expand Down Expand Up @@ -177,7 +178,10 @@
(repos [])
(i 0))
(while (< i (length github-repos))
(setq repos (vconcat repos (vector (cdr (assoc 'full_name (elt github-repos i))))))
(setq repos (vconcat repos (vector (concat
(cdr (assoc 'full_name (elt github-repos i)))
" - "
(cdr (assoc 'description (elt github-repos i)))))))
(setq i (1+ i)))
repos))

Expand Down
22 changes: 11 additions & 11 deletions test/helm-github-stars-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@

(ert-deftest hgs/read-cache-file-test ()
(with-cache
(f-write-text hgs-test/cache-json-string 'utf-8 cache-test-file)
(f-write-text hgs-test/cache-string 'utf-8 cache-test-file)
(should (equal (gethash "repos" (hgs/read-cache-file)) hgs-test/repos-list))
(should (equal (gethash "stars" (hgs/read-cache-file)) hgs-test/stars-list))))

(ert-deftest hgs/write-cache-file-test ()
(with-cache
(hgs/write-cache-file hgs-test/cache-hash-table)
(should (equal (f-read-text cache-test-file)
hgs-test/cache-json-string))))
hgs-test/cache-string))))

(ert-deftest hgs/cache-file-exists-test ()
(with-cache
Expand All @@ -69,16 +69,16 @@
(elt hgs-test/github-repos-response-stub (1- page)))
(hgs/generate-cache-file)
(should (equal (f-read-text cache-test-file)
hgs-test/cache-json-string))))
hgs-test/cache-string))))

(ert-deftest hgs/parse-github-response-test ()
(should (equal (hgs/parse-github-response (elt hgs-test/github-stars-response-stub 0))
["star/1" "star/2"])))
["star/1 - desc-star1" "star/2 - desc-star2"])))

(ert-deftest hgs/get-github-stars-with-generated-cache-test ()
(with-cache
(f-write-text hgs-test/cache-json-string 'utf-8 cache-test-file)
(should (equal (hgs/get-github-stars) ["star/1" "star/2" "star/3"]))))
(f-write-text hgs-test/cache-string 'utf-8 cache-test-file)
(should (equal (hgs/get-github-stars) ["star/1 - desc-star1" "star/2 - desc-star2" "star/3 - desc-star3"]))))

(ert-deftest hgs/get-github-stars-without-generated-cache-test ()
(with-cache
Expand All @@ -88,12 +88,12 @@
(defun hgs/request-github-repos (page)
"Stub github response."
(elt hgs-test/github-repos-response-stub (1- page)))
(should (equal (hgs/get-github-stars) ["star/1" "star/2" "star/3"]))))
(should (equal (hgs/get-github-stars) ["star/1 - desc-star1" "star/2 - desc-star2" "star/3 - desc-star3"]))))

(ert-deftest hgs/get-github-repos-with-generated-cache-test ()
(with-cache
(f-write-text hgs-test/cache-json-string 'utf-8 cache-test-file)
(should (equal (hgs/get-github-repos) ["repo/1" "repo/2" "repo/3"]))))
(f-write-text hgs-test/cache-string 'utf-8 cache-test-file)
(should (equal (hgs/get-github-repos) ["repo/1 - desc-repo1" "repo/2 - desc-repo2" "repo/3 - desc-repo3"]))))

(ert-deftest hgs/get-github-repos-without-generated-cache-test ()
(with-cache
Expand All @@ -103,15 +103,15 @@
(defun hgs/request-github-repos (page)
"Stub github response."
(elt hgs-test/github-repos-response-stub (1- page)))
(should (equal (hgs/get-github-repos) ["repo/1" "repo/2" "repo/3"]))))
(should (equal (hgs/get-github-repos) ["repo/1 - desc-repo1" "repo/2 - desc-repo2" "repo/3 - desc-repo3"]))))

(ert-deftest helm-github-stars-fetch-test ()
"This test just check that cache file is removed before calling helm-github-stars"
(with-cache
(defun helm-github-stars ()
"Overwrite helm-github-stars. Return t if cache file exists, nil if not exists."
(f-file? cache-test-file))
(f-write-text hgs-test/cache-json-string 'utf-8 cache-test-file)
(f-write-text hgs-test/cache-string 'utf-8 cache-test-file)
(should (not (helm-github-stars-fetch)))))

;;; helm-github-stars-test.el ends here
18 changes: 9 additions & 9 deletions test/test-helper.el
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
(expand-file-name "helm-github-stars.el" root-dir))

(defmacro with-cache (&rest body)
"Evaluate TEST with cache interaction."
"Evaluate BODY with cache interaction."
`(let ((helm-github-stars-cache-file cache-test-file))
(when (f-file? cache-test-file)
(f-delete cache-test-file))
Expand All @@ -54,23 +54,23 @@

;;Stubing
(defvar hgs-test/github-stars-response-stub
["[ {\"full_name\": \"star/1\"},{\"full_name\": \"star/2\"}]"
"[ {\"full_name\": \"star/3\"}]"
["[ {\"full_name\": \"star/1\", \"description\": \"desc-star1\"},{\"full_name\": \"star/2\", \"description\": \"desc-star2\"}]"
"[ {\"full_name\": \"star/3\", \"description\": \"desc-star3\"}]"
"[ ]"])

(defvar hgs-test/github-repos-response-stub
["[ {\"full_name\": \"repo/1\"},{\"full_name\": \"repo/2\"}]"
"[ {\"full_name\": \"repo/3\"}]"
["[ {\"full_name\": \"repo/1\", \"description\": \"desc-repo1\"},{\"full_name\": \"repo/2\", \"description\": \"desc-repo2\"}]"
"[ {\"full_name\": \"repo/3\", \"description\": \"desc-repo3\"}]"
"[ ]"])

(defvar hgs-test/cache-json-string
"{\"repos\":[\"repo\\/1\", \"repo\\/2\", \"repo\\/3\"], \"stars\":[\"star\\/1\", \"star\\/2\", \"star\\/3\"]}")
(defvar hgs-test/cache-string
"\n#s(hash-table size 65 test equal rehash-size 1.5 rehash-threshold 0.8 data (\"stars\" [\"star/1 - desc-star1\" \"star/2 - desc-star2\" \"star/3 - desc-star3\"] \"repos\" [\"repo/1 - desc-repo1\" \"repo/2 - desc-repo2\" \"repo/3 - desc-repo3\"]))\n")

(defvar hgs-test/repos-list
["repo/1" "repo/2" "repo/3"])
["repo/1 - desc-repo1" "repo/2 - desc-repo2" "repo/3 - desc-repo3"])

(defvar hgs-test/stars-list
["star/1" "star/2" "star/3"])
["star/1 - desc-star1" "star/2 - desc-star2" "star/3 - desc-star3"])

(defvar hgs-test/cache-hash-table
(make-hash-table :test 'equal))
Expand Down

0 comments on commit 66e4bf3

Please sign in to comment.