Add tests for enum datatypes

This commit is contained in:
Filip Tibell 2023-03-15 20:58:52 +01:00
parent 83284be303
commit b662b86922
No known key found for this signature in database
5 changed files with 53 additions and 5 deletions

View file

@ -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);
}
}

View file

@ -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 {

View file

@ -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::<Vec<_>>())
@ -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);
}
}

View file

@ -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",

View file

@ -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")