-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
213 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
use super::execute_command; | ||
|
||
#[inline] | ||
pub fn format_using_xmllint( | ||
snippet_path: &std::path::Path, | ||
) -> std::io::Result<(bool, Option<String>)> { | ||
let mut cmd = std::process::Command::new("xmllint"); | ||
|
||
cmd.arg("--format") | ||
.arg(snippet_path) | ||
.arg("--output") | ||
.arg(snippet_path); | ||
|
||
execute_command(&mut cmd, snippet_path) | ||
} | ||
|
||
#[cfg(test)] | ||
mod test_isort { | ||
use crate::{formatters::setup_snippet, languages::Language}; | ||
|
||
use super::format_using_xmllint; | ||
|
||
#[test_with::executable(xmllint)] | ||
#[test] | ||
fn it_should_format_xml() { | ||
let input = " | ||
<note> | ||
<to>Tove</to> | ||
<from>Jani</from> | ||
<heading>Reminder</heading> | ||
<body>Don't forget me this weekend!</body> | ||
</note>"; | ||
|
||
let expected_output = r#"<?xml version="1.0"?> | ||
<note> | ||
<to>Tove</to> | ||
<from>Jani</from> | ||
<heading>Reminder</heading> | ||
<body>Don't forget me this weekend!</body> | ||
</note> | ||
"#; | ||
|
||
let snippet = setup_snippet(input, Language::Python.to_file_ext()) | ||
.expect("it to create a snippet file"); | ||
|
||
let output = format_using_xmllint(snippet.path()) | ||
.expect("it to be successful") | ||
.1 | ||
.expect("it to be some"); | ||
|
||
assert_eq!(expected_output, output); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
use schemars::JsonSchema; | ||
|
||
use crate::formatters::{xmllint::format_using_xmllint, MdsfFormatter}; | ||
|
||
use super::{Lang, LanguageFormatter}; | ||
|
||
#[derive(Debug, Default, serde::Serialize, serde::Deserialize, JsonSchema)] | ||
#[cfg_attr(test, derive(PartialEq, Eq))] | ||
pub enum Xml { | ||
#[default] | ||
#[serde(rename = "xmllint")] | ||
Xmllint, | ||
} | ||
|
||
impl Default for Lang<Xml> { | ||
#[inline] | ||
fn default() -> Self { | ||
Self { | ||
enabled: true, | ||
formatter: MdsfFormatter::<Xml>::default(), | ||
} | ||
} | ||
} | ||
|
||
impl Default for MdsfFormatter<Xml> { | ||
#[inline] | ||
fn default() -> Self { | ||
Self::Single(Xml::Xmllint) | ||
} | ||
} | ||
|
||
impl LanguageFormatter for Xml { | ||
#[inline] | ||
fn format_snippet( | ||
&self, | ||
snippet_path: &std::path::Path, | ||
) -> std::io::Result<(bool, Option<String>)> { | ||
match self { | ||
Self::Xmllint => format_using_xmllint(snippet_path), | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test_xml { | ||
use crate::{ | ||
formatters::{setup_snippet, MdsfFormatter}, | ||
languages::Lang, | ||
}; | ||
|
||
use super::Xml; | ||
|
||
const INPUT: &str = " | ||
<note> | ||
<to>Tove</to> | ||
<from>Jani</from> | ||
<heading>Reminder</heading> | ||
<body>Don't forget me this weekend!</body> | ||
</note>"; | ||
|
||
const EXTENSION: &str = crate::languages::Language::Xml.to_file_ext(); | ||
|
||
#[test] | ||
fn it_should_be_enabled_by_default() { | ||
assert!(Lang::<Xml>::default().enabled); | ||
} | ||
|
||
#[test] | ||
fn it_should_not_format_when_enabled_is_false() { | ||
let l = Lang::<Xml> { | ||
enabled: false, | ||
formatter: MdsfFormatter::Single(Xml::Xmllint), | ||
}; | ||
|
||
let snippet = setup_snippet(INPUT, EXTENSION).expect("it to save the file"); | ||
let snippet_path = snippet.path(); | ||
|
||
assert!(l.format(snippet_path).expect("it to not fail").is_none()); | ||
} | ||
|
||
#[test_with::executable(xmllint)] | ||
#[test] | ||
fn test_xmllint() { | ||
let l = Lang::<Xml> { | ||
enabled: true, | ||
formatter: MdsfFormatter::Single(Xml::Xmllint), | ||
}; | ||
|
||
let snippet = setup_snippet(INPUT, EXTENSION).expect("it to save the file"); | ||
let snippet_path = snippet.path(); | ||
|
||
let output = l | ||
.format(snippet_path) | ||
.expect("it to not fail") | ||
.expect("it to be a snippet"); | ||
|
||
let expected_output = r#"<?xml version="1.0"?> | ||
<note> | ||
<to>Tove</to> | ||
<from>Jani</from> | ||
<heading>Reminder</heading> | ||
<body>Don't forget me this weekend!</body> | ||
</note> | ||
"#; | ||
|
||
assert_eq!(output, expected_output); | ||
} | ||
} |