Test AttachmentBuilder

This commit is contained in:
Szymon Uglis 2021-03-11 20:21:37 +01:00
parent 7aeef08fb9
commit 15aa62cbe2
No known key found for this signature in database
GPG key ID: 112376C5BEE91FE2
2 changed files with 27 additions and 6 deletions

View file

@ -19,12 +19,8 @@ class AttachmentBuilder {
String get attachUrl => "attachment://$_name";
/// Open file at [path] then read it's contents and prepare to send. Name will be automatically extracted from path if no name provided.
factory AttachmentBuilder.path(String path, {String? name, bool? spoiler}) {
final bytes = File(path).readAsBytesSync();
final fileName = name ?? path_utils.basename(path);
return AttachmentBuilder._new(bytes, fileName, spoiler);
}
factory AttachmentBuilder.path(String path, {String? name, bool? spoiler}) =>
AttachmentBuilder.file(File(path), name: name, spoiler: spoiler);
/// Create attachment from specified file instance. Name will be automatically extracted from path if no name provided.
factory AttachmentBuilder.file(File file, {String? name, bool? spoiler}) {

View file

@ -323,4 +323,29 @@ void main() {
expect(role.permissions.raw, PermissionsConstants.sendMessages | PermissionsConstants.readMessageHistory);
});
});
group("AttachmentBuilder tests", () {
test(".bytes constructor", () {
final attachment = AttachmentBuilder.bytes([], "test.txt", spoiler: true);
expect(attachment.attachUrl, "attachment://SPOILER_test.txt");
});
test(".file constructor", () async {
final file = File("/tmp/test.txt");
await file.create();
final attachment = AttachmentBuilder.file(file, name: "name_for_file.png", spoiler: true);
expect(attachment.attachUrl, "attachment://SPOILER_name_for_file.png");
});
test(".file constructor", () async {
const path = "/tmp/test.txt";
final file = File(path);
await file.create();
final attachment = AttachmentBuilder.path(path);
expect(attachment.attachUrl, "attachment://test.txt");
});
});
}