core: Merge both test macros into a generic test_method

This commit is contained in:
Nathan Adams 2019-11-26 20:30:48 +01:00 committed by Mike Welsh
parent 1f4405189a
commit 85b9ffe102
5 changed files with 71 additions and 76 deletions

View File

@ -13,6 +13,10 @@ use swf::avm1::types::{Action, Function};
use crate::tag_utils::SwfSlice;
#[cfg(test)]
#[macro_use]
mod test_utils;
mod activation;
mod fscommand;
mod function;
@ -24,9 +28,6 @@ mod scope;
pub mod script_object;
mod value;
#[cfg(test)]
mod test_utils;
#[cfg(test)]
mod tests;
@ -91,7 +92,7 @@ impl<'gc> Avm1<'gc> {
Self {
player_version,
constant_pool: vec![],
globals: GcCell::allocate(gc_context, globals),
globals,
prototypes,
stack_frames: vec![],
stack: vec![],

View File

@ -4,7 +4,7 @@ use crate::avm1::return_value::ReturnValue;
use crate::avm1::{Avm1, Error, Object, ObjectCell, ScriptObject, UpdateContext, Value};
use crate::backend::navigator::NavigationMethod;
use enumset::EnumSet;
use gc_arena::MutationContext;
use gc_arena::{GcCell, MutationContext};
use rand::Rng;
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.
pub fn create_globals<'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 function_proto = function::create_proto(gc_context, object_proto);
@ -233,7 +236,7 @@ pub fn create_globals<'gc>(
function: function_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)]
mod tests {
use super::*;
use crate::avm1::test_utils::with_avm;
use crate::avm1::Error;
macro_rules! test_std {
( $test: ident, $fun: expr, $($versions: expr => { $([$($arg: expr),*] => $out: expr),* }),* ) => {
#[test]
fn $test() -> Result<(), Error> {
$(
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(())
}
};
fn setup<'gc>(
_avm: &mut Avm1<'gc>,
context: &mut UpdateContext<'_, 'gc, '_>,
) -> ObjectCell<'gc> {
create_globals(context.gc_context).1
}
test_std!(boolean_function, boolean,
test_method!(boolean_function, "Boolean", setup,
[19] => {
[true] => true,
[false] => false,
@ -302,7 +285,7 @@ mod tests {
}
);
test_std!(is_nan_function, is_nan,
test_method!(is_nan_function, "isNaN", setup,
[19] => {
[true] => false,
[false] => false,
@ -328,7 +311,7 @@ mod tests {
}
);
test_std!(number_function, number,
test_method!(number_function, "Number", setup,
[19] => {
[true] => 1.0,
[false] => 0.0,

View File

@ -139,38 +139,19 @@ pub fn create<'gc>(
mod tests {
use super::*;
use crate::avm1::test_utils::with_avm;
use crate::avm1::Error;
macro_rules! test_std {
( $test: ident, $name: expr, $($versions: expr => { $([$($arg: expr),*] => $out: expr),* }),* ) => {
#[test]
fn $test() -> Result<(), Error> {
$(
for version in &$versions {
let _ = with_avm(*version, |avm, context, _root| -> Result<(), Error> {
let math = create(context.gc_context, Some(avm.prototypes().object), 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(())
}
};
fn setup<'gc>(
avm: &mut Avm1<'gc>,
context: &mut UpdateContext<'_, 'gc, '_>,
) -> ObjectCell<'gc> {
create(
context.gc_context,
Some(avm.prototypes().object),
Some(avm.prototypes().function),
)
}
test_std!(test_abs, "abs",
test_method!(test_abs, "abs", setup,
[19] => {
[] => NAN,
[Value::Null] => NAN,
@ -179,7 +160,7 @@ mod tests {
}
);
test_std!(test_acos, "acos",
test_method!(test_acos, "acos", setup,
[19] => {
[] => NAN,
[Value::Null] => NAN,
@ -189,7 +170,7 @@ mod tests {
}
);
test_std!(test_asin, "asin",
test_method!(test_asin, "asin", setup,
[19] => {
[] => NAN,
[Value::Null] => NAN,
@ -199,7 +180,7 @@ mod tests {
}
);
test_std!(test_atan, "atan",
test_method!(test_atan, "atan", setup,
[19] => {
[] => NAN,
[Value::Null] => NAN,
@ -209,7 +190,7 @@ mod tests {
}
);
test_std!(test_ceil, "ceil",
test_method!(test_ceil, "ceil", setup,
[19] => {
[] => NAN,
[Value::Null] => NAN,
@ -217,7 +198,7 @@ mod tests {
}
);
test_std!(test_cos, "cos",
test_method!(test_cos, "cos", setup,
[19] => {
[] => NAN,
[Value::Null] => NAN,
@ -226,7 +207,7 @@ mod tests {
}
);
test_std!(test_exp, "exp",
test_method!(test_exp, "exp", setup,
[19] => {
[] => NAN,
[Value::Null] => NAN,
@ -235,7 +216,7 @@ mod tests {
}
);
test_std!(test_floor, "floor",
test_method!(test_floor, "floor", setup,
[19] => {
[] => NAN,
[Value::Undefined] => NAN,
@ -254,7 +235,7 @@ mod tests {
}
);
test_std!(test_round, "round",
test_method!(test_round, "round", setup,
[19] => {
[] => NAN,
[Value::Null] => NAN,
@ -263,7 +244,7 @@ mod tests {
}
);
test_std!(test_sin, "sin",
test_method!(test_sin, "sin", setup,
[19] => {
[] => NAN,
[Value::Null] => NAN,
@ -272,7 +253,7 @@ mod tests {
}
);
test_std!(test_sqrt, "sqrt",
test_method!(test_sqrt, "sqrt", setup,
[19] => {
[] => NAN,
[Value::Null] => NAN,
@ -281,7 +262,7 @@ mod tests {
}
);
test_std!(test_tan, "tan",
test_method!(test_tan, "tan", setup,
[19] => {
[] => NAN,
[Value::Null] => NAN,

View File

@ -61,3 +61,33 @@ where
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(())
}
};
}

View File

@ -516,7 +516,7 @@ mod test {
assert_eq!(n.to_primitive_num(avm, context).unwrap(), n);
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);