From 140af50536ebc32ae6461852daa2df0fc2d197ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?cel=20=F0=9F=8C=B8?= Date: Sun, 10 Nov 2024 17:57:05 +0000 Subject: [PATCH] implement reader testing --- src/element.rs | 6 ++- src/error.rs | 1 + src/lib.rs | 2 +- src/reader.rs | 117 ++++++++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 122 insertions(+), 4 deletions(-) diff --git a/src/element.rs b/src/element.rs index 0e0b8f1..e93a0f3 100644 --- a/src/element.rs +++ b/src/element.rs @@ -9,19 +9,20 @@ use crate::{ // when are namespaces names chosen then if they are automatically calculated // namespaces are held by readers and writers. -#[derive(PartialEq, Eq, Hash, Clone)] +#[derive(PartialEq, Eq, Hash, Clone, Debug)] pub struct Namespace { pub prefix: Option, pub namespace: String, } // names are qualified, they contain a reference to the namespace (held within the reader/writer) -#[derive(PartialEq, Eq, Hash, Clone)] +#[derive(PartialEq, Eq, Hash, Clone, Debug)] pub struct Name { pub namespace: Namespace, pub name: String, } +#[derive(Debug)] pub enum Content { Element(Element), Text(String), @@ -30,6 +31,7 @@ pub enum Content { } // should this be a trait? +#[derive(Debug)] pub struct Element { pub name: Name, // namespace: Name, diff --git a/src/error.rs b/src/error.rs index 96c709c..1f9c1e6 100644 --- a/src/error.rs +++ b/src/error.rs @@ -2,6 +2,7 @@ use std::{num::ParseIntError, str::Utf8Error}; use crate::element::{Name, Namespace}; +#[derive(Debug)] pub enum Error { ReadError(std::io::Error), Utf8Error(Utf8Error), diff --git a/src/lib.rs b/src/lib.rs index 329c092..e8486c4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,6 @@ mod element; mod error; -mod reader; +pub mod reader; mod writer; pub mod xml; diff --git a/src/reader.rs b/src/reader.rs index b51489f..bca8edd 100644 --- a/src/reader.rs +++ b/src/reader.rs @@ -339,7 +339,7 @@ impl Reader { } } text.map(|text| content.push(Content::Text(text))); - todo!() + Ok(content) } } @@ -422,3 +422,118 @@ impl Reader { // todo!() // } // } + +#[cfg(test)] +mod test { + use tokio::io::AsyncRead; + + use super::Reader; + + struct MockAsyncReader<'s>(&'s str); + + impl<'s> MockAsyncReader<'s> { + fn new(data: &'s str) -> Self { + Self(data) + } + } + + impl<'s> AsyncRead for MockAsyncReader<'s> { + fn poll_read( + self: std::pin::Pin<&mut Self>, + _cx: &mut std::task::Context<'_>, + buf: &mut tokio::io::ReadBuf<'_>, + ) -> std::task::Poll> { + buf.put_slice(self.0.as_bytes()); + std::task::Poll::Ready(Ok(())) + } + } + + #[tokio::test] + async fn test_element_read() { + let mock = MockAsyncReader::new( + " + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + asdf +", + ); + let mut reader = Reader::new(mock); + let element = reader.read_element().await.unwrap(); + println!("{:#?}", element); + } +}