/
ilp_codec_test.dart
217 lines (183 loc) · 6.64 KB
/
ilp_codec_test.dart
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import 'dart:typed_data';
import 'package:crypto/crypto.dart' as crypto;
import 'package:ilp_file_codec/ilp_codec.dart';
import 'package:path/path.dart' as path;
import 'package:test/test.dart';
import 'package:universal_io/io.dart';
void main() async {
final currentDirectory = Directory.current;
print('Current directory: ${currentDirectory.path}');
final configFile = File(path.join(
currentDirectory.path,
'test',
'test_config.json',
));
final testFolderPath = path.join(path.current, 'test');
final ilpFile = File(path.join(testFolderPath, 'test.ilp'));
final infoCover = File(path.join(testFolderPath, 'test_0.png'));
final ilpCover = File(path.join(testFolderPath, 'test_1.png'));
final infoCoverContent =
await File(path.join(testFolderPath, 'test_0.png')).readAsBytes();
final background = File(path.join(testFolderPath, 'test_6.png'));
final layerFile = path.join(testFolderPath, 'test_1.png');
final backgroundContent = await background.readAsBytes();
late ILPInfoConfig config;
ilpTester(Uint8List bytes, File file) async {
final ilp1 = ILP.fromBytes(bytes), ilp2 = ILP.fromFileSync(file.path);
expect((await ilp1.length) == (await file.length()), true);
expect((await ilp2.length) == (await file.length()), true);
expect(await ilp1.cover, await ilpCover.readAsBytes());
expect(await ilp1.cover, await ilp2.cover);
final header1 = await ilp1.header, header2 = await ilp2.header;
expect(<int>{2, header1.version, header2.version}, <int>{2});
expect(header1.id, crypto.sha1.convert(backgroundContent).toString());
expect(header1.id, header2.id);
expect(await ilp1.sha1, await ilp2.sha1);
expect(
await ilp1.sha1,
crypto.sha1
.convert([...header2.infoList, ...header2.layerList]).toString());
expect(await ilp1.md5, await ilp2.md5);
expect(
await ilp1.md5,
crypto.md5
.convert([...header2.infoList, ...header2.layerList]).toString());
final info = await ilp1.info(0);
expect(info.contentLayerIdList.length, 6);
expect(
info.contentLayerIdList.contains(crypto.sha1
.convert(await File(layerFile).readAsBytes())
.toString()),
true);
expect(info.cover.isNotEmpty, true);
expect(info.cover, infoCoverContent);
expect(header1.author, 'gzlock');
expect(header1.description, 'hi');
expect(header1.links.first, 'github');
expect(header1.links.last, 'https://github.com');
final layer1 = await ilp1.layer(0), layer2 = await ilp2.layer(0);
expect(layer1.content, backgroundContent);
expect(layer1.content, backgroundContent);
expect(layer1.hasName(), false);
expect(layer2.hasName(), false);
}
/// note: ILPConfig is used internally
test('ILPConfig test', () async {
config = await ILPInfoConfig.fromFile(configFile.path);
expect(await config.validate(), true);
expect(await config.layer.validate(), true);
String file = config.cover;
config.cover = 'not_exists_file.png';
expect(await config.validate(), false);
config.cover = file;
file = config.filePath!;
config.filePath = 'not_exists_file.json';
expect(await config.validate(), false);
config.filePath = file;
file = config.layer.file!;
config.layer.file = 'not_exists_file.png';
expect(await config.layer.validate(), false);
config.layer.file = file;
try {
final layer = config.layer.search('2');
await layer.first.id;
} on ILPConfigException catch (e) {
expect(e.message, 'layer_no_file');
}
expect(config.filePath, configFile.path);
expect(config.name, 'test_config');
expect(config.layer.name, 'background');
/// 2 layers named 5 (1 group, 1 layer)
expect(config.layer.search('5').length, 2);
/// no layer named 7 (hidden)
expect(config.layer.search('7').length, 0);
/// no empty layers
expect(config.layer.search('empty').length, 0);
/// no hidden layers
expect(config.layer.search('hidden').length, 0);
expect(config.cover, infoCover.path);
expect(config.layer.file!, background.path);
});
group('Single ILP config file encoder test', () {
late Uint8List bytes;
test('encoder test', () async {
final ilp = ILP.fromConfigFiles([configFile.path]);
final infos = await ilp.infos;
infos.first.name = 'first info';
bytes = await ilp.toBytes(
name: 'test',
author: 'gzlock',
description: 'hi',
links: ['github', 'https://github.com'],
infos: infos,
coverFilePath: ilpCover.path,
version: 2,
);
expect(bytes.sublist(0, 4), ilpFileFixedStrings);
await ilpFile.writeAsBytes(bytes);
/// test layer name
final testLayerName = ILP.fromBytes(await ilp.toBytes(
name: 'test',
author: 'gzlock',
description: 'hi',
links: ['github', 'https://github.com'],
infos: infos,
coverFilePath: ilpCover.path,
version: 2,
ignoreLayerName: false,
));
expect((await testLayerName.layer(0)).name, 'background');
});
test('decoder test', () async {
await ilpTester(bytes, ilpFile);
final ilp = ILP.fromBytes(bytes);
expect(await ilp.isList, false);
});
});
group('Multiple ILP config files encoder test', () {
late Uint8List bytes;
test('encoder test', () async {
final ilp = ILP.fromConfigFiles([configFile.path, configFile.path]);
final infos = await ilp.infos;
infos.first.name = 'first info';
infos.last.name = 'last info';
bytes = await ilp.toBytes(
name: 'test',
author: 'gzlock',
description: 'hi',
links: ['github', 'https://github.com'],
infos: infos,
coverFilePath: ilpCover.path,
version: 2,
);
expect(bytes.sublist(0, 4), ilpFileFixedStrings);
await ilpFile.writeAsBytes(bytes);
/// test layer name
final testLayerName = ILP.fromBytes(await ilp.toBytes(
name: 'test',
author: 'gzlock',
description: 'hi',
links: ['github', 'https://github.com'],
infos: infos,
coverFilePath: ilpCover.path,
version: 2,
ignoreLayerName: false,
));
expect((await testLayerName.layer(0)).name, 'background');
});
test('decoder test', () async {
await ilpTester(bytes, ilpFile);
final ilp = ILP.fromBytes(bytes);
expect(await ilp.isList, true);
});
});
group('utils tester', () {
test('ILP exception', () {
try {
ILP.fromFileSync('ilp_not_exists');
} on ILPConfigException catch (e) {
expect(e.message, 'ilp_file_not_exists');
}
});
});
}