Skip to content

Commit

Permalink
Use from_str in test code and docs/README
Browse files Browse the repository at this point in the history
  • Loading branch information
bluk committed Nov 11, 2023
1 parent 6ef4d51 commit 88d6078
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 70 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,15 @@ let lexer = Lexer::from_str(input);
let mut iter = lexer.into_iter().map(|token| token.ty());

let token_type = iter.next();
assert_eq!(token_type, Some(Ty::StartTag(StartTag::from("<id>"))));
assert_eq!(token_type, Some(Ty::StartTag(StartTag::from_str("<id>"))));
match token_type {
Some(Ty::StartTag(start_tag)) => {
assert_eq!(start_tag.name().to_str()?, "id");
assert_eq!(start_tag.name().as_str(), "id");
}
_ => panic!("unexpected token"),
}
assert_eq!(iter.next(), Some(Ty::Characters(Characters::from("Example"))));
assert_eq!(iter.next(), Some(Ty::EndTag(EndTag::from("</id>"))));
assert_eq!(iter.next(), Some(Ty::Characters(Characters::from_str("Example"))));
assert_eq!(iter.next(), Some(Ty::EndTag(EndTag::from_str("</id>"))));
assert_eq!(iter.next(), None);
# Ok::<(), core::str::Utf8Error>(())
```
Expand All @@ -89,13 +89,13 @@ let lexer = unsafe { Lexer::from_slice_unchecked(&buf) };
let mut pos = 0;

let ty = lexer.tokenize(&mut pos).map(|token| token.ty());
assert_eq!(Some(Ty::StartTag(StartTag::from("<id>"))), ty);
assert_eq!(Some(Ty::StartTag(StartTag::from_str("<id>"))), ty);

// Position was assigned to the index after the end of the token
assert_eq!(4, pos);

let ty = lexer.tokenize(&mut pos).map(|token| token.ty());
assert_eq!(Some(Ty::Characters(Characters::from("123"))), ty);
assert_eq!(Some(Ty::Characters(Characters::from_str("123"))), ty);

// Position was assigned to the index after the end of the token
assert_eq!(7, pos);
Expand Down
12 changes: 6 additions & 6 deletions maybe_xml/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,15 @@ let lexer = Lexer::from_str(input);
let mut iter = lexer.into_iter().map(|token| token.ty());

let token_type = iter.next();
assert_eq!(token_type, Some(Ty::StartTag(StartTag::from("<id>"))));
assert_eq!(token_type, Some(Ty::StartTag(StartTag::from_str("<id>"))));
match token_type {
Some(Ty::StartTag(start_tag)) => {
assert_eq!(start_tag.name().to_str()?, "id");
assert_eq!(start_tag.name().as_str(), "id");
}
_ => panic!("unexpected token"),
}
assert_eq!(iter.next(), Some(Ty::Characters(Characters::from("Example"))));
assert_eq!(iter.next(), Some(Ty::EndTag(EndTag::from("</id>"))));
assert_eq!(iter.next(), Some(Ty::Characters(Characters::from_str("Example"))));
assert_eq!(iter.next(), Some(Ty::EndTag(EndTag::from_str("</id>"))));
assert_eq!(iter.next(), None);
# Ok::<(), core::str::Utf8Error>(())
```
Expand All @@ -89,13 +89,13 @@ let lexer = unsafe { Lexer::from_slice_unchecked(&buf) };
let mut pos = 0;

let ty = lexer.tokenize(&mut pos).map(|token| token.ty());
assert_eq!(Some(Ty::StartTag(StartTag::from("<id>"))), ty);
assert_eq!(Some(Ty::StartTag(StartTag::from_str("<id>"))), ty);

// Position was assigned to the index after the end of the token
assert_eq!(4, pos);

let ty = lexer.tokenize(&mut pos).map(|token| token.ty());
assert_eq!(Some(Ty::Characters(Characters::from("123"))), ty);
assert_eq!(Some(Ty::Characters(Characters::from_str("123"))), ty);

