mirror of
https://github.com/guilhermewerner/reflection
synced 2025-06-16 13:34:19 +00:00
Test generics and other things
This commit is contained in:
@ -2,22 +2,23 @@ use crate::{Class, Reflect};
|
||||
use anyhow::Result;
|
||||
use std::sync::Arc;
|
||||
|
||||
#[derive(Reflect, Clone)]
|
||||
pub struct Object {
|
||||
inner: Arc<dyn Reflect>,
|
||||
}
|
||||
|
||||
impl Object {
|
||||
fn New(obj: impl Reflect) -> Self {
|
||||
pub fn New(obj: impl Reflect) -> Self {
|
||||
Self {
|
||||
inner: Arc::new(obj),
|
||||
}
|
||||
}
|
||||
|
||||
fn InstanceOf(&self, class: &Class) -> bool {
|
||||
pub fn InstanceOf(&self, class: &Class) -> bool {
|
||||
self.inner.as_ref().type_id() == class.GetId()
|
||||
}
|
||||
|
||||
fn GetClass(&self) -> Result<()> {
|
||||
pub fn GetClass(&self) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@ -25,3 +26,34 @@ impl Object {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod Test {
|
||||
use super::*;
|
||||
use crate::{Class, Reflect};
|
||||
|
||||
struct Foo {}
|
||||
struct Bar {}
|
||||
|
||||
unsafe impl Reflect for Foo {
|
||||
fn TypeName(&self) -> &'static str {
|
||||
std::any::type_name::<Self>()
|
||||
}
|
||||
}
|
||||
|
||||
unsafe impl Reflect for Bar {
|
||||
fn TypeName(&self) -> &'static str {
|
||||
std::any::type_name::<Self>()
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn InstanceOf() {
|
||||
let foo_class: Class = Class::New::<Foo>();
|
||||
let bar_class: Class = Class::New::<Bar>();
|
||||
let foo_instance: Object = Object::New(Foo {});
|
||||
|
||||
assert!(foo_instance.InstanceOf(&foo_class));
|
||||
assert!(!foo_instance.InstanceOf(&bar_class));
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user