Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kennykerr committed Oct 17, 2023
1 parent e6a1b2d commit 5972bb4
Show file tree
Hide file tree
Showing 13 changed files with 143 additions and 149 deletions.
22 changes: 9 additions & 13 deletions crates/tests/metadata/tests/attribute_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,19 @@ use metadata::*;
#[test]
fn attribute_enum() {
let files = tool_lib::default_metadata();
let reader = &Reader::new(&files);
let reader = &Reader::new(files);

let (method, _) = reader
.get_method_def(TypeName::new(
.get_method_def(
"Windows.Win32.UI.WindowsAndMessaging",
"SetWindowLongPtrA",
))
)
.next()
.unwrap();

check_attr_arg_enum(
reader,
reader
.find_attribute(method, "SupportedArchitectureAttribute")
method
.find_attribute( "SupportedArchitectureAttribute")
.unwrap(),
"",
"Windows.Win32.Foundation.Metadata.Architecture",
Expand All @@ -25,35 +24,32 @@ fn attribute_enum() {

// The only attribute currently with a named enum argument is "GCPressure".
let def = reader
.get_type_def(TypeName::new("Windows.Graphics.Imaging", "BitmapBuffer"))
.get_type_def("Windows.Graphics.Imaging", "BitmapBuffer")
.next()
.unwrap();

check_attr_arg_enum(
reader,
reader.find_attribute(def, "GCPressureAttribute").unwrap(),
def.find_attribute( "GCPressureAttribute").unwrap(),
"amount",
"Windows.Foundation.Metadata.GCPressureAmount",
2,
);
}

fn check_attr_arg_enum(
reader: &Reader,
attr: Attribute,
arg_name: &str,
expected_type: &str,
expected_value: i32,
) {
let (_, value) = reader
.attribute_args(attr)
let (_, value) = attr.args()
.drain(..)
.find(|(name, _)| name == arg_name)
.unwrap();

if let Value::EnumDef(ty, value) = value {
if let Value::I32(value) = *value {
assert_eq!(expected_type, reader.type_def_type_name(ty).to_string());
assert_eq!(expected_type, ty.type_name().to_string());
assert_eq!(expected_value, value);
return;
}
Expand Down
10 changes: 5 additions & 5 deletions crates/tests/metadata/tests/fn_call_size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ fn size() {
// dumpbin /exports kernel32.lib | findstr /i RtmConvertIpv6AddressAndLengthToNetAddress

let files = tool_lib::default_metadata();
let reader = &Reader::new(&files);
let reader = &Reader::new(files);

assert_eq!(
struct_size(reader, "Windows.Win32.System.Variant", "VARIANT"),
Expand Down Expand Up @@ -313,16 +313,16 @@ fn size() {

fn function_size(reader: &Reader, namespace: &str, name: &str) -> usize {
let (method, _) = reader
.get_method_def(TypeName::new(namespace, name))
.get_method_def(namespace, name)
.next()
.expect("Function not found");
return reader.method_def_size(method);
method.signature(&[]).size()
}

fn struct_size(reader: &Reader, namespace: &str, name: &str) -> usize {
let def = reader
.get_type_def(TypeName::new(namespace, name))
.get_type_def(namespace, name)
.next()
.expect("Type not found");
return reader.type_def_size(def);
def.size()
}
2 changes: 1 addition & 1 deletion crates/tests/metadata/tests/unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use metadata::*;
#[test]
fn test() {
let files = tool_lib::default_metadata();
let reader = &Reader::new(&files);
let reader = &Reader::new(files);
let filter = Filter::new(
&["Windows", "BadNamespace", "Windows.AI"],
&["Windows.Foundation.Rect", "Windows.Foundation.BadType"],
Expand Down
20 changes: 10 additions & 10 deletions crates/tests/riddle/tests/generic_interfaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use windows_metadata::*;
#[test]
fn test() {
let files = run_riddle("generic_interfaces", "winrt", &[]);
let reader = &Reader::new(&files);
let reader = Reader::new(files);

let types: Vec<Item> = reader
.namespace_items("Test", &Default::default())
Expand All @@ -13,24 +13,24 @@ fn test() {
assert_eq!(types.len(), 4);

let def = reader
.get_type_def(TypeName::new("Test", "IIterable"))
.get_type_def("Test", "IIterable")
.next()
.unwrap();

assert_eq!(reader.type_def_interface_impls(def).count(), 0);
let generics: Vec<GenericParam> = reader.type_def_generics(def).collect();
assert_eq!(def.interface_impls().count(), 0);
let generics: Vec<GenericParam> = def.generics().collect();
assert_eq!(generics.len(), 1);
assert_eq!(reader.generic_param_name(generics[0]), "T");
assert_eq!(generics[0].name(), "T");

let def = reader
.get_type_def(TypeName::new("Test", "IMapView"))
.get_type_def("Test", "IMapView")
.next()
.unwrap();

let impls: Vec<InterfaceImpl> = reader.type_def_interface_impls(def).collect();
let impls: Vec<InterfaceImpl> = def.interface_impls().collect();
assert_eq!(impls.len(), 1);
let generics: Vec<GenericParam> = reader.type_def_generics(def).collect();
let generics: Vec<GenericParam> = def.generics().collect();
assert_eq!(generics.len(), 2);
assert_eq!(reader.generic_param_name(generics[0]), "K");
assert_eq!(reader.generic_param_name(generics[1]), "V");
assert_eq!(generics[0].name(), "K");
assert_eq!(generics[1].name(), "V");
}
2 changes: 1 addition & 1 deletion crates/tests/riddle/tests/module_attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ use windows_metadata::*;
#[test]
fn test() {
let files = run_riddle("module_attributes", "winrt", &[]);
let _reader = &Reader::new(&files);
let _reader = Reader::new(files);
}
22 changes: 11 additions & 11 deletions crates/tests/riddle/tests/nested_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use windows_metadata::*;
#[test]
fn test() {
let files = run_riddle("nested_module", "winrt", &[]);
let reader = &Reader::new(&files);
let reader = Reader::new(files);

let types: Vec<Item> = reader
.namespace_items("Test", &Default::default())
Expand All @@ -15,12 +15,12 @@ fn test() {
panic!("type expected")
};

assert_eq!(reader.type_def_name(def), "TestType");
assert_eq!(reader.type_def_kind(def), TypeKind::Struct);
let fields: Vec<Field> = reader.type_def_fields(def).collect();
assert_eq!(def.name(), "TestType");
assert_eq!(def.kind(), TypeKind::Struct);
let fields: Vec<Field> = def.fields().collect();
assert_eq!(fields.len(), 1);
assert_eq!(reader.field_name(fields[0]), "field");
assert!(matches!(reader.field_type(fields[0], None), Type::I32));
assert_eq!(fields[0].name(), "field");
assert!(matches!(fields[0].ty(None), Type::I32));

let types: Vec<Item> = reader
.namespace_items("Test.NestedModule", &Default::default())
Expand All @@ -31,10 +31,10 @@ fn test() {
panic!("type expected")
};

assert_eq!(reader.type_def_name(def), "NestedType");
assert_eq!(reader.type_def_kind(def), TypeKind::Struct);
let fields: Vec<Field> = reader.type_def_fields(def).collect();
assert_eq!(def.name(), "NestedType");
assert_eq!(def.kind(), TypeKind::Struct);
let fields: Vec<Field> = def.fields().collect();
assert_eq!(fields.len(), 1);
assert_eq!(reader.field_name(fields[0]), "field");
assert!(matches!(reader.field_type(fields[0], None), Type::F32));
assert_eq!(fields[0].name(), "field");
assert!(matches!(fields[0].ty( None), Type::F32));
}
34 changes: 17 additions & 17 deletions crates/tests/riddle/tests/nested_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,38 @@ use windows_metadata::*;
#[test]
fn test() {
let files = run_riddle("nested_struct", "winrt", &[]);
let reader = &Reader::new(&files);
let reader = Reader::new(files);

let def = reader
.get_type_def(TypeName::new("Test", "Inner"))
.get_type_def("Test", "Inner")
.next()
.expect("Type missing");

assert_eq!(reader.type_def_kind(def), TypeKind::Struct);
let fields: Vec<Field> = reader.type_def_fields(def).collect();
assert_eq!(def.kind(), TypeKind::Struct);
let fields: Vec<Field> = def.fields().collect();
assert_eq!(fields.len(), 1);
assert_eq!(reader.field_name(fields[0]), "field_i32");
assert!(matches!(reader.field_type(fields[0], None), Type::I32));
assert_eq!(fields[0].name(), "field_i32");
assert!(matches!(fields[0].ty(None), Type::I32));

let def = reader
.get_type_def(TypeName::new("Test", "Outer"))
.get_type_def("Test", "Outer")
.next()
.expect("Type missing");

assert_eq!(reader.type_def_kind(def), TypeKind::Struct);
let fields: Vec<Field> = reader.type_def_fields(def).collect();
assert_eq!(def.kind(), TypeKind::Struct);
let fields: Vec<Field> = def.fields().collect();
assert_eq!(fields.len(), 3);
assert_eq!(reader.field_name(fields[0]), "field_bool");
assert_eq!(reader.field_name(fields[1]), "field_inner");
assert_eq!(reader.field_name(fields[2]), "field_usize");
assert!(matches!(reader.field_type(fields[0], None), Type::Bool));
assert!(matches!(reader.field_type(fields[2], None), Type::USize));
assert_eq!(fields[0].name(), "field_bool");
assert_eq!(fields[1].name(), "field_inner");
assert_eq!(fields[2].name(), "field_usize");
assert!(matches!(fields[0].ty(None), Type::Bool));
assert!(matches!(fields[2].ty(None), Type::USize));

let Type::TypeDef(def, generics) = reader.field_type(fields[1], None) else {
let Type::TypeDef(def, generics) = fields[1].ty( None) else {
panic!("wrong type")
};

assert_eq!(reader.type_def_namespace(def), "Test");
assert_eq!(reader.type_def_name(def), "Inner");
assert_eq!(def.namespace(), "Test");
assert_eq!(def.name(), "Inner");
assert!(generics.is_empty());
}
72 changes: 36 additions & 36 deletions crates/tests/riddle/tests/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,56 +4,56 @@ use windows_metadata::*;
#[test]
fn test() {
let files = run_riddle("params", "winrt", &[]);
let reader = &Reader::new(&files);
let reader = Reader::new(files);

let def = reader
.get_type_def(TypeName::new("Test", "IParams"))
.get_type_def("Test", "IParams")
.next()
.expect("Type missing");

assert_eq!(reader.type_def_kind(def), TypeKind::Interface);
assert_eq!(def.kind(), TypeKind::Interface);
let generics = &vec![];

assert!(reader.type_def_fields(def).next().is_none());
let methods: Vec<MethodDef> = reader.type_def_methods(def).collect();
assert!(def.fields().next().is_none());
let methods: Vec<MethodDef> = def.methods().collect();
assert_eq!(methods.len(), 14);

assert_eq!(reader.method_def_name(methods[0]), "Nothing");
let sig = reader.method_def_signature(methods[0], generics);
assert_eq!(methods[0].name(), "Nothing");
let sig = methods[0].signature(generics);
assert_eq!(sig.return_type, Type::Void);
assert!(sig.params.is_empty());

assert_eq!(reader.method_def_name(methods[1]), "Bool");
assert_eq!(reader.method_def_name(methods[2]), "I8");
assert_eq!(reader.method_def_name(methods[3]), "U8");
assert_eq!(reader.method_def_name(methods[4]), "I16");
assert_eq!(reader.method_def_name(methods[5]), "U16");
assert_eq!(reader.method_def_name(methods[6]), "I32");
assert_eq!(reader.method_def_name(methods[7]), "U32");
assert_eq!(reader.method_def_name(methods[8]), "I64");
assert_eq!(reader.method_def_name(methods[9]), "U64");
assert_eq!(reader.method_def_name(methods[10]), "F32");
assert_eq!(reader.method_def_name(methods[11]), "F64");
assert_eq!(reader.method_def_name(methods[12]), "ISize");
assert_eq!(reader.method_def_name(methods[13]), "USize");

method(reader, generics, methods[1], Type::Bool);
method(reader, generics, methods[2], Type::I8);
method(reader, generics, methods[3], Type::U8);
method(reader, generics, methods[4], Type::I16);
method(reader, generics, methods[5], Type::U16);
method(reader, generics, methods[6], Type::I32);
method(reader, generics, methods[7], Type::U32);
method(reader, generics, methods[8], Type::I64);
method(reader, generics, methods[9], Type::U64);
method(reader, generics, methods[10], Type::F32);
method(reader, generics, methods[11], Type::F64);
method(reader, generics, methods[12], Type::ISize);
method(reader, generics, methods[13], Type::USize);
assert_eq!(methods[1].name(), "Bool");
assert_eq!(methods[2].name(), "I8");
assert_eq!(methods[3].name(), "U8");
assert_eq!(methods[4].name(), "I16");
assert_eq!(methods[5].name(), "U16");
assert_eq!(methods[6].name(), "I32");
assert_eq!(methods[7].name(), "U32");
assert_eq!(methods[8].name(), "I64");
assert_eq!(methods[9].name(), "U64");
assert_eq!(methods[10].name(), "F32");
assert_eq!(methods[11].name(), "F64");
assert_eq!(methods[12].name(), "ISize");
assert_eq!(methods[13].name(), "USize");

method(generics, methods[1], Type::Bool);
method(generics, methods[2], Type::I8);
method(generics, methods[3], Type::U8);
method(generics, methods[4], Type::I16);
method(generics, methods[5], Type::U16);
method(generics, methods[6], Type::I32);
method(generics, methods[7], Type::U32);
method(generics, methods[8], Type::I64);
method(generics, methods[9], Type::U64);
method(generics, methods[10], Type::F32);
method(generics, methods[11], Type::F64);
method(generics, methods[12], Type::ISize);
method(generics, methods[13], Type::USize);
}

fn method(reader: &Reader, generics: &[Type], method: MethodDef, expected: Type) {
let sig = reader.method_def_signature(method, generics);
fn method( generics: &[Type], method: MethodDef, expected: Type) {
let sig = method.signature(generics);
assert_eq!(sig.return_type, expected);
assert_eq!(sig.params.len(), 2);
assert_eq!(sig.params[0], expected);
Expand Down
Loading

0 comments on commit 5972bb4

Please sign in to comment.