Skip to content

Commit df54d98

Browse files
committed
test(class): add extends and implements tests
Refs: #326
1 parent 13a74d7 commit df54d98

File tree

7 files changed

+633
-19
lines changed

7 files changed

+633
-19
lines changed

src/builders/class.rs

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,3 +372,98 @@ impl ClassBuilder {
372372
Ok(())
373373
}
374374
}
375+
376+
#[cfg(test)]
377+
mod tests {
378+
use crate::test::test_function;
379+
380+
use super::*;
381+
382+
#[test]
383+
fn test_new() {
384+
let class = ClassBuilder::new("Foo");
385+
assert_eq!(class.name, "Foo");
386+
assert_eq!(class.extends, None);
387+
assert_eq!(class.interfaces, vec![]);
388+
assert_eq!(class.methods.len(), 0);
389+
assert_eq!(class.object_override, None);
390+
assert_eq!(class.properties, vec![]);
391+
assert_eq!(class.constants.len(), 0);
392+
assert_eq!(class.register, None);
393+
assert_eq!(class.docs, &[] as DocComments);
394+
}
395+
396+
#[test]
397+
fn test_extends() {
398+
let extends: ClassEntryInfo = (|| todo!(), "Bar");
399+
let class = ClassBuilder::new("Foo").extends(extends);
400+
assert_eq!(class.extends, Some(extends));
401+
}
402+
403+
#[test]
404+
fn test_implements() {
405+
let implements: ClassEntryInfo = (|| todo!(), "Bar");
406+
let class = ClassBuilder::new("Foo").implements(implements);
407+
assert_eq!(class.interfaces, vec![implements]);
408+
}
409+
410+
#[test]
411+
fn test_method() {
412+
let method = FunctionBuilder::new("foo", test_function);
413+
let class = ClassBuilder::new("Foo").method(method, MethodFlags::Public);
414+
assert_eq!(class.methods.len(), 1);
415+
}
416+
417+
#[test]
418+
fn test_property() {
419+
let class = ClassBuilder::new("Foo").property("bar", PropertyFlags::Public, &["Doc 1"]);
420+
assert_eq!(
421+
class.properties,
422+
vec![(
423+
"bar".to_string(),
424+
PropertyFlags::Public,
425+
&["Doc 1"] as DocComments
426+
)]
427+
);
428+
}
429+
430+
#[test]
431+
fn test_constant() {
432+
let class = ClassBuilder::new("Foo")
433+
.constant("bar", 42, &["Doc 1"])
434+
.expect("Failed to create constant");
435+
assert_eq!(class.constants.len(), 1);
436+
assert_eq!(class.constants[0].0, "bar");
437+
assert_eq!(class.constants[0].2, &["Doc 1"] as DocComments);
438+
}
439+
440+
#[test]
441+
fn test_dyn_constant() {
442+
let class = ClassBuilder::new("Foo")
443+
.dyn_constant("bar", &42, &["Doc 1"])
444+
.expect("Failed to create constant");
445+
assert_eq!(class.constants.len(), 1);
446+
assert_eq!(class.constants[0].0, "bar");
447+
assert_eq!(class.constants[0].2, &["Doc 1"] as DocComments);
448+
}
449+
450+
#[test]
451+
fn test_flags() {
452+
let class = ClassBuilder::new("Foo").flags(ClassFlags::Abstract);
453+
assert_eq!(class.ce.ce_flags, ClassFlags::Abstract.bits());
454+
}
455+
456+
#[test]
457+
fn test_registration() {
458+
let class = ClassBuilder::new("Foo").registration(|_| {});
459+
assert!(class.register.is_some());
460+
}
461+
462+
#[test]
463+
fn test_docs() {
464+
let class = ClassBuilder::new("Foo").docs(&["Doc 1"]);
465+
assert_eq!(class.docs, &["Doc 1"] as DocComments);
466+
}
467+
468+
// TODO: Test the register function
469+
}

