aboutsummaryrefslogtreecommitdiff
path: root/pkg/field/field_test.go
blob: 66b0291012063ff68be210507315e195e2557891 (plain)
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
package field

import (
	"io"
	"os"
	"testing"
	"testing/iotest"

	"github.com/stretchr/testify/require"
)

func TestLoad(t *testing.T) {
	// minimal b98 file
	minimalField := Field{
		x:  0,
		y:  0,
		lx: 1,
		ly: 1,
		lines: []Line{
			Line{
				x:       0,
				l:       1,
				columns: []int{'@'},
			},
		},
	}
	// hello b98 file
	helloField := Field{
		x:  0,
		y:  0,
		lx: 24,
		ly: 1,
		lines: []Line{
			Line{
				x:       0,
				l:       24,
				columns: []int{'6', '4', '+', '"', '!', 'd', 'l', 'r', 'o', 'W', ' ', ',', 'o', 'l', 'l', 'e', 'H', '"', '>', ':', '#', ',', '_', '@'},
			},
		},
	}
	// factorial b98 file
	factorialField := Field{
		x:  0,
		y:  0,
		lx: 15,
		ly: 2,
		lines: []Line{
			Line{
				x:       0,
				l:       15,
				columns: []int{'&', '>', ':', '1', '-', ':', 'v', ' ', 'v', ' ', '*', '_', '$', '.', '@'},
			},
			Line{
				x:       1,
				l:       11,
				columns: []int{'^', ' ', ' ', ' ', ' ', '_', '$', '>', '\\', ':', '^'},
			},
		},
	}
	// dna b98 file
	dnaField := Field{
		x:  0,
		y:  0,
		lx: 7,
		ly: 8,
		lines: []Line{
			Line{
				x:       0,
				l:       7,
				columns: []int{'7', '^', 'D', 'N', '>', 'v', 'A'},
			},
			Line{
				x:       0,
				l:       7,
				columns: []int{'v', '_', '#', 'v', '?', ' ', 'v'},
			},
			Line{
				x:       0,
				l:       7,
				columns: []int{'7', '^', '<', '"', '"', '"', '"'},
			},
			Line{
				x:       0,
				l:       7,
				columns: []int{'3', ' ', ' ', 'A', 'C', 'G', 'T'},
			},
			Line{
				x:       0,
				l:       7,
				columns: []int{'9', '0', '!', '"', '"', '"', '"'},
			},
			Line{
				x:       0,
				l:       7,
				columns: []int{'4', '*', ':', '>', '>', '>', 'v'},
			},
			Line{
				x:       0,
				l:       7,
				columns: []int{'+', '8', '^', '-', '1', ',', '<'},
			},
			Line{
				x:       0,
				l:       7,
				columns: []int{'>', ' ', ',', '+', ',', '@', ')'},
			},
		},
	}
	// \r\n file b98 file
	rnField := Field{
		x:  0,
		y:  0,
		lx: 24,
		ly: 1,
		lines: []Line{
			Line{
				x:       0,
				l:       24,
				columns: []int{'6', '4', '+', '"', '!', 'd', 'l', 'r', 'o', 'W', ' ', ',', 'o', 'l', 'l', 'e', 'H', '"', '>', ':', '#', ',', '_', '@'},
			},
		},
	}
	// Test cases
	type addError func(r io.Reader) io.Reader
	testCases := []struct {
		name          string
		input         string
		inputAddError addError
		expected      *Field
		expectedError error
	}{
		{"Empty file", "test_data/empty.b98", nil, nil, &DecodeError{}},
		{"Invalid file content", "test_data/invalid.b98", nil, nil, &DecodeError{}},
		{"io error", "test_data/minimal.b98", iotest.TimeoutReader, nil, &ReadError{}},
		{"minimal", "test_data/minimal.b98", nil, &minimalField, nil},
		{"hello", "test_data/hello.b98", nil, &helloField, nil},
		{"factorial", "test_data/factorial.b98", nil, &factorialField, nil},
		{"dna", "test_data/dna.b98", nil, &dnaField, nil},
		{"\\r\\n file", "test_data/rn.b98", nil, &rnField, nil},
	}
	for _, tc := range testCases {
		t.Run(tc.name, func(t *testing.T) {
			var fd io.Reader
			file, err := os.Open(tc.input)
			require.NoError(t, err, "Failed to open file")
			defer file.Close()
			if tc.inputAddError != nil {
				fd = tc.inputAddError(file)
			} else {
				fd = file
			}
			valid, err := Load(fd)
			if tc.expectedError != nil {
				require.Error(t, err)
				requireErrorTypeMatch(t, err, tc.expectedError)
				require.Nil(t, valid)
			} else {
				require.NoError(t, err)
			}
			require.Equal(t, tc.expected, valid, "Invalid value")
		})
	}
}