From 15aa62cbe2d18372825e596260d75e523d2594d3 Mon Sep 17 00:00:00 2001 From: Szymon Uglis Date: Thu, 11 Mar 2021 20:21:37 +0100 Subject: [PATCH] Test AttachmentBuilder --- .../src/utils/builders/AttachmentBuilder.dart | 8 ++---- nyxx/test/unit.dart | 25 +++++++++++++++++++ 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/nyxx/lib/src/utils/builders/AttachmentBuilder.dart b/nyxx/lib/src/utils/builders/AttachmentBuilder.dart index 0914799c..f6020949 100644 --- a/nyxx/lib/src/utils/builders/AttachmentBuilder.dart +++ b/nyxx/lib/src/utils/builders/AttachmentBuilder.dart @@ -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}) { diff --git a/nyxx/test/unit.dart b/nyxx/test/unit.dart index bb64b8a3..80a715a8 100644 --- a/nyxx/test/unit.dart +++ b/nyxx/test/unit.dart @@ -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"); + }); + }); } \ No newline at end of file