src/builders/module.rs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,3 +304,86 @@ impl TryFrom<ModuleBuilder<'_>> for (ModuleEntry, ModuleStartup) {
304304
))
305305
}
306306
}
307+
308+
#[cfg(test)]
309+
mod tests {
310+
use crate::test::{
311+
test_deactivate_function, test_function, test_info_function, test_startup_shutdown_function,
312+
};
313+
314+
use super::*;
315+
316+
#[test]
317+
fn test_new() {
318+
let builder = ModuleBuilder::new("test", "1.0");
319+
assert_eq!(builder.name, "test");
320+
assert_eq!(builder.version, "1.0");
321+
assert!(builder.functions.is_empty());
322+
assert!(builder.constants.is_empty());
323+
assert!(builder.classes.is_empty());
324+
assert!(builder.startup_func.is_none());
325+
assert!(builder.shutdown_func.is_none());
326+
assert!(builder.request_startup_func.is_none());
327+
assert!(builder.request_shutdown_func.is_none());
328+
assert!(builder.post_deactivate_func.is_none());
329+
assert!(builder.info_func.is_none());
330+
}
331+
332+
#[test]
333+
fn test_startup_function() {
334+
let builder =
335+
ModuleBuilder::new("test", "1.0").startup_function(test_startup_shutdown_function);
336+
assert!(builder.startup_func.is_some());
337+
}
338+
339+
#[test]
340+
fn test_shutdown_function() {
341+
let builder =
342+
ModuleBuilder::new("test", "1.0").shutdown_function(test_startup_shutdown_function);
343+
assert!(builder.shutdown_func.is_some());
344+
}
345+
346+
#[test]
347+
fn test_request_startup_function() {
348+
let builder = ModuleBuilder::new("test", "1.0")
349+
.request_startup_function(test_startup_shutdown_function);
350+
assert!(builder.request_startup_func.is_some());
351+
}
352+
353+
#[test]
354+
fn test_request_shutdown_function() {
355+
let builder = ModuleBuilder::new("test", "1.0")
356+
.request_shutdown_function(test_startup_shutdown_function);
357+
assert!(builder.request_shutdown_func.is_some());
358+
}
359+
360+
#[test]
361+
fn test_set_post_deactivate_function() {
362+
let builder =
363+
ModuleBuilder::new("test", "1.0").post_deactivate_function(test_deactivate_function);
364+
assert!(builder.post_deactivate_func.is_some());
365+
}
366+
367+
#[test]
368+
fn test_set_info_function() {
369+
let builder = ModuleBuilder::new("test", "1.0").info_function(test_info_function);
370+
assert!(builder.info_func.is_some());
371+
}
372+
373+
#[test]
374+
fn test_add_function() {
375+
let builder =
376+
ModuleBuilder::new("test", "1.0").function(FunctionBuilder::new("test", test_function));
377+
assert_eq!(builder.functions.len(), 1);
378+
}
379+
380+
#[test]
381+
fn test_add_constant() {
382+
let builder =
383+
ModuleBuilder::new("test", "1.0").constant(("TEST_CONST", 42, DocComments::default()));
384+
assert_eq!(builder.constants.len(), 1);
385+
assert_eq!(builder.constants[0].0, "TEST_CONST");
386+
// TODO: Check if the value is 42
387+
assert_eq!(builder.constants[0].2, DocComments::default());
388+
}
389+
}

src/describe/abi.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use std::{fmt::Display, ops::Deref, vec::Vec as StdVec};
1717

1818
/// An immutable, ABI-stable [`Vec`][std::vec::Vec].
1919
#[repr(C)]
20+
#[derive(Debug)]
2021
pub struct Vec<T> {
2122
ptr: *mut T,
2223
len: usize,
@@ -48,8 +49,18 @@ impl<T> From<StdVec<T>> for Vec<T> {
4849
}
4950
}
5051

52+
impl<T> PartialEq for Vec<T>
53+
where
54+
T: PartialEq,
55+
{
56+
fn eq(&self, other: &Self) -> bool {
57+
self.len == other.len && self.as_ref() == other.as_ref()
58+
}
59+
}
60+
5161
/// An immutable, ABI-stable borrowed [`&'static str`][str].
5262
#[repr(C)]
63+
#[derive(Debug)]
5364
pub struct Str {
5465
ptr: *const u8,
5566
len: usize,
@@ -86,8 +97,15 @@ impl Display for Str {
8697
}
8798
}
8899

100+
impl PartialEq for Str {
101+
fn eq(&self, other: &Self) -> bool {
102+
self.len == other.len && self.str() == other.str()
103+
}
104+
}
105+
89106
/// An ABI-stable String
90107
#[repr(C)]
108+
#[derive(Debug, PartialEq)]
91109
pub struct RString {
92110
inner: Vec<u8>,
93111
}
@@ -134,6 +152,7 @@ impl Display for RString {
134152

135153
/// An ABI-stable [`Option`][std::option::Option].
136154
#[repr(C, u8)]
155+
#[derive(Debug)]
137156
pub enum Option<T> {
138157
/// [`Option::Some`][std::option::Option::Some] variant.
139158
Some(T),
@@ -149,3 +168,16 @@ impl<T> From<std::option::Option<T>> for Option<T> {
149168
}
150169
}
151170
}
171+
172+
impl<T> PartialEq for Option<T>
173+
where
174+
T: PartialEq,
175+
{
176+
fn eq(&self, other: &Self) -> bool {
177+
match (self, other) {
178+
(Self::Some(a), Self::Some(b)) => a == b,
179+
(Self::None, Self::None) => true,
180+
_ => false,
181+
}
182+
}
183+
}

0 commit comments

Comments
 (0)