-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodefile_test.go
More file actions
167 lines (148 loc) · 6.38 KB
/
codefile_test.go
File metadata and controls
167 lines (148 loc) · 6.38 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Copyright (c) 2023-2026 thorsphere.
// All Rights Reserved. Use is governed with GNU Affero General Public License v3.0
// that can be found in the LICENSE file.
package lpcode_test
import (
"testing" // testing
"github.com/thorsphere/lpcode" // lpcode
"github.com/thorsphere/tserr" // tserr
"github.com/thorsphere/tsfio" // tsfio
)
// TestCodeFile1 tests the functionality of the Codefile struct and its methods.
// It creates a temporary directory, copies the header and footer files to the temporary directory,
// creates a new Codefile with the temporary directory and filename, starts the code file,
// writes a code snippet to the code file, finishes the code file, evaluates the contents of the code file against
// a golden file and finally removes the temporary directory and files. The test fails if any of these steps fail.
func TestCodeFile1(t *testing.T) {
testCodeFile(t, true)
}
// TestCodeFile1 tests the functionality of the Codefile struct and its methods.
// It creates a temporary directory, creates a new Codefile with the temporary directory and filename, starts the code file,
// writes a code snippet to the code file, finishes the code file, evaluates the contents of the code file against
// a golden file and finally removes the temporary directory and files. The test fails if any of these steps fail.
func TestCodeFile2(t *testing.T) {
testCodeFile(t, false)
}
// TestFilepathNil tests the Filepath method of Codefile with a nil pointer.
// The test fails if Filepath does not return an empty string in case the Codefile is nil.
func TestFilepathNil(t *testing.T) {
// Declare c as type *Code and assign nil.
var c *lpcode.Codefile = nil
// The test fails if Filepath does not return an empty string.
if n := c.Filepath(); n != "" {
t.Error(tserr.Empty("Filepath"))
}
}
// testCodeFile tests the functionality of the Codefile struct and its methods.
// It creates a temporary directory, copies the header and footer files to the temporary directory if tmp is true,
// creates a new Codefile with the temporary directory and filename, starts the code file,
// writes a code snippet to the code file, finishes the code file, evaluates the contents of the code file against
// a golden file and finally removes the temporary directory and files. The test fails if any of these steps fail.
func testCodeFile(t *testing.T, tmp bool) {
// Retrieve a temporary directory
dn := tmpDir(t)
// If tmp is true, copy the header file and the footer file to the temporary directory.
if tmp {
copyHF(t, dn)
}
// Create a new Codefile with the temporary directory and filename
cf, err := lpcode.NewCodefile(dn, tsfio.Filename(testcfname))
// The test fails if a Codefile cannot be created with the temporary directory and filename
if err != nil {
t.Fatal(err)
}
// Start the code file
if err := cf.StartFile(); err != nil {
// The test fails if the code file cannot be started
t.Fatal(err)
}
// Write the code snippet generated by testIfErr to the code file
if err := cf.WriteCode(testIfErr()); err != nil {
// The test fails if the code cannot be written to the code file
t.Fatal(err)
}
// Finish the code file
if err := cf.FinishFile(); err != nil {
// The test fails if the code file cannot be finished
t.Fatal(err)
}
// Evaluate the contents of the code file against the golden file.
if err := tsfio.EvalGoldenFile(&tsfio.Testcase{Name: testcfname, Data: cf.String()}); err != nil {
// The test fails if the contents of the code file do not match the contents of the golden file
t.Fatal(err)
}
// Remove the temporary directory, header file, footer file, code file and the temporary directory.
if tmp {
rmHF(t, dn)
}
rm(t, cf.Filepath())
rm(t, dn)
}
// TestStringNil tests the String method of Codefile with a nil pointer.
// The test fails if String does not return an empty string in case the Codefile is nil.
func TestStringNil2(t *testing.T) {
// Declare c as type *Code and assign nil.
var c *lpcode.Codefile = nil
// The test fails if String does not return an empty string.
if n := c.String(); n != "" {
t.Error(tserr.Empty("String"))
}
}
// TestStartFileNil tests the StartFile method of Codefile with a nil pointer.
// The test fails if StartFile does not return an error in case the Codefile is nil.
func TestStartFileNil(t *testing.T) {
// Declare c as type *Code and assign nil.
var c *lpcode.Codefile = nil
// The test fails if StartFile does not return an error.
if err := c.StartFile(); err == nil {
t.Error(tserr.NilFailed("StartFile"))
}
}
// TestFinishFileNil tests the FinishFile method of Codefile with a nil pointer.
// The test fails if FinishFile does not return an error in case the Codefile is nil.
func TestFinishFileNil(t *testing.T) {
// Declare c as type *Code and assign nil.
var c *lpcode.Codefile = nil
// The test fails if FinishFile does not return an error.
if err := c.FinishFile(); err == nil {
t.Error(tserr.NilFailed("FinishFile"))
}
}
// TestWriteCodeNil tests the WriteCode method of Codefile with a nil pointer.
// The test fails if WriteCode does not return an error in case the Codefile is nil.
func TestWriteCodeNil(t *testing.T) {
// Declare c as type *Code and assign nil.
var c *lpcode.Codefile = nil
// The test fails if WriteCode does not return an error.
if err := c.WriteCode(lpcode.NewCode()); err == nil {
t.Error(tserr.NilFailed("WriteCode"))
}
}
// TestWriteCodeNil tests the WriteCode method of Codefile with a nil pointer.
// The test fails if WriteCode does not return an error in case the Codefile is nil.
func TestWriteCodeNil2(t *testing.T) {
// Retrieve a temporary directory
dn := tmpDir(t)
// Create a new Codefile with the temporary directory and filename
c, err := lpcode.NewCodefile(dn, tsfio.Filename(testcfname))
if err != nil {
// The test fails if a Codefile cannot be created with the temporary directory and filename
t.Fatal(err)
}
// The test fails if WriteCode does not return an error.
if err := c.WriteCode(nil); err == nil {
t.Error(tserr.NilFailed("WriteCode"))
}
// Remove the temporary directory.
rm(t, dn)
}
// TestFormatNil tests the Format method of Codefile with a nil pointer.
// The test fails if Format does not return an error in case the Codefile is nil.
func TestFormatNil2(t *testing.T) {
// Declare c as type *Code and assign nil.
var c *lpcode.Codefile = nil
// The test fails if Format does not return an error.
if err := c.Format(); err == nil {
t.Error(tserr.NilFailed("Format"))
}
}