From b54b05c1c8a823f3aaa3fa8f0a37a42369e2ea2a Mon Sep 17 00:00:00 2001 From: Daniel Thaler Date: Wed, 23 Aug 2023 19:39:13 +0200 Subject: [PATCH] Use strings instead of enums for ElementName, AttributeName, etc Tt is much more obvious to directly use the names from the arxml files. It also allows autosar-data-specification to drop its dependency on pyo3. --- Cargo.lock | 3 - Cargo.toml | 2 +- README.md | 38 +++++----- src/arxmlfile.rs | 16 ++--- src/element.rs | 88 +++++++++++++---------- src/lib.rs | 155 ++++++++++++++++++++++++++++++----------- src/model.rs | 4 +- src/version.rs | 98 ++++++++++++++++++++++++++ test/arxmlfile_test.py | 38 +++++----- test/element_test.py | 118 +++++++++++++++---------------- test/model_test.py | 58 +++++++-------- test/test.py | 6 +- 12 files changed, 405 insertions(+), 219 deletions(-) create mode 100644 src/version.rs diff --git a/Cargo.lock b/Cargo.lock index 4c64595..ecdd486 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -26,9 +26,6 @@ name = "autosar-data-specification" version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a49cef1cb790fc97f6e3816c8232b41689a1a7b6be3265d93ab6e85fca3df292" -dependencies = [ - "pyo3", -] [[package]] name = "bitflags" diff --git a/Cargo.toml b/Cargo.toml index f9fc77a..a148b12 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,5 +13,5 @@ crate-type = ["cdylib"] [dependencies] autosar-data = {version = "0.7"} -autosar-data-specification = {version = "0.7", features = ["pylib"]} +autosar-data-specification = {version = "0.7"} pyo3 = "0.19.2" diff --git a/README.md b/README.md index 310afa6..d543fb4 100644 --- a/README.md +++ b/README.md @@ -49,27 +49,27 @@ from autosar_data import * model = AutosarModel() # create a file in the model -file1 = model.create_file("filename.arxml", specification.AutosarVersion.Autosar_4_3_0) +file1 = model.create_file("filename.arxml", AutosarVersion.Autosar_4_3_0) # a model can consist of multiple files - elements appear in all of them by default, unless restrictions are set -file2 = model.create_file("filename2.arxml", specification.AutosarVersion.Autosar_00051) +file2 = model.create_file("filename2.arxml", AutosarVersion.Autosar_00051) # initially the model only has its root element, . Create some elements el_elements = model.root_element \ - .create_sub_element(specification.ElementName.ArPackages) \ - .create_named_sub_element(specification.ElementName.ArPackage, "Pkg") \ - .create_sub_element(specification.ElementName.Elements) + .create_sub_element("AR-PACKAGES") \ + .create_named_sub_element("AR-PACKAGE", "Pkg") \ + .create_sub_element("ELEMENTS") # create some more elements el_fibex_element_ref = el_elements \ - .create_named_sub_element(specification.ElementName.System, "System") \ - .create_sub_element(specification.ElementName.FibexElements) \ - .create_sub_element(specification.ElementName.FibexElementRefConditional) \ - .create_sub_element(specification.ElementName.FibexElementRef) + .create_named_sub_element("SYSTEM", "System") \ + .create_sub_element("FIBEX-ELEMENTS") \ + .create_sub_element("FIBEX-ELEMENT-REF-CONDITIONAL") \ + .create_sub_element("FIBEX-ELEMENT-REF") el_can_cluster = model.root_element \ - .get_sub_element(specification.ElementName.ArPackages) \ - .create_named_sub_element(specification.ElementName.ArPackage, "Pkg2") \ - .create_sub_element(specification.ElementName.Elements) \ - .create_named_sub_element(specification.ElementName.CanCluster, "CanCluster") + .get_sub_element("AR-PACKAGES") \ + .create_named_sub_element("AR-PACKAGE", "Pkg2") \ + .create_sub_element("ELEMENTS") \ + .create_named_sub_element("CAN-CLUSTER", "CanCluster") # set a cross reference el_fibex_element_ref.reference_target = el_can_cluster @@ -81,17 +81,17 @@ el_fibex_element_ref.reference_target == el_can_cluster # True # get an attribute -el_fibex_element_ref.attribute_value(specification.AttributeName.Dest) +el_fibex_element_ref.attribute_value("DEST") # EnumItem.CanCluster -model.root_element.attribute_value(specification.AttributeName.xmlns) +model.root_element.attribute_value("xmlns") # 'http://autosar.org/schema/r4.0' # set an attribute value -el_fibex_element_ref.set_attribute(specification.AttributeName.Dest, specification.EnumItem.ISignal) +el_fibex_element_ref.set_attribute("DEST", "I-SIGNAL") # setting the DEST of the reference to an invalid value has invalidated the # reference, so accessing el_fibex_element_ref.reference_target will now cause an exception -el_can_cluster.set_attribute(specification.AttributeName.Uuid, "1234567890abcdefg") +el_can_cluster.set_attribute("UUID", "1234567890abcdefg") # get the current xml text of the model: print(file1.serialize()) @@ -127,8 +127,8 @@ model = AutosarModel() # display all the triggered PDUs in the file for (depth, element) in model.elements_dfs: - if element.element_name == specification.ElementName.PduTriggering: - pdu = element.get_sub_element(specification.ElementName.IPduRef).reference_target + if element.element_name == "PDU-TRIGGERING": + pdu = element.get_sub_element("I-PDU-REF").reference_target print(str.format("PDU: <{}> = {}", pdu.element_name, pdu.item_name)) ``` diff --git a/src/arxmlfile.rs b/src/arxmlfile.rs index f9acea0..e5ef6db 100644 --- a/src/arxmlfile.rs +++ b/src/arxmlfile.rs @@ -46,24 +46,24 @@ impl ArxmlFile { } #[getter] - fn version(&self) -> autosar_data_rs::AutosarVersion { - self.0.version() + fn version(&self) -> AutosarVersion { + self.0.version().into() } #[setter] - fn set_version(&self, version: autosar_data_rs::AutosarVersion) -> PyResult<()> { + fn set_version(&self, version: AutosarVersion) -> PyResult<()> { self.0 - .set_version(version) + .set_version(version.into()) .map_err(|error| AutosarDataError::new_err(error.to_string())) } fn check_version_compatibility( &self, - target_version: autosar_data_rs::AutosarVersion, + target_version: AutosarVersion, ) -> Vec { Python::with_gil(|py| { self.0 - .check_version_compatibility(target_version) + .check_version_compatibility(target_version.into()) .0 .iter() .map(|cerr| -> PyObject { @@ -76,7 +76,7 @@ impl ArxmlFile { py, IncompatibleAttributeError { element: Element(element.to_owned()), - attribute: *attribute, + attribute: attribute.to_string(), version_mask: *version_mask, target_version, }, @@ -92,7 +92,7 @@ impl ArxmlFile { py, IncompatibleAttributeValueError { element: Element(element.to_owned()), - attribute: *attribute, + attribute: attribute.to_string(), attribute_value: attribute_value.to_owned(), version_mask: *version_mask, target_version, diff --git a/src/element.rs b/src/element.rs index 62177ca..074fb2e 100644 --- a/src/element.rs +++ b/src/element.rs @@ -48,8 +48,8 @@ impl Element { } #[getter] - fn element_name(&self) -> autosar_data_rs::ElementName { - self.0.element_name() + fn element_name(&self) -> String { + self.0.element_name().to_string() } #[getter] @@ -105,29 +105,24 @@ impl Element { } } - fn create_sub_element(&self, element_name: autosar_data_rs::ElementName) -> PyResult { + fn create_sub_element(&self, name_str: String) -> PyResult { + let element_name = get_element_name(name_str)?; match self.0.create_sub_element(element_name) { Ok(element) => Ok(Element(element)), Err(error) => Err(AutosarDataError::new_err(error.to_string())), } } - fn create_sub_element_at( - &self, - element_name: autosar_data_rs::ElementName, - position: usize, - ) -> PyResult { + fn create_sub_element_at(&self, name_str: String, position: usize) -> PyResult { + let element_name = get_element_name(name_str)?; match self.0.create_sub_element_at(element_name, position) { Ok(element) => Ok(Element(element)), Err(error) => Err(AutosarDataError::new_err(error.to_string())), } } - fn create_named_sub_element( - &self, - element_name: autosar_data_rs::ElementName, - item_name: &str, - ) -> PyResult { + fn create_named_sub_element(&self, name_str: String, item_name: &str) -> PyResult { + let element_name = get_element_name(name_str)?; match self.0.create_named_sub_element(element_name, item_name) { Ok(element) => Ok(Element(element)), Err(error) => Err(AutosarDataError::new_err(error.to_string())), @@ -136,10 +131,11 @@ impl Element { fn create_named_sub_element_at( &self, - element_name: autosar_data_rs::ElementName, + name_str: String, item_name: &str, position: usize, ) -> PyResult { + let element_name = get_element_name(name_str)?; match self .0 .create_named_sub_element_at(element_name, item_name, position) @@ -198,8 +194,9 @@ impl Element { } } - fn get_sub_element(&self, name: autosar_data_rs::ElementName) -> Option { - self.0.get_sub_element(name).map(Element) + fn get_sub_element(&self, name_str: String) -> PyResult> { + let element_name = get_element_name(name_str)?; + Ok(self.0.get_sub_element(element_name).map(Element)) } #[getter] @@ -214,7 +211,14 @@ impl Element { #[setter] fn set_character_data(&self, chardata: PyObject) -> PyResult<()> { - let cdata = extract_character_data(chardata)?; + let spec = self + .0 + .element_type() + .chardata_spec() + .ok_or(AutosarDataError::new_err( + autosar_data_rs::AutosarDataError::IncorrectContentType.to_string(), + ))?; + let cdata = extract_character_data(spec, chardata)?; self.0 .set_character_data(cdata) .map_err(|error| AutosarDataError::new_err(error.to_string())) @@ -256,41 +260,53 @@ impl Element { AttributeIterator(self.0.attributes()) } - fn attribute_value(&self, attrname: autosar_data_rs::AttributeName) -> Option { - Some(character_data_to_object(&self.0.attribute_value(attrname)?)) - } - - fn set_attribute( - &self, - attrname: autosar_data_rs::AttributeName, - value: PyObject, - ) -> PyResult<()> { - let cdata = extract_character_data(value)?; + fn attribute_value(&self, attrname_str: String) -> PyResult> { + let attrname = get_attribute_name(attrname_str)?; + Ok(self + .0 + .attribute_value(attrname) + .map(|cdata| character_data_to_object(&cdata))) + } + + fn set_attribute(&self, attrname_str: String, value: PyObject) -> PyResult<()> { + let attrname = get_attribute_name(attrname_str)?; + let attrspec = self.0.element_type().find_attribute_spec(attrname).ok_or( + AutosarDataError::new_err( + autosar_data_rs::AutosarDataError::IncorrectContentType.to_string(), + ), + )?; + let cdata = extract_character_data(attrspec.spec, value)?; self.0 .set_attribute(attrname, cdata) .map_err(|error| AutosarDataError::new_err(error.to_string())) } - fn set_attribute_string( - &self, - attrname: autosar_data_rs::AttributeName, - text: &str, - ) -> PyResult<()> { + fn set_attribute_string(&self, attrname_str: String, text: &str) -> PyResult<()> { + let attrname = get_attribute_name(attrname_str)?; self.0 .set_attribute_string(attrname, text) .map_err(|error| AutosarDataError::new_err(error.to_string())) } - fn remove_attribute(&self, attrname: autosar_data_rs::AttributeName) -> bool { - self.0.remove_attribute(attrname) + fn remove_attribute(&self, attrname_str: String) -> PyResult { + let attrname = get_attribute_name(attrname_str)?; + Ok(self.0.remove_attribute(attrname)) } fn sort(&self) { self.0.sort() } - fn list_valid_sub_elements(&self) -> Vec<(autosar_data_rs::ElementName, bool, bool)> { - self.0.list_valid_sub_elements() + fn list_valid_sub_elements(&self) -> Vec { + self.0 + .list_valid_sub_elements() + .iter() + .map(|(name, is_named, is_allowed)| ValidSubElementInfo { + element_name: name.to_string(), + is_named: *is_named, + is_allowed: *is_allowed, + }) + .collect() } #[getter] diff --git a/src/lib.rs b/src/lib.rs index 345d1a4..2858a68 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,7 @@ +use std::str::FromStr; + use ::autosar_data as autosar_data_rs; +use autosar_data_specification::CharacterDataSpec; use pyo3::create_exception; use pyo3::prelude::*; use pyo3::types::*; @@ -6,6 +9,9 @@ use pyo3::types::*; mod arxmlfile; mod element; mod model; +mod version; + +use version::*; create_exception!(module, AutosarDataError, pyo3::exceptions::PyException); @@ -41,26 +47,26 @@ struct AttributeIterator(autosar_data_rs::AttributeIterator); struct IncompatibleElementError { element: Element, version_mask: u32, - target_version: autosar_data_rs::AutosarVersion, + target_version: AutosarVersion, } #[pyclass(frozen)] #[derive(Debug, Clone)] struct IncompatibleAttributeError { element: Element, - attribute: autosar_data_rs::AttributeName, + attribute: String, version_mask: u32, - target_version: autosar_data_rs::AutosarVersion, + target_version: AutosarVersion, } #[pyclass(frozen)] #[derive(Debug, Clone)] struct IncompatibleAttributeValueError { element: Element, - attribute: autosar_data_rs::AttributeName, + attribute: String, attribute_value: String, version_mask: u32, - target_version: autosar_data_rs::AutosarVersion, + target_version: AutosarVersion, } #[pyclass(frozen)] @@ -69,10 +75,21 @@ struct ElementType(autosar_data_specification::ElementType); #[pyclass(frozen)] struct Attribute { - pub attrname: autosar_data_rs::AttributeName, + pub attrname: String, pub content: PyObject, } +#[pyclass(frozen)] +#[derive(Debug, Clone)] +struct ValidSubElementInfo { + #[pyo3(get)] + element_name: String, + #[pyo3(get)] + is_named: bool, + #[pyo3(get)] + is_allowed: bool, +} + #[pyclass(frozen)] #[derive(Debug, Clone)] enum ContentType { @@ -92,7 +109,7 @@ impl IncompatibleAttributeError { fn __str__(&self) -> String { format!( - "Attribute {} in <{}> is incompatible with version {}", + "Attribute {} in <{}> is incompatible with version {:?}", self.attribute, self.element.0.xml_path(), self.target_version @@ -100,8 +117,8 @@ impl IncompatibleAttributeError { } #[getter] - fn get_attribute(&self) -> autosar_data_rs::AttributeName { - self.attribute + fn get_attribute(&self) -> String { + self.attribute.to_string() } #[getter] @@ -123,7 +140,7 @@ impl IncompatibleAttributeValueError { fn __str__(&self) -> String { format!( - "Attribute value {} in attribue {} of element <{}> is incompatible with version {}", + "Attribute value {} in attribue {} of element <{}> is incompatible with version {:?}", self.attribute_value, self.attribute, self.element.0.xml_path(), @@ -132,8 +149,8 @@ impl IncompatibleAttributeValueError { } #[getter] - fn get_attribute(&self) -> autosar_data_rs::AttributeName { - self.attribute + fn get_attribute(&self) -> String { + self.attribute.to_string() } #[getter] @@ -160,7 +177,7 @@ impl IncompatibleElementError { fn __str__(&self) -> String { format!( - "Element <{}> is incompatible with version {}", + "Element <{}> is incompatible with version {:?}", self.element.0.xml_path(), self.target_version ) @@ -203,22 +220,20 @@ impl ElementType { self.0.splittable() } - fn splittable_in(&self, version: autosar_data_rs::AutosarVersion) -> bool { - self.0.splittable_in(version) + fn splittable_in(&self, version: AutosarVersion) -> bool { + self.0.splittable_in(version.into()) } - fn reference_dest_value(&self, target: &ElementType) -> Option { - self.0.reference_dest_value(&target.0) + fn reference_dest_value(&self, target: &ElementType) -> Option { + self.0.reference_dest_value(&target.0).map(|enumitem| enumitem.to_string()) } - fn find_sub_element( - &self, - target_name: autosar_data_rs::ElementName, - version: u32, - ) -> Option { - self.0 - .find_sub_element(target_name, version) - .map(|(etype, _)| ElementType(etype)) + fn find_sub_element(&self, target_name: String, version: u32) -> PyResult> { + let elem_name = get_element_name(target_name)?; + Ok(self + .0 + .find_sub_element(elem_name, version) + .map(|(etype, _)| ElementType(etype))) } } @@ -314,7 +329,7 @@ impl AttributeIterator { fn __next__(&mut self) -> Option { let autosar_data_rs::Attribute { attrname, content } = self.0.next()?; Some(Attribute { - attrname, + attrname: attrname.to_string(), content: character_data_to_object(&content), }) } @@ -334,8 +349,8 @@ impl Attribute { } #[getter] - fn attrname(&self) -> autosar_data_rs::AttributeName { - self.attrname + fn attrname(&self) -> String { + self.attrname.to_string() } #[getter] @@ -344,16 +359,18 @@ impl Attribute { } } +#[pymethods] +impl ValidSubElementInfo { + fn __repr__(&self) -> String { + format!("{:#?}", self) + } +} + /// A Python module implemented in Rust. #[pymodule] fn autosar_data(py: Python, m: &PyModule) -> PyResult<()> { - let submodule = PyModule::new(py, "specification")?; - submodule.add_class::()?; - submodule.add_class::()?; - submodule.add_class::()?; - submodule.add_class::()?; - submodule.add_class::()?; - m.add_submodule(submodule)?; + m.add_class::()?; + m.add_class::()?; m.add_class::()?; m.add_class::()?; m.add_class::()?; @@ -367,20 +384,21 @@ fn autosar_data(py: Python, m: &PyModule) -> PyResult<()> { m.add_class::()?; m.add_class::()?; m.add_class::()?; + m.add_class::()?; m.add("AutosarDataError", py.get_type::())?; Ok(()) } -fn extract_character_data(any: PyObject) -> PyResult { +fn extract_character_data(spec: &CharacterDataSpec, any: PyObject) -> PyResult { Python::with_gil(|py| { if let Ok(text) = any.extract::(py) { - Ok(autosar_data_rs::CharacterData::String(text)) + parse_cdata_string(spec, text).map_err(|_| AutosarDataError::new_err( + autosar_data_rs::AutosarDataError::IncorrectContentType.to_string(), + )) } else if let Ok(val) = any.extract::(py) { Ok(autosar_data_rs::CharacterData::UnsignedInteger(val)) } else if let Ok(val) = any.extract::(py) { Ok(autosar_data_rs::CharacterData::Double(val)) - } else if let Ok(enumitem) = any.extract::(py) { - Ok(autosar_data_rs::CharacterData::Enum(enumitem)) } else { Err(AutosarDataError::new_err( autosar_data_rs::AutosarDataError::IncorrectContentType.to_string(), @@ -389,13 +407,70 @@ fn extract_character_data(any: PyObject) -> PyResult Result { + match spec { + CharacterDataSpec::Enum { items } => { + let enumitem = autosar_data_rs::EnumItem::from_str(&text).map_err(|_| ())?; + if items.iter().any(|(spec_item, _)| *spec_item == enumitem) { + Ok(autosar_data_rs::CharacterData::Enum(enumitem)) + } else { + Err(()) + } + } + CharacterDataSpec::Pattern { check_fn, max_length, .. } => { + if text.len() < max_length.unwrap_or(usize::MAX) && check_fn(text.as_bytes()) { + Ok(autosar_data_rs::CharacterData::String(text)) + } else { + Err(()) + } + } + CharacterDataSpec::String { max_length, .. } => { + if text.len() < max_length.unwrap_or(usize::MAX) { + Ok(autosar_data_rs::CharacterData::String(text)) + } else { + Err(()) + } + } + CharacterDataSpec::UnsignedInteger => { + if let Ok(val) = text.parse() { + Ok(autosar_data_rs::CharacterData::UnsignedInteger(val)) + } else { + Err(()) + } + } + CharacterDataSpec::Double => { + if let Ok(val) = text.parse() { + Ok(autosar_data_rs::CharacterData::Double(val)) + } else { + Err(()) + } + } + } +} + fn character_data_to_object(cdata: &autosar_data_rs::CharacterData) -> PyObject { Python::with_gil(|py| match cdata { autosar_data_rs::CharacterData::Enum(enumitem) => { - Py::new(py, *enumitem).unwrap().into_py(py) + PyString::new(py, enumitem.to_str()).into_py(py) } autosar_data_rs::CharacterData::String(s) => PyString::new(py, s).into_py(py), autosar_data_rs::CharacterData::UnsignedInteger(val) => val.to_object(py), autosar_data_rs::CharacterData::Double(val) => val.to_object(py), }) } + +fn get_element_name(name_str: String) -> PyResult { + autosar_data_rs::ElementName::from_str(&name_str).or_else(|_| { + PyResult::Err(AutosarDataError::new_err(format!( + "Cannot convert \"{name_str}\" to ElementName" + ))) + }) +} + +fn get_attribute_name(name_str: String) -> PyResult { + autosar_data_rs::AttributeName::from_str(&name_str).or_else(|_| { + PyResult::Err(AutosarDataError::new_err(format!( + "Cannot convert \"{name_str}\" to AttributeName" + ))) + }) +} diff --git a/src/model.rs b/src/model.rs index 1f9630b..d228c2f 100644 --- a/src/model.rs +++ b/src/model.rs @@ -41,9 +41,9 @@ impl AutosarModel { fn create_file( &self, filename: &str, - version: autosar_data_rs::AutosarVersion, + version: AutosarVersion, ) -> PyResult { - match self.0.create_file(filename, version) { + match self.0.create_file(filename, version.into()) { Ok(file) => Ok(ArxmlFile(file)), Err(error) => PyResult::Err(AutosarDataError::new_err(error.to_string())), } diff --git a/src/version.rs b/src/version.rs new file mode 100644 index 0000000..4f63592 --- /dev/null +++ b/src/version.rs @@ -0,0 +1,98 @@ +use crate::*; +use std::str::FromStr; + +#[allow(non_camel_case_types)] +#[pyclass(frozen)] +#[derive(Debug, Clone, Copy)] +pub(crate) enum AutosarVersion { + Autosar_4_0_1, + Autosar_4_0_2, + Autosar_4_0_3, + Autosar_4_1_1, + Autosar_4_1_2, + Autosar_4_1_3, + Autosar_4_2_1, + Autosar_4_2_2, + Autosar_4_3_0, + Autosar_00042, + Autosar_00043, + Autosar_00044, + Autosar_00045, + Autosar_00046, + Autosar_00047, + Autosar_00048, + Autosar_00049, + Autosar_00050, + Autosar_00051, +} + +#[pymethods] +impl AutosarVersion { + #[new] + fn new(input: String) -> PyResult { + let spec_ver = + autosar_data_specification::AutosarVersion::from_str(&input).or_else(|_| { + PyResult::Err(AutosarDataError::new_err(format!( + "Cannot convert \"{input}\" to AutosarVersion" + ))) + })?; + Ok(spec_ver.into()) + } + + fn __str__(&self) -> String { + let ver: autosar_data_specification::AutosarVersion = (*self).into(); + ver.to_string() + } +} + +impl Into for AutosarVersion { + fn into(self) -> autosar_data_specification::AutosarVersion { + match self { + Self::Autosar_4_0_1 => autosar_data_specification::AutosarVersion::Autosar_4_0_1, + Self::Autosar_4_0_2 => autosar_data_specification::AutosarVersion::Autosar_4_0_2, + Self::Autosar_4_0_3 => autosar_data_specification::AutosarVersion::Autosar_4_0_3, + Self::Autosar_4_1_1 => autosar_data_specification::AutosarVersion::Autosar_4_1_1, + Self::Autosar_4_1_2 => autosar_data_specification::AutosarVersion::Autosar_4_1_2, + Self::Autosar_4_1_3 => autosar_data_specification::AutosarVersion::Autosar_4_1_3, + Self::Autosar_4_2_1 => autosar_data_specification::AutosarVersion::Autosar_4_2_1, + Self::Autosar_4_2_2 => autosar_data_specification::AutosarVersion::Autosar_4_2_2, + Self::Autosar_4_3_0 => autosar_data_specification::AutosarVersion::Autosar_4_3_0, + Self::Autosar_00042 => autosar_data_specification::AutosarVersion::Autosar_00042, + Self::Autosar_00043 => autosar_data_specification::AutosarVersion::Autosar_00043, + Self::Autosar_00044 => autosar_data_specification::AutosarVersion::Autosar_00044, + Self::Autosar_00045 => autosar_data_specification::AutosarVersion::Autosar_00045, + Self::Autosar_00046 => autosar_data_specification::AutosarVersion::Autosar_00046, + Self::Autosar_00047 => autosar_data_specification::AutosarVersion::Autosar_00047, + Self::Autosar_00048 => autosar_data_specification::AutosarVersion::Autosar_00048, + Self::Autosar_00049 => autosar_data_specification::AutosarVersion::Autosar_00049, + Self::Autosar_00050 => autosar_data_specification::AutosarVersion::Autosar_00050, + Self::Autosar_00051 => autosar_data_specification::AutosarVersion::Autosar_00051, + } + } +} + +impl From for AutosarVersion { + fn from(value: autosar_data_specification::AutosarVersion) -> Self { + match value { + autosar_data_specification::AutosarVersion::Autosar_4_0_1 => Self::Autosar_4_0_1, + autosar_data_specification::AutosarVersion::Autosar_4_0_2 => Self::Autosar_4_0_2, + autosar_data_specification::AutosarVersion::Autosar_4_0_3 => Self::Autosar_4_0_3, + autosar_data_specification::AutosarVersion::Autosar_4_1_1 => Self::Autosar_4_1_1, + autosar_data_specification::AutosarVersion::Autosar_4_1_2 => Self::Autosar_4_1_2, + autosar_data_specification::AutosarVersion::Autosar_4_1_3 => Self::Autosar_4_1_3, + autosar_data_specification::AutosarVersion::Autosar_4_2_1 => Self::Autosar_4_2_1, + autosar_data_specification::AutosarVersion::Autosar_4_2_2 => Self::Autosar_4_2_2, + autosar_data_specification::AutosarVersion::Autosar_4_3_0 => Self::Autosar_4_3_0, + autosar_data_specification::AutosarVersion::Autosar_00042 => Self::Autosar_00042, + autosar_data_specification::AutosarVersion::Autosar_00043 => Self::Autosar_00043, + autosar_data_specification::AutosarVersion::Autosar_00044 => Self::Autosar_00044, + autosar_data_specification::AutosarVersion::Autosar_00045 => Self::Autosar_00045, + autosar_data_specification::AutosarVersion::Autosar_00046 => Self::Autosar_00046, + autosar_data_specification::AutosarVersion::Autosar_00047 => Self::Autosar_00047, + autosar_data_specification::AutosarVersion::Autosar_00048 => Self::Autosar_00048, + autosar_data_specification::AutosarVersion::Autosar_00049 => Self::Autosar_00049, + autosar_data_specification::AutosarVersion::Autosar_00050 => Self::Autosar_00050, + autosar_data_specification::AutosarVersion::Autosar_00051 => Self::Autosar_00051, + } + } +} diff --git a/test/arxmlfile_test.py b/test/arxmlfile_test.py index 32ff990..2927284 100644 --- a/test/arxmlfile_test.py +++ b/test/arxmlfile_test.py @@ -4,8 +4,8 @@ def test_arxlfile_basic(): model = AutosarModel() - file1 = model.create_file("filename1.arxml", specification.AutosarVersion.Autosar_00051) - file2 = model.create_file("filename2.arxml", specification.AutosarVersion.Autosar_00051) + file1 = model.create_file("filename1.arxml", AutosarVersion.Autosar_00051) + file2 = model.create_file("filename2.arxml", AutosarVersion.Autosar_00051) assert isinstance(file1, ArxmlFile) assert isinstance(file2, ArxmlFile) @@ -27,9 +27,9 @@ def test_arxlfile_basic(): file1.filename = file2.filename # each file has a version - assert file1.version == specification.AutosarVersion.Autosar_00051 - file1.version = specification.AutosarVersion.Autosar_4_3_0 - assert file1.version == specification.AutosarVersion.Autosar_4_3_0 + assert file1.version == AutosarVersion.Autosar_00051 + file1.version = AutosarVersion.Autosar_4_3_0 + assert file1.version == AutosarVersion.Autosar_4_3_0 # ArxmlFile has __str__ and __repr__ arxmlfile_repr = file1.__repr__() @@ -59,22 +59,22 @@ def test_arxlfile_basic(): def test_check_version_compatibility(): model = AutosarModel() - file1 = model.create_file("filename", specification.AutosarVersion.Autosar_00050) + file1 = model.create_file("filename", AutosarVersion.Autosar_00050) el_elements = model.root_element \ - .create_sub_element(specification.ElementName.ArPackages) \ - .create_named_sub_element(specification.ElementName.ArPackage, "Pkg") \ - .create_sub_element(specification.ElementName.Elements) - el_acl_object_set = el_elements.create_named_sub_element(specification.ElementName.AclObjectSet, "AclObjectSet") - el_short_name = el_acl_object_set.get_sub_element(specification.ElementName.ShortName) - el_short_name.set_attribute(specification.AttributeName.BlueprintValue, "xyz") + .create_sub_element("AR-PACKAGES") \ + .create_named_sub_element("AR-PACKAGE", "Pkg") \ + .create_sub_element("ELEMENTS") + el_acl_object_set = el_elements.create_named_sub_element("ACL-OBJECT-SET", "AclObjectSet") + el_short_name = el_acl_object_set.get_sub_element("SHORT-NAME") + el_short_name.set_attribute("BLUEPRINT-VALUE", "xyz") el_blueprint_ref = el_acl_object_set \ - .create_sub_element(specification.ElementName.DerivedFromBlueprintRefs) \ - .create_sub_element(specification.ElementName.DerivedFromBlueprintRef) - el_blueprint_ref.set_attribute(specification.AttributeName.Dest, specification.EnumItem.AbstractImplementationDataType) + .create_sub_element("DERIVED-FROM-BLUEPRINT-REFS") \ + .create_sub_element("DERIVED-FROM-BLUEPRINT-REF") + el_blueprint_ref.set_attribute("DEST", "ABSTRACT-IMPLEMENTATION-DATA-TYPE") el_adaptive_sw_component_type = el_elements \ - .create_named_sub_element(specification.ElementName.AdaptiveApplicationSwComponentType, "AdaptiveApplicationSwComponentType") + .create_named_sub_element("ADAPTIVE-APPLICATION-SW-COMPONENT-TYPE", "AdaptiveApplicationSwComponentType") - compat_problems = file1.check_version_compatibility(specification.AutosarVersion.Autosar_4_3_0) + compat_problems = file1.check_version_compatibility(AutosarVersion.Autosar_4_3_0) assert len(compat_problems) == 3 assert isinstance(compat_problems[0], IncompatibleAttributeError) assert isinstance(compat_problems[1], IncompatibleAttributeValueError) @@ -82,7 +82,7 @@ def test_check_version_compatibility(): # IncompatibleAttributeError assert compat_problems[0].element == el_short_name - assert compat_problems[0].attribute == specification.AttributeName.BlueprintValue + assert compat_problems[0].attribute == "BLUEPRINT-VALUE" error_str = compat_problems[0].__str__() error_repr = compat_problems[0].__repr__() assert not error_str is None @@ -90,7 +90,7 @@ def test_check_version_compatibility(): # IncompatibleAttributeValueError assert compat_problems[1].element == el_blueprint_ref - assert compat_problems[1].attribute == specification.AttributeName.Dest + assert compat_problems[1].attribute == "DEST" assert compat_problems[1].attribute_value == "ABSTRACT-IMPLEMENTATION-DATA-TYPE" # todo - type conversion of AttributeValue to string? error_str = compat_problems[1].__str__() error_repr = compat_problems[1].__repr__() diff --git a/test/element_test.py b/test/element_test.py index 7f25735..97ade72 100644 --- a/test/element_test.py +++ b/test/element_test.py @@ -5,8 +5,8 @@ def test_element_basic(): model = AutosarModel() # create some elements - el_ar_packages = model.root_element.create_sub_element(specification.ElementName.ArPackages) - el_ar_package = el_ar_packages.create_named_sub_element(specification.ElementName.ArPackage, "Pkg1") + el_ar_packages = model.root_element.create_sub_element("AR-PACKAGES") + el_ar_package = el_ar_packages.create_named_sub_element("AR-PACKAGE", "Pkg1") assert isinstance(el_ar_packages, Element) assert isinstance(el_ar_package, Element) @@ -20,7 +20,7 @@ def test_element_basic(): # identifiable elements always have a ShortName sub element assert el_ar_package.is_identifiable - el_short_name = el_ar_package.get_sub_element(specification.ElementName.ShortName) + el_short_name = el_ar_package.get_sub_element("SHORT-NAME") assert isinstance(el_short_name, Element) # properties of named elements @@ -65,7 +65,7 @@ def test_element_basic(): assert el_ar_packages != el_ar_package # Elements can be sorted - el_ar_packages.create_named_sub_element(specification.ElementName.ArPackage, "AAA") + el_ar_packages.create_named_sub_element("AR-PACKAGE", "AAA") sub_elements = [e for e in el_ar_packages.sub_elements] assert sub_elements[0].item_name == "NewName" assert sub_elements[1].item_name == "AAA" @@ -83,12 +83,12 @@ def test_element_content(): model = AutosarModel() # create some elements for the test - el_ar_packages = model.root_element.create_sub_element(specification.ElementName.ArPackages) - el_pkg1 = el_ar_packages.create_named_sub_element(specification.ElementName.ArPackage, "Pkg1") - el_short_name = el_pkg1.get_sub_element(specification.ElementName.ShortName) + el_ar_packages = model.root_element.create_sub_element("AR-PACKAGES") + el_pkg1 = el_ar_packages.create_named_sub_element("AR-PACKAGE", "Pkg1") + el_short_name = el_pkg1.get_sub_element("SHORT-NAME") el_l2 = el_pkg1 \ - .create_sub_element(specification.ElementName.Desc) \ - .create_sub_element(specification.ElementName.L2) + .create_sub_element("DESC") \ + .create_sub_element("L-2") # different elements have different content types assert el_pkg1.content_type == ContentType.Elements @@ -98,7 +98,7 @@ def test_element_content(): # create some items for the content of el_l2 el_l2.insert_character_content_item("text", 0) assert len([c for c in el_l2.content]) == 1 - el_l2.create_sub_element(specification.ElementName.Br) + el_l2.create_sub_element("BR") assert len([c for c in el_l2.content]) == 2 # check the content @@ -115,18 +115,18 @@ def test_element_content(): el_short_name.insert_character_content_item("text", 0) el_elements = el_ar_packages \ - .create_named_sub_element(specification.ElementName.ArPackage, "SysPkg") \ - .create_sub_element(specification.ElementName.Elements) + .create_named_sub_element("AR-PACKAGE", "SysPkg") \ + .create_sub_element("ELEMENTS") el_fibex_element_ref = el_elements \ - .create_named_sub_element(specification.ElementName.System, "System") \ - .create_sub_element(specification.ElementName.FibexElements) \ - .create_sub_element(specification.ElementName.FibexElementRefConditional) \ - .create_sub_element(specification.ElementName.FibexElementRef) + .create_named_sub_element("SYSTEM", "System") \ + .create_sub_element("FIBEX-ELEMENTS") \ + .create_sub_element("FIBEX-ELEMENT-REF-CONDITIONAL") \ + .create_sub_element("FIBEX-ELEMENT-REF") el_can_cluster = model.root_element \ - .get_sub_element(specification.ElementName.ArPackages) \ - .create_named_sub_element(specification.ElementName.ArPackage, "CanPkg") \ - .create_sub_element(specification.ElementName.Elements) \ - .create_named_sub_element(specification.ElementName.CanCluster, "CanCluster") + .get_sub_element("AR-PACKAGES") \ + .create_named_sub_element("AR-PACKAGE", "CanPkg") \ + .create_sub_element("ELEMENTS") \ + .create_named_sub_element("CAN-CLUSTER", "CanCluster") # various character data elements have constraints, e.g the reference element can only contain an autosar path # integers, or strings that do not look like paths cause an exception @@ -153,28 +153,28 @@ def test_element_creation(): model = AutosarModel() # create an unnamed element - el_ar_packages = model.root_element.create_sub_element(specification.ElementName.ArPackages) + el_ar_packages = model.root_element.create_sub_element("AR-PACKAGES") # create a named element - el_pkg1 = el_ar_packages.create_named_sub_element(specification.ElementName.ArPackage, "Pkg1") + el_pkg1 = el_ar_packages.create_named_sub_element("AR-PACKAGE", "Pkg1") # create an element at a given position # not every position is allowed with pytest.raises(AutosarDataError): - el_elements = el_pkg1.create_sub_element_at(specification.ElementName.Elements, 0) + el_elements = el_pkg1.create_sub_element_at("ELEMENTS", 0) # position 1 (after ShortName) is allowed assert len([e for e in el_pkg1.sub_elements]) == 1 - el_elements = el_pkg1.create_sub_element_at(specification.ElementName.Elements, 1) + el_elements = el_pkg1.create_sub_element_at("ELEMENTS", 1) # create a named sub element at a given position - el_elements.create_named_sub_element_at(specification.ElementName.System, "System", 0) + el_elements.create_named_sub_element_at("SYSTEM", "System", 0) # create an element by copying another element and all of its sub elements el_pkg2 = el_ar_packages.create_copied_sub_element(el_pkg1) # because the item_name must be unique among the siblings, the element might be renamed while copying assert el_pkg2.item_name == "Pkg1_1" el_pkg2.item_name = "Pkg2" - copied_system = el_pkg2.get_sub_element(specification.ElementName.Elements).get_sub_element(specification.ElementName.System) + copied_system = el_pkg2.get_sub_element("ELEMENTS").get_sub_element("SYSTEM") assert copied_system.item_name == "System" assert copied_system.path == "/Pkg2/System" @@ -187,7 +187,7 @@ def test_element_creation(): with pytest.raises(AutosarDataError): el_pkg1.move_element_here(el_pkg2) # valid: ArPackage inside Arpackages - el_pkg1.create_sub_element(specification.ElementName.ArPackages).move_element_here(el_pkg2) + el_pkg1.create_sub_element("AR-PACKAGES").move_element_here(el_pkg2) assert el_pkg2.path == "/Pkg1/Pkg2" assert copied_system.path == "/Pkg1/Pkg2/System" @@ -204,16 +204,16 @@ def test_element_creation(): # in all cases only valid sub elements can be created # el_pkg1 already has a ShortName, and only one of these is allowed with pytest.raises(AutosarDataError): - el_pkg1.create_sub_element(specification.ElementName.ShortName) + el_pkg1.create_sub_element("SHORT-NAME") # the element Autosar is not a valid sub element of ArPackage with pytest.raises(AutosarDataError): - el_pkg1.create_sub_element_at(specification.ElementName.Autosar, 1) + el_pkg1.create_sub_element_at("AUTOSAR", 1) # it is possible to check which sub elements would be valid # returns a list of tuples: (ElementName, is_named, currently_allowed) - allowed_elements = [ename if allowed else None for (ename, identifiable, allowed) in el_pkg1.list_valid_sub_elements()] - assert not specification.ElementName.Autosar in allowed_elements - assert specification.ElementName.Category in allowed_elements + allowed_elements = [vsi.element_name if vsi.is_allowed else None for vsi in el_pkg1.list_valid_sub_elements()] + assert not "AUTOSAR" in allowed_elements + assert "CATEGORY" in allowed_elements # remove an element el_ar_packages.remove_sub_element(el_pkg3) @@ -225,23 +225,23 @@ def test_element_creation(): # validate the resulting model element_info = [x for x in model.root_element.elements_dfs] # element info is a list of tuple(depth, element) - assert element_info[0][1].element_name == specification.ElementName.Autosar - assert element_info[1][1].element_name == specification.ElementName.ArPackages - assert element_info[2][1].element_name == specification.ElementName.ArPackage + assert element_info[0][1].element_name == "AUTOSAR" + assert element_info[1][1].element_name == "AR-PACKAGES" + assert element_info[2][1].element_name == "AR-PACKAGE" assert element_info[2][1].item_name == "Pkg1" - assert element_info[3][1].element_name == specification.ElementName.ShortName - assert element_info[4][1].element_name == specification.ElementName.Elements - assert element_info[5][1].element_name == specification.ElementName.System + assert element_info[3][1].element_name == "SHORT-NAME" + assert element_info[4][1].element_name == "ELEMENTS" + assert element_info[5][1].element_name == "SYSTEM" assert element_info[5][1].item_name == "System" - assert element_info[6][1].element_name == specification.ElementName.ShortName - assert element_info[7][1].element_name == specification.ElementName.ArPackages - assert element_info[8][1].element_name == specification.ElementName.ArPackage + assert element_info[6][1].element_name == "SHORT-NAME" + assert element_info[7][1].element_name == "AR-PACKAGES" + assert element_info[8][1].element_name == "AR-PACKAGE" assert element_info[8][1].item_name == "Pkg2" - assert element_info[9][1].element_name == specification.ElementName.ShortName - assert element_info[10][1].element_name == specification.ElementName.Elements - assert element_info[11][1].element_name == specification.ElementName.System + assert element_info[9][1].element_name == "SHORT-NAME" + assert element_info[10][1].element_name == "ELEMENTS" + assert element_info[11][1].element_name == "SYSTEM" assert element_info[11][1].item_name == "System" - assert element_info[12][1].element_name == specification.ElementName.ShortName + assert element_info[12][1].element_name == "SHORT-NAME" assert len(element_info) == 13 def test_element_attributes(): @@ -250,10 +250,10 @@ def test_element_attributes(): attributes = [attr for attr in el_autosar.attributes] assert isinstance(attributes[0], Attribute) - assert attributes[0].attrname == specification.AttributeName.xsiSchemalocation - assert attributes[1].attrname == specification.AttributeName.xmlns + assert attributes[0].attrname == "xsi:schemaLocation" + assert attributes[1].attrname == "xmlns" assert attributes[1].content == "http://autosar.org/schema/r4.0" - assert attributes[2].attrname == specification.AttributeName.xmlnsXsi + assert attributes[2].attrname == "xmlns:xsi" assert attributes[2].content == "http://www.w3.org/2001/XMLSchema-instance" assert len(attributes) == 3 @@ -261,27 +261,27 @@ def test_element_attributes(): assert not attributes[0].__repr__() is None assert not attributes[0].__str__() is None - el_autosar.set_attribute(specification.AttributeName.S, "some text") + el_autosar.set_attribute("S", "some text") # attribute values are checked - attribute T must contain a valid timestamp with pytest.raises(AutosarDataError): - el_autosar.set_attribute(specification.AttributeName.T, "some text") + el_autosar.set_attribute("T", "some text") # the function set_attribute_string automatically converts an input string to enum or integer if the attribute requires this - el_autosar.set_attribute_string(specification.AttributeName.T, "2023-04-05T12:34:56Z") - assert el_autosar.attribute_value(specification.AttributeName.T) == "2023-04-05T12:34:56Z" + el_autosar.set_attribute_string("T", "2023-04-05T12:34:56Z") + assert el_autosar.attribute_value("T") == "2023-04-05T12:34:56Z" assert len([attr for attr in el_autosar.attributes]) == 5 - el_autosar.remove_attribute(specification.AttributeName.T) + el_autosar.remove_attribute("T") assert len([attr for attr in el_autosar.attributes]) == 4 def test_file_membership(): model = AutosarModel() - file1 = model.create_file("file1", specification.AutosarVersion.Autosar_00050) - file2 = model.create_file("file2", specification.AutosarVersion.Autosar_00050) - el_ar_packages = model.root_element.create_sub_element(specification.ElementName.ArPackages) - el_pkg1 = el_ar_packages.create_named_sub_element(specification.ElementName.ArPackage, "Pkg1") - el_pkg1.create_sub_element(specification.ElementName.Elements) - el_pkg2 = el_ar_packages.create_named_sub_element(specification.ElementName.ArPackage, "Pkg2") + file1 = model.create_file("file1", AutosarVersion.Autosar_00050) + file2 = model.create_file("file2", AutosarVersion.Autosar_00050) + el_ar_packages = model.root_element.create_sub_element("AR-PACKAGES") + el_pkg1 = el_ar_packages.create_named_sub_element("AR-PACKAGE", "Pkg1") + el_pkg1.create_sub_element("ELEMENTS") + el_pkg2 = el_ar_packages.create_named_sub_element("AR-PACKAGE", "Pkg2") total_element_count = len([e for e in model.elements_dfs]) # initially all elements are part of every file diff --git a/test/model_test.py b/test/model_test.py index b761abd..7b4b707 100644 --- a/test/model_test.py +++ b/test/model_test.py @@ -7,7 +7,7 @@ def test_model_basic(): # check that the object was created - model is not None assert isinstance(model, AutosarModel) assert isinstance(model.root_element, Element) - assert model.root_element.element_name == specification.ElementName.Autosar + assert model.root_element.element_name == "AUTOSAR" assert len(model.files) == 0 assert len(model.identifiable_elements) == 0 @@ -17,19 +17,19 @@ def test_model_files(tmp_path): # create a file filename1 = os.path.join(tmp_path, "test.arxml") - file1 = model.create_file(filename1, specification.AutosarVersion.Autosar_00051) + file1 = model.create_file(filename1, AutosarVersion.Autosar_00051) assert isinstance(file1, ArxmlFile) assert file1.filename == os.path.join(tmp_path, "test.arxml") # create another file - file2 = model.create_file("test2.arxml", specification.AutosarVersion.Autosar_00051) + file2 = model.create_file("test2.arxml", AutosarVersion.Autosar_00051) assert isinstance(file2, ArxmlFile) assert len(model.files) == 2 # create a file with the same name as file1 with pytest.raises(AutosarDataError): # a file called "$tmp_path/test.arxml" already exists in the model - file3 = model.create_file(filename1, specification.AutosarVersion.Autosar_00051) + file3 = model.create_file(filename1, AutosarVersion.Autosar_00051) # remove file2 from the model again model.remove_file(file2) @@ -72,19 +72,19 @@ def test_model_identifiables(): model = AutosarModel() # create some elements el_elements = model.root_element \ - .create_sub_element(specification.ElementName.ArPackages) \ - .create_named_sub_element(specification.ElementName.ArPackage, "Pkg") \ - .create_sub_element(specification.ElementName.Elements) + .create_sub_element("AR-PACKAGES") \ + .create_named_sub_element("AR-PACKAGE", "Pkg") \ + .create_sub_element("ELEMENTS") el_fibex_element_ref = el_elements \ - .create_named_sub_element(specification.ElementName.System, "System") \ - .create_sub_element(specification.ElementName.FibexElements) \ - .create_sub_element(specification.ElementName.FibexElementRefConditional) \ - .create_sub_element(specification.ElementName.FibexElementRef) + .create_named_sub_element("SYSTEM", "System") \ + .create_sub_element("FIBEX-ELEMENTS") \ + .create_sub_element("FIBEX-ELEMENT-REF-CONDITIONAL") \ + .create_sub_element("FIBEX-ELEMENT-REF") el_can_cluster = model.root_element \ - .get_sub_element(specification.ElementName.ArPackages) \ - .create_named_sub_element(specification.ElementName.ArPackage, "Pkg2") \ - .create_sub_element(specification.ElementName.Elements) \ - .create_named_sub_element(specification.ElementName.CanCluster, "CanCluster") + .get_sub_element("AR-PACKAGES") \ + .create_named_sub_element("AR-PACKAGE", "Pkg2") \ + .create_sub_element("ELEMENTS") \ + .create_named_sub_element("CAN-CLUSTER", "CanCluster") assert isinstance(el_elements, Element) assert isinstance(el_fibex_element_ref, Element) assert isinstance(el_can_cluster, Element) @@ -127,29 +127,29 @@ def test_model_misc(): # dfs iterator test: create some elements el_elements = model.root_element \ - .create_sub_element(specification.ElementName.ArPackages) \ - .create_named_sub_element(specification.ElementName.ArPackage, "Pkg1") \ - .create_sub_element(specification.ElementName.Elements) + .create_sub_element("AR-PACKAGES") \ + .create_named_sub_element("AR-PACKAGE", "Pkg1") \ + .create_sub_element("ELEMENTS") elements = [{"depth":depth, "element":element} for (depth, element) in model.elements_dfs] assert len(elements) == 5 assert elements[0]['depth'] == 0 - assert elements[0]['element'].element_name == specification.ElementName.Autosar + assert elements[0]['element'].element_name == "AUTOSAR" assert elements[0]['element'] == model.root_element assert elements[1]['depth'] == 1 - assert elements[1]['element'].element_name == specification.ElementName.ArPackages + assert elements[1]['element'].element_name == "AR-PACKAGES" assert elements[2]['depth'] == 2 - assert elements[2]['element'].element_name == specification.ElementName.ArPackage + assert elements[2]['element'].element_name == "AR-PACKAGE" assert elements[3]['depth'] == 3 - assert elements[3]['element'].element_name == specification.ElementName.ShortName + assert elements[3]['element'].element_name == "SHORT-NAME" assert elements[4]['depth'] == 3 - assert elements[4]['element'].element_name == specification.ElementName.Elements + assert elements[4]['element'].element_name == "ELEMENTS" # create a ref element for check_references() el_fibex_element_ref = el_elements \ - .create_named_sub_element(specification.ElementName.System, "System") \ - .create_sub_element(specification.ElementName.FibexElements) \ - .create_sub_element(specification.ElementName.FibexElementRefConditional) \ - .create_sub_element(specification.ElementName.FibexElementRef) + .create_named_sub_element("SYSTEM", "System") \ + .create_sub_element("FIBEX-ELEMENTS") \ + .create_sub_element("FIBEX-ELEMENT-REF-CONDITIONAL") \ + .create_sub_element("FIBEX-ELEMENT-REF") # set the referecne to a nonexistent path el_fibex_element_ref.character_data = "/Pkg" broken_refs = model.check_references() @@ -157,8 +157,8 @@ def test_model_misc(): assert broken_refs[0] == el_fibex_element_ref # create a second ArPackage "pkg2" and put it in front of the existing "Pkg1" - el_ar_packages = model.root_element.get_sub_element(specification.ElementName.ArPackages) - el_pkg2 = el_ar_packages.create_named_sub_element_at(specification.ElementName.ArPackage, "Pkg2", 0) + el_ar_packages = model.root_element.get_sub_element("AR-PACKAGES") + el_pkg2 = el_ar_packages.create_named_sub_element_at("AR-PACKAGE", "Pkg2", 0) el_pkg1 = model.get_element_by_path("/Pkg1") # verify the initial order subelements = [elem for elem in el_ar_packages.sub_elements] diff --git a/test/test.py b/test/test.py index ee1d311..269940a 100644 --- a/test/test.py +++ b/test/test.py @@ -11,9 +11,9 @@ def test_others(): assert not ct_repr is None # ElementType - assert model.root_element.element_type.splittable_in(specification.AutosarVersion.Autosar_00042) == False - ar_pkg_type = model.root_element.element_type.find_sub_element(specification.ElementName.ArPackages, 1) - assert ar_pkg_type.splittable_in(specification.AutosarVersion.Autosar_00042) == True + assert model.root_element.element_type.splittable_in(AutosarVersion.Autosar_00042) == False + ar_pkg_type = model.root_element.element_type.find_sub_element("AR-PACKAGES", 1) + assert ar_pkg_type.splittable_in(AutosarVersion.Autosar_00042) == True et_str = ar_pkg_type.__str__() et_repr = ar_pkg_type.__repr__() assert not et_str is None