Skip to content

Commit

Permalink
Improvement for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
guiferpa committed Oct 8, 2018
1 parent 610dfd4 commit 85e6e67
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 36 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ $(GORELEASER): $(DEP)
$(DEP):
go get -v -u github.com/golang/dep/cmd/dep

release: $(GORELEASER)
release: test $(GORELEASER)
goreleaser --rm-dist

cover: test
Expand Down
8 changes: 4 additions & 4 deletions args/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (
)

// ErrEndOfArgs is a error type for EOA (End of arguments) error
type ErrEndOfArgs struct{}
type errEndOfArgs struct{}

func (e *ErrEndOfArgs) Error() string {
func (e *errEndOfArgs) Error() string {
return "end of arguments"
}

Expand All @@ -25,15 +25,15 @@ func (a Arguments) Binary() string {
// First is a func to get the first argument
func (a Arguments) First() (string, error) {
if len(a.args) == 0 {
return "", &ErrEndOfArgs{}
return "", &errEndOfArgs{}
}
return a.args[0], nil
}

// Tail is a func that leaving the first and get the rest
func (a Arguments) Tail() Arguments {
if len(a.args) <= 1 {
return Arguments{args: []string{}}
return Arguments{bin: a.bin, args: []string{}}
}
return Arguments{bin: a.bin, args: a.args[1:]}
}
Expand Down
28 changes: 27 additions & 1 deletion args/args_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ func TestBinaryFromTail(t *testing.T) {
}
}

func TestBinaryFromTailWithOnlyOneArg(t *testing.T) {
in := []string{"binary", "a"}
expected := "binary"
args := Parse(in).Tail()
if result := args.Binary(); result != expected {
t.Errorf("unexpected result, result: %v, expected: %v", result, expected)
}
}

func TestCorretArgumentsPassed(t *testing.T) {
in := []string{"binary", "a", "b", "c", "d"}
expected := fmt.Sprintf("%v", []string{"a", "b", "c", "d"})
Expand Down Expand Up @@ -55,14 +64,23 @@ func TestTailArguments(t *testing.T) {
}
}

func TestTailWithOnlyOneArg(t *testing.T) {
in := []string{"binary", "a"}
expected := fmt.Sprintf("%v", []string{})
args := Parse(in).Tail()
if result := fmt.Sprintf("%v", args.args); result != expected {
t.Errorf("unexpected result, result: %v, expected: %v", result, expected)
}
}

func TestFirstWithOnlyBinary(t *testing.T) {
in := []string{"binary"}
_, err := Parse(in).First()
if err == nil {
t.Error("unexpected behavior, error expected")
return
}
if _, ok := err.(*ErrEndOfArgs); !ok {
if _, ok := err.(*errEndOfArgs); !ok {
t.Error("unexpected error type")
}
}
Expand Down Expand Up @@ -98,3 +116,11 @@ func TestSanitizeFlagsFromTail(t *testing.T) {
t.Errorf("unexpected result, result: %v, expected: %v", result, expected)
}
}

func TestErrEndOfArgs(t *testing.T) {
expected := "end of arguments"
err := &errEndOfArgs{}
if result := err.Error(); result != expected {
t.Errorf("unexpected result, result: %v, expected: %v", result, expected)
}
}
26 changes: 17 additions & 9 deletions file/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@ package file
import (
"bufio"
"bytes"
"errors"
"fmt"
"io/ioutil"
"strings"
)

type errAnyTargetFounded struct{}

func (e *errAnyTargetFounded) Error() string {
return "any target founded"
}

// Target is a struct to save the target info
type Target struct {
Tag string
Expand All @@ -24,21 +29,24 @@ func FindOutTargets(tag, s string) ([]Target, error) {
targets := make([]Target, 0)
scnr := bufio.NewScanner(bytes.NewBufferString(s))
for scnr.Scan() {
if strings.HasPrefix(scnr.Text(), tag) {
claimLibrary := strings.Split(scnr.Text(), " ")
if len(claimLibrary) > 2 {
return nil, errors.New("import syntax wrong")
line := strings.TrimSpace(scnr.Text())
if strings.HasPrefix(line, tag) {
claimLibrary := strings.Split(line, " ")
if len(claimLibrary) != 2 {
continue
}
if len(claimLibrary) == 2 && claimLibrary[0] == tag {
lineTag := claimLibrary[0]
lineValue := claimLibrary[1]
if lineTag == tag {
targets = append(targets, Target{
Tag: claimLibrary[0],
Library: claimLibrary[1],
Tag: lineTag,
Library: lineValue,
})
}
}
}
if len(targets) == 0 {
return nil, errors.New("any target founded")
return nil, &errAnyTargetFounded{}
}
return targets, nil
}
Expand Down
92 changes: 71 additions & 21 deletions file/target_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,48 @@ package file

import (
"fmt"
"reflect"
"testing"
)

func TestFindOutTargets(t *testing.T) {
tag := "test"
data := []byte(`
test 1
test 2
test 3
test 4
t 4
te 5
tes 6
test 7
tes 8
te 9
t 10
testing 11
testin 12
testi 13
`)
result, err := FindOutTargets(tag, string(data))
tag := "join"
data := `
join 1
join 2
join 3
join 4
j 5
jo 6
joi 7
join ('8')
joi 9
jo 10
j 11
joi 12
join('13')
testing to pkg=file
joining to pkg=file
import test = "testing.." (
variable t = 1000
join test
)
`
result, err := FindOutTargets(tag, data)
if err != nil {
t.Error(err)
return
}
expected := []Target{
{Tag: "test", Library: "1"},
{Tag: "test", Library: "2"},
{Tag: "test", Library: "7"},
{Tag: "join", Library: "1"},
{Tag: "join", Library: "2"},
{Tag: "join", Library: "3"},
{Tag: "join", Library: "4"},
{Tag: "join", Library: "('8')"},
{Tag: "join", Library: "test"},
}
if fmt.Sprintf("%v", result) != fmt.Sprintf("%v", expected) {
t.Errorf("unexpected result, result: %v, expected: %v", result, expected)
Expand Down Expand Up @@ -74,3 +86,41 @@ hello test
t.Errorf("unexpected result, result: %v, expected: %v", result, expected)
}
}

func TestFindOutTargetsWithErrAnyTargetFounded(t *testing.T) {
cases := []struct {
Tag string
Data string
}{
{
Tag: "include",
Data: `
import { test } from './suite';
Test.do(func(tester=test) {
tester lib/fakerfunc
})
`},
{
Tag: "include",
Data: `
include('test');
include(' test ');
`},
}

for _, test := range cases {
_, err := FindOutTargets(test.Tag, test.Data)
if _, ok := err.(*errAnyTargetFounded); !ok {
t.Error("unexpected error type", reflect.TypeOf(err))
}
}
}

func TestErrAnyTargetFounded(t *testing.T) {
expected := "any target founded"
err := &errAnyTargetFounded{}
if result := err.Error(); result != expected {
t.Errorf("unexpected result, result: %v, expected: %v", result, expected)
}
}

0 comments on commit 85e6e67

Please sign in to comment.