generated from OtusGolang/home_work
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcopy_test.go
69 lines (56 loc) · 1.42 KB
/
copy_test.go
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 main
import (
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"regexp"
"strconv"
"testing"
"github.com/stretchr/testify/require"
)
func TestCopy(t *testing.T) {
// Place your code here.
files, err := ioutil.ReadDir("testdata")
var filenames []string
for _, file := range files {
filename := file.Name()
if filename != "input.txt" {
filenames = append(filenames, file.Name())
}
}
if err != nil {
log.Fatal(err)
}
for _, filename := range filenames {
filename := filename
t.Run(fmt.Sprintf("Test - %s", filename), func(t *testing.T) {
tmpFile, err := os.CreateTemp("", "tmp.*.txt")
if err != nil {
log.Fatalf("Some error occurred: %v", err)
}
re := regexp.MustCompile(`\d+`)
nums := re.FindAllString(filename, -1)
offset, err := strconv.Atoi(nums[0])
if err != nil {
log.Fatalf("Some error occurred: %v", err)
}
limit, err := strconv.Atoi(nums[1])
if err != nil {
log.Fatalf("Some error occurred: %v", err)
}
err = Copy(filepath.Join("testdata", "input.txt"), tmpFile.Name(), int64(offset), int64(limit))
require.NoError(t, err)
expected, err := ioutil.ReadFile(filepath.Join("testdata", filename))
if err != nil {
log.Fatalf("Some error occurred: %v", err)
}
result, err := ioutil.ReadFile(tmpFile.Name())
if err != nil {
log.Fatalf("Some error occurred: %v", err)
}
require.Equal(t, string(expected), string(result))
})
}
}