Skip to content

Commit

Permalink
Use testify for unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sei40kr committed Aug 31, 2019
1 parent 15eb117 commit 14b28bb
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 88 deletions.
41 changes: 41 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Gopkg.toml example
#
# Refer to https://golang.github.io/dep/docs/Gopkg.toml.html
# for detailed Gopkg.toml documentation.
#
# required = ["github.com/user/thing/cmd/thing"]
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
#
# [[constraint]]
# name = "github.com/user/project"
# version = "1.0.0"
#
# [[constraint]]
# name = "github.com/user/project2"
# branch = "dev"
# source = "github.com/myfork/project2"
#
# [[override]]
# name = "github.com/x/y"
# version = "2.4.0"
#
# [prune]
# non-go = false
# go-tests = true
# unused-packages = true


[prune]
go-tests = true
unused-packages = true
111 changes: 23 additions & 88 deletions def-matcher_test.go
Original file line number Diff line number Diff line change
@@ -1,54 +1,15 @@
package main

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
)

func TestParseDef(t *testing.T) {
mockArgs := []struct {
subject string
line string
expected Def
}{
{
subject: "when neither of the alias nor the abbreviation are quoted",
line: "dk=docker",
expected: Def{
alias: "dk",
abbr: "docker",
},
},
{
subject: "when the abbreviation is quoted",
line: "gb='git branch'",
expected: Def{
alias: "gb",
abbr: "git branch",
},
},
{
subject: "when both of the alias and the abbreviation are quoted",
line: "'g cb'='git checkout -b'",
expected: Def{
alias: "g cb",
abbr: "git checkout -b",
},
},
}

for _, mockArg := range mockArgs {
fmt.Printf("%s - ", mockArg.subject)

actual := ParseDef(mockArg.line)
expected := mockArg.expected
if actual == expected {
fmt.Println("ok")
} else {
fmt.Println("ng")
t.Fatalf("expected=%s, aParseDef(mockArg.line)ctual=%s\n", expected, actual)
}
}
assert.Equal(t, ParseDef("dk=docker"), Def{alias: "dk", abbr: "docker"})
assert.Equal(t, ParseDef("gb='git branch'"), Def{alias: "gb", abbr: "git branch"})
assert.Equal(t, ParseDef("'g cb'='git checkout -b'"), Def{alias: "g cb", abbr: "git checkout -b"})
}

func TestMatchDef(t *testing.T) {
Expand Down Expand Up @@ -79,54 +40,28 @@ func TestMatchDef(t *testing.T) {
},
}

mockArgs := []struct {
subject string
command string
expected *Def
}{
{
subject: "when the command has single token",
command: "docker",
expected: &Def{alias: "dk"},
},
{
subject: "when the command has multiple tokens",
command: "git branch",
expected: &Def{alias: "gb"},
},
{
subject: "when it has more than 2 matches, then return the longest one",
command: "git checkout -b",
expected: &Def{alias: "gcb"},
},
{
subject: "when it has no matches, then return a empty string",
command: "cd ..",
expected: nil,
},
{
subject: "when it was expanded recursively from >1 aliases, then reduce it fully",
command: "ls -G -lh", // ll expands to that with ls='ls -G' and ll='ls -lh' aliases defined
expected: &Def{alias: "ll"},
},
{
candidate, _ := MatchDef(mockDefs, "docker")
assert.Equal(t, candidate.alias, "dk")
}

for _, mockArg := range mockArgs {
fmt.Printf("%s - ", mockArg.subject)
{
candidate, _ := MatchDef(mockDefs, "git branch")
assert.Equal(t, candidate.alias, "gb")
}

expected := mockArg.expected
actual, _ := MatchDef(mockDefs, mockArg.command)
{
candidate, _ := MatchDef(mockDefs, "git checkout -b")
assert.Equal(t, candidate.alias, "gcb", "should return the longest match when multiple matches found")
}

if (actual == nil && expected == nil) || actual.alias == expected.alias {
fmt.Println("ok")
} else {
fmt.Println("ng")
{
candidate, _ := MatchDef(mockDefs, "cd ..")
assert.Nil(t, candidate, "should return nil when no matches found")
}

if actual != nil {
t.Fatalf("expected=%s, actual=%s\n", expected.alias, actual.alias)
} else {
t.Fatalf("expected=%s, actual=nil\n", expected.alias)
}
}
{
candidate, _ := MatchDef(mockDefs, "ls -G -lh")
assert.Equal(t, candidate.alias, "ll", "should apply aliases recursively")
}
}

0 comments on commit 14b28bb

Please sign in to comment.