~rbn/neinp

ref: 062a107b140085acd6c48867e4328fb3f3466a87 neinp/stat/reader_test.go -rw-r--r-- 1.2 KiB
062a107bRuben Schuller use custom go.rbn.im import path 1 year, 2 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package stat // import "go.rbn.im/neinp/stat"

import (
	"bytes"
	"io"
	"testing"
	"time"
)

var stats = []Stat{
	Stat{Name: "a", Atime: time.Unix(1234567, 0), Mtime: time.Unix(1234567, 0)},
	Stat{Name: "b", Atime: time.Unix(1234567, 0), Mtime: time.Unix(1234567, 0)},
}

func TestReaderRead(t *testing.T) {
	r := NewReader(stats...)

	var buf bytes.Buffer
	buf.ReadFrom(r)

	for _, oldstat := range stats {
		var newstat Stat
		newstat.Decode(&buf)

		if newstat != oldstat {
			t.Fail()
		}
	}
}

func TestReaderSeek(t *testing.T) {
	r := NewReader(stats...)

	// We need to read into a buffer because stat.decode does multiple reads,
	// and StatReader only supports reading a whole stat at once.
	var buf bytes.Buffer
	var a0 Stat
	var a1 Stat

	// Read the encoded stats to the buffer
	buf.ReadFrom(r)

	_, err := a0.Decode(&buf)
	if err != nil {
		t.Error(err)
	}

	// Seek to start.
	_, err = r.Seek(0, io.SeekStart)
	if err != nil {
		t.Error(err)
	}

	// Reset the buffer, then read again.
	// The read now should contain the first stat again.
	buf.Reset()
	buf.ReadFrom(r)

	_, err = a1.Decode(&buf)
	if err != nil {
		t.Error(err)
	}

	if a0 != a1 {
		t.Logf("\n%v\n%v", a0, a1)
		t.Fail()
	}

}