aboutsummaryrefslogtreecommitdiffstats
path: root/src/util/char.rs
diff options
context:
space:
mode:
authorLibravatar Titus Wormer <tituswormer@gmail.com>2022-10-13 20:37:29 +0200
committerLibravatar Titus Wormer <tituswormer@gmail.com>2022-10-13 20:37:29 +0200
commited23a4a3630d0f06f86f8482ecb495ccf0892e62 (patch)
treeaec242e01f475b79d8d20af2f2216bd7881af403 /src/util/char.rs
parent1db7caab017b5814d8a8ac3baa5786af18870869 (diff)
downloadmarkdown-rs-ed23a4a3630d0f06f86f8482ecb495ccf0892e62.tar.gz
markdown-rs-ed23a4a3630d0f06f86f8482ecb495ccf0892e62.tar.bz2
markdown-rs-ed23a4a3630d0f06f86f8482ecb495ccf0892e62.zip
Add some unit tests for internal character functions
Diffstat (limited to '')
-rw-r--r--src/util/char.rs117
1 files changed, 102 insertions, 15 deletions
diff --git a/src/util/char.rs b/src/util/char.rs
index 70367f3..79cd32e 100644
--- a/src/util/char.rs
+++ b/src/util/char.rs
@@ -104,29 +104,21 @@ pub fn classify(char: char) -> Kind {
/// Like [`classify`], but supports eof as whitespace.
pub fn classify_opt(char_opt: Option<char>) -> Kind {
- if let Some(char) = char_opt {
- classify(char)
- }
- // EOF.
- else {
- Kind::Whitespace
- }
+ char_opt.map_or(Kind::Whitespace, classify)
}
/// Format an optional `char` (`none` means eof).
pub fn format_opt(char: Option<char>) -> String {
- match char {
- None => "end of file".into(),
- Some(char) => format!("character {}", format(char)),
- }
+ char.map_or("end of file".into(), |char| {
+ format!("character {}", format(char))
+ })
}
/// Format an optional `byte` (`none` means eof).
pub fn format_byte_opt(byte: Option<u8>) -> String {
- match byte {
- None => "end of file".into(),
- Some(byte) => format!("byte {}", format_byte(byte)),
- }
+ byte.map_or("end of file".into(), |byte| {
+ format!("byte {}", format_byte(byte))
+ })
}
/// Format a `char`.
@@ -160,3 +152,98 @@ pub fn format_byte(byte: u8) -> String {
representation
}
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use alloc::string::ToString;
+
+ #[test]
+ fn test_classify() {
+ assert_eq!(
+ classify(' '),
+ Kind::Whitespace,
+ "should classify whitespace"
+ );
+
+ assert_eq!(
+ classify('.'),
+ Kind::Punctuation,
+ "should classify punctuation"
+ );
+
+ assert_eq!(classify('a'), Kind::Other, "should classify other");
+ }
+
+ #[test]
+ fn test_format_opt() {
+ assert_eq!(
+ format_opt(None),
+ "end of file".to_string(),
+ "should format an optional char: none -> eof"
+ );
+
+ assert_eq!(
+ format_opt(Some('!')),
+ "character `!` (U+0021)".to_string(),
+ "should format an optional char: char -> pretty"
+ );
+ }
+
+ #[test]
+ fn test_format_byte_opt() {
+ assert_eq!(
+ format_byte_opt(None),
+ "end of file".to_string(),
+ "should format an optional byte: none -> eof"
+ );
+
+ assert_eq!(
+ format_byte_opt(Some(b'!')),
+ "byte `!` (U+0021)".to_string(),
+ "should format an optional byte: char -> pretty"
+ );
+ }
+
+ #[test]
+ fn test_format() {
+ assert_eq!(
+ format('`'),
+ "`` ` `` (U+0060)".to_string(),
+ "should format a char: grave accent"
+ );
+
+ assert_eq!(
+ format('!'),
+ "`!` (U+0021)".to_string(),
+ "should format a char: regular"
+ );
+
+ assert_eq!(
+ format(' '),
+ "U+0020".to_string(),
+ "should format a char: unprintable"
+ );
+ }
+
+ #[test]
+ fn test_format_byte() {
+ assert_eq!(
+ format_byte(b'`'),
+ "`` ` `` (U+0060)".to_string(),
+ "should format a byte: grave accent"
+ );
+
+ assert_eq!(
+ format_byte(b'!'),
+ "`!` (U+0021)".to_string(),
+ "should format a byte: regular"
+ );
+
+ assert_eq!(
+ format_byte(b' '),
+ "U+0020".to_string(),
+ "should format a byte: unprintable"
+ );
+ }
+}