// Position was assigned to the index after the end of the token
assert_eq!(7, pos);
Expand Down
14 changes: 7 additions & 7 deletions maybe_xml/src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ use scanner::scan;
/// let mut pos = 0;
///
/// let ty = lexer.tokenize(&mut pos).map(|token| token.ty());
/// assert_eq!(Some(Ty::StartTag(StartTag::from("<id>"))), ty);
/// assert_eq!(Some(Ty::StartTag(StartTag::from_str("<id>"))), ty);
///
/// // Position was assigned to the index after the end of the token
/// assert_eq!(4, pos);
///
/// let ty = lexer.tokenize(&mut pos).map(|token| token.ty());
/// assert_eq!(Some(Ty::Characters(Characters::from("123"))), ty);
/// assert_eq!(Some(Ty::Characters(Characters::from_str("123"))), ty);
///
/// // Position was assigned to the index after the end of the token
/// assert_eq!(7, pos);
Expand All @@ -81,7 +81,7 @@ use scanner::scan;
/// let lexer = unsafe { Lexer::from_slice_unchecked(&buf) };
///
/// let ty = lexer.tokenize(&mut pos).map(|token| token.ty());
/// assert_eq!(Some(Ty::EndTag(EndTag::from("</id>"))), ty);
/// assert_eq!(Some(Ty::EndTag(EndTag::from_str("</id>"))), ty);
///
/// // Position was assigned to the index after the end of the token
/// assert_eq!(5, pos);
Expand Down Expand Up @@ -151,7 +151,7 @@ impl<'a> Lexer<'a> {
/// let mut pos = 0;
///
/// let ty = lexer.tokenize(&mut pos).map(|token| token.ty());
/// assert_eq!(Some(Ty::StartTag(StartTag::from("<id>"))), ty);
/// assert_eq!(Some(Ty::StartTag(StartTag::from_str("<id>"))), ty);
///
/// // Position was assigned to the index after the end of the token
/// assert_eq!(4, pos);
Expand Down Expand Up @@ -212,12 +212,12 @@ impl<'a> Lexer<'a> {
/// let mut iter = lexer.iter(pos);
///
/// let token = iter.next();
/// assert_eq!(Some(Ty::StartTag(StartTag::from("<id>"))), token.map(|t| t.ty()));
/// assert_eq!(Some(Ty::StartTag(StartTag::from_str("<id>"))), token.map(|t| t.ty()));
///
/// let pos = pos + token.map(|t| t.len()).unwrap_or_default();
///
/// let token = iter.next();
/// assert_eq!(Some(Ty::Characters(Characters::from("123"))), token.map(|t| t.ty()));
/// assert_eq!(Some(Ty::Characters(Characters::from_str("123"))), token.map(|t| t.ty()));
///
/// let pos = pos + token.map(|t| t.len()).unwrap_or_default();
///
Expand All @@ -241,7 +241,7 @@ impl<'a> Lexer<'a> {
/// let mut iter = lexer.iter(pos);
///
/// let token = iter.next();
/// assert_eq!(Some(Ty::EndTag(EndTag::from("</id>"))), token.map(|t| t.ty()));
/// assert_eq!(Some(Ty::EndTag(EndTag::from_str("</id>"))), token.map(|t| t.ty()));
///
/// let pos = pos + token.map(|t| t.len()).unwrap_or_default();
///
Expand Down
10 changes: 5 additions & 5 deletions maybe_xml/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@
//! let mut iter = lexer.into_iter().map(|token| token.ty());
//!
//! let token_type = iter.next();
//! assert_eq!(token_type, Some(Ty::StartTag(StartTag::from("<id>"))));
//! assert_eq!(token_type, Some(Ty::StartTag(StartTag::from_str("<id>"))));
//! match token_type {
//! Some(Ty::StartTag(start_tag)) => {
//! assert_eq!(start_tag.name().as_str(), "id");
//! }
//! _ => panic!("unexpected token"),
//! }
//! assert_eq!(iter.next(), Some(Ty::Characters(Characters::from("Example"))));
//! assert_eq!(iter.next(), Some(Ty::EndTag(EndTag::from("</id>"))));
//! assert_eq!(iter.next(), Some(Ty::Characters(Characters::from_str("Example"))));
//! assert_eq!(iter.next(), Some(Ty::EndTag(EndTag::from_str("</id>"))));
//! assert_eq!(iter.next(), None);
//! # Ok::<(), core::str::Utf8Error>(())
//! ```
Expand All @@ -56,13 +56,13 @@
//! let mut pos = 0;
//!
//! let ty = lexer.tokenize(&mut pos).map(|token| token.ty());
//! assert_eq!(Some(Ty::StartTag(StartTag::from("<id>"))), ty);
//! assert_eq!(Some(Ty::StartTag(StartTag::from_str("<id>"))), ty);
//!
//! // Position was assigned to the index after the end of the token
//! assert_eq!(4, pos);
//!
//! let ty = lexer.tokenize(&mut pos).map(|token| token.ty());
//! assert_eq!(Some(Ty::Characters(Characters::from("123"))), ty);
//! assert_eq!(Some(Ty::Characters(Characters::from_str("123"))), ty);
//!
//! // Position was assigned to the index after the end of the token
//! assert_eq!(7, pos);
Expand Down
2 changes: 1 addition & 1 deletion maybe_xml/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ mod tests {
let empty_element_tag = EmptyElementTag::from_str("<abc attr=\"1\" id=\"#example\"/>");
assert_eq!(
empty_element_tag.attributes(),
Some(Attributes::from("attr=\"1\" id=\"#example\""))
Some(Attributes::from_str("attr=\"1\" id=\"#example\""))
);
}

Expand Down
90 changes: 45 additions & 45 deletions maybe_xml/tests/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ fn tokenize_simple_1_xml() {
tokenize(
SIMPLE_1_XML,
&[
Ty::ProcessingInstruction(ProcessingInstruction::from(r#"<?xml version="1.0"?>"#)),
Ty::ProcessingInstruction(ProcessingInstruction::from_str(r#"<?xml version="1.0"?>"#)),
#[cfg(not(target_os = "windows"))]
Ty::Characters(Characters::from("\n")),
Ty::Characters(Characters::from_str("\n")),
#[cfg(target_os = "windows")]
Ty::Characters(Characters::from("\r\n")),
Ty::StartTag(StartTag::from("<document>")),
Ty::Characters(Characters::from("Hello world!")),
Ty::EndTag(EndTag::from("</document>")),
Ty::StartTag(StartTag::from_str("<document>")),
Ty::Characters(Characters::from_str("Hello world!")),
Ty::EndTag(EndTag::from_str("</document>")),
],
);
}
Expand All @@ -54,14 +54,14 @@ fn tokenize_iter_simple_1_xml() {
tokenize_via_iterator(
SIMPLE_1_XML,
&[
Ty::ProcessingInstruction(ProcessingInstruction::from(r#"<?xml version="1.0"?>"#)),
Ty::ProcessingInstruction(ProcessingInstruction::from_str(r#"<?xml version="1.0"?>"#)),
#[cfg(not(target_os = "windows"))]
Ty::Characters(Characters::from("\n")),
Ty::Characters(Characters::from_str("\n")),
#[cfg(target_os = "windows")]
Ty::Characters(Characters::from("\r\n")),
Ty::StartTag(StartTag::from("<document>")),
Ty::Characters(Characters::from("Hello world!")),
Ty::EndTag(EndTag::from("</document>")),
Ty::StartTag(StartTag::from_str("<document>")),
Ty::Characters(Characters::from_str("Hello world!")),
Ty::EndTag(EndTag::from_str("</document>")),
],
);
}
Expand All @@ -72,31 +72,31 @@ fn tokenize_svg_1_xml() {
SVG_1_XML,
#[cfg(not(target_os = "windows"))]
&[
Ty::ProcessingInstruction(ProcessingInstruction::from(
Ty::ProcessingInstruction(ProcessingInstruction::from_str(
r#"<?xml version="1.0"?>"#,
)),
Ty::Characters(Characters::from("\n")),
Ty::Declaration(Declaration::from("<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">")),
Ty::Characters(Characters::from("\n\n")),
Ty::StartTag(StartTag::from("<svg xmlns=\"http://www.w3.org/2000/svg\"\n width=\"800\" height=\"800\">")),
Ty::Characters(Characters::from("\n ")),
Ty::EmptyElementTag(EmptyElementTag::from("<circle cx=\"400\" cy=\"400\" r=\"50\" stroke=\"blue\"\n stroke-width=\"1\" fill=\"yellow\" />")),
Ty::Characters(Characters::from("\n")),
Ty::EndTag(EndTag::from("</svg>")),
Ty::Characters(Characters::from_str("\n")),
Ty::Declaration(Declaration::from_str("<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">")),
Ty::Characters(Characters::from_str("\n\n")),
Ty::StartTag(StartTag::from_str("<svg xmlns=\"http://www.w3.org/2000/svg\"\n width=\"800\" height=\"800\">")),
Ty::Characters(Characters::from_str("\n ")),
Ty::EmptyElementTag(EmptyElementTag::from_str("<circle cx=\"400\" cy=\"400\" r=\"50\" stroke=\"blue\"\n stroke-width=\"1\" fill=\"yellow\" />")),
Ty::Characters(Characters::from_str("\n")),
Ty::EndTag(EndTag::from_str("</svg>")),
],
#[cfg(target_os = "windows")]
&[
Ty::ProcessingInstruction(ProcessingInstruction::from(
r#"<?xml version="1.0"?>"#,
)),
Ty::Characters(Characters::from("\r\n")),
Ty::Declaration(Declaration::from("<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\r\n \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">")),
Ty::Characters(Characters::from("\r\n\r\n")),
Ty::StartTag(StartTag::from("<svg xmlns=\"http://www.w3.org/2000/svg\"\r\n width=\"800\" height=\"800\">")),
Ty::Characters(Characters::from("\r\n ")),
Ty::EmptyElementTag(EmptyElementTag::from("<circle cx=\"400\" cy=\"400\" r=\"50\" stroke=\"blue\"\r\n stroke-width=\"1\" fill=\"yellow\" />")),
Ty::Characters(Characters::from("\r\n")),
Ty::EndTag(EndTag::from("</svg>")),
Ty::Characters(Characters::from_str("\r\n")),
Ty::Declaration(Declaration::from_str("<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\r\n \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">")),
Ty::Characters(Characters::from_str("\r\n\r\n")),
Ty::StartTag(StartTag::from_str("<svg xmlns=\"http://www.w3.org/2000/svg\"\r\n width=\"800\" height=\"800\">")),
Ty::Characters(Characters::from_str("\r\n ")),
Ty::EmptyElementTag(EmptyElementTag::from_str("<circle cx=\"400\" cy=\"400\" r=\"50\" stroke=\"blue\"\r\n stroke-width=\"1\" fill=\"yellow\" />")),
Ty::Characters(Characters::from_str("\r\n")),
Ty::EndTag(EndTag::from_str("</svg>")),
],
);
}
Expand All @@ -107,31 +107,31 @@ fn tokenize_iter_svg_1_xml() {
SVG_1_XML,
#[cfg(not(target_os = "windows"))]
&[
Ty::ProcessingInstruction(ProcessingInstruction::from(
Ty::ProcessingInstruction(ProcessingInstruction::from_str(
r#"<?xml version="1.0"?>"#,
)),
Ty::Characters(Characters::from("\n")),
Ty::Declaration(Declaration::from("<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">")),
Ty::Characters(Characters::from("\n\n")),
Ty::StartTag(StartTag::from("<svg xmlns=\"http://www.w3.org/2000/svg\"\n width=\"800\" height=\"800\">")),
Ty::Characters(Characters::from("\n ")),
Ty::EmptyElementTag(EmptyElementTag::from("<circle cx=\"400\" cy=\"400\" r=\"50\" stroke=\"blue\"\n stroke-width=\"1\" fill=\"yellow\" />")),
Ty::Characters(Characters::from("\n")),
Ty::EndTag(EndTag::from("</svg>")),
Ty::Characters(Characters::from_str("\n")),
Ty::Declaration(Declaration::from_str("<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">")),
Ty::Characters(Characters::from_str("\n\n")),
Ty::StartTag(StartTag::from_str("<svg xmlns=\"http://www.w3.org/2000/svg\"\n width=\"800\" height=\"800\">")),
Ty::Characters(Characters::from_str("\n ")),
Ty::EmptyElementTag(EmptyElementTag::from_str("<circle cx=\"400\" cy=\"400\" r=\"50\" stroke=\"blue\"\n stroke-width=\"1\" fill=\"yellow\" />")),
Ty::Characters(Characters::from_str("\n")),
Ty::EndTag(EndTag::from_str("</svg>")),
],
#[cfg(target_os = "windows")]
&[
Ty::ProcessingInstruction(ProcessingInstruction::from(
Ty::ProcessingInstruction(ProcessingInstruction::from_str(
r#"<?xml version="1.0"?>"#,
)),
Ty::Characters(Characters::from("\r\n")),
Ty::Declaration(Declaration::from("<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\r\n \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">")),
Ty::Characters(Characters::from("\r\n\r\n")),
Ty::StartTag(StartTag::from("<svg xmlns=\"http://www.w3.org/2000/svg\"\r\n width=\"800\" height=\"800\">")),
Ty::Characters(Characters::from("\r\n ")),
Ty::EmptyElementTag(EmptyElementTag::from("<circle cx=\"400\" cy=\"400\" r=\"50\" stroke=\"blue\"\r\n stroke-width=\"1\" fill=\"yellow\" />")),
Ty::Characters(Characters::from("\r\n")),
Ty::EndTag(EndTag::from("</svg>")),
Ty::Characters(Characters::from_str("\r\n")),
Ty::Declaration(Declaration::from_str("<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\r\n \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">")),
Ty::Characters(Characters::from_str("\r\n\r\n")),
Ty::StartTag(StartTag::from_str("<svg xmlns=\"http://www.w3.org/2000/svg\"\r\n width=\"800\" height=\"800\">")),
Ty::Characters(Characters::from_str("\r\n ")),
Ty::EmptyElementTag(EmptyElementTag::from_str("<circle cx=\"400\" cy=\"400\" r=\"50\" stroke=\"blue\"\r\n stroke-width=\"1\" fill=\"yellow\" />")),
Ty::Characters(Characters::from_str("\r\n")),
Ty::EndTag(EndTag::from_str("</svg>")),
],
);
}

0 comments on commit 88d6078

Please sign in to comment.