Skip to content

Commit

Permalink
Added some tests and refactored the search command
Browse files Browse the repository at this point in the history
  • Loading branch information
pepegar committed Jul 29, 2014
1 parent 77e4078 commit 693b700
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 7 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
SHELL = /bin/bash
PACKAGES := ./commands

mkfile_path := $(abspath $(lastword $(MAKEFILE_LIST)))
current_dir := $(notdir $(patsubst %/,%,$(dir $(mkfile_path))))
Expand All @@ -9,6 +10,6 @@ $(app):
@go build

test:
@echo NO TESTS!
@go test $(PACKAGES)

.PHONY: clean $(app) run release test
4 changes: 2 additions & 2 deletions commands/json_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func GetJson(url string) ([]byte, error) {
func ParsePluginsList(body []byte) (Response, error) {
var r Response

json.Unmarshal(body, &r)
parseJsonError := json.Unmarshal(body, &r)

return r, nil
return r, parseJsonError
}
40 changes: 40 additions & 0 deletions commands/json_utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package commands_test

import (
"testing"

"github.com/pepegar/vkg-go/commands"
)

func TestParsePluginsList(t *testing.T) {
validJson := []byte(`{
"total_results": 273,
"results_per_page": 20,
"total_pages": 14,
"plugins": [
{
"author": "Tim Pope",
"slug": "fugitive-vim",
"short_desc": "fugitive.vim: a Git wrapper so awesome, it should be illegal",
"github_url": "https://github.com/tpope/vim-fugitive"
}
]
}`)

response, parseError := commands.ParsePluginsList(validJson)

if parseError != nil {
t.Error("parsing a valid json throws an error")
}

if response.TotalResults != 273 {
t.Error(response.TotalResults)
}

invalidJson := []byte(`{asdf`)

_, parseErrorTwo := commands.ParsePluginsList(invalidJson)
if parseErrorTwo == nil {
t.Error("parsing invalid json doesn't throw an error")
}
}
8 changes: 4 additions & 4 deletions commands/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ var SearchCommand = Command{
json, jsonError := GetJson(url)

if nil != jsonError {
fmt.Println("error")
fmt.Println("invalid request")
}

response, parseError := ParsePluginsList(json)
response, parseJsonError := ParsePluginsList(json)

if nil != parseError {
fmt.Println("error")
if nil != parseJsonError {
fmt.Println("invalid json response")
}

for _, plugin := range response.Plugins {
Expand Down

0 comments on commit 693b700

Please sign in to comment.