"""Step definitions for CLI features.""" from behave import given, then, when from click.testing import CliRunner from hypothesis import given as hypothesis_given from hypothesis import strategies as st from boilerplate.cli import main @given("the CLI is available") def step_cli_available(context): """Ensure CLI is importable.""" context.runner = CliRunner() assert context.runner is not None @when('I run "{command}"') def step_run_command(context, command): """Execute a CLI command.""" parts = command.split() if len(parts) >= 3 and parts[0] == "python" and parts[1] == "-m" and parts[2] == "boilerplate": args = parts[3:] # Remove "python -m boilerplate" else: args = parts context.result = context.runner.invoke(main, args) @then("the exit code should be {code:d}") def step_check_exit_code(context, code): """Verify exit code.""" assert context.result.exit_code == code @then('the output should contain "{text}"') def step_output_contains(context, text): """Check if output contains text.""" assert text in context.result.output @then('the output should contain "{text}" {count:d} times') def step_output_contains_count(context, text, count): """Check if output contains text N times.""" actual_count = context.result.output.count(text) assert actual_count == count, f"Expected {count} occurrences, found {actual_count}" @when("I fuzz test the CLI with random names") def step_fuzz_cli(context): """Fuzz test the CLI with Hypothesis.""" runner = context.runner results = [] @hypothesis_given(st.text(min_size=1, max_size=100), st.integers(min_value=1, max_value=10)) def test_random_inputs(name, count): result = runner.invoke(main, ["--name", name, "--count", str(count)]) results.append(result) assert result.exit_code == 0 # Check that the expected greeting appears exactly count times expected_greeting = f"Hello, {name}!" assert result.output.count(expected_greeting) == count # Run 1000 test cases test_random_inputs() context.fuzz_results = results @then("all invocations should succeed") def step_all_succeed(context): """Verify all fuzz test invocations succeeded.""" assert hasattr(context, "fuzz_results") # Hypothesis will raise if any test failed