From b662b86922823d0465df730e34c88b557ce9a587 Mon Sep 17 00:00:00 2001 From: Filip Tibell Date: Wed, 15 Mar 2023 20:58:52 +0100 Subject: [PATCH] Add tests for enum datatypes --- .../lib-roblox/src/datatypes/types/enum.rs | 8 +++-- .../src/datatypes/types/enum_item.rs | 7 +++- .../lib-roblox/src/datatypes/types/enums.rs | 8 +++-- packages/lib-roblox/src/tests.rs | 1 + tests/roblox/datatypes/Enum.luau | 34 +++++++++++++++++++ 5 files changed, 53 insertions(+), 5 deletions(-) create mode 100644 tests/roblox/datatypes/Enum.luau diff --git a/packages/lib-roblox/src/datatypes/types/enum.rs b/packages/lib-roblox/src/datatypes/types/enum.rs index c797464..070a0df 100644 --- a/packages/lib-roblox/src/datatypes/types/enum.rs +++ b/packages/lib-roblox/src/datatypes/types/enum.rs @@ -3,7 +3,7 @@ use core::fmt; use mlua::prelude::*; use rbx_reflection::EnumDescriptor; -use super::EnumItem; +use super::{super::*, EnumItem}; /** An implementation of the [Enum](https://create.roblox.com/docs/reference/engine/datatypes/Enum) Roblox datatype. @@ -17,6 +17,7 @@ pub struct Enum { impl LuaUserData for Enum { fn add_methods<'lua, M: LuaUserDataMethods<'lua, Self>>(methods: &mut M) { + // Methods methods.add_method("GetEnumItems", |_, this, ()| { Ok(this .desc @@ -41,7 +42,10 @@ impl LuaUserData for Enum { name, this.desc.name ))), } - }) + }); + // Metamethods + methods.add_meta_method(LuaMetaMethod::Eq, userdata_impl_eq); + methods.add_meta_method(LuaMetaMethod::ToString, userdata_impl_to_string); } } diff --git a/packages/lib-roblox/src/datatypes/types/enum_item.rs b/packages/lib-roblox/src/datatypes/types/enum_item.rs index 783aec3..1a148cc 100644 --- a/packages/lib-roblox/src/datatypes/types/enum_item.rs +++ b/packages/lib-roblox/src/datatypes/types/enum_item.rs @@ -4,7 +4,7 @@ use mlua::prelude::*; use rbx_dom_weak::types::Enum as RbxEnum; use rbx_reflection::DataType as RbxDataType; -use super::Enum; +use super::{super::*, Enum}; /** An implementation of the [EnumItem](https://create.roblox.com/docs/reference/engine/datatypes/EnumItem) Roblox datatype. @@ -65,6 +65,11 @@ impl LuaUserData for EnumItem { fields.add_field_method_get("Value", |_, this| Ok(this.value)); fields.add_field_method_get("EnumType", |_, this| Ok(this.parent.clone())); } + + fn add_methods<'lua, M: LuaUserDataMethods<'lua, Self>>(methods: &mut M) { + methods.add_meta_method(LuaMetaMethod::Eq, userdata_impl_eq); + methods.add_meta_method(LuaMetaMethod::ToString, userdata_impl_to_string); + } } impl fmt::Display for EnumItem { diff --git a/packages/lib-roblox/src/datatypes/types/enums.rs b/packages/lib-roblox/src/datatypes/types/enums.rs index 6397568..6158e6d 100644 --- a/packages/lib-roblox/src/datatypes/types/enums.rs +++ b/packages/lib-roblox/src/datatypes/types/enums.rs @@ -2,7 +2,7 @@ use core::fmt; use mlua::prelude::*; -use super::Enum; +use super::{super::*, Enum}; /** An implementation of the [Enums](https://create.roblox.com/docs/reference/engine/datatypes/Enums) Roblox datatype. @@ -20,6 +20,7 @@ impl Enums { impl LuaUserData for Enums { fn add_methods<'lua, M: LuaUserDataMethods<'lua, Self>>(methods: &mut M) { + // Methods methods.add_method("GetEnums", |_, _, ()| { let db = rbx_reflection_database::get(); Ok(db.enums.values().map(Enum::from).collect::>()) @@ -33,7 +34,10 @@ impl LuaUserData for Enums { name ))), } - }) + }); + // Metamethods + methods.add_meta_method(LuaMetaMethod::Eq, userdata_impl_eq); + methods.add_meta_method(LuaMetaMethod::ToString, userdata_impl_to_string); } } diff --git a/packages/lib-roblox/src/tests.rs b/packages/lib-roblox/src/tests.rs index 58796de..330abb4 100644 --- a/packages/lib-roblox/src/tests.rs +++ b/packages/lib-roblox/src/tests.rs @@ -40,6 +40,7 @@ macro_rules! create_tests { create_tests! { datatypes_brick_color: "datatypes/BrickColor", datatypes_color3: "datatypes/Color3", + datatypes_enum: "datatypes/Enum", datatypes_udim: "datatypes/UDim", datatypes_udim2: "datatypes/UDim2", datatypes_vector2: "datatypes/Vector2", diff --git a/tests/roblox/datatypes/Enum.luau b/tests/roblox/datatypes/Enum.luau new file mode 100644 index 0000000..83c256a --- /dev/null +++ b/tests/roblox/datatypes/Enum.luau @@ -0,0 +1,34 @@ +-- HACK: Make luau happy, with the mlua rust +-- crate all globals are also present in _G +local Enum = _G.Enum + +-- Constructors & properties + +assert(tostring(Enum) == "Enum") +assert(tostring(Enum.KeyCode) == "Enum.KeyCode") +assert(tostring(Enum.KeyCode.X) == "Enum.KeyCode.X") + +-- NOTE: We use the axis enum here since it is unlikely +-- any more will be added to it and change the value +assert(Enum.Axis.X.Name == "X") +assert(Enum.Axis.X.Value == 0) + +-- Methods + +local foundKeyCode = false +for _, enum in Enum:GetEnums() do + if enum == Enum.KeyCode then + foundKeyCode = true + break + end +end +assert(foundKeyCode, "GetEnums did not contain Enum.KeyCode") + +local foundKeyCodeX = false +for _, keyCode in Enum.KeyCode:GetEnumItems() do + if keyCode == Enum.KeyCode.X then + foundKeyCodeX = true + break + end +end +assert(foundKeyCodeX, "GetEnumItems did not contain X for Enum.KeyCode")