fix tests

This commit is contained in:
vms 2019-03-23 22:21:21 +03:00
parent 6b78411a21
commit abc22ef57e
2 changed files with 47 additions and 31 deletions

View File

@ -758,8 +758,6 @@ mod integration_tests {
#[test] #[test]
fn names() { fn names() {
use super::super::name_section::NameSection;
let module = deserialize_file("./res/cases/v1/with_names.wasm") let module = deserialize_file("./res/cases/v1/with_names.wasm")
.expect("Should be deserialized") .expect("Should be deserialized")
.parse_names() .parse_names()
@ -769,14 +767,15 @@ mod integration_tests {
for section in module.sections() { for section in module.sections() {
match *section { match *section {
Section::Name(ref name_section) => { Section::Name(ref name_section) => {
match *name_section { println!("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA1");
NameSection::Function(ref function_name_section) => { let function_name_subsection = name_section.function_name_subsection().expect("function_name_subsection should presence");
println!("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA2");
assert_eq!( assert_eq!(
function_name_section.names().get(0).expect("Should be entry #0"), function_name_subsection.names().get(0).expect("Should be entry #0"),
"elog" "elog"
); );
assert_eq!( assert_eq!(
function_name_section.names().get(11).expect("Should be entry #0"), function_name_subsection.names().get(11).expect("Should be entry #0"),
"_ZN48_$LT$pwasm_token_contract..Endpoint$LT$T$GT$$GT$3new17hc3ace6dea0978cd9E" "_ZN48_$LT$pwasm_token_contract..Endpoint$LT$T$GT$$GT$3new17hc3ace6dea0978cd9E"
); );
@ -784,9 +783,6 @@ mod integration_tests {
}, },
_ => {}, _ => {},
} }
},
_ => {},
}
} }
assert!(found_section, "Name section should be present in dedicated example"); assert!(found_section, "Name section should be present in dedicated example");

View File

@ -22,6 +22,17 @@ pub struct NameSection {
} }
impl NameSection { impl NameSection {
/// Creates new name section.
pub fn new(module_name_subsection: Option<ModuleNameSubsection>,
function_name_subsection: Option<FunctionNameSubsection>,
local_name_subsection: Option<LocalNameSubsection>) -> Self {
Self {
module_name_subsection,
function_name_subsection,
local_name_subsection
}
}
/// Module name subsection of this section. /// Module name subsection of this section.
pub fn module_name_subsection(&self) -> Option<&ModuleNameSubsection> { pub fn module_name_subsection(&self) -> Option<&ModuleNameSubsection> {
self.module_name_subsection.as_ref() self.module_name_subsection.as_ref()
@ -295,34 +306,43 @@ mod tests {
#[test] #[test]
fn serialize_module_name() { fn serialize_module_name() {
let original = NameSection::Module(ModuleNameSubsection::new("my_mod")); let module_name_subsection = ModuleNameSubsection::new("my_mod");
let original = NameSection::new(Some(module_name_subsection), None, None);
serialize_test(original.clone()); serialize_test(original.clone());
} }
#[test] #[test]
fn serialize_function_names() { fn serialize_function_names() {
let mut sect = FunctionNameSubsection::default(); let mut function_name_subsection = FunctionNameSubsection::default();
sect.names_mut().insert(0, "hello_world".to_string()); function_name_subsection.names_mut().insert(0, "hello_world".to_string());
serialize_test(NameSection::Function(sect)); let name_section = NameSection::new(None, Some(function_name_subsection), None);
serialize_test(name_section);
} }
#[test] #[test]
fn serialize_local_names() { fn serialize_local_names() {
let mut sect = LocalNameSubsection::default(); let mut local_name_subsection = LocalNameSubsection::default();
let mut locals = NameMap::default(); let mut locals = NameMap::default();
locals.insert(0, "msg".to_string()); locals.insert(0, "msg".to_string());
sect.local_names_mut().insert(0, locals); local_name_subsection.local_names_mut().insert(0, locals);
serialize_test(NameSection::Local(sect));
let name_section = NameSection::new(None, None, Some(local_name_subsection));
serialize_test(name_section);
} }
#[test] #[test]
fn serialize_and_deserialize_unparsed() { fn serialize_all_subsections() {
let original = NameSection::Unparsed { let module_name_subsection = ModuleNameSubsection::new("my_mod");
// A made-up name section type which is unlikely to be allocated
// soon, in order to allow us to test `Unparsed`. let mut function_name_subsection = FunctionNameSubsection::default();
name_type: 120, function_name_subsection.names_mut().insert(0, "hello_world".to_string());
name_payload: vec![0u8, 1, 2],
}; let mut local_name_subsection = LocalNameSubsection::default();
serialize_test(original.clone()); let mut locals = NameMap::default();
locals.insert(0, "msg".to_string());
local_name_subsection.local_names_mut().insert(0, locals);
let name_section = NameSection::new(Some(module_name_subsection), Some(function_name_subsection), Some(local_name_subsection));
serialize_test(name_section);
} }
} }