ecformat 0.2.0

command line tool to keep files correct in respect of your EditorConfig
Documentation
// SPDX-FileCopyrightText: Contributors to ecformat project <https://codeberg.org/BaumiCoder/ecformat>
//
// SPDX-License-Identifier: BlueOak-1.0.0

//! Tests for the function
//! [`check_line_indentation`](super::super::IndentationHandler::check_line_indentation)

use ec4rs::property::{IndentSize, IndentStyle, TabWidth};
use rstest::rstest;

use super::test_utils::build_indentation_handler;

/// Test on lines with wrong indentation (i.e., function should return false).
#[rstest]
#[case::style_no_tabs_allowed(Some(IndentStyle::Spaces), None, None, "\tsome content")]
#[case::no_style_too_narrow_spaces(None, Some(IndentSize::Value(4)), None, "  no style")]
#[case::no_style_too_narrow_tab(
    None,
    Some(IndentSize::Value(4)),
    Some(TabWidth::Value(2)),
    "\tWith a tab width 2 this is not one level"
)]
#[case::no_style_too_wide_tabs(
    None,
    Some(IndentSize::Value(4)),
    Some(TabWidth::Value(3)),
    "\t\t3 + 3 = 6 which is not divisible by 4"
)]
#[case::style_mix_too_wide(
    None,
    Some(IndentSize::Value(2)),
    Some(TabWidth::Value(3)),
    "\t  tab of width 3 + 2 spaces = 5 which is not divisible by 2"
)]
#[case::one_level_too_narrow(Some(IndentStyle::Spaces), Some(IndentSize::Value(3)), None, "  line")]
#[case::one_level_too_wide(Some(IndentStyle::Spaces), Some(IndentSize::Value(2)), None, "   line")]
#[case::spaces_instead_tab(Some(IndentStyle::Tabs), Some(IndentSize::Value(2)), None, "  line")]
#[case::one_tab_too_wide(
    Some(IndentStyle::Tabs),
    Some(IndentSize::Value(3)),
    Some(TabWidth::Value(4)),
    "\tA single tab can be too big"
)]
#[case::additional_space(
    Some(IndentStyle::Tabs),
    Some(IndentSize::UseTabWidth),
    Some(TabWidth::Value(2)),
    "\t line"
)]
#[case::almost_three_levels(
    Some(IndentStyle::Spaces),
    Some(IndentSize::Value(2)),
    None,
    "     5 is not divisible by 2"
)]
fn test_check_line_indentation_wrong(
    #[case] indent_style: Option<IndentStyle>,
    #[case] indent_size: Option<IndentSize>,
    #[case] tab_width: Option<TabWidth>,
    #[case] line: &str,
) {
    let handler = build_indentation_handler(indent_style, indent_size, tab_width);

    assert!(
        !handler.check_line_indentation(line),
        "No wrong indentation detected"
    );
}

/// Test on lines with correct indentation (i.e., function should return true).
#[rstest]
#[case::style_spaces(Some(IndentStyle::Spaces), None, None, " some content")]
#[case::style_one_tab(Some(IndentStyle::Tabs), None, None, "\tsome content")]
#[case::style_tabs_allow_spaces(
    Some(IndentStyle::Tabs),
    None,
    None,
    "  fine as one tab could be too big for one level"
)]
#[case::no_style_spaces(None, Some(IndentSize::Value(2)), None, "  no style")]
#[case::no_style_tab(None, Some(IndentSize::Value(2)), None, "\tno style")]
#[case::no_style_tabs(
    None,
    Some(IndentSize::Value(4)),
    Some(TabWidth::Value(2)),
    "\t\t2 + 2 = 4"
)]
#[case::style_mix(None, Some(IndentSize::Value(2)), None, "\t  mix allowed")]
#[case::one_tab_too_wide(
    Some(IndentStyle::Tabs),
    Some(IndentSize::Value(2)),
    Some(TabWidth::Value(4)),
    "  correct as one tab is too big for one level"
)]
#[case::tab_width_for_indent_size(
    Some(IndentStyle::Spaces),
    Some(IndentSize::UseTabWidth),
    Some(TabWidth::Value(3)),
    "   3 spaces but set via tab_width"
)]
#[case::additional_space(
    Some(IndentStyle::Tabs),
    Some(IndentSize::Value(3)),
    Some(TabWidth::Value(2)),
    "\t tab of size 2 + 1 space = indentation of 3"
)]
#[case::additional_spaces(
    Some(IndentStyle::Tabs),
    Some(IndentSize::Value(5)),
    Some(TabWidth::Value(3)),
    " \t tab of size 3 + 2 space = indentation of 5 (do not care about order)"
)]
#[case::no_indentation_no_problems(
    Some(IndentStyle::Spaces),
    Some(IndentSize::Value(8)),
    None,
    "line without leading spaces and tabs"
)]
fn test_check_line_indentation_correct(
    #[case] indent_style: Option<IndentStyle>,
    #[case] indent_size: Option<IndentSize>,
    #[case] tab_width: Option<TabWidth>,
    #[case] line: &str,
) {
    let handler = build_indentation_handler(indent_style, indent_size, tab_width);

    assert!(
        handler.check_line_indentation(line),
        "No detected as correct indented line"
    );
}