mirror of
https://github.com/lune-org/lune.git
synced 2024-12-13 13:30:38 +00:00
Add tests for enum datatypes
This commit is contained in:
parent
83284be303
commit
b662b86922
5 changed files with 53 additions and 5 deletions
|
@ -3,7 +3,7 @@ use core::fmt;
|
||||||
use mlua::prelude::*;
|
use mlua::prelude::*;
|
||||||
use rbx_reflection::EnumDescriptor;
|
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.
|
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 {
|
impl LuaUserData for Enum {
|
||||||
fn add_methods<'lua, M: LuaUserDataMethods<'lua, Self>>(methods: &mut M) {
|
fn add_methods<'lua, M: LuaUserDataMethods<'lua, Self>>(methods: &mut M) {
|
||||||
|
// Methods
|
||||||
methods.add_method("GetEnumItems", |_, this, ()| {
|
methods.add_method("GetEnumItems", |_, this, ()| {
|
||||||
Ok(this
|
Ok(this
|
||||||
.desc
|
.desc
|
||||||
|
@ -41,7 +42,10 @@ impl LuaUserData for Enum {
|
||||||
name, this.desc.name
|
name, this.desc.name
|
||||||
))),
|
))),
|
||||||
}
|
}
|
||||||
})
|
});
|
||||||
|
// Metamethods
|
||||||
|
methods.add_meta_method(LuaMetaMethod::Eq, userdata_impl_eq);
|
||||||
|
methods.add_meta_method(LuaMetaMethod::ToString, userdata_impl_to_string);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,7 @@ use mlua::prelude::*;
|
||||||
use rbx_dom_weak::types::Enum as RbxEnum;
|
use rbx_dom_weak::types::Enum as RbxEnum;
|
||||||
use rbx_reflection::DataType as RbxDataType;
|
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.
|
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("Value", |_, this| Ok(this.value));
|
||||||
fields.add_field_method_get("EnumType", |_, this| Ok(this.parent.clone()));
|
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 {
|
impl fmt::Display for EnumItem {
|
||||||
|
|
|
@ -2,7 +2,7 @@ use core::fmt;
|
||||||
|
|
||||||
use mlua::prelude::*;
|
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.
|
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 {
|
impl LuaUserData for Enums {
|
||||||
fn add_methods<'lua, M: LuaUserDataMethods<'lua, Self>>(methods: &mut M) {
|
fn add_methods<'lua, M: LuaUserDataMethods<'lua, Self>>(methods: &mut M) {
|
||||||
|
// Methods
|
||||||
methods.add_method("GetEnums", |_, _, ()| {
|
methods.add_method("GetEnums", |_, _, ()| {
|
||||||
let db = rbx_reflection_database::get();
|
let db = rbx_reflection_database::get();
|
||||||
Ok(db.enums.values().map(Enum::from).collect::<Vec<_>>())
|
Ok(db.enums.values().map(Enum::from).collect::<Vec<_>>())
|
||||||
|
@ -33,7 +34,10 @@ impl LuaUserData for Enums {
|
||||||
name
|
name
|
||||||
))),
|
))),
|
||||||
}
|
}
|
||||||
})
|
});
|
||||||
|
// Metamethods
|
||||||
|
methods.add_meta_method(LuaMetaMethod::Eq, userdata_impl_eq);
|
||||||
|
methods.add_meta_method(LuaMetaMethod::ToString, userdata_impl_to_string);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -40,6 +40,7 @@ macro_rules! create_tests {
|
||||||
create_tests! {
|
create_tests! {
|
||||||
datatypes_brick_color: "datatypes/BrickColor",
|
datatypes_brick_color: "datatypes/BrickColor",
|
||||||
datatypes_color3: "datatypes/Color3",
|
datatypes_color3: "datatypes/Color3",
|
||||||
|
datatypes_enum: "datatypes/Enum",
|
||||||
datatypes_udim: "datatypes/UDim",
|
datatypes_udim: "datatypes/UDim",
|
||||||
datatypes_udim2: "datatypes/UDim2",
|
datatypes_udim2: "datatypes/UDim2",
|
||||||
datatypes_vector2: "datatypes/Vector2",
|
datatypes_vector2: "datatypes/Vector2",
|
||||||
|
|
34
tests/roblox/datatypes/Enum.luau
Normal file
34
tests/roblox/datatypes/Enum.luau
Normal 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")
|
Loading…
Reference in a new issue