#include "TestHarness.h" #include "src/weather/WeatherParser.h" #include "src/weather/WeatherSummaryFormatter.h" QA_TEST(WeatherParser, ExtractsExplicitCitiesAndDates) { const WeatherParser parser; const WeatherQuery current = parser.parse(QStringLiteral("西安天气怎么样")); const WeatherQuery tomorrow = parser.parse(QStringLiteral("明天西安天气怎么样")); const WeatherQuery afterTomorrow = parser.parse(QStringLiteral("后天纽约天气")); QA_EXPECT_EQ(current.cityName, QStringLiteral("西安")); QA_EXPECT_EQ(current.kind, WeatherQueryKind::Current); QA_EXPECT_EQ(tomorrow.cityName, QStringLiteral("西安")); QA_EXPECT_EQ(tomorrow.dateOffset, 1); QA_EXPECT_EQ(afterTomorrow.cityName, QStringLiteral("纽约")); QA_EXPECT_EQ(afterTomorrow.dateOffset, 2); } QA_TEST(WeatherParser, ParsesRangeAndDefaultCityRequest) { const WeatherParser parser; const WeatherQuery range = parser.parse(QStringLiteral("未来三天北京天气")); const WeatherQuery defaultCity = parser.parse(QStringLiteral("今天天气怎么样")); QA_EXPECT_EQ(range.kind, WeatherQueryKind::Range); QA_EXPECT_EQ(range.forecastDays, 3); QA_EXPECT_EQ(range.cityName, QStringLiteral("北京")); QA_EXPECT(defaultCity.cityName.isEmpty()); } QA_TEST(WeatherParser, RejectsUnsupportedQueries) { const WeatherParser parser; QA_EXPECT(!parser.parse(QStringLiteral("西安空气质量")).unsupportedReason.isEmpty()); QA_EXPECT(!parser.parse(QStringLiteral("北京天气预警")).unsupportedReason.isEmpty()); QA_EXPECT(!parser.parse(QStringLiteral("明天穿什么")).unsupportedReason.isEmpty()); } QA_TEST(WeatherFormatter, MapsWeatherCodesAndWindDirections) { QA_EXPECT_EQ(WeatherSummaryFormatter::weatherCodeText(0), QStringLiteral("晴")); QA_EXPECT_EQ(WeatherSummaryFormatter::weatherCodeText(95), QStringLiteral("雷暴")); QA_EXPECT_EQ(WeatherSummaryFormatter::weatherCodeText(-1), QStringLiteral("未知天气")); QA_EXPECT_EQ(WeatherSummaryFormatter::windDirectionText(0), QStringLiteral("北风")); QA_EXPECT_EQ(WeatherSummaryFormatter::windDirectionText(90), QStringLiteral("东风")); QA_EXPECT_EQ(WeatherSummaryFormatter::windDirectionText(225), QStringLiteral("西南风")); } QA_TEST(WeatherFormatter, FormatsCurrentWeatherWithSource) { WeatherReport report; report.query.kind = WeatherQueryKind::Current; report.location.cityName = QStringLiteral("西安"); report.location.countryName = QStringLiteral("中国"); report.location.source = WeatherLocationSource::SettingsDefault; report.current.valid = true; report.current.weatherCode = 0; report.current.hasTemperature = true; report.current.temperatureC = 28.5; const QString message = WeatherSummaryFormatter().format(report); QA_EXPECT(message.contains(QStringLiteral("使用设置页默认城市"))); QA_EXPECT(message.contains(QStringLiteral("西安,中国当前天气:晴"))); QA_EXPECT(message.contains(QStringLiteral("28.5℃"))); } QA_TEST(WeatherFormatter, FormatsAmbiguousLocationAndMissingData) { WeatherReport report; report.query.kind = WeatherQueryKind::Current; report.location.cityName = QStringLiteral("Springfield"); report.location.countryName = QStringLiteral("United States"); report.hasLocationAmbiguity = true; report.locationCandidates = { {QStringLiteral("Springfield"), QStringLiteral("Illinois"), QStringLiteral("United States"), {}, 0.0, 0.0}, {QStringLiteral("Springfield"), QStringLiteral("Missouri"), QStringLiteral("United States"), {}, 0.0, 0.0}, }; const QString message = WeatherSummaryFormatter().format(report); QA_EXPECT(message.contains(QStringLiteral("可能存在同名城市"))); QA_EXPECT(message.contains(QStringLiteral("Missouri"))); QA_EXPECT(message.contains(QStringLiteral("缺少当前天气"))); }