1979/tests/test_exploration.py

tests / test_exploration.py

tests/test_exploration.py · 12.9 KB

# -*- coding: utf-8 -*-
"""Test Exploration - 探索系统测试

覆盖:探索区域/通用模板/城市探索/物品发现/事件触发
"""

import pytest
import sys
import os

sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', 'src'))

from exploration import (
    ExploreArea, ExplorationSystem,
    GENERIC_AREA_TEMPLATES, CITY_EXPLORE_AREAS,
)


class TestExploreArea:
    """测试探索区域类。"""

    def test_creation(self):
        area = ExploreArea('测试区', 3, ['物品A'], ['NPC1'], ['事件1'], '描述')
        assert area.name == '测试区'
        assert area.danger_level == 3
        assert area.possible_items == ['物品A']
        assert area.possible_npcs == ['NPC1']
        assert area.possible_events == ['事件1']
        assert area.description == '描述'
        assert area.is_safe_camp is False
        assert area.explored is False

    def test_safe_camp(self):
        area = ExploreArea('安全区', 1, [], [], [], '安全', is_safe_camp=True)
        assert area.is_safe_camp is True

    def test_explore_basic(self):
        area = ExploreArea('测试区', 1, ['物品A', '物品B'], ['NPC1'], ['探索'], '描述')
        result = area.explore()
        assert result['success'] is True
        assert 'description' in result
        assert result['description'] == '描述'

    def test_explore_safe_camp(self):
        area = ExploreArea('安全区', 1, [], [], [], '安全', is_safe_camp=True)
        result = area.explore()
        assert result['success'] is True
        assert result['events'] == ['安全营地休息']

    def test_explore_high_danger(self):
        area = ExploreArea('危险区', 5, ['弹药'], ['土匪'], ['战斗', '遭遇'], '危险')
        # 危险度5,探索可能遇到危险事件
        result = area.explore()
        assert result['success'] is True

    def test_explore_no_items(self):
        area = ExploreArea('空区', 1, [], [], ['探索'], '空')
        result = area.explore()
        assert result['success'] is True
        assert result['items'] == []

    def test_explore_no_npcs(self):
        area = ExploreArea('无人区', 1, ['物品'], [], ['探索'], '空')
        result = area.explore()
        assert result['success'] is True
        assert result['npcs'] == []

    def test_repr(self):
        area = ExploreArea('测试区', 3, [], [], [], '描述')
        r = repr(area)
        assert '测试区' in r
        assert 'danger=3' in r


class TestGenericTemplates:
    """测试通用探索模板。"""

    def test_factory_template(self):
        assert 'factory' in GENERIC_AREA_TEMPLATES
        t = GENERIC_AREA_TEMPLATES['factory']
        assert t['danger'] == 3
        assert '零件' in t['items']
        assert '流民' in t['npcs']
        assert '探索' in t['events']

    def test_residential_template(self):
        assert 'residential' in GENERIC_AREA_TEMPLATES
        t = GENERIC_AREA_TEMPLATES['residential']
        assert t['danger'] == 1
        assert '食物' in t['items']
        assert '流民' in t['npcs']

    def test_warzone_template(self):
        assert 'warzone' in GENERIC_AREA_TEMPLATES
        t = GENERIC_AREA_TEMPLATES['warzone']
        assert t['danger'] == 5
        assert '弹药' in t['items']
        assert '土匪' in t['npcs']
        assert '战斗' in t['events']

    def test_warehouse_template(self):
        assert 'warehouse' in GENERIC_AREA_TEMPLATES
        t = GENERIC_AREA_TEMPLATES['warehouse']
        assert t['danger'] == 3
        assert '大量食物' in t['items']

    def test_school_template(self):
        assert 'school' in GENERIC_AREA_TEMPLATES
        t = GENERIC_AREA_TEMPLATES['school']
        assert t['danger'] == 1
        assert '书籍' in t['items']

    def test_hospital_template(self):
        assert 'hospital' in GENERIC_AREA_TEMPLATES
        t = GENERIC_AREA_TEMPLATES['hospital']
        assert t['danger'] == 2
        assert '医疗物资' in t['items']

    def test_gas_station_template(self):
        assert 'gas_station' in GENERIC_AREA_TEMPLATES
        t = GENERIC_AREA_TEMPLATES['gas_station']
        assert t['danger'] == 2
        assert '柴油' in t['items']

    def test_police_station_template(self):
        assert 'police_station' in GENERIC_AREA_TEMPLATES
        t = GENERIC_AREA_TEMPLATES['police_station']
        assert t['danger'] == 4
        assert '弹药' in t['items']
        assert '武器' in t['items']

    def test_train_station_template(self):
        assert 'train_station' in GENERIC_AREA_TEMPLATES
        t = GENERIC_AREA_TEMPLATES['train_station']
        assert '情报' in t['items']
        assert '商人' in t['npcs']

    def test_market_template(self):
        assert 'market' in GENERIC_AREA_TEMPLATES
        t = GENERIC_AREA_TEMPLATES['market']
        assert '交易' in t['events']
        assert '商人' in t['npcs']


