This repository has been archived on 2021-03-14. You can view files and clone it, but cannot push or open issues/pull-requests.
git-config/src/test_util.rs

76 lines
2.1 KiB
Rust
Raw Normal View History

2021-03-01 23:33:26 +00:00
//! This module is only included for tests, and contains common unit test helper
//! functions.
2021-02-24 00:47:24 +00:00
2021-03-06 06:23:23 +00:00
use crate::parser::{Event, Key, ParsedComment, ParsedSectionHeader};
2021-03-01 23:33:26 +00:00
use std::borrow::Cow;
2021-02-24 00:47:24 +00:00
pub fn section_header_event(
name: &str,
subsection: impl Into<Option<(&'static str, &'static str)>>,
) -> Event<'_> {
Event::SectionHeader(section_header(name, subsection))
}
pub fn section_header(
name: &str,
subsection: impl Into<Option<(&'static str, &'static str)>>,
) -> ParsedSectionHeader<'_> {
2021-02-28 03:18:44 +00:00
let name = name.into();
2021-02-24 00:47:24 +00:00
if let Some((separator, subsection_name)) = subsection.into() {
ParsedSectionHeader {
name,
2021-02-28 03:18:44 +00:00
separator: Some(Cow::Borrowed(separator)),
subsection_name: Some(Cow::Borrowed(subsection_name)),
2021-02-24 00:47:24 +00:00
}
} else {
ParsedSectionHeader {
name,
separator: None,
subsection_name: None,
}
}
}
pub(crate) fn name_event(name: &'static str) -> Event<'static> {
2021-03-06 06:23:23 +00:00
Event::Key(Key(Cow::Borrowed(name)))
2021-02-24 00:47:24 +00:00
}
pub(crate) fn value_event(value: &'static str) -> Event<'static> {
2021-02-28 03:18:44 +00:00
Event::Value(Cow::Borrowed(value.as_bytes()))
2021-02-24 00:47:24 +00:00
}
pub(crate) fn value_not_done_event(value: &'static str) -> Event<'static> {
2021-02-28 03:18:44 +00:00
Event::ValueNotDone(Cow::Borrowed(value.as_bytes()))
2021-02-24 00:47:24 +00:00
}
pub(crate) fn value_done_event(value: &'static str) -> Event<'static> {
2021-02-28 03:18:44 +00:00
Event::ValueDone(Cow::Borrowed(value.as_bytes()))
2021-02-24 00:47:24 +00:00
}
pub(crate) fn newline_event() -> Event<'static> {
newline_custom_event("\n")
}
pub(crate) fn newline_custom_event(value: &'static str) -> Event<'static> {
2021-02-28 03:18:44 +00:00
Event::Newline(Cow::Borrowed(value))
2021-02-24 00:47:24 +00:00
}
pub(crate) fn whitespace_event(value: &'static str) -> Event<'static> {
2021-02-28 03:18:44 +00:00
Event::Whitespace(Cow::Borrowed(value))
2021-02-24 00:47:24 +00:00
}
pub(crate) fn comment_event(tag: char, msg: &'static str) -> Event<'static> {
Event::Comment(comment(tag, msg))
}
pub(crate) fn comment(comment_tag: char, comment: &'static str) -> ParsedComment<'static> {
ParsedComment {
comment_tag,
2021-02-28 03:18:44 +00:00
comment: Cow::Borrowed(comment.as_bytes()),
2021-02-24 00:47:24 +00:00
}
}
2021-02-24 23:00:15 +00:00
pub(crate) fn fully_consumed<T>(t: T) -> (&'static [u8], T) {
(&[], t)
}