-
-
Notifications
You must be signed in to change notification settings - Fork 73
/
calendar_serialization_test.go
79 lines (68 loc) · 2.05 KB
/
calendar_serialization_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
//go:build go1.16
// +build go1.16
package ics
import (
"bytes"
"fmt"
"os"
"path/filepath"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/require"
)
func TestCalendar_ReSerialization(t *testing.T) {
testDir := "testdata/serialization"
expectedDir := filepath.Join(testDir, "expected")
actualDir := filepath.Join(testDir, "actual")
testFileNames := []string{
"input1.ics",
"input2.ics",
"input3.ics",
"input4.ics",
"input5.ics",
"input6.ics",
"input7.ics",
}
for _, filename := range testFileNames {
fp := filepath.Join(testDir, filename)
t.Run(fmt.Sprintf("compare serialized -> deserialized -> serialized: %s", fp), func(t *testing.T) {
//given
originalSeriailizedCal, err := os.ReadFile(fp)
require.NoError(t, err)
//when
deserializedCal, err := ParseCalendar(bytes.NewReader(originalSeriailizedCal))
require.NoError(t, err)
serializedCal := deserializedCal.Serialize(WithNewLineWindows)
//then
expectedCal, err := os.ReadFile(filepath.Join(expectedDir, filename))
require.NoError(t, err)
if diff := cmp.Diff(string(expectedCal), serializedCal); diff != "" {
err = os.MkdirAll(actualDir, 0755)
if err != nil {
t.Logf("failed to create actual dir: %v", err)
}
err = os.WriteFile(filepath.Join(actualDir, filename), []byte(serializedCal), 0644)
if err != nil {
t.Logf("failed to write actual file: %v", err)
}
t.Error(diff)
}
})
t.Run(fmt.Sprintf("compare deserialized -> serialized -> deserialized: %s", filename), func(t *testing.T) {
//given
loadIcsContent, err := os.ReadFile(filepath.Join(testDir, filename))
require.NoError(t, err)
originalDeserializedCal, err := ParseCalendar(bytes.NewReader(loadIcsContent))
require.NoError(t, err)
//when
serializedCal := originalDeserializedCal.Serialize()
deserializedCal, err := ParseCalendar(strings.NewReader(serializedCal))
require.NoError(t, err)
//then
if diff := cmp.Diff(originalDeserializedCal, deserializedCal); diff != "" {
t.Error(diff)
}
})
}
}