Skip to content

Commit

Permalink
Merge pull request #6 from gtalarico/improve-tests
Browse files Browse the repository at this point in the history
Improve test coverage
  • Loading branch information
gtalarico committed Jan 28, 2019
2 parents ee92300 + e9ef4db commit 1fba908
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 5 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
dist/

coverage.out
### GoLand ###
.idea/*
.idea/*
26 changes: 26 additions & 0 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package config

import (
"os"
"strings"
"testing"
)

func TestUserHomePath(t *testing.T) {
homePath := UserHomePath()
if homePath != os.Getenv("HOME") {
t.Error("Could not get HOME path")
}
}

func TestConfigFilePath(t *testing.T) {
path := configFilepath()
endsWithConfigFile := strings.Contains(path, ConfigFilename)
if !endsWithConfigFile {
t.Error("Config filepath does not end with config filename: {}", path)
}
}

// func WriteConfig(config Config) (err error) {
// func CreateConfig(path string) (cfg Config, err error) {
// func ReadConfig() (cfg Config, err error) {
5 changes: 4 additions & 1 deletion internal/config/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"
)

// Searches projects in config, matches by strings.Contains, returns list of all matching project
func SearchProjects(query string, config Config) []*Project {
var projects []*Project
for _, project := range config.Projects {
Expand All @@ -16,6 +17,7 @@ func SearchProjects(query string, config Config) []*Project {
return projects
}

// Searches projects in config, returns first exact match or nil
func MatchProjects(query string, config Config) (project *Project) {
for _, p := range config.Projects {
if p.Name == query {
Expand All @@ -25,6 +27,7 @@ func MatchProjects(query string, config Config) (project *Project) {
return
}

// Gets on exact project using SearchProjects, if none or multiples error is raised
func GetOneProject(query string, config Config) (project *Project, err error) {
projects := SearchProjects(query, config)
numProjects := len(projects)
Expand All @@ -37,7 +40,7 @@ func GetOneProject(query string, config Config) (project *Project, err error) {
}
if numProjects > 1 {
exactMatch := MatchProjects(query, config)
if exactMatch.Name == query {
if exactMatch != nil {
project = exactMatch
} else {
for _, project := range projects {
Expand Down
69 changes: 67 additions & 2 deletions internal/config/search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ package config

import "testing"

func TestSearchProjects(t *testing.T) {
type Fixtures struct {
projects []*Project
cfg *Config
}

func fixtures() Fixtures {

projects := []*Project{
{
Name: "project",
Expand All @@ -13,8 +19,19 @@ func TestSearchProjects(t *testing.T) {
Path: "~",
},
}

cfg := Config{projects}

return Fixtures{
projects,
&cfg,
}

}

func TestSearchProjects(t *testing.T) {
cfg := fixtures().cfg

var tests = []struct {
input string
expected int
Expand All @@ -25,11 +42,59 @@ func TestSearchProjects(t *testing.T) {
}

for _, test := range tests {
projects := SearchProjects(test.input, cfg)
projects := SearchProjects(test.input, *cfg)
if len(projects) != test.expected {
t.Error("Test Failed: {} inputted, {} expected, recieved: {}",
test.input, test.expected, projects)
}
}

}

func TestMatchProjectsMatch(t *testing.T) {
cfg := fixtures().cfg

project := MatchProjects("project", *cfg)
if project.Name != "project" {
t.Error("Test Failed: {} inputed, {} expected, recieved: {}",
"project", "project", project.Name)
}
}

func TestMatchProjectsNoMatch(t *testing.T) {
cfg := fixtures().cfg

project := MatchProjects("xxx", *cfg)
if project != nil {
t.Error("Test Failed: {} inputed, {} expected, recieved: {}",
"xxx", "no match", project)
}
}

func TestGetProjects(t *testing.T) {
cfg := fixtures().cfg

var tests = []struct {
input string
hasErr bool
}{
{"xxx", true},
{"project", false},
{"o", true},
}

for _, test := range tests {
project, err := GetOneProject(test.input, *cfg)
hasErr := err != nil
if test.hasErr != hasErr {
t.Error("Test Failed: {} inputed, {} expected, received: {}",
test.input, test.hasErr, hasErr)
}
if err != nil {
if project != nil {
t.Error("Test Failed: input: {}, expected: {}, received: {}",
test.input, "project is null", project)
}
}
}
}

0 comments on commit 1fba908

Please sign in to comment.