core: Merge both test macros into a generic test_method
This commit is contained in:
parent
1f4405189a
commit
85b9ffe102
|
@ -13,6 +13,10 @@ use swf::avm1::types::{Action, Function};
|
||||||
|
|
||||||
use crate::tag_utils::SwfSlice;
|
use crate::tag_utils::SwfSlice;
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
#[macro_use]
|
||||||
|
mod test_utils;
|
||||||
|
|
||||||
mod activation;
|
mod activation;
|
||||||
mod fscommand;
|
mod fscommand;
|
||||||
mod function;
|
mod function;
|
||||||
|
@ -24,9 +28,6 @@ mod scope;
|
||||||
pub mod script_object;
|
pub mod script_object;
|
||||||
mod value;
|
mod value;
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod test_utils;
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests;
|
mod tests;
|
||||||
|
|
||||||
|
@ -91,7 +92,7 @@ impl<'gc> Avm1<'gc> {
|
||||||
Self {
|
Self {
|
||||||
player_version,
|
player_version,
|
||||||
constant_pool: vec![],
|
constant_pool: vec![],
|
||||||
globals: GcCell::allocate(gc_context, globals),
|
globals,
|
||||||
prototypes,
|
prototypes,
|
||||||
stack_frames: vec![],
|
stack_frames: vec![],
|
||||||
stack: vec![],
|
stack: vec![],
|
||||||
|
|
|
@ -4,7 +4,7 @@ use crate::avm1::return_value::ReturnValue;
|
||||||
use crate::avm1::{Avm1, Error, Object, ObjectCell, ScriptObject, UpdateContext, Value};
|
use crate::avm1::{Avm1, Error, Object, ObjectCell, ScriptObject, UpdateContext, Value};
|
||||||
use crate::backend::navigator::NavigationMethod;
|
use crate::backend::navigator::NavigationMethod;
|
||||||
use enumset::EnumSet;
|
use enumset::EnumSet;
|
||||||
use gc_arena::MutationContext;
|
use gc_arena::{GcCell, MutationContext};
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
use std::f64;
|
use std::f64;
|
||||||
|
|
||||||
|
@ -141,7 +141,10 @@ unsafe impl<'gc> gc_arena::Collect for SystemPrototypes<'gc> {
|
||||||
/// Initialize default global scope and builtins for an AVM1 instance.
|
/// Initialize default global scope and builtins for an AVM1 instance.
|
||||||
pub fn create_globals<'gc>(
|
pub fn create_globals<'gc>(
|
||||||
gc_context: MutationContext<'gc, '_>,
|
gc_context: MutationContext<'gc, '_>,
|
||||||
) -> (SystemPrototypes<'gc>, Box<dyn Object<'gc> + 'gc>) {
|
) -> (
|
||||||
|
SystemPrototypes<'gc>,
|
||||||
|
GcCell<'gc, Box<dyn Object<'gc> + 'gc>>,
|
||||||
|
) {
|
||||||
let object_proto = ScriptObject::object_cell(gc_context, None);
|
let object_proto = ScriptObject::object_cell(gc_context, None);
|
||||||
let function_proto = function::create_proto(gc_context, object_proto);
|
let function_proto = function::create_proto(gc_context, object_proto);
|
||||||
|
|
||||||
|
@ -233,7 +236,7 @@ pub fn create_globals<'gc>(
|
||||||
function: function_proto,
|
function: function_proto,
|
||||||
movie_clip: movie_clip_proto,
|
movie_clip: movie_clip_proto,
|
||||||
},
|
},
|
||||||
Box::new(globals),
|
GcCell::allocate(gc_context, Box::new(globals)),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -241,35 +244,15 @@ pub fn create_globals<'gc>(
|
||||||
#[allow(clippy::unreadable_literal)]
|
#[allow(clippy::unreadable_literal)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::avm1::test_utils::with_avm;
|
|
||||||
use crate::avm1::Error;
|
|
||||||
|
|
||||||
macro_rules! test_std {
|
fn setup<'gc>(
|
||||||
( $test: ident, $fun: expr, $($versions: expr => { $([$($arg: expr),*] => $out: expr),* }),* ) => {
|
_avm: &mut Avm1<'gc>,
|
||||||
#[test]
|
context: &mut UpdateContext<'_, 'gc, '_>,
|
||||||
fn $test() -> Result<(), Error> {
|
) -> ObjectCell<'gc> {
|
||||||
$(
|
create_globals(context.gc_context).1
|
||||||
for version in &$versions {
|
|
||||||
with_avm(*version, |avm, context, this| {
|
|
||||||
|
|
||||||
$(
|
|
||||||
#[allow(unused_mut)]
|
|
||||||
let mut args: Vec<Value> = Vec::new();
|
|
||||||
$(
|
|
||||||
args.push($arg.into());
|
|
||||||
)*
|
|
||||||
assert_eq!($fun(avm, context, this, &args).unwrap(), ReturnValue::Immediate($out.into()), "{:?} => {:?} in swf {}", args, $out, version);
|
|
||||||
)*
|
|
||||||
});
|
|
||||||
}
|
|
||||||
)*
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
test_std!(boolean_function, boolean,
|
test_method!(boolean_function, "Boolean", setup,
|
||||||
[19] => {
|
[19] => {
|
||||||
[true] => true,
|
[true] => true,
|
||||||
[false] => false,
|
[false] => false,
|
||||||
|
@ -302,7 +285,7 @@ mod tests {
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
test_std!(is_nan_function, is_nan,
|
test_method!(is_nan_function, "isNaN", setup,
|
||||||
[19] => {
|
[19] => {
|
||||||
[true] => false,
|
[true] => false,
|
||||||
[false] => false,
|
[false] => false,
|
||||||
|
@ -328,7 +311,7 @@ mod tests {
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
test_std!(number_function, number,
|
test_method!(number_function, "Number", setup,
|
||||||
[19] => {
|
[19] => {
|
||||||
[true] => 1.0,
|
[true] => 1.0,
|
||||||
[false] => 0.0,
|
[false] => 0.0,
|
||||||
|
|
|
@ -139,38 +139,19 @@ pub fn create<'gc>(
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::avm1::test_utils::with_avm;
|
use crate::avm1::test_utils::with_avm;
|
||||||
use crate::avm1::Error;
|
|
||||||
|
|
||||||
macro_rules! test_std {
|
fn setup<'gc>(
|
||||||
( $test: ident, $name: expr, $($versions: expr => { $([$($arg: expr),*] => $out: expr),* }),* ) => {
|
avm: &mut Avm1<'gc>,
|
||||||
#[test]
|
context: &mut UpdateContext<'_, 'gc, '_>,
|
||||||
fn $test() -> Result<(), Error> {
|
) -> ObjectCell<'gc> {
|
||||||
$(
|
create(
|
||||||
for version in &$versions {
|
context.gc_context,
|
||||||
let _ = with_avm(*version, |avm, context, _root| -> Result<(), Error> {
|
Some(avm.prototypes().object),
|
||||||
let math = create(context.gc_context, Some(avm.prototypes().object), Some(avm.prototypes().function));
|
Some(avm.prototypes().function),
|
||||||
let function = math.read().get($name, avm, context, math)?.unwrap_immediate();
|
)
|
||||||
|
|
||||||
$(
|
|
||||||
#[allow(unused_mut)]
|
|
||||||
let mut args: Vec<Value> = Vec::new();
|
|
||||||
$(
|
|
||||||
args.push($arg.into());
|
|
||||||
)*
|
|
||||||
assert_eq!(function.call(avm, context, math, &args)?, ReturnValue::Immediate($out.into()), "{:?} => {:?} in swf {}", args, $out, version);
|
|
||||||
)*
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
})?;
|
|
||||||
}
|
|
||||||
)*
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
test_std!(test_abs, "abs",
|
test_method!(test_abs, "abs", setup,
|
||||||
[19] => {
|
[19] => {
|
||||||
[] => NAN,
|
[] => NAN,
|
||||||
[Value::Null] => NAN,
|
[Value::Null] => NAN,
|
||||||
|
@ -179,7 +160,7 @@ mod tests {
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
test_std!(test_acos, "acos",
|
test_method!(test_acos, "acos", setup,
|
||||||
[19] => {
|
[19] => {
|
||||||
[] => NAN,
|
[] => NAN,
|
||||||
[Value::Null] => NAN,
|
[Value::Null] => NAN,
|
||||||
|
@ -189,7 +170,7 @@ mod tests {
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
test_std!(test_asin, "asin",
|
test_method!(test_asin, "asin", setup,
|
||||||
[19] => {
|
[19] => {
|
||||||
[] => NAN,
|
[] => NAN,
|
||||||
[Value::Null] => NAN,
|
[Value::Null] => NAN,
|
||||||
|
@ -199,7 +180,7 @@ mod tests {
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
test_std!(test_atan, "atan",
|
test_method!(test_atan, "atan", setup,
|
||||||
[19] => {
|
[19] => {
|
||||||
[] => NAN,
|
[] => NAN,
|
||||||
[Value::Null] => NAN,
|
[Value::Null] => NAN,
|
||||||
|
@ -209,7 +190,7 @@ mod tests {
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
test_std!(test_ceil, "ceil",
|
test_method!(test_ceil, "ceil", setup,
|
||||||
[19] => {
|
[19] => {
|
||||||
[] => NAN,
|
[] => NAN,
|
||||||
[Value::Null] => NAN,
|
[Value::Null] => NAN,
|
||||||
|
@ -217,7 +198,7 @@ mod tests {
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
test_std!(test_cos, "cos",
|
test_method!(test_cos, "cos", setup,
|
||||||
[19] => {
|
[19] => {
|
||||||
[] => NAN,
|
[] => NAN,
|
||||||
[Value::Null] => NAN,
|
[Value::Null] => NAN,
|
||||||
|
@ -226,7 +207,7 @@ mod tests {
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
test_std!(test_exp, "exp",
|
test_method!(test_exp, "exp", setup,
|
||||||
[19] => {
|
[19] => {
|
||||||
[] => NAN,
|
[] => NAN,
|
||||||
[Value::Null] => NAN,
|
[Value::Null] => NAN,
|
||||||
|
@ -235,7 +216,7 @@ mod tests {
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
test_std!(test_floor, "floor",
|
test_method!(test_floor, "floor", setup,
|
||||||
[19] => {
|
[19] => {
|
||||||
[] => NAN,
|
[] => NAN,
|
||||||
[Value::Undefined] => NAN,
|
[Value::Undefined] => NAN,
|
||||||
|
@ -254,7 +235,7 @@ mod tests {
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
test_std!(test_round, "round",
|
test_method!(test_round, "round", setup,
|
||||||
[19] => {
|
[19] => {
|
||||||
[] => NAN,
|
[] => NAN,
|
||||||
[Value::Null] => NAN,
|
[Value::Null] => NAN,
|
||||||
|
@ -263,7 +244,7 @@ mod tests {
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
test_std!(test_sin, "sin",
|
test_method!(test_sin, "sin", setup,
|
||||||
[19] => {
|
[19] => {
|
||||||
[] => NAN,
|
[] => NAN,
|
||||||
[Value::Null] => NAN,
|
[Value::Null] => NAN,
|
||||||
|
@ -272,7 +253,7 @@ mod tests {
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
test_std!(test_sqrt, "sqrt",
|
test_method!(test_sqrt, "sqrt", setup,
|
||||||
[19] => {
|
[19] => {
|
||||||
[] => NAN,
|
[] => NAN,
|
||||||
[Value::Null] => NAN,
|
[Value::Null] => NAN,
|
||||||
|
@ -281,7 +262,7 @@ mod tests {
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
test_std!(test_tan, "tan",
|
test_method!(test_tan, "tan", setup,
|
||||||
[19] => {
|
[19] => {
|
||||||
[] => NAN,
|
[] => NAN,
|
||||||
[Value::Null] => NAN,
|
[Value::Null] => NAN,
|
||||||
|
|
|
@ -61,3 +61,33 @@ where
|
||||||
|
|
||||||
rootless_arena(|gc_context| in_the_arena(swf_version, test, gc_context))
|
rootless_arena(|gc_context| in_the_arena(swf_version, test, gc_context))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
macro_rules! test_method {
|
||||||
|
( $test: ident, $name: expr, $object: expr, $($versions: expr => { $([$($arg: expr),*] => $out: expr),* }),* ) => {
|
||||||
|
#[test]
|
||||||
|
fn $test() -> Result<(), Error> {
|
||||||
|
use $crate::avm1::test_utils::*;
|
||||||
|
$(
|
||||||
|
for version in &$versions {
|
||||||
|
let _ = with_avm(*version, |avm, context, _root| -> Result<(), Error> {
|
||||||
|
let object = $object(avm, context);
|
||||||
|
let function = object.read().get($name, avm, context, object)?.unwrap_immediate();
|
||||||
|
|
||||||
|
$(
|
||||||
|
#[allow(unused_mut)]
|
||||||
|
let mut args: Vec<Value> = Vec::new();
|
||||||
|
$(
|
||||||
|
args.push($arg.into());
|
||||||
|
)*
|
||||||
|
assert_eq!(function.call(avm, context, object, &args)?, ReturnValue::Immediate($out.into()), "{:?} => {:?} in swf {}", args, $out, version);
|
||||||
|
)*
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
})?;
|
||||||
|
}
|
||||||
|
)*
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
|
@ -516,7 +516,7 @@ mod test {
|
||||||
assert_eq!(n.to_primitive_num(avm, context).unwrap(), n);
|
assert_eq!(n.to_primitive_num(avm, context).unwrap(), n);
|
||||||
|
|
||||||
let (protos, global) = create_globals(context.gc_context);
|
let (protos, global) = create_globals(context.gc_context);
|
||||||
let vglobal = Value::Object(GcCell::allocate(context.gc_context, global));
|
let vglobal = Value::Object(global);
|
||||||
|
|
||||||
assert_eq!(vglobal.to_primitive_num(avm, context).unwrap(), u);
|
assert_eq!(vglobal.to_primitive_num(avm, context).unwrap(), u);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue