-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmdclasses_test.go
109 lines (98 loc) · 2.46 KB
/
mdclasses_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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package mdclasses
import (
"bytes"
"github.com/stretchr/testify/require"
"github.com/v8platform/mdclasses/encoding/xml"
"io/ioutil"
"os"
"path/filepath"
"reflect"
"testing"
)
func TestUnpackConfiguration(t *testing.T) {
type args struct {
dir string
}
tests := []struct {
name string
dir string
want Configuration
wantErr bool
}{
{
"simple",
"./tests/metadata/edt/src",
Configuration{},
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := UnpackConfiguration(tt.dir)
if (err != nil) != tt.wantErr {
t.Errorf("UnpackConfiguration() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("UnpackConfiguration() got = %v, want %v", got, tt.want)
}
})
}
}
func Test(t *testing.T) {
tests := []struct {
name string
dir string
want Configuration
wantErr bool
}{
{
"simple",
"./tests/metadata/edt/src",
Configuration{},
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := UnpackConfiguration(tt.dir)
if (err != nil) != tt.wantErr {
t.Errorf("UnpackConfiguration() error = %v, wantErr %v", err, tt.wantErr)
return
}
TempDir, err := ioutil.TempDir(os.TempDir(), "prefix")
if err != nil {
t.Fatalf("Error create temp dir %s", TempDir)
}
os.MkdirAll(filepath.Join(TempDir, "Configuration"), os.ModePerm)
xmlFile, err := os.Create(filepath.Join(TempDir, "Configuration", "Configuration.mdo"))
if err != nil {
t.Fatalf("Ошибка тестирования %v", got)
return
}
xmlFile.WriteString(xml.Header)
encoder := xml.NewEncoder(xmlFile)
encoder.Indent("", " ")
err = encoder.Encode(&got)
xmlFile.WriteString("\n")
if err != nil {
t.Fatalf("Ошибка тестирования %v", got)
return
}
require.True(t, fileCompare(t, filepath.Join(tt.dir, "Configuration", "Configuration.mdo"), xmlFile.Name()))
})
}
}
func fileCompare(t *testing.T, file1, file2 string) bool {
// per comment, better to not read an entire file into memory
// this is simply a trivial example.
f1, err1 := ioutil.ReadFile(file1)
if err1 != nil {
t.Fatalf("Ошибка тестирования %v", err1)
}
f2, err2 := ioutil.ReadFile(file2)
if err2 != nil {
t.Fatalf("Ошибка тестирования %v", err1)
}
return bytes.Equal(f1, f2) // Per comment, this is significantly more performant.
}