From 14b28bbb20e27855774fb93a39b033348cf93ba5 Mon Sep 17 00:00:00 2001 From: Seong Yong-ju Date: Sat, 31 Aug 2019 02:10:57 +0900 Subject: [PATCH] Use testify for unit tests --- Gopkg.lock | 41 ++++++++++++++++ Gopkg.toml | 30 ++++++++++++ def-matcher_test.go | 111 +++++++++----------------------------------- 3 files changed, 94 insertions(+), 88 deletions(-) create mode 100644 Gopkg.lock create mode 100644 Gopkg.toml diff --git a/Gopkg.lock b/Gopkg.lock new file mode 100644 index 0000000..82983ff --- /dev/null +++ b/Gopkg.lock @@ -0,0 +1,41 @@ +# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'. + + +[[projects]] + digest = "1:ffe9824d294da03b391f44e1ae8281281b4afc1bdaa9588c9097785e3af10cec" + name = "github.com/davecgh/go-spew" + packages = ["spew"] + pruneopts = "UT" + revision = "8991bc29aa16c548c550c7ff78260e27b9ab7c73" + version = "v1.1.1" + +[[projects]] + digest = "1:0028cb19b2e4c3112225cd871870f2d9cf49b9b4276531f03438a88e94be86fe" + name = "github.com/pmezard/go-difflib" + packages = ["difflib"] + pruneopts = "UT" + revision = "792786c7400a136282c1664665ae0a8db921c6c2" + version = "v1.0.0" + +[[projects]] + digest = "1:8548c309c65a85933a625be5e7d52b6ac927ca30c56869fae58123b8a77a75e1" + name = "github.com/stretchr/testify" + packages = ["assert"] + pruneopts = "UT" + revision = "221dbe5ed46703ee255b1da0dec05086f5035f62" + version = "v1.4.0" + +[[projects]] + digest = "1:4d2e5a73dc1500038e504a8d78b986630e3626dc027bc030ba5c75da257cdb96" + name = "gopkg.in/yaml.v2" + packages = ["."] + pruneopts = "UT" + revision = "51d6538a90f86fe93ac480b35f37b2be17fef232" + version = "v2.2.2" + +[solve-meta] + analyzer-name = "dep" + analyzer-version = 1 + input-imports = ["github.com/stretchr/testify/assert"] + solver-name = "gps-cdcl" + solver-version = 1 diff --git a/Gopkg.toml b/Gopkg.toml new file mode 100644 index 0000000..d7072c2 --- /dev/null +++ b/Gopkg.toml @@ -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 diff --git a/def-matcher_test.go b/def-matcher_test.go index eebc9ed..cababe1 100644 --- a/def-matcher_test.go +++ b/def-matcher_test.go @@ -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) { @@ -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") } }