From 9cc1ad4a90616b7fb4ae7b425a5b9844887f4584 Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Fri, 14 Oct 2022 10:26:37 +0200 Subject: Add some tests for line endings --- src/lib.rs | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 56 insertions(+), 4 deletions(-) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index 3e3407a..870269c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -15,11 +15,13 @@ #![deny(clippy::pedantic)] #![allow(clippy::doc_link_with_quotes)] #![allow(clippy::too_many_lines)] -// Would be nice to use `logo-chromatic`, but that looks horrible on Safari at relatively small sizes currently. 😢 #![doc( html_logo_url = "https://raw.githubusercontent.com/wooorm/markdown-rs/8924580/media/logo-monochromatic.svg?sanitize=true" )] +// ^-- Would be nice to use `logo-chromatic`, but that looks horrible on Safari +// at relatively small sizes currently. 😢 + extern crate alloc; mod construct; @@ -39,16 +41,16 @@ use alloc::{boxed::Box, fmt, string::String}; use mdast::Node; use parser::parse; -#[doc(hidden)] // Do not use: exported for quick prototyping, will be removed. +#[doc(hidden)] pub use util::identifier::{id_cont, id_start}; -#[doc(hidden)] // Do not use: exported for quick prototyping, will be removed. +#[doc(hidden)] pub use util::sanitize_uri::sanitize; -#[doc(hidden)] // Do not use: exported for quick prototyping, will be removed. +#[doc(hidden)] pub use util::location::Location; /// Type of line endings in markdown. @@ -1531,3 +1533,53 @@ pub fn to_mdast(value: &str, options: &ParseOptions) -> Result { let node = to_mdast::compile(&events, parse_state.bytes)?; Ok(node) } + +#[cfg(test)] +mod tests { + extern crate std; + use super::*; + + #[test] + fn test_line_ending() { + assert_eq!( + LineEnding::from_str("\r"), + LineEnding::CarriageReturn, + "should support turning a string into a carriage return" + ); + assert_eq!( + LineEnding::CarriageReturn.as_str(), + "\r", + "should support turning a carriage return into a string" + ); + + assert_eq!( + LineEnding::from_str("\n"), + LineEnding::LineFeed, + "should support turning a string into a line feed" + ); + assert_eq!( + LineEnding::LineFeed.as_str(), + "\n", + "should support turning a line feed into a string" + ); + + assert_eq!( + LineEnding::from_str("\r\n"), + LineEnding::CarriageReturnLineFeed, + "should support turning a string into a carriage return + line feed" + ); + assert_eq!( + LineEnding::CarriageReturnLineFeed.as_str(), + "\r\n", + "should support turning a carriage return + line feed into a string" + ); + } + + #[test] + #[should_panic = "invalid str"] + fn test_line_ending_broken() { + // Hide stack trace. + std::panic::set_hook(Box::new(|_| {})); + LineEnding::from_str("a"); + } +} -- cgit