Mike Gerwitz

Activist for User Freedom

aboutsummaryrefslogtreecommitdiffstats
path: root/tamer/src
diff options
context:
space:
mode:
authorMike Gerwitz <mike.gerwitz@ryansg.com>2020-02-13 11:50:18 -0500
committerMike Gerwitz <mike.gerwitz@ryansg.com>2020-02-26 10:49:01 -0500
commitb89408e5bbc9804e5e80aee0ebd53b747375eb3d (patch)
tree20ba3b8135f796f889f2a3cfe9b22c10df5b198b /tamer/src
parent19a6d67dc44214f905d0fb3aab09dd4c23eb4ae9 (diff)
downloadtame-b89408e5bbc9804e5e80aee0ebd53b747375eb3d.tar.gz
tame-b89408e5bbc9804e5e80aee0ebd53b747375eb3d.tar.bz2
tame-b89408e5bbc9804e5e80aee0ebd53b747375eb3d.zip
TAMER: Extract quick_xml event-related mocks
Diffstat (limited to 'tamer/src')
-rw-r--r--tamer/src/lib.rs3
-rw-r--r--tamer/src/obj/xmlo/reader.rs113
-rw-r--r--tamer/src/test/mod.rs18
-rw-r--r--tamer/src/test/quick_xml/mod.rs125
4 files changed, 149 insertions, 110 deletions
diff --git a/tamer/src/lib.rs b/tamer/src/lib.rs
index 5b6a107..f69be3a 100644
--- a/tamer/src/lib.rs
+++ b/tamer/src/lib.rs
@@ -22,3 +22,6 @@ pub mod ir;
pub mod ld;
pub mod obj;
pub mod sym;
+
+#[cfg(test)]
+pub mod test;
diff --git a/tamer/src/obj/xmlo/reader.rs b/tamer/src/obj/xmlo/reader.rs
index dfc8d34..3f33ddd 100644
--- a/tamer/src/obj/xmlo/reader.rs
+++ b/tamer/src/obj/xmlo/reader.rs
@@ -132,9 +132,9 @@
use crate::ir::legacyir::{PackageAttrs, SymAttrs, SymType};
use crate::sym::{Interner, Symbol};
#[cfg(test)]
-use mock::MockBytesStart as BytesStart;
+use crate::test::quick_xml::MockBytesStart as BytesStart;
#[cfg(test)]
-use mock::MockXmlEvent as XmlEvent;
+use crate::test::quick_xml::MockXmlEvent as XmlEvent;
#[cfg(test)]
use mock::MockXmlReader as XmlReader;
#[cfg(not(test))]
@@ -826,8 +826,6 @@ impl std::error::Error for XmloError {
mod mock {
use super::*;
use quick_xml::Result as XmlResult;
- use std::borrow::Cow;
- use std::cell::Cell;
pub struct MockXmlReader<B: BufRead> {
_reader: B,
@@ -908,119 +906,14 @@ mod mock {
Ok(())
}
}
-
- pub enum MockXmlEvent<'a> {
- Start(MockBytesStart<'a>),
- End(MockBytesEnd<'a>),
- Empty(MockBytesStart<'a>),
- #[allow(dead_code)]
- Text(MockBytesText<'a>),
- }
-
- pub struct MockBytesStart<'a> {
- name: &'a [u8],
- attrs: Cell<Option<MockAttributes<'a>>>,
- }
-
- impl<'a> MockBytesStart<'a> {
- pub fn new(name: &'a [u8], attrs: Option<MockAttributes<'a>>) -> Self {
- Self {
- name,
- attrs: Cell::new(attrs),
- }
- }
-
- pub fn name(&self) -> &[u8] {
- self.name
- }
-
- pub fn attributes(&self) -> MockAttributes {
- self.attrs.take().expect("missing mock attributes")
- }
- }
-
- pub struct MockBytesEnd<'a> {
- name: Cow<'a, [u8]>,
- }
-
- impl<'a> MockBytesEnd<'a> {
- pub fn new(name: &'a [u8]) -> Self {
- Self {
- name: Cow::Borrowed(name),
- }
- }
-
- pub fn name(&self) -> &[u8] {
- &*self.name
- }
- }
-
- pub struct MockBytesText<'a> {
- #[allow(dead_code)]
- content: Cow<'a, [u8]>,
- }
-
- impl<'a> MockBytesText<'a> {
- pub fn new(content: &'a [u8]) -> Self {
- Self {
- content: Cow::Borrowed(content),
- }
- }
- }
-
- pub struct MockAttributes<'a> {
- attrs: Vec<MockAttribute<'a>>,
- with_checks: Option<bool>,
- }
-
- impl<'a> MockAttributes<'a> {
- pub fn new(attrs: Vec<MockAttribute<'a>>) -> Self {
- Self {
- attrs,
- with_checks: None,
- }
- }
-
- pub fn with_checks(&mut self, val: bool) -> &mut Self {
- self.with_checks = Some(val);
- self
- }
- }
-
- impl<'a> Iterator for MockAttributes<'a> {
- type Item = XmlResult<MockAttribute<'a>>;
-
- fn next(&mut self) -> Option<Self::Item> {
- // We read output from Saxon, which will always be valid
- if self.with_checks != Some(false) {
- panic!("MockAttributes expected with_checks false")
- }
-
- self.attrs.pop().map(|attr| Ok(attr))
- }
- }
-
- pub struct MockAttribute<'a> {
- pub key: &'a [u8],
- pub value: Cow<'a, [u8]>,
- }
-
- impl<'a> MockAttribute<'a> {
- pub fn new(key: &'a [u8], value: &'a [u8]) -> Self {
- Self {
- key,
- value: Cow::Borrowed(&value[..]),
- }
- }
- }
}
#[cfg(test)]
mod test {
- use super::mock::*;
use super::*;
use crate::ir::legacyir::{SymDtype, SymType};
use crate::sym::DefaultInterner;
+ use crate::test::quick_xml::*;
type Sut<'i, B, I> = XmloReader<'i, B, I>;
diff --git a/tamer/src/test/mod.rs b/tamer/src/test/mod.rs
new file mode 100644
index 0000000..9b3b846
--- /dev/null
+++ b/tamer/src/test/mod.rs
@@ -0,0 +1,18 @@
+// Mocks, stubs, and other stuff for testing
+//
+// Copyright (C) 2014-2019 Ryan Specialty Group, LLC.
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+pub mod quick_xml;
diff --git a/tamer/src/test/quick_xml/mod.rs b/tamer/src/test/quick_xml/mod.rs
new file mode 100644
index 0000000..a9b6291
--- /dev/null
+++ b/tamer/src/test/quick_xml/mod.rs
@@ -0,0 +1,125 @@
+// quick_xml mocks
+//
+// Copyright (C) 2014-2019 Ryan Specialty Group, LLC.
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+use quick_xml::Result as XmlResult;
+use std::borrow::Cow;
+use std::cell::Cell;
+
+pub enum MockXmlEvent<'a> {
+ Start(MockBytesStart<'a>),
+ End(MockBytesEnd<'a>),
+ Empty(MockBytesStart<'a>),
+ #[allow(dead_code)]
+ Text(MockBytesText<'a>),
+}
+
+pub struct MockBytesStart<'a> {
+ name: &'a [u8],
+ attrs: Cell<Option<MockAttributes<'a>>>,
+}
+
+impl<'a> MockBytesStart<'a> {
+ pub fn new(name: &'a [u8], attrs: Option<MockAttributes<'a>>) -> Self {
+ Self {
+ name,
+ attrs: Cell::new(attrs),
+ }
+ }
+
+ pub fn name(&self) -> &[u8] {
+ self.name
+ }
+
+ pub fn attributes(&self) -> MockAttributes {
+ self.attrs.take().expect("missing mock attributes")
+ }
+}
+
+pub struct MockBytesEnd<'a> {
+ name: Cow<'a, [u8]>,
+}
+
+impl<'a> MockBytesEnd<'a> {
+ pub fn new(name: &'a [u8]) -> Self {
+ Self {
+ name: Cow::Borrowed(name),
+ }
+ }
+
+ pub fn name(&self) -> &[u8] {
+ &*self.name
+ }
+}
+
+pub struct MockBytesText<'a> {
+ #[allow(dead_code)]
+ content: Cow<'a, [u8]>,
+}
+
+impl<'a> MockBytesText<'a> {
+ pub fn new(content: &'a [u8]) -> Self {
+ Self {
+ content: Cow::Borrowed(content),
+ }
+ }
+}
+
+pub struct MockAttributes<'a> {
+ attrs: Vec<MockAttribute<'a>>,
+ with_checks: Option<bool>,
+}
+
+impl<'a> MockAttributes<'a> {
+ pub fn new(attrs: Vec<MockAttribute<'a>>) -> Self {
+ Self {
+ attrs,
+ with_checks: None,
+ }
+ }
+
+ pub fn with_checks(&mut self, val: bool) -> &mut Self {
+ self.with_checks = Some(val);
+ self
+ }
+}
+
+impl<'a> Iterator for MockAttributes<'a> {
+ type Item = XmlResult<MockAttribute<'a>>;
+
+ fn next(&mut self) -> Option<Self::Item> {
+ // We read output from Saxon, which will always be valid
+ if self.with_checks != Some(false) {
+ panic!("MockAttributes expected with_checks false")
+ }
+
+ self.attrs.pop().map(|attr| Ok(attr))
+ }
+}
+
+pub struct MockAttribute<'a> {
+ pub key: &'a [u8],
+ pub value: Cow<'a, [u8]>,
+}
+
+impl<'a> MockAttribute<'a> {
+ pub fn new(key: &'a [u8], value: &'a [u8]) -> Self {
+ Self {
+ key,
+ value: Cow::Borrowed(&value[..]),
+ }
+ }
+}