Add tests for skip magic trailing comma

<!--
Thank you for contributing to Ruff! To help us out with reviewing, please consider the following:

- Does this pull request include a summary of the change? (See below.)
- Does this pull request include a descriptive title?
- Does this pull request include references to any relevant issues?
-->

## Summary

This PR adds tests that verify that the magic trailing comma is not respected if disabled in the formatter options. 

Our test setup now allows to create a `<fixture-name>.options.json` file that contains an array of configurations that should be tested. 

<!-- What's the purpose of the change? What does it do, and why? -->

## Test Plan

It's all about tests :) 

<!-- How was it tested? -->
This commit is contained in:
Micha Reiser 2023-06-26 14:15:55 +02:00 committed by GitHub
parent dd0d1afb66
commit f18a1f70de
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
27 changed files with 268 additions and 79 deletions

View file

@ -220,7 +220,7 @@ impl<'fmt, 'ast, 'buf> JoinCommaSeparatedBuilder<'fmt, 'ast, 'buf> {
if let Some(last_end) = self.last_end.take() {
if_group_breaks(&text(",")).fmt(self.fmt)?;
if self.fmt.options().magic_trailing_comma().is_preserve()
if self.fmt.options().magic_trailing_comma().is_respect()
&& matches!(
first_non_trivia_token(last_end, self.fmt.context().contents()),
Some(Token {

View file

@ -2,6 +2,11 @@ use ruff_formatter::printer::{LineEnding, PrinterOptions};
use ruff_formatter::{FormatOptions, IndentStyle, LineWidth};
#[derive(Clone, Debug)]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
serde(default)
)]
pub struct PyFormatOptions {
/// Specifies the indent style:
/// * Either a tab
@ -9,6 +14,7 @@ pub struct PyFormatOptions {
indent_style: IndentStyle,
/// The preferred line width at which the formatter should wrap lines.
#[cfg_attr(feature = "serde", serde(default = "default_line_width"))]
line_width: LineWidth,
/// The preferred quote style to use (single vs double quotes).
@ -18,6 +24,10 @@ pub struct PyFormatOptions {
magic_trailing_comma: MagicTrailingComma,
}
fn default_line_width() -> LineWidth {
LineWidth::try_from(88).unwrap()
}
impl PyFormatOptions {
pub fn magic_trailing_comma(&self) -> MagicTrailingComma {
self.magic_trailing_comma
@ -36,6 +46,16 @@ impl PyFormatOptions {
self.magic_trailing_comma = trailing_comma;
self
}
pub fn with_indent_style(&mut self, indent_style: IndentStyle) -> &mut Self {
self.indent_style = indent_style;
self
}
pub fn with_line_width(&mut self, line_width: LineWidth) -> &mut Self {
self.line_width = line_width;
self
}
}
impl FormatOptions for PyFormatOptions {
@ -69,6 +89,11 @@ impl Default for PyFormatOptions {
}
#[derive(Copy, Clone, Debug, Default, Eq, PartialEq)]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
serde(rename_all = "kebab-case")
)]
pub enum QuoteStyle {
Single,
#[default]
@ -105,14 +130,19 @@ impl TryFrom<char> for QuoteStyle {
}
#[derive(Copy, Clone, Debug, Default)]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
serde(rename_all = "kebab-case")
)]
pub enum MagicTrailingComma {
#[default]
Preserve,
Skip,
Respect,
Ignore,
}
impl MagicTrailingComma {
pub const fn is_preserve(self) -> bool {
matches!(self, Self::Preserve)
pub const fn is_respect(self) -> bool {
matches!(self, Self::Respect)
}
}