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/tests/integration_tests/parser_integration_tests.rs

228 lines
5.4 KiB
Rust
Raw Normal View History

2021-03-06 06:23:23 +00:00
use git_config::parser::{parse_from_str, Event, Key, ParsedSectionHeader, SectionHeaderName};
2021-02-21 19:43:41 +00:00
use std::borrow::Cow;
2021-02-24 00:47:24 +00:00
pub fn section_header_event(
2021-02-20 00:18:02 +00:00
name: &str,
subsection: impl Into<Option<(&'static str, &'static str)>>,
) -> Event<'_> {
2021-02-24 00:47:24 +00:00
Event::SectionHeader(section_header(name, subsection))
}
pub fn section_header(
name: &str,
subsection: impl Into<Option<(&'static str, &'static str)>>,
) -> ParsedSectionHeader<'_> {
2021-03-06 06:23:23 +00:00
let name = SectionHeaderName(Cow::Borrowed(name));
2021-02-24 00:47:24 +00:00
if let Some((separator, subsection_name)) = subsection.into() {
ParsedSectionHeader {
name,
2021-03-04 01:43:00 +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,
}
}
2021-02-19 05:12:59 +00:00
}
2021-02-24 00:47:24 +00:00
2021-02-19 05:12:59 +00:00
fn name(name: &'static str) -> Event<'static> {
2021-03-06 06:23:23 +00:00
Event::Key(Key(Cow::Borrowed(name)))
2021-02-19 05:12:59 +00:00
}
fn value(value: &'static str) -> Event<'static> {
2021-02-28 03:18:44 +00:00
Event::Value(Cow::Borrowed(value.as_bytes()))
2021-02-19 05:12:59 +00:00
}
2021-02-20 00:18:02 +00:00
fn newline() -> Event<'static> {
2021-02-24 00:47:24 +00:00
newline_custom("\n")
}
fn newline_custom(value: &'static str) -> Event<'static> {
2021-03-04 01:43:00 +00:00
Event::Newline(Cow::Borrowed(value))
2021-02-20 00:18:02 +00:00
}
fn whitespace(value: &'static str) -> Event<'static> {
2021-03-04 01:43:00 +00:00
Event::Whitespace(Cow::Borrowed(value))
2021-02-20 00:18:02 +00:00
}
2021-02-23 16:30:48 +00:00
fn separator() -> Event<'static> {
Event::KeyValueSeparator
}
2021-02-19 05:12:59 +00:00
#[test]
2021-02-20 00:18:02 +00:00
#[rustfmt::skip]
2021-02-19 05:12:59 +00:00
fn personal_config() {
let config = r#"[user]
email = code@eddie.sh
2021-02-23 16:30:48 +00:00
name = Foo Bar
2021-02-19 05:12:59 +00:00
[core]
autocrlf = input
[push]
default = simple
[commit]
gpgsign = true
[gpg]
program = gpg
[url "ssh://git@github.com/"]
insteadOf = "github://"
[url "ssh://git@git.eddie.sh/edward/"]
insteadOf = "gitea://"
[pull]
ff = only
[init]
defaultBranch = master"#;
assert_eq!(
parse_from_str(config)
.unwrap()
.into_vec(),
2021-02-19 05:12:59 +00:00
vec![
2021-02-24 00:47:24 +00:00
section_header_event("user", None),
2021-02-20 00:18:02 +00:00
newline(),
whitespace(" "),
2021-02-19 05:12:59 +00:00
name("email"),
2021-02-20 00:18:02 +00:00
whitespace(" "),
2021-02-23 16:30:48 +00:00
separator(),
2021-02-20 00:18:02 +00:00
whitespace(" "),
2021-02-19 05:12:59 +00:00
value("code@eddie.sh"),
2021-02-20 00:18:02 +00:00
newline(),
whitespace(" "),
2021-02-19 05:12:59 +00:00
name("name"),
2021-02-20 00:18:02 +00:00
whitespace(" "),
2021-02-23 16:30:48 +00:00
separator(),
2021-02-20 00:18:02 +00:00
whitespace(" "),
2021-02-23 16:30:48 +00:00
value("Foo Bar"),
2021-02-20 00:18:02 +00:00
newline(),
2021-02-24 00:47:24 +00:00
section_header_event("core", None),
2021-02-20 00:18:02 +00:00
newline(),
whitespace(" "),
2021-02-19 05:12:59 +00:00
name("autocrlf"),
2021-02-20 00:18:02 +00:00
whitespace(" "),
2021-02-23 16:30:48 +00:00
separator(),
2021-02-20 00:18:02 +00:00
whitespace(" "),
2021-02-19 05:12:59 +00:00
value("input"),
2021-02-20 00:18:02 +00:00
newline(),
2021-02-24 00:47:24 +00:00
section_header_event("push", None),
2021-02-20 00:18:02 +00:00
newline(),
whitespace(" "),
2021-02-19 05:12:59 +00:00
name("default"),
2021-02-20 00:18:02 +00:00
whitespace(" "),
2021-02-23 16:30:48 +00:00
separator(),
2021-02-20 00:18:02 +00:00
whitespace(" "),
2021-02-19 05:12:59 +00:00
value("simple"),
2021-02-20 00:18:02 +00:00
newline(),
2021-02-24 00:47:24 +00:00
section_header_event("commit", None),
2021-02-20 00:18:02 +00:00
newline(),
whitespace(" "),
2021-02-19 05:12:59 +00:00
name("gpgsign"),
2021-02-20 00:18:02 +00:00
whitespace(" "),
2021-02-23 16:30:48 +00:00
separator(),
2021-02-20 00:18:02 +00:00
whitespace(" "),
2021-02-19 05:12:59 +00:00
value("true"),
2021-02-20 00:18:02 +00:00
newline(),
2021-02-24 00:47:24 +00:00
section_header_event("gpg", None),
2021-02-20 00:18:02 +00:00
newline(),
whitespace(" "),
2021-02-19 05:12:59 +00:00
name("program"),
2021-02-20 00:18:02 +00:00
whitespace(" "),
2021-02-23 16:30:48 +00:00
separator(),
2021-02-20 00:18:02 +00:00
whitespace(" "),
2021-02-19 05:12:59 +00:00
value("gpg"),
2021-02-20 00:18:02 +00:00
newline(),
2021-02-24 00:47:24 +00:00
section_header_event("url", (" ", "ssh://git@github.com/")),
2021-02-20 00:18:02 +00:00
newline(),
whitespace(" "),
2021-02-19 05:12:59 +00:00
name("insteadOf"),
2021-02-20 00:18:02 +00:00
whitespace(" "),
2021-02-23 16:30:48 +00:00
separator(),
2021-02-20 00:18:02 +00:00
whitespace(" "),
2021-02-19 17:08:42 +00:00
value("\"github://\""),
2021-02-20 00:18:02 +00:00
newline(),
2021-02-24 00:47:24 +00:00
section_header_event("url", (" ", "ssh://git@git.eddie.sh/edward/")),
2021-02-20 00:18:02 +00:00
newline(),
whitespace(" "),
2021-02-19 05:12:59 +00:00
name("insteadOf"),
2021-02-20 00:18:02 +00:00
whitespace(" "),
2021-02-23 16:30:48 +00:00
separator(),
2021-02-20 00:18:02 +00:00
whitespace(" "),
2021-02-19 17:08:42 +00:00
value("\"gitea://\""),
2021-02-20 00:18:02 +00:00
newline(),
2021-02-24 00:47:24 +00:00
section_header_event("pull", None),
2021-02-20 00:18:02 +00:00
newline(),
whitespace(" "),
2021-02-19 05:12:59 +00:00
name("ff"),
2021-02-20 00:18:02 +00:00
whitespace(" "),
2021-02-23 16:30:48 +00:00
separator(),
2021-02-20 00:18:02 +00:00
whitespace(" "),
2021-02-19 05:12:59 +00:00
value("only"),
2021-02-20 00:18:02 +00:00
newline(),
2021-02-24 00:47:24 +00:00
section_header_event("init", None),
2021-02-20 00:18:02 +00:00
newline(),
whitespace(" "),
2021-02-19 05:12:59 +00:00
name("defaultBranch"),
2021-02-20 00:18:02 +00:00
whitespace(" "),
2021-02-23 16:30:48 +00:00
separator(),
2021-02-20 00:18:02 +00:00
whitespace(" "),
2021-02-19 05:12:59 +00:00
value("master"),
]
);
}
#[test]
fn parse_empty() {
assert_eq!(parse_from_str("").unwrap().into_vec(), vec![]);
}
2021-02-22 02:16:08 +00:00
#[test]
fn parse_whitespace() {
assert_eq!(
parse_from_str("\n \n \n").unwrap().into_vec(),
vec![
newline(),
whitespace(" "),
newline(),
whitespace(" "),
newline(),
]
)
}
#[test]
fn newline_events_are_merged() {
assert_eq!(
parse_from_str("\n\n\n\n\n").unwrap().into_vec(),
2021-02-24 00:47:24 +00:00
vec![newline_custom("\n\n\n\n\n")]
2021-02-22 02:16:08 +00:00
);
}
2021-02-25 04:19:45 +00:00
#[test]
fn error() {
let input = "[core] a=b\n 4a=3";
println!("{}", parse_from_str(input).unwrap_err());
2021-02-25 16:11:26 +00:00
let input = "[core] a=b\n =3";
println!("{}", parse_from_str(input).unwrap_err());
let input = "[core";
println!("{}", parse_from_str(input).unwrap_err());
2021-02-25 04:19:45 +00:00
}