TAMER: Extract quick_xml event-related mocks

master
Mike Gerwitz 2020-02-13 11:50:18 -05:00
parent 19a6d67dc4
commit b89408e5bb
4 changed files with 149 additions and 110 deletions

View File

@ -22,3 +22,6 @@ pub mod ir;
pub mod ld;
pub mod obj;
pub mod sym;
#[cfg(test)]
pub mod test;

View File

@ -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>;

View File

@ -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;

View File

@ -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[..]),
}
}
}