]> Sergey Matveev's repositories - btrtrc.git/blobdiff - bencode/both_test.go
Handle torrentfs failure in test.sh
[btrtrc.git] / bencode / both_test.go
index 837e611573254eb82f8128ebb845f3daff04be82..fdcb90d9f8e44184039c3c6aa825b79d1a6f6667 100644 (file)
@@ -1,38 +1,39 @@
 package bencode
 
-import "testing"
-import "bytes"
-import "io/ioutil"
-import "time"
+import (
+       "bytes"
+       "os"
+       "testing"
 
-func load_file(name string, t *testing.T) []byte {
-       data, err := ioutil.ReadFile(name)
-       if err != nil {
-               t.Fatal(err)
-       }
+       "github.com/stretchr/testify/assert"
+       "github.com/stretchr/testify/require"
+)
+
+func loadFile(name string, t *testing.T) []byte {
+       data, err := os.ReadFile(name)
+       require.NoError(t, err)
        return data
 }
 
-func TestBothInterface(t *testing.T) {
-       data1 := load_file("_testdata/archlinux-2011.08.19-netinstall-i686.iso.torrent", t)
-       var iface interface{}
+func testFileInterface(t *testing.T, filename string) {
+       data1 := loadFile(filename, t)
 
+       var iface interface{}
        err := Unmarshal(data1, &iface)
-       if err != nil {
-               t.Fatal(err)
-       }
+       require.NoError(t, err)
 
        data2, err := Marshal(iface)
-       if err != nil {
-               t.Fatal(err)
-       }
+       require.NoError(t, err)
 
-       if !bytes.Equal(data1, data2) {
-               t.Fatalf("equality expected\n")
-       }
+       assert.EqualValues(t, data1, data2)
+}
+
+func TestBothInterface(t *testing.T) {
+       testFileInterface(t, "testdata/archlinux-2011.08.19-netinstall-i686.iso.torrent")
+       testFileInterface(t, "testdata/continuum.torrent")
 }
 
-type torrent_file struct {
+type torrentFile struct {
        Info struct {
                Name        string `bencode:"name"`
                Length      int64  `bencode:"length"`
@@ -50,28 +51,26 @@ type torrent_file struct {
        URLList      interface{} `bencode:"url-list,omitempty"`
 }
 
-func TestBoth(t *testing.T) {
-       data1 := load_file("_testdata/archlinux-2011.08.19-netinstall-i686.iso.torrent", t)
-       var f torrent_file
+func testFile(t *testing.T, filename string) {
+       data1 := loadFile(filename, t)
+       var f torrentFile
 
        err := Unmarshal(data1, &f)
        if err != nil {
                t.Fatal(err)
        }
 
-       t.Logf("Name: %s\n", f.Info.Name)
-       t.Logf("Length: %v bytes\n", f.Info.Length)
-       t.Logf("Announce: %s\n", f.Announce)
-       t.Logf("CreationDate: %s\n", time.Unix(f.CreationDate, 0).String())
-       t.Logf("CreatedBy: %s\n", f.CreatedBy)
-       t.Logf("Comment: %s\n", f.Comment)
-
        data2, err := Marshal(&f)
        if err != nil {
                t.Fatal(err)
        }
 
        if !bytes.Equal(data1, data2) {
+               println(string(data2))
                t.Fatalf("equality expected")
        }
 }
+
+func TestBoth(t *testing.T) {
+       testFile(t, "testdata/archlinux-2011.08.19-netinstall-i686.iso.torrent")
+}