Skip to content

Commit

Permalink
Added some more macro compile test
Browse files Browse the repository at this point in the history
  • Loading branch information
DelSkayn committed Jul 28, 2023
1 parent 7848611 commit ab98622
Showing 1 changed file with 100 additions and 0 deletions.
100 changes: 100 additions & 0 deletions tests/macros/pass_method.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
use rquickjs::{class::Trace, CatchResultExt, Class, Context, Runtime};

#[derive(Trace)]
#[rquickjs::class]
pub struct TestClass {
value: u32,
another_value: u32,
}

#[rquickjs::methods]
impl TestClass {
#[qjs(constructor)]
pub fn new(value: u32) -> Self {
TestClass {
value,
another_value: value,
}
}

#[qjs(get, rename = "value")]
pub fn get_value(&self) -> u32 {
self.value
}

#[qjs(set, rename = "value")]
pub fn set_value(&mut self, v: u32) {
self.value = v
}

#[qjs(get, rename = "anotherValue", enumerable)]
pub fn get_another_value(&self) -> u32 {
self.another_value
}

#[qjs(set, rename = "anotherValue", enumerable)]
pub fn set_another_value(&mut self, v: u32) {
self.another_value = v
}

#[qjs(r#static)]
pub fn compare(a: &Self, b: &Self) -> bool {
a.value == b.value && a.another_value == b.another_value
}

#[qjs(skip)]
pub fn inner_function(&self) {}
}

pub fn main() {
let rt = Runtime::new().unwrap();
let ctx = Context::full(&rt).unwrap();

ctx.with(|ctx| {
Class::<TestClass>::define(&ctx.globals()).unwrap();
ctx.globals()
.set(
"t",
TestClass {
value: 1,
another_value: 2,
},
)
.unwrap();

ctx.eval::<(), _>(
r#"
if(t.value !== 1){
throw new Error(1)
}
if(t.anotherValue !== 2){
throw new Error(2)
}
t.value = 5;
if(t.value !== 5){
throw new Error(3)
}
let nv = new TestClass(5);
if(nv.value !== 5){
throw new Error(4)
}
t.anotherValue = 5;
if(!TestClass.compare(t,nv)){
throw new Error(5)
}
if(nv.inner_function !== undefined){
throw new Error(6)
}
let proto = TestClass.prototype;
if(!Object.keys(proto).includes("anotherValue")){
throw new Error(7)
}
if(Object.keys(proto).includes("value")){
throw new Error(8)
}
"#,
)
.catch(&ctx)
.unwrap();
});
}

0 comments on commit ab98622

Please sign in to comment.