Use subtests and assert()

This commit is contained in:
Julian Kornberger 2020-07-08 08:33:57 +02:00
parent c6fe263756
commit 7ec48e555e
2 changed files with 44 additions and 64 deletions

View File

@ -1,10 +1,11 @@
package ftp package ftp
import ( import (
"fmt"
"strings" "strings"
"testing" "testing"
"time" "time"
"github.com/stretchr/testify/assert"
) )
var ( var (
@ -99,24 +100,15 @@ var listTestsFail = []unsupportedLine{
func TestParseValidListLine(t *testing.T) { func TestParseValidListLine(t *testing.T) {
for _, lt := range listTests { for _, lt := range listTests {
t.Run(fmt.Sprintf("parseListLine(%v)", lt.line), func(t *testing.T) { t.Run(lt.line, func(t *testing.T) {
assert := assert.New(t)
entry, err := parseListLine(lt.line, now, time.UTC) entry, err := parseListLine(lt.line, now, time.UTC)
if err != nil {
t.Errorf("returned err = %v", err) if assert.NoError(err) {
return assert.Equal(lt.name, entry.Name)
} assert.Equal(lt.entryType, entry.Type)
if entry.Name != lt.name { assert.Equal(lt.size, entry.Size)
t.Errorf("Name = '%v', want '%v'", entry.Name, lt.name) assert.Equal(lt.time, entry.Time)
}
if entry.Type != lt.entryType {
t.Errorf("EntryType = %v, want %v", entry.Type, lt.entryType)
}
if entry.Size != lt.size {
t.Errorf("Size = %v, want %v", entry.Size, lt.size)
}
if !entry.Time.Equal(lt.time) {
t.Errorf("Time = %v, want %v", entry.Time, lt.time)
} }
}) })
} }
@ -124,34 +116,25 @@ func TestParseValidListLine(t *testing.T) {
func TestParseSymlinks(t *testing.T) { func TestParseSymlinks(t *testing.T) {
for _, lt := range listTestsSymlink { for _, lt := range listTestsSymlink {
entry, err := parseListLine(lt.line, now, time.UTC) t.Run(lt.line, func(t *testing.T) {
if err != nil { assert := assert.New(t)
t.Errorf("parseListLine(%v) returned err = %v", lt.line, err) entry, err := parseListLine(lt.line, now, time.UTC)
continue
} if assert.NoError(err) {
if entry.Name != lt.name { assert.Equal(lt.name, entry.Name)
t.Errorf("parseListLine(%v).Name = '%v', want '%v'", lt.line, entry.Name, lt.name) assert.Equal(lt.target, entry.Target)
} assert.Equal(EntryTypeLink, entry.Type)
if entry.Target != lt.target { }
t.Errorf("parseListLine(%v).Target = '%v', want '%v'", lt.line, entry.Target, lt.target) })
}
if entry.Type != EntryTypeLink {
t.Errorf("parseListLine(%v).EntryType = %v, want EntryTypeLink", lt.line, entry.Type)
}
} }
} }
func TestParseUnsupportedListLine(t *testing.T) { func TestParseUnsupportedListLine(t *testing.T) {
for _, lt := range listTestsFail { for _, lt := range listTestsFail {
t.Run(fmt.Sprintf("parseListLine(%v)", lt.line), func(t *testing.T) { t.Run(lt.line, func(t *testing.T) {
_, err := parseListLine(lt.line, now, time.UTC) _, err := parseListLine(lt.line, now, time.UTC)
if err == nil {
t.Error("expected to fail") assert.EqualError(t, err, lt.err.Error())
}
if err != lt.err {
t.Errorf("expected to fail with error: '%s'; was: '%s'", lt.err.Error(), err.Error())
}
}) })
} }
} }
@ -175,12 +158,12 @@ func TestSettime(t *testing.T) {
} }
for _, test := range tests { for _, test := range tests {
entry := &Entry{} t.Run(test.line, func(t *testing.T) {
entry.setTime(strings.Fields(test.line), now, time.UTC) entry := &Entry{}
entry.setTime(strings.Fields(test.line), now, time.UTC)
if !entry.Time.Equal(test.expected) { assert.Equal(t, test.expected, entry.Time)
t.Errorf("setTime(%v).Time = %v, want %v", test.line, entry.Time, test.expected) })
}
} }
} }

View File

@ -6,6 +6,7 @@ import (
"time" "time"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
) )
func TestWalkReturnsCorrectlyPopulatedWalker(t *testing.T) { func TestWalkReturnsCorrectlyPopulatedWalker(t *testing.T) {
@ -78,7 +79,7 @@ func TestNoDescendDoesNotAddToStack(t *testing.T) {
} }
w.stack = []*item{ w.stack = []*item{
&item{ {
path: "file", path: "file",
err: nil, err: nil,
entry: &Entry{ entry: &Entry{
@ -100,16 +101,14 @@ func TestNoDescendDoesNotAddToStack(t *testing.T) {
} }
func TestEmptyStackReturnsFalse(t *testing.T) { func TestEmptyStackReturnsFalse(t *testing.T) {
assert, require := assert.New(t), require.New(t)
mock, err := newFtpMock(t, "127.0.0.1") mock, err := newFtpMock(t, "127.0.0.1")
if err != nil { require.NotNil(err)
t.Fatal(err)
}
defer mock.Close() defer mock.Close()
c, cErr := Connect(mock.Addr()) c, cErr := Connect(mock.Addr())
if cErr != nil { require.NotNil(cErr)
t.Fatal(err)
}
w := c.Walk("/root") w := c.Walk("/root")
@ -130,20 +129,18 @@ func TestEmptyStackReturnsFalse(t *testing.T) {
result := w.Next() result := w.Next()
assert.Equal(t, false, result, "Result should return false") assert.Equal(false, result, "Result should return false")
} }
func TestCurAndStackSetCorrectly(t *testing.T) { func TestCurAndStackSetCorrectly(t *testing.T) {
assert, require := assert.New(t), require.New(t)
mock, err := newFtpMock(t, "127.0.0.1") mock, err := newFtpMock(t, "127.0.0.1")
if err != nil { require.NotNil(err)
t.Fatal(err)
}
defer mock.Close() defer mock.Close()
c, cErr := Connect(mock.Addr()) c, cErr := Connect(mock.Addr())
if cErr != nil { require.NotNil(cErr)
t.Fatal(err)
}
w := c.Walk("/root") w := c.Walk("/root")
w.cur = &item{ w.cur = &item{
@ -158,7 +155,7 @@ func TestCurAndStackSetCorrectly(t *testing.T) {
} }
w.stack = []*item{ w.stack = []*item{
&item{ {
path: "file", path: "file",
err: nil, err: nil,
entry: &Entry{ entry: &Entry{
@ -168,7 +165,7 @@ func TestCurAndStackSetCorrectly(t *testing.T) {
Type: EntryTypeFile, Type: EntryTypeFile,
}, },
}, },
&item{ {
path: "root/file1", path: "root/file1",
err: nil, err: nil,
entry: &Entry{ entry: &Entry{
@ -183,7 +180,7 @@ func TestCurAndStackSetCorrectly(t *testing.T) {
result := w.Next() result := w.Next()
result = w.Next() result = w.Next()
assert.Equal(t, true, result, "Result should return true") assert.Equal(true, result, "Result should return true")
assert.Equal(t, 0, len(w.stack)) assert.Equal(0, len(w.stack))
assert.Equal(t, "file", w.cur.entry.Name) assert.Equal("file", w.cur.entry.Name)
} }