' ============================================================================== ' 模块: M99_TestRunner ' 职责: 单元测试,验证 M03_Logic 的核心算法 ' 依赖: M03_Logic, clsErrorLogger (无需引用 Scripting Runtime) ' ============================================================================== Option Explicit Private m_Logger As clsErrorLogger Private m_FailCount As Long Private m_PassCount As Long Private m_wsMapping As Worksheet ' 用于测试的映射表工作表 ' ------------------------------------------------------------------------------ ' 主入口: 运行所有测试 ' ------------------------------------------------------------------------------ Public Sub RunAllTests() ' 初始化环境 Set m_Logger = New clsErrorLogger M03_Logic.InitLogic m_Logger m_FailCount = 0 m_PassCount = 0 Debug.Print String(50, "=") Debug.Print "开始运行单元测试: " & Now Debug.Print String(50, "-") ' 执行测试用例 Test_01_SimpleAtom Test_02_SimpleAND Test_03_SimpleOR Test_04_CartesianProduct ' 核心:测试 (A OR B) AND C Test_05_InequalityMerge ' 核心:测试 !=A AND !=B Test_06_LogicConflict ' 核心:测试 A=1 AND A=2 Test_07_ComplexNested ' 核心:多层括号 ' 新增预处理测试 Debug.Print String(50, "-") Debug.Print "新增预处理测试:" Debug.Print String(50, "-") Test_PP_01_AzxsMappingLoad Test_PP_02_LcfwMappingLoad Test_PP_03_AzxsReplacement Test_PP_04_LcfwReplacement Test_PP_05_ORMerging Test_PP_06_FullIntegration Test_PP_07_NonJointCategory Test_PP_08_UnmappedValues Test_PP_09_ComponentCategory_AzxsMapping Test_PP_10_ComponentCategory_LcfwUnchanged Test_PP_11_ComponentCategory_ORMerging Test_PP_12_ComponentCategory_ParenthesesSimplification ' 汇总结果 Debug.Print String(50, "-") If m_FailCount = 0 Then Debug.Print "测试结果: ALL PASS! (共 " & m_PassCount & " 个测试点)" Else Debug.Print "测试结果: 失败 " & m_FailCount & " 个, 通过 " & m_PassCount & " 个" End If Debug.Print String(50, "=") End Sub ' ------------------------------------------------------------------------------ ' 测试用例 01: 简单赋值 ' 输入: gclj=M20 ' 期望: 1行数据, gclj字段为M20 ' ------------------------------------------------------------------------------ Private Sub Test_01_SimpleAtom() Dim col As Collection Dim row As Object ' Dictionary Set col = M03_Logic.ParseRule("gclj=M20", 1) Assert_NotNull col, "T01_Col_Not_Null" Assert_Equal col.count, 1, "T01_Count" Set row = col(1) Assert_Equal row("gclj"), "M20", "T01_Value_Check" End Sub ' ------------------------------------------------------------------------------ ' 测试用例 02: AND 逻辑 (属性合并) ' 输入: gclj=M20 AND jycz=1 ' 期望: 1行数据, 包含两个字段 ' ------------------------------------------------------------------------------ Private Sub Test_02_SimpleAND() Dim col As Collection Dim row As Object Set col = M03_Logic.ParseRule("gclj=M20 AND jycz=1", 2) Assert_Equal col.count, 1, "T02_Count" Set row = col(1) Assert_Equal row("gclj"), "M20", "T02_Key1" Assert_Equal row("jycz"), "1", "T02_Key2" End Sub ' ------------------------------------------------------------------------------ ' 测试用例 03: OR 逻辑 (记录分裂) ' 输入: azxs=A0 OR azxs=A1 ' 期望: 2行数据 ' ------------------------------------------------------------------------------ Private Sub Test_03_SimpleOR() Dim col As Collection Set col = M03_Logic.ParseRule("azxs=A0 OR azxs=A1", 3) Assert_Equal col.count, 2, "T03_Count" End Sub ' ------------------------------------------------------------------------------ ' 测试用例 04: 笛卡尔积 (AND 连接 OR) ' 输入: gclj=M20 AND (azxs=A0 OR azxs=A1) ' 期望: 2行数据。行1(gclj=M20, azxs=A0), 行2(gclj=M20, azxs=A1) ' ------------------------------------------------------------------------------ Private Sub Test_04_CartesianProduct() Dim col As Collection Dim r1 As Object, r2 As Object Set col = M03_Logic.ParseRule("gclj=M20 AND (azxs=A0 OR azxs=A1)", 4) Assert_Equal col.count, 2, "T04_Count" Set r1 = col(1) Set r2 = col(2) ' 验证公共部分 Assert_Equal r1("gclj"), "M20", "T04_Row1_Common" Assert_Equal r2("gclj"), "M20", "T04_Row2_Common" ' 验证差异部分 Dim hasA0 As Boolean, hasA1 As Boolean If r1("azxs") = "A0" Or r2("azxs") = "A0" Then hasA0 = True If r1("azxs") = "A1" Or r2("azxs") = "A1" Then hasA1 = True Assert_Equal hasA0, True, "T04_Has_A0" Assert_Equal hasA1, True, "T04_Has_A1" End Sub ' ------------------------------------------------------------------------------ ' 测试用例 05: 不等于逻辑合并 ' 输入: gclj!=M20 AND gclj!=M30 ' 期望: 1行数据, gclj字段为 "!=M20,!=M30" ' ------------------------------------------------------------------------------ Private Sub Test_05_InequalityMerge() Dim col As Collection Dim row As Object Set col = M03_Logic.ParseRule("gclj!=M20 AND gclj!=M30", 5) Assert_Equal col.count, 1, "T05_Count" Set row = col(1) Assert_Equal row("gclj"), "!=M20,!=M30", "T05_Value_Merge" End Sub ' ------------------------------------------------------------------------------ ' 测试用例 06: 逻辑冲突检测 (修正版) ' 输入: gclj=M20 AND gclj=M30 ' 期望: 返回 Nothing 或 空集合,且 Logger 中有记录 ' ------------------------------------------------------------------------------ Private Sub Test_06_LogicConflict() Dim col As Collection ' 重置 Logger Set m_Logger = New clsErrorLogger M03_Logic.InitLogic m_Logger ' 此时 ParseRule 内部会捕捉冲突 Set col = M03_Logic.ParseRule("gclj=M20 AND gclj=M30", 6) ' 检查结果: 应该是 Nothing 或者 Count=0 Dim isInvalid As Boolean If col Is Nothing Then isInvalid = True Else If col.count = 0 Then isInvalid = True Else isInvalid = False End If Assert_Equal isInvalid, True, "T06_Should_Return_Empty_Or_Nothing" Assert_Equal m_Logger.HasErrors, True, "T06_Should_Log_Error" End Sub ' ------------------------------------------------------------------------------ ' 测试用例 07: 复杂嵌套 ' 输入: A=1 AND (B=1 OR (B=2 AND C=3)) ' 期望: 2行 ' Row 1: A=1, B=1 ' Row 2: A=1, B=2, C=3 ' ------------------------------------------------------------------------------ Private Sub Test_07_ComplexNested() Dim col As Collection Set col = M03_Logic.ParseRule("A=1 AND (B=1 OR (B=2 AND C=3))", 7) Assert_Equal col.count, 2, "T07_Count" Dim count2 As Long, count3 As Long Dim i As Long Dim r As Object For i = 1 To col.count Set r = col(i) If r.count = 2 Then count2 = count2 + 1 If r.count = 3 Then count3 = count3 + 1 Next i Assert_Equal count2, 1, "T07_Row_With_2_Keys" Assert_Equal count3, 1, "T07_Row_With_3_Keys" End Sub ' ============================================================================== ' 辅助断言函数 ' ============================================================================== Private Sub Assert_Equal(actual As Variant, expected As Variant, testName As String) If CStr(actual) = CStr(expected) Then ' Debug.Print " [PASS] " & testName m_PassCount = m_PassCount + 1 Else Debug.Print " [FAIL] " & testName & " | Expected: " & expected & ", Actual: " & actual m_FailCount = m_FailCount + 1 End If End Sub Private Sub Assert_NotNull(obj As Object, testName As String) If Not obj Is Nothing Then m_PassCount = m_PassCount + 1 Else Debug.Print " [FAIL] " & testName & " | Object is Nothing" m_FailCount = m_FailCount + 1 End If End Sub ' ============================================================================== ' 预处理测试用例 ' ============================================================================== ' ------------------------------------------------------------------------------ ' 测试用例 PP_01: 测试azxs映射加载 ' 验证所有12个azxs值是否正确映射 ' ------------------------------------------------------------------------------ Private Sub Test_PP_01_AzxsMappingLoad() SetupPreProcessorTest ' 测试径向映射 Assert_Equal M05_PreProcessor.GetAzxsMappedValue("A0"), "径向", "PP01_A0_To_径向" Assert_Equal M05_PreProcessor.GetAzxsMappedValue("AT"), "径向", "PP01_AT_To_径向" Assert_Equal M05_PreProcessor.GetAzxsMappedValue("AH"), "径向", "PP01_AH_To_径向" ' 测试下轴向映射 Assert_Equal M05_PreProcessor.GetAzxsMappedValue("B0"), "下轴向", "PP01_B0_To_下轴向" Assert_Equal M05_PreProcessor.GetAzxsMappedValue("BT"), "下轴向", "PP01_BT_To_下轴向" Assert_Equal M05_PreProcessor.GetAzxsMappedValue("BZ"), "下轴向", "PP01_BZ_To_下轴向" Assert_Equal M05_PreProcessor.GetAzxsMappedValue("BH"), "下轴向", "PP01_BH_To_下轴向" ' 测试中轴向映射 Assert_Equal M05_PreProcessor.GetAzxsMappedValue("Z0"), "中轴向", "PP01_Z0_To_中轴向" Assert_Equal M05_PreProcessor.GetAzxsMappedValue("ZT"), "中轴向", "PP01_ZT_To_中轴向" Assert_Equal M05_PreProcessor.GetAzxsMappedValue("ZZ"), "中轴向", "PP01_ZZ_To_中轴向" Assert_Equal M05_PreProcessor.GetAzxsMappedValue("ZH"), "中轴向", "PP01_ZH_To_中轴向" End Sub ' ------------------------------------------------------------------------------ ' 测试用例 PP_02: 测试lcfw映射加载 ' 验证M01-M11都映射到"低压" ' ------------------------------------------------------------------------------ Private Sub Test_PP_02_LcfwMappingLoad() SetupPreProcessorTest Assert_Equal M05_PreProcessor.GetLcfwMappedValue("M01"), "低压", "PP02_M01_To_低压" Assert_Equal M05_PreProcessor.GetLcfwMappedValue("M02"), "低压", "PP02_M02_To_低压" Assert_Equal M05_PreProcessor.GetLcfwMappedValue("M03"), "低压", "PP02_M03_To_低压" Assert_Equal M05_PreProcessor.GetLcfwMappedValue("M11"), "低压", "PP02_M11_To_低压" End Sub ' ------------------------------------------------------------------------------ ' 测试用例 PP_03: 测试azxs值替换 ' ------------------------------------------------------------------------------ Private Sub Test_PP_03_AzxsReplacement() SetupPreProcessorTest ' 单个原子 Dim result1 As String result1 = M05_PreProcessor.PreprocessCondition("azxs=A0", "接头", 1) Assert_Equal result1, "azxs=径向", "PP03_Single_Azxs_Replacement" ' 与AND结合 Dim result2 As String result2 = M05_PreProcessor.PreprocessCondition("gclj=M20 AND azxs=B0", "接头", 2) Assert_Equal result2, "gclj=M20 AND azxs=下轴向", "PP03_Azxs_With_AND" ' 在OR中 Dim result3 As String result3 = M05_PreProcessor.PreprocessCondition("azxs=A0 OR azxs=AT", "接头", 3) Assert_Equal result3, "azxs=径向", "PP03_Azxs_With_OR_Merged" End Sub ' ------------------------------------------------------------------------------ ' 测试用例 PP_04: 测试lcfw值替换 ' ------------------------------------------------------------------------------ Private Sub Test_PP_04_LcfwReplacement() SetupPreProcessorTest ' 单个原子 Dim result1 As String result1 = M05_PreProcessor.PreprocessCondition("lcfw=M01", "接头", 1) Assert_Equal result1, "lcfw=低压", "PP04_Single_Lcfw_Replacement" ' 与AND结合 Dim result2 As String result2 = M05_PreProcessor.PreprocessCondition("gclj=M20 AND lcfw=M02", "接头", 2) Assert_Equal result2, "gclj=M20 AND lcfw=低压", "PP04_Lcfw_With_AND" End Sub ' ------------------------------------------------------------------------------ ' 测试用例 PP_05: 测试OR条件合并 ' ------------------------------------------------------------------------------ Private Sub Test_PP_05_ORMerging() SetupPreProcessorTest ' 精确重复 Dim result1 As String result1 = M05_PreProcessor.PreprocessCondition("lcfw=低压 OR lcfw=低压", "接头", 1) Assert_Equal result1, "lcfw=低压", "PP05_Exact_Duplicate_Merging" ' 多次重复 Dim result2 As String result2 = M05_PreProcessor.PreprocessCondition("azxs=径向 OR azxs=径向 OR azxs=径向", "接头", 2) Assert_Equal result2, "azxs=径向", "PP05_Multiple_Duplicate_Merging" ' 混合情况(保留不同的) Dim result3 As String result3 = M05_PreProcessor.PreprocessCondition("lcfw=低压 OR lcfw=高压", "接头", 3) ' 注意:高压不会在映射表中,所以保持原值 Dim hasLow As Boolean, hasHigh As Boolean hasLow = InStr(result3, "lcfw=低压") > 0 hasHigh = InStr(result3, "lcfw=高压") > 0 Assert_Equal hasLow, True, "PP05_Mixed_Has_Low" Assert_Equal hasHigh, True, "PP05_Mixed_Has_High" End Sub ' ------------------------------------------------------------------------------ ' 测试用例 PP_06: 测试完整集成 ' 验证预处理后的结果能被M03_Logic正确解析 ' ------------------------------------------------------------------------------ Private Sub Test_PP_06_FullIntegration() SetupPreProcessorTest Dim inputCond As String inputCond = "gclj=M16 AND (azxs=A0 OR azxs=AT) AND (lcfw=M01 OR lcfw=M15)" ' 预处理 Dim preprocessed As String preprocessed = M05_PreProcessor.PreprocessCondition(inputCond, "接头", 1) ' Debug output Debug.Print "PP06 Debug:" Debug.Print " Input: " & inputCond Debug.Print " Expected: gclj=M16 AND azxs=径向 AND (lcfw=低压 OR lcfw=高压)" Debug.Print " Actual: " & preprocessed ' 解析预处理后的条件 Dim col As Collection Set col = M03_Logic.ParseRule(preprocessed, 1) Assert_NotNull col, "PP06_Result_Not_Null" Assert_Equal col.count, 2, "PP06_Count_After_Preprocessing" Dim row As Object Set row = col(1) Assert_Equal row("gclj"), "M16", "PP06_gclj_Value" Assert_Equal row("azxs"), "径向", "PP06_azxs_Mapped_Value" Assert_Equal row("lcfw"), "低压", "PP06_lcfw_Mapped_Value" End Sub ' ------------------------------------------------------------------------------ ' 测试用例 PP_07: 测试非"接头"/"部件"类别 ' 验证其他类别(如"弹性元件")不受预处理影响 ' ------------------------------------------------------------------------------ Private Sub Test_PP_07_NonJointCategory() SetupPreProcessorTest Dim inputCond As String inputCond = "gclj=M20 AND lcfw=M01" ' 使用"弹性元件"类别 Dim result As String result = M05_PreProcessor.PreprocessCondition(inputCond, "弹性元件", 1) ' 应该保持不变 Assert_Equal result, inputCond, "PP07_NonJoint_Unchanged" End Sub ' ------------------------------------------------------------------------------ ' 测试用例 PP_08: 测试未映射的值 ' 验证不在映射表中的值保持原样并记录警告 ' ------------------------------------------------------------------------------ Private Sub Test_PP_08_UnmappedValues() SetupPreProcessorTest ' 重置logger以捕获警告 Set m_Logger = New clsErrorLogger M05_PreProcessor.InitPreProcessor m_Logger, m_wsMapping Dim result As String result = M05_PreProcessor.PreprocessCondition("lcfw=INVALID", "接头", 1) ' 值应该保持不变 Assert_Equal result, "lcfw=INVALID", "PP08_Unmapped_Value_Unchanged" ' 应该记录警告(如果实现了) ' 注意:这个测试可能需要根据实际日志记录行为调整 End Sub ' ------------------------------------------------------------------------------ ' 测试用例 PP_09: 测试"部件"类别的azxs映射 ' ------------------------------------------------------------------------------ Private Sub Test_PP_09_ComponentCategory_AzxsMapping() SetupPreProcessorTest Dim result1 As String result1 = M05_PreProcessor.PreprocessCondition("azxs=A0 AND gclj=M20", "部件", 1) Assert_Equal result1, "azxs=径向 AND gclj=M20", "PP09_Component_Azxs_Mapping" Dim result2 As String result2 = M05_PreProcessor.PreprocessCondition("gclj=M20 AND azxs=B0", "部件", 2) Assert_Equal result2, "gclj=M20 AND azxs=下轴向", "PP09_Component_Azxs_With_AND" End Sub ' ------------------------------------------------------------------------------ ' 测试用例 PP_10: 测试"部件"类别的lcfw条件保持不变 ' ------------------------------------------------------------------------------ Private Sub Test_PP_10_ComponentCategory_LcfwUnchanged() SetupPreProcessorTest Dim result1 As String result1 = M05_PreProcessor.PreprocessCondition("lcfw=M01 AND gclj=M20", "部件", 1) Assert_Equal result1, "lcfw=M01 AND gclj=M20", "PP10_Component_Lcfw_Unchanged" Dim result2 As String result2 = M05_PreProcessor.PreprocessCondition("gclj=M20 AND lcfw=M01", "部件", 2) Assert_Equal result2, "gclj=M20 AND lcfw=M01", "PP10_Component_Lcfw_Unchanged_Reverse" End Sub ' ------------------------------------------------------------------------------ ' 测试用例 PP_11: 测试"部件"类别的OR条件合并 ' ------------------------------------------------------------------------------ Private Sub Test_PP_11_ComponentCategory_ORMerging() SetupPreProcessorTest Dim result1 As String result1 = M05_PreProcessor.PreprocessCondition("(azxs=A0 OR azxs=AT) AND gclj=M20", "部件", 1) Assert_Equal result1, "azxs=径向 AND gclj=M20", "PP11_Component_OR_Merging_Azxs" ' 测试精确重复的OR条件合并 Dim result2 As String result2 = M05_PreProcessor.PreprocessCondition("azxs=径向 OR azxs=径向", "部件", 2) Assert_Equal result2, "azxs=径向", "PP11_Component_Exact_Duplicate_Merging" End Sub ' ------------------------------------------------------------------------------ ' 测试用例 PP_12: 测试"部件"类别的括号简化 ' ------------------------------------------------------------------------------ Private Sub Test_PP_12_ComponentCategory_ParenthesesSimplification() SetupPreProcessorTest Dim result1 As String result1 = M05_PreProcessor.PreprocessCondition("(azxs=A0 OR azxs=AT) AND gclj=M20", "部件", 1) Assert_Equal result1, "azxs=径向 AND gclj=M20", "PP12_Component_Parentheses_Simplified" Dim result2 As String result2 = M05_PreProcessor.PreprocessCondition("gclj=M20 AND (azxs=A0 OR azxs=AT)", "部件", 2) Assert_Equal result2, "gclj=M20 AND azxs=径向", "PP12_Component_Parentheses_Simplified_Reverse" End Sub ' ------------------------------------------------------------------------------ ' 辅助函数:设置预处理测试环境 ' ------------------------------------------------------------------------------ Private Sub SetupPreProcessorTest() ' 查找[对照表]工作表 On Error Resume Next Set m_wsMapping = ActiveWorkbook.Sheets("对照表") On Error GoTo 0 If m_wsMapping Is Nothing Then Debug.Print " [SKIP] 预处理测试 - 未找到[对照表]工作表" Exit Sub End If ' 初始化预处理器 Set m_Logger = New clsErrorLogger M03_Logic.InitLogic m_Logger M05_PreProcessor.InitPreProcessor m_Logger, m_wsMapping End Sub