Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

Commit

Permalink
use remote derive from serde
Browse files Browse the repository at this point in the history
  • Loading branch information
ematipico committed Feb 4, 2022
1 parent 82d6fd4 commit 293ebbc
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 45 deletions.
15 changes: 5 additions & 10 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -239,15 +239,10 @@ the cases you need to verify. If we needed to follow the previous example:
3. The content would be something like:
```json
{
"cases": [
{
"line_width": 120,
"indent_style": {"Space": 4}
}
]
"line_width": 120,
"indent_style": {"Space": 4}
}
````
4. the `cases` keyword is mandatory;
5. then each object of the array will contain the matrix of options you'd want to test.
In this case the test suite will run a **second test case** with `line_width` to 120 and `ident_style` with 4 spaces
6. when the test suite is run, you will have two outputs in your snapshot: the default one and the custom one
4. The file will contain the options to pass the formatter.
In this case, the test suite will run a **second test case** with `line_width` to 120 and `ident_style` with 4 spaces
5. when the test suite is run, you will have two outputs in your snapshot: the default one and the custom one
46 changes: 17 additions & 29 deletions crates/rome_formatter/tests/spec_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,47 +8,34 @@ use std::fs;
use std::path::{Path, PathBuf};

#[derive(Debug, Eq, PartialEq, Clone, Copy, Deserialize, Serialize)]
#[serde(remote = "IndentStyle")]
pub enum SerializableIndentStyle {
/// Tab
Tab,
/// Space, with its quantity
Space(u8),
}

impl From<SerializableIndentStyle> for IndentStyle {
fn from(test: SerializableIndentStyle) -> Self {
match test {
SerializableIndentStyle::Tab => IndentStyle::Tab,
SerializableIndentStyle::Space(s) => IndentStyle::Space(s),
}
}
}

#[derive(Debug, Deserialize, Serialize, Clone)]
#[derive(Debug, Deserialize, Serialize, Clone, Copy)]
#[serde(remote = "FormatOptions")]
pub struct SerializableFormatOptions {
/// The indent style
pub indent_style: Option<SerializableIndentStyle>,
#[serde(with = "SerializableIndentStyle")]
pub indent_style: IndentStyle,

/// What's the max width of a line. Defaults to 80
pub line_width: Option<u16>,
pub line_width: u16,
}

impl From<SerializableFormatOptions> for FormatOptions {
fn from(test: SerializableFormatOptions) -> Self {
Self {
indent_style: test
.indent_style
.map_or_else(|| IndentStyle::Tab, |value| value.into()),
line_width: test.line_width.unwrap_or(80),
indent_style: test.indent_style,
line_width: test.line_width,
}
}
}

#[derive(Debug, Deserialize, Serialize)]
pub struct TestOptions {
cases: Vec<SerializableFormatOptions>,
}

#[derive(Debug, Default)]
struct SnapshotContent {
input: String,
Expand Down Expand Up @@ -141,14 +128,15 @@ pub fn run(spec_input_file: &str, _expected_file: &str, test_directory: &str, fi
{
let mut options_path = RomePath::new(options_path.display().to_string().as_str());
// SAFETY: we checked its existence already, we assume we have rights to read it
let options: TestOptions =
serde_json::from_str(options_path.get_buffer_from_file().as_str()).unwrap();

for test_case in options.cases {
let options = test_case.clone();
let formatted_result = format(test_case.into(), &root).unwrap();
snapshot_content.add_output(formatted_result.as_code(), options.into());
}
let input = options_path.get_buffer_from_file();
let mut de = serde_json::Deserializer::from_str(input.as_str());

let options: FormatOptions =
SerializableFormatOptions::deserialize(&mut de).unwrap();

let copy_options = options;
let formatted_result = format(options, &root).unwrap();
snapshot_content.add_output(formatted_result.as_code(), copy_options);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
{
"cases": [
{
"line_width": 120,
"indent_style": {"Space": 4}
}
]
"line_width": 120,
"indent_style": {"Space": 4}
}

0 comments on commit 293ebbc

Please sign in to comment.