class TestCityExploreAreas:
    """测试城市探索区域。"""

    def test_dalian_areas(self):
        assert '大连' in CITY_EXPLORE_AREAS
        areas = CITY_EXPLORE_AREAS['大连']
        assert len(areas) >= 5
        # 检查关键区域
        names = [a.name for a in areas]
        assert '大连理工大学' in names
        assert '星海广场' in names
        assert '老虎滩' in names
        assert '父亲的山洞' in names
        assert '中山广场' in names

    def test_shenyang_areas(self):
        assert '沈阳' in CITY_EXPLORE_AREAS
        areas = CITY_EXPLORE_AREAS['沈阳']
        assert len(areas) >= 5
        names = [a.name for a in areas]
        assert '沈阳故宫' in names
        assert '沈阳站' in names
        assert '铁西区工业废墟' in names

    def test_changchun_areas(self):
        assert '长春' in CITY_EXPLORE_AREAS
        areas = CITY_EXPLORE_AREAS['长春']
        assert len(areas) >= 5
        names = [a.name for a in areas]
        assert '长春电影制片厂' in names
        assert '第一汽车制造厂' in names

    def test_harbin_areas(self):
        assert '哈尔滨' in CITY_EXPLORE_AREAS
        areas = CITY_EXPLORE_AREAS['哈尔滨']
        assert len(areas) >= 5
        names = [a.name for a in areas]
        assert '哈尔滨工业大学' in names
        assert '中央大街' in names
        assert '平房区' in names

    def test_mudanjiang_areas(self):
        assert '牡丹江' in CITY_EXPLORE_AREAS
        areas = CITY_EXPLORE_AREAS['牡丹江']
        assert len(areas) >= 5

    def test_suifenhe_areas(self):
        assert '绥芬河' in CITY_EXPLORE_AREAS
        areas = CITY_EXPLORE_AREAS['绥芬河']
        assert len(areas) >= 5
        names = [a.name for a in areas]
        assert '绥芬河口岸' in names

    def test_heihe_areas(self):
        assert '黑河' in CITY_EXPLORE_AREAS
        areas = CITY_EXPLORE_AREAS['黑河']
        assert len(areas) >= 5
        names = [a.name for a in areas]
        assert '黑河口岸' in names

    def test_qiqihar_areas(self):
        assert '齐齐哈尔' in CITY_EXPLORE_AREAS
        areas = CITY_EXPLORE_AREAS['齐齐哈尔']
        assert len(areas) >= 5

    def test_jiamusi_areas(self):
        assert '佳木斯' in CITY_EXPLORE_AREAS
        areas = CITY_EXPLORE_AREAS['佳木斯']
        assert len(areas) >= 5

    def test_dandong_areas(self):
        assert '丹东' in CITY_EXPLORE_AREAS
        areas = CITY_EXPLORE_AREAS['丹东']
        assert len(areas) >= 5
        names = [a.name for a in areas]
        assert '鸭绿江畔' in names

    def test_jinzhou_areas(self):
        assert '锦州' in CITY_EXPLORE_AREAS
        areas = CITY_EXPLORE_AREAS['锦州']
        assert len(areas) >= 5

    def test_chifeng_areas(self):
        assert '赤峰' in CITY_EXPLORE_AREAS
        areas = CITY_EXPLORE_AREAS['赤峰']
        assert len(areas) >= 5

    def test_tongliao_areas(self):
        assert '通辽' in CITY_EXPLORE_AREAS
        areas = CITY_EXPLORE_AREAS['通辽']
        assert len(areas) >= 5

    def test_siping_areas(self):
        assert '四平' in CITY_EXPLORE_AREAS
        areas = CITY_EXPLORE_AREAS['四平']
        assert len(areas) >= 5

    def test_yanji_areas(self):
        assert '延吉' in CITY_EXPLORE_AREAS
        areas = CITY_EXPLORE_AREAS['延吉']
        assert len(areas) >= 5

    def test_daqing_areas(self):
        assert '大庆' in CITY_EXPLORE_AREAS
        areas = CITY_EXPLORE_AREAS['大庆']
        assert len(areas) >= 5
        names = [a.name for a in areas]
        assert '大庆油田' in names

    def test_yichun_areas(self):
        assert '伊春' in CITY_EXPLORE_AREAS
        areas = CITY_EXPLORE_AREAS['伊春']
        assert len(areas) >= 5

    def test_hegang_areas(self):
        assert '鹤岗' in CITY_EXPLORE_AREAS
        areas = CITY_EXPLORE_AREAS['鹤岗']
        assert len(areas) >= 5

    def test_jixi_areas(self):
        assert '鸡西' in CITY_EXPLORE_AREAS
        areas = CITY_EXPLORE_AREAS['鸡西']
        assert len(areas) >= 5

    def test_qitaihe_areas(self):
        assert '七台河' in CITY_EXPLORE_AREAS
        areas = CITY_EXPLORE_AREAS['七台河']
        assert len(areas) >= 5

    def test_father_cave_safe(self):
        """父亲的山洞是安全营地。"""
        areas = CITY_EXPLORE_AREAS['大连']
        cave = [a for a in areas if a.name == '父亲的山洞'][0]
        assert cave.is_safe_camp is True
        assert cave.danger_level == 1

    def test_dalian_danger_levels(self):
        areas = CITY_EXPLORE_AREAS['大连']
        for area in areas:
            assert 1 <= area.danger_level <= 5

    def test_shenyang_danger_levels(self):
        areas = CITY_EXPLORE_AREAS['沈阳']
        for area in areas:
            assert 1 <= area.danger_level <= 5


