-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(formatter): fix formatting literal strings
Signed-off-by: azjezz <[email protected]>
- Loading branch information
Showing
8 changed files
with
112 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 @@ | ||
pub mod string; |
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,68 @@ | ||
use indoc::indoc; | ||
|
||
use mago_formatter::settings::FormatSettings; | ||
use mago_source::error::SourceError; | ||
|
||
use crate::test_format; | ||
|
||
#[test] | ||
pub fn test_single_quote() -> Result<(), SourceError> { | ||
let code = indoc! {r#" | ||
<?php | ||
$a = "Hello, world!"; | ||
$b = 'Hello, world!'; | ||
$c = "Hello, 'world'!"; | ||
$d = 'Hello, "world"!'; | ||
$e = "Hello, \"world\"!"; | ||
$f = 'Hello, \'world\'!'; | ||
$g = "Hello, 'world'!"; | ||
$h = 'Hello, \"world\"!'; | ||
"#}; | ||
|
||
let expected = indoc! {r#" | ||
<?php | ||
$a = 'Hello, world!'; | ||
$b = 'Hello, world!'; | ||
$c = "Hello, 'world'!"; | ||
$d = 'Hello, "world"!'; | ||
$e = "Hello, \"world\"!"; | ||
$f = 'Hello, \'world\'!'; | ||
$g = "Hello, 'world'!"; | ||
$h = 'Hello, \"world\"!'; | ||
"#}; | ||
|
||
test_format(code, expected, FormatSettings { single_quote: true, ..Default::default() }) | ||
} | ||
|
||
#[test] | ||
pub fn test_double_quote() -> Result<(), SourceError> { | ||
let code = indoc! {r#" | ||
<?php | ||
$a = "Hello, world!"; | ||
$b = 'Hello, world!'; | ||
$c = "Hello, 'world'!"; | ||
$d = 'Hello, "world"!'; | ||
$e = "Hello, \"world\"!"; | ||
$f = 'Hello, \'world\'!'; | ||
$g = "Hello, 'world'!"; | ||
$h = 'Hello, \"world\"!'; | ||
"#}; | ||
|
||
let expected = indoc! {r#" | ||
<?php | ||
$a = "Hello, world!"; | ||
$b = "Hello, world!"; | ||
$c = "Hello, 'world'!"; | ||
$d = 'Hello, "world"!'; | ||
$e = "Hello, \"world\"!"; | ||
$f = 'Hello, \'world\'!'; | ||
$g = "Hello, 'world'!"; | ||
$h = 'Hello, \"world\"!'; | ||
"#}; | ||
|
||
test_format(code, expected, FormatSettings { single_quote: false, ..Default::default() }) | ||
} |
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,20 @@ | ||
use mago_formatter::settings::FormatSettings; | ||
use mago_interner::ThreadedInterner; | ||
use mago_parser::parse_source; | ||
use mago_source::error::SourceError; | ||
use mago_source::SourceManager; | ||
|
||
mod format; | ||
|
||
pub fn test_format(code: impl AsRef<str>, expected: &str, settings: FormatSettings) -> Result<(), SourceError> { | ||
let interner = ThreadedInterner::new(); | ||
let mut manager = SourceManager::new(interner.clone()); | ||
let source_id = manager.insert_content("code.php".to_string(), code.as_ref().to_string(), true); | ||
let source = manager.load(&source_id)?; | ||
let (program, _) = parse_source(&interner, &source); | ||
let formatted = mago_formatter::format(settings, &interner, &source, &program); | ||
|
||
pretty_assertions::assert_eq!(expected, formatted, "Formatted code does not match expected"); | ||
|
||
Ok(()) | ||
} |