Skip to content

Commit 7c5fce7

Browse files
committed
Update unit test to use testify require
1 parent 29059cf commit 7c5fce7

File tree

3 files changed

+47
-34
lines changed

3 files changed

+47
-34
lines changed

go/go.mod

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
11
module github.com/danielbosnich/huffman-coding
22

33
go 1.24.9
4+
5+
require github.com/stretchr/testify v1.11.1
6+
7+
require (
8+
github.com/davecgh/go-spew v1.1.1 // indirect
9+
github.com/pmezard/go-difflib v1.0.0 // indirect
10+
gopkg.in/yaml.v3 v3.0.1 // indirect
11+
)

go/go.sum

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
2+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
4+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
5+
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
6+
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
7+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
8+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
9+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
10+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

go/huffman_coding_test.go

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
// Unit tests for the Go implementation of Huffman Coding
21
package huffman
32

43
import (
54
"bufio"
6-
"fmt"
5+
"errors"
76
"io"
87
"os"
98
"path/filepath"
109
"testing"
10+
11+
"github.com/stretchr/testify/require"
1112
)
1213

1314
const (
@@ -16,12 +17,9 @@ const (
1617
testUncompressedFilename = "plrabn12_compressed_uncompressed.txt"
1718
)
1819

19-
// Ensures that the original file matches the uncompressed file
20-
func TestCompareFiles(t *testing.T) {
20+
func TestHuffmanCoding(t *testing.T) {
2121
cwd, err := os.Getwd()
22-
if err != nil {
23-
t.Errorf("Unable to determine the current working directory. err: %v", err)
24-
}
22+
require.NoError(t, err, "Failed to get the working directory")
2523
testDir := filepath.Join(cwd, "..", "test")
2624

2725
testFilepath := filepath.Join(testDir, testFilename)
@@ -30,43 +28,40 @@ func TestCompareFiles(t *testing.T) {
3028
defer os.Remove(testCompressedFilepath)
3129
defer os.Remove(testUncompressedFilepath)
3230

33-
// Compress and uncompress the files first
34-
Compress(testFilepath)
35-
Uncompress(testCompressedFilepath)
31+
t.Run("Compress", func(t *testing.T) {
32+
err = Compress(testFilepath)
33+
require.NoError(t, err, "Failed to compress the test file")
34+
})
35+
36+
t.Run("Uncompress", func(t *testing.T) {
37+
err = Uncompress(testCompressedFilepath)
38+
require.NoError(t, err, "Failed to uncompress the compressed file")
39+
})
3640

37-
// Open both files
3841
originalFile, err := os.Open(testFilepath)
39-
if err != nil {
40-
fmt.Println(err)
41-
t.Error("There was an error opening the original file!")
42-
return
43-
}
42+
require.NoError(t, err, "Failed to open the test file")
4443
defer originalFile.Close()
4544

4645
uncompressedFile, err := os.Open(testUncompressedFilepath)
47-
if err != nil {
48-
fmt.Println(err)
49-
t.Error("There was an error opening the uncompressed file!")
50-
}
46+
require.NoError(t, err, "Failed to open the uncompressed file")
5147
defer uncompressedFile.Close()
5248

5349
originalReader := bufio.NewReader(originalFile)
5450
uncompressedReader := bufio.NewReader(uncompressedFile)
5551

56-
for {
57-
originalByte, err1 := originalReader.ReadByte()
58-
uncompressedByte, err2 := uncompressedReader.ReadByte()
59-
if err1 == io.EOF && err2 == io.EOF {
60-
break
61-
} else if err1 == io.EOF && err2 != io.EOF {
62-
t.Error("The two files are not the same length")
63-
} else if err1 != io.EOF && err2 == io.EOF {
64-
t.Error("The two files are not the same length")
65-
}
52+
t.Run("CompareFiles", func(t *testing.T) {
53+
for {
54+
originalByte, err1 := originalReader.ReadByte()
55+
uncompressedByte, err2 := uncompressedReader.ReadByte()
56+
if errors.Is(err1, io.EOF) && errors.Is(err2, io.EOF) {
57+
break
58+
}
6659

67-
if originalByte != uncompressedByte {
68-
t.Error("The two files are not equivalent")
69-
return
60+
require.NotErrorIs(t, err1, io.EOF, "Test file is shorter than the uncompressed file")
61+
require.NotErrorIs(t, err2, io.EOF, "Uncompressed file is shorter than the test file")
62+
require.NoError(t, err1, "Failed to read byte from the test file")
63+
require.NoError(t, err2, "Failed to read byte from the uncompressed file")
64+
require.Equal(t, originalByte, uncompressedByte, "Unequal character between the two files")
7065
}
71-
}
66+
})
7267
}

0 commit comments

Comments
 (0)