"""Tests for the Gherkin generator module.""" import pytest from nl2gherkin.gherkin.generator import ( GherkinFeature, GherkinGenerator, GherkinScenario, ScenarioType, ) from nl2gherkin.nlp.analyzer import ActionType, ActorType, RequirementAnalysis class TestGherkinGenerator: """Test cases for GherkinGenerator.""" @pytest.fixture def generator(self): """Create a generator instance.""" return GherkinGenerator() @pytest.fixture def analysis(self): """Create a sample analysis.""" return RequirementAnalysis( raw_text="As a user, I want to login", actor="user", actor_type=ActorType.USER, action="login", action_type=ActionType.LOGIN, target="account", ) def test_generate_simple_scenario(self, generator, analysis): """Test generating a simple scenario.""" result = generator.generate(analysis) assert "Feature:" in result assert "Scenario:" in result assert "Given" in result assert "When" in result assert "Then" in result def test_feature_name_from_actor_and_action(self, generator, analysis): """Test feature name is derived from actor and action.""" result = generator.generate(analysis) assert "user login" in result.lower() def test_scenario_name_generation(self, generator, analysis): """Test scenario name is properly generated.""" scenario = generator.generate_scenario(analysis) assert scenario.name is not None assert len(scenario.name) > 0 def test_scenario_type_regular(self, generator, analysis): """Test regular scenario type.""" scenario = generator.generate_scenario(analysis) assert scenario.scenario_type == ScenarioType.SCENARIO def test_scenario_type_outline_with_variables(self, generator): """Test scenario outline type with variables.""" analysis = RequirementAnalysis( raw_text="As a user, I want to search for ", actor="user", action="search", variables={"product": "string"}, examples=["product"], ) scenario = generator.generate_scenario(analysis) assert scenario.scenario_type == ScenarioType.SCENARIO_OUTLINE def test_examples_table_generation(self, generator): """Test Examples table is generated for outlines.""" analysis = RequirementAnalysis( raw_text="As a user, I want to search for ", actor="user", action="search", variables={"product": "string"}, examples=["product"], ) result = generator.generate(analysis) assert "Examples:" in result assert "| product |" in result def test_steps_have_correct_keywords(self, generator, analysis): """Test that steps have correct Gherkin keywords.""" scenario = generator.generate_scenario(analysis) keywords = [step.keyword for step in scenario.steps] assert "Given" in keywords assert "When" in keywords assert "Then" in keywords def test_benefit_in_description(self, generator): """Test that benefit is included in description.""" analysis = RequirementAnalysis( raw_text="As a user, I want to login so that I can access my account", actor="user", action="login", benefit="I can access my account", ) result = generator.generate(analysis) assert "can access my account" in result def test_condition_as_given(self, generator): """Test that conditions are converted to Given steps.""" analysis = RequirementAnalysis( raw_text="When the user is logged in", actor="user", action="view", target="dashboard", condition="the user is logged in", ) scenario = generator.generate_scenario(analysis) given_steps = [s for s in scenario.steps if s.keyword == "Given"] assert len(given_steps) >= 1 def test_read_action_display_then_step(self, generator): """Test that read actions use 'displayed' in Then step.""" analysis = RequirementAnalysis( raw_text="As a user, I want to view my profile", actor="user", action="view", action_type=ActionType.READ, target="profile", ) scenario = generator.generate_scenario(analysis) then_steps = [s for s in scenario.steps if s.keyword == "Then"] assert len(then_steps) > 0 assert "displayed" in then_steps[0].text.lower() def test_empty_analysis(self, generator): """Test handling of empty analysis.""" analysis = RequirementAnalysis(raw_text="") result = generator.generate(analysis) assert "Feature:" in result def test_render_feature_structure(self, generator, analysis): """Test that rendered feature has proper structure.""" feature = generator._create_feature(analysis) assert isinstance(feature, GherkinFeature) assert feature.name is not None assert len(feature.scenarios) == 1 assert isinstance(feature.scenarios[0], GherkinScenario) def test_multiple_scenarios(self, generator): """Test generating multiple scenarios.""" analysis1 = RequirementAnalysis( raw_text="As a user, I want to login", actor="user", action="login", ) analysis2 = RequirementAnalysis( raw_text="As a user, I want to logout", actor="user", action="logout", ) feature1 = generator._create_feature(analysis1) feature2 = generator._create_feature(analysis2) assert len(feature1.scenarios) == 1 assert len(feature2.scenarios) == 1