From 0bc1e37e27e530ca2a3293917267f9da7aed1d22 Mon Sep 17 00:00:00 2001 From: Szymon Uglis Date: Tue, 9 Mar 2021 00:30:46 +0100 Subject: [PATCH] Tests using rest version of nyxx. Restructure tests --- nyxx/lib/src/utils/CacheUtility.dart | 18 +++--- nyxx/lib/src/utils/EntityUtility.dart | 16 ++--- nyxx/test/unit.dart | 92 ++++++++++++++++++++++++++- 3 files changed, 106 insertions(+), 20 deletions(-) diff --git a/nyxx/lib/src/utils/CacheUtility.dart b/nyxx/lib/src/utils/CacheUtility.dart index 86894e6d..9f0790bf 100644 --- a/nyxx/lib/src/utils/CacheUtility.dart +++ b/nyxx/lib/src/utils/CacheUtility.dart @@ -17,7 +17,7 @@ class CacheUtility { /// } /// ``` static Cacheable createCacheableUser( - Nyxx client, Snowflake id) => + INyxx client, Snowflake id) => _UserCacheable(client, id); /// Retrieves a cached Guild. @@ -28,7 +28,7 @@ class CacheUtility { /// } /// ``` static Cacheable createCacheableGuild( - Nyxx client, Snowflake id) => + INyxx client, Snowflake id) => _GuildCacheable(client, id); /// Retrieves a cached Role. @@ -40,7 +40,7 @@ class CacheUtility { /// } /// ``` static Cacheable createCacheableRole( - Nyxx client, Snowflake id, Cacheable guild) => + INyxx client, Snowflake id, Cacheable guild) => _RoleCacheable(client, id, guild); /// Retrieves a cached IChannel. Can be cast. @@ -51,7 +51,7 @@ class CacheUtility { /// } /// ``` static Cacheable createCacheableChannel( - Nyxx client, Snowflake id) => + INyxx client, Snowflake id) => _ChannelCacheable(client, id); /// Retrieves a cached TextChannel. @@ -62,7 +62,7 @@ class CacheUtility { /// } /// ``` static Cacheable createCacheableTextChannel( - Nyxx client, Snowflake id) => + INyxx client, Snowflake id) => _ChannelCacheable(client, id); /// Retrieves a cached VoiceChannel. @@ -73,7 +73,7 @@ class CacheUtility { /// } /// ``` static Cacheable createCacheableVoiceChannel( - Nyxx client, Snowflake id) => + INyxx client, Snowflake id) => _ChannelCacheable(client, id); /// Retrieves a cached DMChannel. @@ -84,7 +84,7 @@ class CacheUtility { /// } /// ``` static Cacheable createCacheableDMChannel( - Nyxx client, Snowflake id) => + INyxx client, Snowflake id) => _ChannelCacheable(client, id); /// Retrieves a cached Guild Member. @@ -96,7 +96,7 @@ class CacheUtility { /// } /// ``` static Cacheable createCacheableMember( - Nyxx client, Snowflake id, Cacheable guild) => + INyxx client, Snowflake id, Cacheable guild) => _MemberCacheable(client, id, guild); /// Retrieves a cached Guild Message. @@ -107,7 +107,7 @@ class CacheUtility { /// Cacheable cachedMember = CacheUtility.createCacheableMessage(bot, Snowflake(''), cachedChannel); /// } /// ``` - static Cacheable createCacheableMessage(Nyxx client, + static Cacheable createCacheableMessage(INyxx client, Snowflake id, Cacheable channel) => _MessageCacheable(client, id, channel); } diff --git a/nyxx/lib/src/utils/EntityUtility.dart b/nyxx/lib/src/utils/EntityUtility.dart index 036c8ee9..8f5da1ab 100644 --- a/nyxx/lib/src/utils/EntityUtility.dart +++ b/nyxx/lib/src/utils/EntityUtility.dart @@ -18,7 +18,7 @@ class EntityUtility { /// User user = EntityUtility.createUser(bot, rawJson); /// } /// ``` - static User createUser(Nyxx client, Map rawJson) => + static User createUser(INyxx client, Map rawJson) => User._new(client, rawJson); /// Creates a Guild object, can be used for other classes where you have correct rawJson data from the API. @@ -29,7 +29,7 @@ class EntityUtility { /// Guild guild = EntityUtility.createGuild(bot, rawJson); /// } /// ``` - static Guild createGuild(Nyxx client, Map rawJson) => + static Guild createGuild(INyxx client, Map rawJson) => Guild._new(client, rawJson); /// Creates a Role object, can be used for other classes where you have correct rawJson data from the API. @@ -41,7 +41,7 @@ class EntityUtility { /// } /// ``` static Role createRole( - Nyxx client, Snowflake guildId, Map rawJson) => + INyxx client, Snowflake guildId, Map rawJson) => Role._new(client, rawJson, guildId); /// Creates a CategoryGuildChannel object, can be used for other classes where you have correct rawJson data from the API. @@ -53,7 +53,7 @@ class EntityUtility { /// } /// ``` static CategoryGuildChannel createCategoryGuildChannel( - Nyxx client, Snowflake guildId, Map rawJson) => + INyxx client, Snowflake guildId, Map rawJson) => CategoryGuildChannel._new(client, rawJson, guildId); /// Creates a VoiceGuildChannel object, can be used for other classes where you have correct rawJson data from the API. @@ -65,7 +65,7 @@ class EntityUtility { /// } /// ``` static VoiceGuildChannel createVoiceGuildChannel( - Nyxx client, Snowflake guildId, Map rawJson) => + INyxx client, Snowflake guildId, Map rawJson) => VoiceGuildChannel._new(client, rawJson, guildId); /// Creates a Guild object, can be used for other classes where you have correct rawJson data from the API. @@ -77,7 +77,7 @@ class EntityUtility { /// } /// ``` static TextGuildChannel createTextGuildChannel( - Nyxx client, Snowflake guildId, Map rawJson) => + INyxx client, Snowflake guildId, Map rawJson) => TextGuildChannel._new(client, rawJson, guildId); /// Creates a Guild object, can be used for other classes where you have correct rawJson data from the API. @@ -88,7 +88,7 @@ class EntityUtility { /// DMChannel dmChannel = EntityUtility.createDMChannel(bot, rawJson); /// } /// ``` - static DMChannel createDMChannel(Nyxx client, Map rawJson) => + static DMChannel createDMChannel(INyxx client, Map rawJson) => DMChannel._new(client, rawJson); /// Creates a Guild Member object, can be used for other classes where you have correct rawJson data from the API. @@ -100,6 +100,6 @@ class EntityUtility { /// } /// ``` static Member createGuildMember( - Nyxx client, Snowflake guildId, Map rawJson) => + INyxx client, Snowflake guildId, Map rawJson) => Member._new(client, rawJson, guildId); } diff --git a/nyxx/test/unit.dart b/nyxx/test/unit.dart index 69df6a67..3840bcf1 100644 --- a/nyxx/test/unit.dart +++ b/nyxx/test/unit.dart @@ -7,10 +7,36 @@ import "package:test/test.dart"; const snowflakeAYear = 2017; const snowflakeBYear = 2018; -void main() { - final snowflakeA = Snowflake.fromDateTime(DateTime.utc(snowflakeAYear)); - final snowflakeB = Snowflake.fromDateTime(DateTime.utc(snowflakeBYear)); +final snowflakeA = Snowflake.fromDateTime(DateTime.utc(snowflakeAYear)); +final snowflakeB = Snowflake.fromDateTime(DateTime.utc(snowflakeBYear)); +final sampleUserRawData = { + "id": 123, + "username": "Test test", + "discriminator": "123", + "avatar": null, + "bot": false, + "system": false, + "public_flags": 1 << 0 // Discord employee +}; + +final sampleMemberData = { + "user": { + "id": 123 + }, + "nick": "This is nick", + "deaf": false, + "mute": false, + "roles": [ + "1234564" + "1234563" + ], + "joined_at": DateTime.now().toIso8601String() +}; + +final client = NyxxRest("dum", 0); + +void main() { group("Snowflake tests", () { test("Snowflakes should have correct date", () { expect(snowflakeA.timestamp.year, snowflakeAYear); @@ -102,4 +128,64 @@ void main() { expect(PermissionsUtils.isApplied(permissionInt, 0x10), false); }); }); + + group("Cache utils", () { + test("Cacheable User", () { + final cacheable = CacheUtility.createCacheableUser(client, 123.toSnowflake()); + expect(123.toSnowflake(), cacheable.id); + }); + + test("Cacheable Guild", () { + final cacheable = CacheUtility.createCacheableGuild(client, 123.toSnowflake()); + expect(123.toSnowflake(), cacheable.id); + }); + + test("Cacheable Role", () { + final cacheableGuild = CacheUtility.createCacheableGuild(client, 123.toSnowflake()); + final cacheable = CacheUtility.createCacheableRole(client, 123.toSnowflake(), cacheableGuild); + expect(123.toSnowflake(), cacheable.id); + expect(123.toSnowflake(), cacheableGuild.id); + }); + + test("Cacheable Channel", () { + final cacheable = CacheUtility.createCacheableChannel(client, 123.toSnowflake()); + expect(123.toSnowflake(), cacheable.id); + }); + + test("Cacheable Member", () { + final cacheableGuild = CacheUtility.createCacheableGuild(client, 123.toSnowflake()); + final cacheable = CacheUtility.createCacheableMember(client, 123.toSnowflake(), cacheableGuild); + expect(123.toSnowflake(), cacheable.id); + expect(123.toSnowflake(), cacheableGuild.id); + }); + + test("Cacheable Message", () { + final cacheableChannel = CacheUtility.createCacheableTextChannel(client, 123.toSnowflake()); + final cacheable = CacheUtility.createCacheableMessage(client, 123.toSnowflake(), cacheableChannel); + expect(123.toSnowflake(), cacheable.id); + expect(123.toSnowflake(), cacheableChannel.id); + }); + }); + + group("Entity utils", () { + test("Create user object", () { + final user = EntityUtility.createUser(client, sampleUserRawData); + + expect(123.toSnowflake(), user.id); + expect("Test test", user.username); + expect(123, user.discriminator); + expect(user.avatarURL(), isNotNull); + expect(user.avatarURL(), contains("${123 % 5}.png")); + expect(user.bot, false); + expect(user.system, false); + expect(user.userFlags, isNotNull); + expect(user.userFlags!.discordEmployee, true); + expect(user.userFlags!.earlySupporter, false); + }); + + test("Create member object", () { + final member = EntityUtility.createGuildMember(client, 123.toSnowflake(), sampleMemberData); + + }); + }); } \ No newline at end of file