Skip to content

Commit 2a8db87

Browse files
committed
Update Value tests
1 parent cbae4fe commit 2a8db87

File tree

5 files changed

+75
-8
lines changed

5 files changed

+75
-8
lines changed

src/value.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -501,9 +501,9 @@ impl Value {
501501
(_, Value::Boolean(_)) => Ordering::Greater,
502502
// Integer && Number
503503
(Value::Integer(a), Value::Integer(b)) => a.cmp(b),
504-
(&Value::Integer(a), &Value::Number(b)) => cmp_num(a as Number, b),
505-
(&Value::Number(a), &Value::Integer(b)) => cmp_num(a, b as Number),
506-
(&Value::Number(a), &Value::Number(b)) => cmp_num(a, b),
504+
(Value::Integer(a), Value::Number(b)) => cmp_num(*a as Number, *b),
505+
(Value::Number(a), Value::Integer(b)) => cmp_num(*a, *b as Number),
506+
(Value::Number(a), Value::Number(b)) => cmp_num(*a, *b),
507507
(Value::Integer(_) | Value::Number(_), _) => Ordering::Less,
508508
(_, Value::Integer(_) | Value::Number(_)) => Ordering::Greater,
509509
// Vector (Luau)

tests/buffer.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ fn test_buffer() -> Result<()> {
3535

3636
let buf3 = lua.create_buffer(b"")?;
3737
assert!(buf3.is_empty());
38+
assert!(!Value::Buffer(buf3).to_pointer().is_null());
3839

3940
Ok(())
4041
}

tests/table.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -393,12 +393,24 @@ fn test_table_error() -> Result<()> {
393393
fn test_table_fmt() -> Result<()> {
394394
let lua = Lua::new();
395395

396-
let table = lua.load(r#"{1, 2, 3, a = 5, b = { 6 }}"#).eval::<Table>()?;
397-
// assert_eq!(format!("{:?}", table), "{1, 2, 3, a = 5, b = {6}}");
396+
let table = lua
397+
.load(
398+
r#"
399+
local t = {1, 2, 3, a = 5, b = { 6 }}
400+
t[9.2] = 9.2
401+
t[1.99] = 1.99
402+
t[true] = true
403+
t[false] = false
404+
return t
405+
"#,
406+
)
407+
.eval::<Table>()?;
398408
assert!(format!("{table:?}").starts_with("Table(Ref("));
409+
410+
// Pretty print
399411
assert_eq!(
400412
format!("{table:#?}"),
401-
"{\n [1] = 1,\n [2] = 2,\n [3] = 3,\n [\"a\"] = 5,\n [\"b\"] = {\n [1] = 6,\n },\n}"
413+
"{\n [false] = false,\n [true] = true,\n [1] = 1,\n [1.99] = 1.99,\n [2] = 2,\n [3] = 3,\n [9.2] = 9.2,\n [\"a\"] = 5,\n [\"b\"] = {\n [1] = 6,\n },\n}"
402414
);
403415

404416
Ok(())

tests/userdata.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -803,7 +803,6 @@ fn test_userdata_method_errors() -> Result<()> {
803803
} => {
804804
assert_eq!(to.as_deref(), Some("MyUserData.get_value"));
805805
assert_eq!(name.as_deref(), Some("self"));
806-
println!("{}", cause2.to_string());
807806
assert_eq!(
808807
cause2.to_string(),
809808
"error converting Lua string to userdata (expected userdata of type 'MyUserData')"

tests/value.rs

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,49 +88,102 @@ fn test_multi_value() {
8888
assert!(multi_value.is_empty());
8989
}
9090

91+
#[test]
92+
fn test_value_to_pointer() -> Result<()> {
93+
let lua = Lua::new();
94+
95+
let globals = lua.globals();
96+
lua.load(
97+
r#"
98+
table = {}
99+
string = "hello"
100+
num = 1
101+
func = function() end
102+
thread = coroutine.create(function() end)
103+
"#,
104+
)
105+
.exec()?;
106+
globals.set("null", Value::NULL)?;
107+
108+
let table: Value = globals.get("table")?;
109+
let string: Value = globals.get("string")?;
110+
let num: Value = globals.get("num")?;
111+
let func: Value = globals.get("func")?;
112+
let thread: Value = globals.get("thread")?;
113+
let null: Value = globals.get("null")?;
114+
let ud: Value = Value::UserData(lua.create_any_userdata(())?);
115+
116+
assert!(!table.to_pointer().is_null());
117+
assert!(!string.to_pointer().is_null());
118+
assert!(num.to_pointer().is_null());
119+
assert!(!func.to_pointer().is_null());
120+
assert!(!thread.to_pointer().is_null());
121+
assert!(null.to_pointer().is_null());
122+
assert!(!ud.to_pointer().is_null());
123+
124+
Ok(())
125+
}
126+
91127
#[test]
92128
fn test_value_to_string() -> Result<()> {
93129
let lua = Lua::new();
94130

95131
assert_eq!(Value::Nil.to_string()?, "nil");
132+
assert_eq!(Value::Nil.type_name(), "nil");
96133
assert_eq!(Value::Boolean(true).to_string()?, "true");
134+
assert_eq!(Value::Boolean(true).type_name(), "boolean");
97135
assert_eq!(Value::NULL.to_string()?, "null");
136+
assert_eq!(Value::NULL.type_name(), "lightuserdata");
98137
assert_eq!(
99138
Value::LightUserData(LightUserData(0x1 as *const c_void as *mut _)).to_string()?,
100139
"lightuserdata: 0x1"
101140
);
102141
assert_eq!(Value::Integer(1).to_string()?, "1");
142+
assert_eq!(Value::Integer(1).type_name(), "integer");
103143
assert_eq!(Value::Number(34.59).to_string()?, "34.59");
144+
assert_eq!(Value::Number(34.59).type_name(), "number");
104145
#[cfg(all(feature = "luau", not(feature = "luau-vector4")))]
105146
assert_eq!(
106147
Value::Vector(mlua::Vector::new(10.0, 11.1, 12.2)).to_string()?,
107148
"vector(10, 11.1, 12.2)"
108149
);
150+
#[cfg(all(feature = "luau", not(feature = "luau-vector4")))]
151+
assert_eq!(
152+
Value::Vector(mlua::Vector::new(10.0, 11.1, 12.2)).type_name(),
153+
"vector"
154+
);
109155
#[cfg(feature = "luau-vector4")]
110156
assert_eq!(
111157
Value::Vector(mlua::Vector::new(10.0, 11.1, 12.2, 13.3)).to_string()?,
112158
"vector(10, 11.1, 12.2, 13.3)"
113159
);
114-
assert_eq!(Value::String(lua.create_string("hello")?).to_string()?, "hello");
160+
161+
let s = Value::String(lua.create_string("hello")?);
162+
assert_eq!(s.to_string()?, "hello");
163+
assert_eq!(s.type_name(), "string");
115164

116165
let table: Value = lua.load("{}").eval()?;
117166
assert!(table.to_string()?.starts_with("table:"));
118167
let table: Value = lua
119168
.load("setmetatable({}, {__tostring = function() return 'test table' end})")
120169
.eval()?;
121170
assert_eq!(table.to_string()?, "test table");
171+
assert_eq!(table.type_name(), "table");
122172

123173
let func: Value = lua.load("function() end").eval()?;
124174
assert!(func.to_string()?.starts_with("function:"));
175+
assert_eq!(func.type_name(), "function");
125176

126177
let thread: Value = lua.load("coroutine.create(function() end)").eval()?;
127178
assert!(thread.to_string()?.starts_with("thread:"));
179+
assert_eq!(thread.type_name(), "thread");
128180

129181
lua.register_userdata_type::<StdString>(|reg| {
130182
reg.add_meta_method("__tostring", |_, this, ()| Ok(this.clone()));
131183
})?;
132184
let ud: Value = Value::UserData(lua.create_any_userdata(String::from("string userdata"))?);
133185
assert_eq!(ud.to_string()?, "string userdata");
186+
assert_eq!(ud.type_name(), "userdata");
134187

135188
struct MyUserData;
136189
impl UserData for MyUserData {}
@@ -139,11 +192,13 @@ fn test_value_to_string() -> Result<()> {
139192

140193
let err = Value::Error(Box::new(Error::runtime("test error")));
141194
assert_eq!(err.to_string()?, "runtime error: test error");
195+
assert_eq!(err.type_name(), "error");
142196

143197
#[cfg(feature = "luau")]
144198
{
145199
let buf = Value::Buffer(lua.create_buffer(b"hello")?);
146200
assert!(buf.to_string()?.starts_with("buffer:"));
201+
assert_eq!(buf.type_name(), "buffer");
147202

148203
// Set `__tostring` metamethod for buffer
149204
let mt = lua.load("{__tostring = buffer.tostring}").eval()?;

0 commit comments

Comments
 (0)