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
//! [`fix_line_indentation`](super::super::IndentationHandler::fix_line_indentation)

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

use super::test_utils::build_indentation_handler;

#[rstest]
#[case::tab_to_spaces_default(
    Some(IndentStyle::Spaces),
    None,
    None,
    "\tDefault tab width of ecformat is 4",
    "    Default tab width of ecformat is 4"
)]
#[case::spaces_to_tab_default(
    Some(IndentStyle::Tabs),
    None,
    None,
    "    Default tab width of ecformat is 4",
    "\tDefault tab width of ecformat is 4"
)]
#[case::tab_to_spaces(
    Some(IndentStyle::Spaces),
    None,
    Some(TabWidth::Value(2)),
    "\tUse configured tab width of 2",
    "  Use configured tab width of 2"
)]
#[case::spaces_to_tab(
    Some(IndentStyle::Tabs),
    None,
    Some(TabWidth::Value(2)),
    "  Use configured tab width of 2",
    "\tUse configured tab width of 2"
)]
#[case::tabs_to_spaces(
    Some(IndentStyle::Spaces),
    None,
    Some(TabWidth::Value(2)),
    "\t\tUse configured tab width of 2",
    "    Use configured tab width of 2"
)]
#[case::spaces_to_tabs(
    Some(IndentStyle::Tabs),
    None,
    Some(TabWidth::Value(2)),
    "    Use configured tab width of 2",
    "\t\tUse configured tab width of 2"
)]
#[case::no_style_almost_one_level(
    None,
    Some(IndentSize::Value(2)),
    None,
    " one spaces is not one level",
    "  one spaces is not one level"
)]
#[case::no_style_almost_two_levels_spaces(
    None,
    Some(IndentSize::Value(2)),
    Some(TabWidth::Value(6)),
    "   two levels are 4 spaces and not 3",
    "    two levels are 4 spaces and not 3"
)]
#[case::no_style_almost_two_levels_tabs(
    None,
    Some(IndentSize::Value(6)),
    Some(TabWidth::Value(3)),
    "\t\t\ttwo levels are 4 tabs and not 3",
    "\t\t\t\ttwo levels are 4 tabs and not 3"
)]
#[case::almost_one_level(
    Some(IndentStyle::Spaces),
    Some(IndentSize::Value(4)),
    None,
    "   3 spaces are not enough",
    "    3 spaces are not enough"
)]
#[case::one_tab_is_not_enough(
    Some(IndentStyle::Tabs),
    Some(IndentSize::Value(3)),
    Some(TabWidth::Value(2)),
    "\tOne tab of width 2 is not an indentation of 3",
    "\t One tab of width 2 is not an indentation of 3"
)]
#[case::almost_three_levels(
    Some(IndentStyle::Spaces),
    Some(IndentSize::Value(2)),
    None,
    "     5 spaces is not divisible by 2",
    "      5 spaces is not divisible by 2"
)]
#[case::use_multi_tabs(
    Some(IndentStyle::Tabs),
    Some(IndentSize::Value(20)),
    Some(TabWidth::Value(10)),
    "\tOne tab of wiidth 10 is not an indentation of 20",
    "\t\tOne tab of wiidth 10 is not an indentation of 20"
)]
#[case::use_multi_tabs_and_spaces(
    Some(IndentStyle::Tabs),
    Some(IndentSize::Value(42)),
    Some(TabWidth::Value(10)),
    "\t \t2 tabs of wiidth 10 and 1 space is only the half answer",
    "\t\t\t\t  2 tabs of wiidth 10 and 1 space is only the half answer"
)]
#[case::tab_for_muli_levels(
    Some(IndentStyle::Tabs),
    Some(IndentSize::Value(3)),
    Some(TabWidth::Value(9)),
    "       7 spaces is not enough for 3 levels",
    "\t7 spaces is not enough for 3 levels"
)]
#[case::tabs_and_spaces_to_spaces(
    Some(IndentStyle::Spaces),
    Some(IndentSize::Value(5)),
    Some(TabWidth::Value(3)),
    "\t \t \t 3 tabs of width 3 and 3 spaces gives 12 which is not divisible by 5",
    "               3 tabs of width 3 and 3 spaces gives 12 which is not divisible by 5"
)]
#[case::tab_style_without_tabs(
    Some(IndentStyle::Tabs),
    Some(IndentSize::Value(2)),
    Some(TabWidth::Value(6)),
    "   two levels are 4 spaces and not 3",
    "    two levels are 4 spaces and not 3"
)]
fn test_fix_line_indentation(
    #[case] indent_style: Option<IndentStyle>,
    #[case] indent_size: Option<IndentSize>,
    #[case] tab_width: Option<TabWidth>,
    #[case] wrong_line: &str,
    #[case] fixed_line: &str,
) {
    let handler = build_indentation_handler(indent_style, indent_size, tab_width);

    assert_eq!(
        handler.fix_line_indentation(wrong_line),
        fixed_line,
        "Line not fixed as expected"
    );
}