class TestExplorationSystem:
    """测试探索系统。"""

    def test_creation(self):
        sys = ExplorationSystem()
        assert len(sys.city_areas) > 0

    def test_get_areas(self):
        sys = ExplorationSystem()
        areas = sys.get_areas('大连')
        assert len(areas) >= 5
        for area in areas:
            assert isinstance(area, ExploreArea)

    def test_get_areas_unknown(self):
        sys = ExplorationSystem()
        areas = sys.get_areas('不存在的城市')
        assert len(areas) == 0

    def test_get_area_by_name(self):
        sys = ExplorationSystem()
        area = sys.get_area_by_name('大连', '大连理工大学')
        assert area is not None
        assert area.name == '大连理工大学'

    def test_get_area_by_name_unknown(self):
        sys = ExplorationSystem()
        area = sys.get_area_by_name('大连', '不存在')
        assert area is None

    def test_explore_area(self):
        sys = ExplorationSystem()
        result = sys.explore_area('大连', '大连理工大学')
        assert result['success'] is True
        assert result['area_name'] == '大连理工大学'
        assert result['city'] == '大连'

    def test_explore_area_unknown(self):
        sys = ExplorationSystem()
        result = sys.explore_area('大连', '不存在')
        assert result['success'] is False

    def test_get_all_cities(self):
        sys = ExplorationSystem()
        cities = sys.get_all_cities()
        assert '大连' in cities
        assert '沈阳' in cities
        assert '哈尔滨' in cities
        assert len(cities) > 20

    def test_generic_areas_generated(self):
        sys = ExplorationSystem()
        # 通用城市应该有5个区域
        areas = sys.get_areas('铁岭')
        assert len(areas) == 5
        names = [a.name for a in areas]
        assert '铁岭站' in names
        assert '铁岭居民区' in names
        assert '铁岭工厂' in names
        assert '铁岭公园' in names
        assert '铁岭市场' in names

    def test_generic_area_danger(self):
        sys = ExplorationSystem()
        areas = sys.get_areas('铁岭')
        # 居民区危险度1
        residential = [a for a in areas if '居民区' in a.name][0]
        assert residential.danger_level == 1
        # 工厂危险度3
        factory = [a for a in areas if '工厂' in a.name][0]
        assert factory.danger_level == 3
        # 火车站危险度3
        station = [a for a in areas if '站' in a.name and '公园' not in a.name][0]
        assert station.danger_level == 3

    def test_repr(self):
        sys = ExplorationSystem()
        r = repr(sys)
        assert 'ExplorationSystem' in r

    def test_dalian_key_areas(self):
        sys = ExplorationSystem()
        areas = sys.get_areas('大连')
        names = [a.name for a in areas]
        assert '大连理工大学' in names
        assert '星海广场' in names
        assert '老虎滩' in names
        assert '父亲的山洞' in names
        assert '中山广场' in names

    def test_shenyang_key_areas(self):
        sys = ExplorationSystem()
        areas = sys.get_areas('沈阳')
        names = [a.name for a in areas]
        assert '沈阳故宫' in names
        assert '沈阳站' in names
        assert '铁西区工业废墟' in names
        assert '中街' in names
        assert '沈阳兵工厂' in names

← 返回文件树 · 在 GitHub 上查看