File: util_test.go

package info (click to toggle)
golang-github-djherbis-times 1.0.1%2Bgit20170215.d25002f-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 124 kB
  • sloc: makefile: 2
file content (50 lines) | stat: -rw-r--r-- 1,152 bytes parent folder | download | duplicates (2)
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
package times

import (
	"io/ioutil"
	"os"
	"testing"
	"time"
)

type timeRange struct {
	start time.Time
	end   time.Time
}

func newInterval(t time.Time, dur time.Duration) timeRange {
	return timeRange{start: t.Add(-dur), end: t.Add(dur)}
}

func (t timeRange) Contains(findTime time.Time) bool {
	return !findTime.Before(t.start) && !findTime.After(t.end)
}

// creates a file and cleans it up after the test is run.
func fileTest(t testing.TB, testFunc func(f *os.File)) {
	f, err := ioutil.TempFile("", "")
	if err != nil {
		t.Error(err)
	}
	defer os.Remove(f.Name())
	defer f.Close()
	testFunc(f)
}

func timespecTest(ts Timespec, r timeRange, t testing.TB) {
	if !r.Contains(ts.AccessTime()) {
		t.Errorf("expected %s to be in range: %s\n", ts.AccessTime(), r.start)
	}

	if !r.Contains(ts.ModTime()) {
		t.Errorf("expected %s to be in range: %s\n", ts.ModTime(), r.start)
	}

	if ts.HasChangeTime() && !r.Contains(ts.ChangeTime()) {
		t.Errorf("expected %s to be in range: %s\n", ts.ChangeTime(), r.start)
	}

	if ts.HasBirthTime() && !r.Contains(ts.BirthTime()) {
		t.Errorf("expected %s to be in range: %s\n", ts.BirthTime(), r.start)
	}
}