Skip to content

Commit 22b93e0

Browse files
Copilotjosecelano
andcommitted
refactor: add parameterized tests and remove duplicates
Co-authored-by: josecelano <[email protected]>
1 parent 47311b6 commit 22b93e0

File tree

1 file changed

+129
-95
lines changed

1 file changed

+129
-95
lines changed

src/presentation/user_output.rs

Lines changed: 129 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1270,73 +1270,145 @@ mod tests {
12701270
}
12711271

12721272
// ============================================================================
1273-
// UserOutput Tests - Basic Output
1273+
// UserOutput Tests - Parameterized Tests
12741274
// ============================================================================
1275+
//
1276+
// These tests use rstest for parameterized testing to reduce duplication
1277+
// and make the test matrix clear and maintainable.
1278+
//
1279+
// Test Matrix:
1280+
// Message Type | Symbol | Min Verbosity | Channel | Always Shown
1281+
// -------------|--------|---------------|---------|-------------
1282+
// progress | ⏳ | Normal | stderr | No
1283+
// success | ✅ | Normal | stderr | No
1284+
// warning | ⚠️ | Normal | stderr | No
1285+
// error | ❌ | Quiet | stderr | Yes
1286+
// result | (none) | Quiet | stdout | Yes
1287+
// data | (none) | Quiet | stdout | Yes
1288+
1289+
mod parameterized_tests {
1290+
use super::*;
1291+
use rstest::rstest;
12751292

1276-
#[test]
1277-
fn it_should_write_progress_messages_to_stderr() {
1278-
let mut test_output = test_support::TestUserOutput::new(VerbosityLevel::Normal);
1279-
1280-
test_output.output.progress("Testing progress message");
1281-
1282-
// Verify message went to stderr
1283-
assert_eq!(test_output.stderr(), "⏳ Testing progress message\n");
1284-
1285-
// Verify stdout is empty
1286-
assert_eq!(test_output.stdout(), "");
1287-
}
1288-
1289-
#[test]
1290-
fn it_should_write_success_messages_to_stderr() {
1291-
let mut test_output = test_support::TestUserOutput::new(VerbosityLevel::Normal);
1292-
1293-
test_output.output.success("Testing success message");
1294-
1295-
// Verify message went to stderr
1296-
assert_eq!(test_output.stderr(), "✅ Testing success message\n");
1297-
1298-
// Verify stdout is empty
1299-
assert_eq!(test_output.stdout(), "");
1300-
}
1301-
1302-
#[test]
1303-
fn it_should_write_warning_messages_to_stderr() {
1304-
let mut test_output = test_support::TestUserOutput::new(VerbosityLevel::Normal);
1305-
1306-
test_output.output.warn("Testing warning message");
1293+
/// Test that each message type routes to the correct output channel
1294+
///
1295+
/// Verifies stdout vs stderr routing for all message types.
1296+
/// This replaces 5 individual channel routing tests with one parameterized test.
1297+
#[rstest]
1298+
#[case("progress", "⏳ Test message\n", VerbosityLevel::Normal, "stderr")]
1299+
#[case("success", "✅ Test message\n", VerbosityLevel::Normal, "stderr")]
1300+
#[case("warning", "⚠️ Test message\n", VerbosityLevel::Normal, "stderr")]
1301+
#[case("error", "❌ Test message\n", VerbosityLevel::Normal, "stderr")]
1302+
#[case("result", "Test message\n", VerbosityLevel::Normal, "stdout")]
1303+
fn it_should_route_message_to_correct_channel(
1304+
#[case] method: &str,
1305+
#[case] expected_output: &str,
1306+
#[case] verbosity: VerbosityLevel,
1307+
#[case] expected_channel: &str,
1308+
) {
1309+
let mut test_output = test_support::TestUserOutput::new(verbosity);
1310+
1311+
// Call the appropriate method
1312+
match method {
1313+
"progress" => test_output.output.progress("Test message"),
1314+
"success" => test_output.output.success("Test message"),
1315+
"warning" => test_output.output.warn("Test message"),
1316+
"error" => test_output.output.error("Test message"),
1317+
"result" => test_output.output.result("Test message"),
1318+
_ => panic!("Unknown method: {}", method),
1319+
}
13071320

1308-
// Verify message went to stderr
1309-
assert_eq!(test_output.stderr(), "⚠️ Testing warning message\n");
1321+
// Verify output went to the correct channel
1322+
match expected_channel {
1323+
"stdout" => {
1324+
assert_eq!(test_output.stdout(), expected_output);
1325+
assert_eq!(test_output.stderr(), "");
1326+
}
1327+
"stderr" => {
1328+
assert_eq!(test_output.stderr(), expected_output);
1329+
assert_eq!(test_output.stdout(), "");
1330+
}
1331+
_ => panic!("Unknown channel: {}", expected_channel),
1332+
}
1333+
}
13101334

1311-
// Verify stdout is empty
1312-
assert_eq!(test_output.stdout(), "");
1313-
}
1335+
/// Test that normal-level messages respect verbosity settings
1336+
///
1337+
/// Progress, success, and warning messages should only appear at Normal or higher.
1338+
/// This replaces 3 individual verbosity tests with one parameterized test.
1339+
#[rstest]
1340+
#[case("progress", VerbosityLevel::Quiet, false)]
1341+
#[case("progress", VerbosityLevel::Normal, true)]
1342+
#[case("progress", VerbosityLevel::Verbose, true)]
1343+
#[case("success", VerbosityLevel::Quiet, false)]
1344+
#[case("success", VerbosityLevel::Normal, true)]
1345+
#[case("success", VerbosityLevel::Verbose, true)]
1346+
#[case("warning", VerbosityLevel::Quiet, false)]
1347+
#[case("warning", VerbosityLevel::Normal, true)]
1348+
#[case("warning", VerbosityLevel::Verbose, true)]
1349+
fn it_should_respect_verbosity_for_normal_level_messages(
1350+
#[case] method: &str,
1351+
#[case] verbosity: VerbosityLevel,
1352+
#[case] should_show: bool,
1353+
) {
1354+
let mut test_output = test_support::TestUserOutput::new(verbosity);
13141355

1315-
#[test]
1316-
fn it_should_write_error_messages_to_stderr() {
1317-
let mut test_output = test_support::TestUserOutput::new(VerbosityLevel::Normal);
1356+
match method {
1357+
"progress" => test_output.output.progress("Test"),
1358+
"success" => test_output.output.success("Test"),
1359+
"warning" => test_output.output.warn("Test"),
1360+
_ => panic!("Unknown method: {}", method),
1361+
}
13181362

1319-
test_output.output.error("Testing error message");
1363+
if should_show {
1364+
assert!(!test_output.stderr().is_empty());
1365+
} else {
1366+
assert_eq!(test_output.stderr(), "");
1367+
}
1368+
}
13201369

1321-
// Verify message went to stderr
1322-
assert_eq!(test_output.stderr(), "❌ Testing error message\n");
1370+
/// Test that error messages are always shown regardless of verbosity
1371+
///
1372+
/// Errors are critical and must be shown at all verbosity levels.
1373+
#[rstest]
1374+
#[case(VerbosityLevel::Quiet)]
1375+
#[case(VerbosityLevel::Normal)]
1376+
#[case(VerbosityLevel::Verbose)]
1377+
#[case(VerbosityLevel::VeryVerbose)]
1378+
#[case(VerbosityLevel::Debug)]
1379+
fn it_should_always_show_errors_at_all_verbosity_levels(#[case] verbosity: VerbosityLevel) {
1380+
let mut test_output = test_support::TestUserOutput::new(verbosity);
1381+
1382+
test_output.output.error("Critical error");
1383+
1384+
assert!(!test_output.stderr().is_empty());
1385+
assert!(test_output.stderr().contains("Critical error"));
1386+
}
13231387

1324-
// Verify stdout is empty
1325-
assert_eq!(test_output.stdout(), "");
1388+
/// Test that result messages are always shown at all verbosity levels
1389+
///
1390+
/// Results are final outputs and must be shown at all verbosity levels.
1391+
#[rstest]
1392+
#[case(VerbosityLevel::Quiet)]
1393+
#[case(VerbosityLevel::Normal)]
1394+
#[case(VerbosityLevel::Verbose)]
1395+
#[case(VerbosityLevel::VeryVerbose)]
1396+
#[case(VerbosityLevel::Debug)]
1397+
fn it_should_always_show_results_at_all_verbosity_levels(#[case] verbosity: VerbosityLevel) {
1398+
let mut test_output = test_support::TestUserOutput::new(verbosity);
1399+
1400+
test_output.output.result("Result data");
1401+
1402+
assert_eq!(test_output.stdout(), "Result data\n");
1403+
assert_eq!(test_output.stderr(), "");
1404+
}
13261405
}
13271406

1328-
#[test]
1329-
fn it_should_write_results_to_stdout() {
1330-
let mut test_output = test_support::TestUserOutput::new(VerbosityLevel::Normal);
1331-
1332-
test_output.output.result("Test result data");
1333-
1334-
// Verify message went to stdout
1335-
assert_eq!(test_output.stdout(), "Test result data\n");
1336-
1337-
// Verify stderr is empty
1338-
assert_eq!(test_output.stderr(), "");
1339-
}
1407+
// ============================================================================
1408+
// UserOutput Tests - Basic Output (Non-parameterized)
1409+
// ============================================================================
1410+
//
1411+
// These tests cover specific functionality not included in parameterized tests:
13401412

13411413
#[test]
13421414
fn it_should_write_data_to_stdout() {
@@ -1351,45 +1423,7 @@ mod tests {
13511423
assert_eq!(test_output.stderr(), "");
13521424
}
13531425

1354-
#[test]
1355-
fn it_should_respect_verbosity_levels_for_progress() {
1356-
let mut test_output = test_support::TestUserOutput::new(VerbosityLevel::Quiet);
1357-
1358-
test_output.output.progress("This should not appear");
1359-
1360-
// Verify no output at Quiet level
1361-
assert_eq!(test_output.stderr(), "");
1362-
}
1363-
1364-
#[test]
1365-
fn it_should_respect_verbosity_levels_for_success() {
1366-
let mut test_output = test_support::TestUserOutput::new(VerbosityLevel::Quiet);
1367-
1368-
test_output.output.success("This should not appear");
1369-
1370-
// Verify no output at Quiet level
1371-
assert_eq!(test_output.stderr(), "");
1372-
}
1373-
1374-
#[test]
1375-
fn it_should_respect_verbosity_levels_for_warn() {
1376-
let mut test_output = test_support::TestUserOutput::new(VerbosityLevel::Quiet);
13771426

1378-
test_output.output.warn("This should not appear");
1379-
1380-
// Verify no output at Quiet level
1381-
assert_eq!(test_output.stderr(), "");
1382-
}
1383-
1384-
#[test]
1385-
fn it_should_always_show_errors_regardless_of_verbosity() {
1386-
let mut test_output = test_support::TestUserOutput::new(VerbosityLevel::Quiet);
1387-
1388-
test_output.output.error("Critical error message");
1389-
1390-
// Verify error appears even at Quiet level
1391-
assert_eq!(test_output.stderr(), "❌ Critical error message\n");
1392-
}
13931427

13941428
#[test]
13951429
fn it_should_use_normal_as_default_verbosity() {

0 commit comments

Comments
 (0)