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

View File

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