feat: add core VBA source code modules
Add VBA directory with essential project code including: - ClassModules: BomExtractor, BomItem, ConditionEvaluator, ProductModelParser - Modules: MainModule, TestModule - Forms and DocumentModules - vba_metadata.json for module metadata Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
338
VBA/Modules/TestModule.bas
Normal file
338
VBA/Modules/TestModule.bas
Normal file
@@ -0,0 +1,338 @@
|
||||
'=====================================================================
|
||||
' 模块名: TestModule
|
||||
' 功能: 单元测试模块
|
||||
' 作者: Auto-generated
|
||||
' 日期: 2025-01-29
|
||||
'=====================================================================
|
||||
|
||||
Option Explicit
|
||||
|
||||
'=====================================================================
|
||||
' 过程: RunAllTests
|
||||
' 功能: 运行所有测试
|
||||
'=====================================================================
|
||||
Public Sub RunAllTests()
|
||||
Debug.Print "=========================================="
|
||||
Debug.Print "开始运行所有测试"
|
||||
Debug.Print "时间: " & Now
|
||||
Debug.Print "=========================================="
|
||||
Debug.Print ""
|
||||
|
||||
' 运行各个测试
|
||||
TestProductModelParser
|
||||
TestConditionEvaluator
|
||||
TestBomExtractor
|
||||
|
||||
Debug.Print ""
|
||||
Debug.Print "=========================================="
|
||||
Debug.Print "所有测试完成"
|
||||
Debug.Print "=========================================="
|
||||
|
||||
MsgBox "所有测试完成,请查看立即窗口查看结果", vbInformation
|
||||
End Sub
|
||||
|
||||
'=====================================================================
|
||||
' 过程: TestProductModelParser
|
||||
' 功能: 测试产品型号解析器
|
||||
'=====================================================================
|
||||
Public Sub TestProductModelParser()
|
||||
Debug.Print ">>> 测试 ProductModelParser"
|
||||
Debug.Print ""
|
||||
|
||||
Dim parser As ProductModelParser
|
||||
Set parser = New ProductModelParser
|
||||
|
||||
' 测试用例1: 正常型号
|
||||
Debug.Print "测试用例1: 正常型号"
|
||||
Dim testModel1 As String
|
||||
testModel1 = "YTHN-100.A0.531.M203.M06.Y3|BP-088.2312.M06.0A3"
|
||||
|
||||
If parser.Parse(testModel1) Then
|
||||
Debug.Print " 解析成功"
|
||||
Debug.Print " 表头型号: " & parser.HeaderModel
|
||||
Debug.Print " 条件:"
|
||||
Debug.Print " azxs = " & parser.GetConditionValue("azxs")
|
||||
Debug.Print " bkxs = " & parser.GetConditionValue("bkxs")
|
||||
Debug.Print " gclj = " & parser.GetConditionValue("gclj")
|
||||
Debug.Print " jycz = " & parser.GetConditionValue("jycz")
|
||||
Debug.Print " lcfw = " & parser.GetConditionValue("lcfw")
|
||||
|
||||
' 验证结果
|
||||
AssertEquals "azxs", "A0", parser.GetConditionValue("azxs")
|
||||
AssertEquals "bkxs", "531", parser.GetConditionValue("bkxs")
|
||||
AssertEquals "gclj", "M20", parser.GetConditionValue("gclj")
|
||||
AssertEquals "jycz", "3", parser.GetConditionValue("jycz")
|
||||
AssertEquals "lcfw", "M06", parser.GetConditionValue("lcfw")
|
||||
Else
|
||||
Debug.Print " 解析失败: " & parser.ErrorMessage
|
||||
End If
|
||||
Debug.Print ""
|
||||
|
||||
' 测试用例2: 不同材质代码
|
||||
Debug.Print "测试用例2: 不同材质代码"
|
||||
Dim testModel2 As String
|
||||
testModel2 = "YTHN-100.BZ.531.M201.M09.Y3|BP-088.2312.M37.0A3"
|
||||
|
||||
If parser.Parse(testModel2) Then
|
||||
Debug.Print " 解析成功"
|
||||
Debug.Print " gclj = " & parser.GetConditionValue("gclj")
|
||||
Debug.Print " jycz = " & parser.GetConditionValue("jycz")
|
||||
|
||||
AssertEquals "gclj", "M20", parser.GetConditionValue("gclj")
|
||||
AssertEquals "jycz", "1", parser.GetConditionValue("jycz")
|
||||
Else
|
||||
Debug.Print " 解析失败: " & parser.ErrorMessage
|
||||
End If
|
||||
Debug.Print ""
|
||||
|
||||
' 测试用例3: 带附件的型号
|
||||
Debug.Print "测试用例3: 带附件的型号"
|
||||
Dim testModel3 As String
|
||||
testModel3 = "YTHN-100.A0.531.G123.M04.Y3|BP-088.2312.B09.0A3"
|
||||
|
||||
If parser.Parse(testModel3) Then
|
||||
Debug.Print " 解析成功"
|
||||
Debug.Print " gclj = " & parser.GetConditionValue("gclj")
|
||||
Debug.Print " jycz = " & parser.GetConditionValue("jycz")
|
||||
|
||||
AssertEquals "gclj", "G12", parser.GetConditionValue("gclj")
|
||||
AssertEquals "jycz", "3", parser.GetConditionValue("jycz")
|
||||
Else
|
||||
Debug.Print " 解析失败: " & parser.ErrorMessage
|
||||
End If
|
||||
Debug.Print ""
|
||||
|
||||
Debug.Print "<<< ProductModelParser 测试完成"
|
||||
Debug.Print ""
|
||||
End Sub
|
||||
|
||||
'=====================================================================
|
||||
' 过程: TestConditionEvaluator
|
||||
' 功能: 测试条件评估器
|
||||
'=====================================================================
|
||||
Public Sub TestConditionEvaluator()
|
||||
Debug.Print ">>> 测试 ConditionEvaluator"
|
||||
Debug.Print ""
|
||||
|
||||
Dim evaluator As ConditionEvaluator
|
||||
Set evaluator = New ConditionEvaluator
|
||||
|
||||
' 创建测试条件字典
|
||||
Dim Conditions As Object
|
||||
Set Conditions = CreateObject("Scripting.Dictionary")
|
||||
Conditions.Add "azxs", "A0"
|
||||
Conditions.Add "bkxs", "531"
|
||||
Conditions.Add "gclj", "M20"
|
||||
Conditions.Add "jycz", "3"
|
||||
Conditions.Add "lcfw", "M06"
|
||||
|
||||
' 测试用例1: 简单等式
|
||||
Debug.Print "测试用例1: 简单等式"
|
||||
Dim expr1 As String
|
||||
expr1 = "azxs=A0"
|
||||
Debug.Print " 表达式: " & expr1
|
||||
Debug.Print " 结果: " & evaluator.Evaluate(expr1, Conditions)
|
||||
AssertTrue "简单等式", evaluator.Evaluate(expr1, Conditions)
|
||||
Debug.Print ""
|
||||
|
||||
' 测试用例2: AND运算
|
||||
Debug.Print "测试用例2: AND运算"
|
||||
Dim expr2 As String
|
||||
expr2 = "azxs=A0 AND bkxs=531"
|
||||
Debug.Print " 表达式: " & expr2
|
||||
Debug.Print " 结果: " & evaluator.Evaluate(expr2, Conditions)
|
||||
AssertTrue "AND运算", evaluator.Evaluate(expr2, Conditions)
|
||||
Debug.Print ""
|
||||
|
||||
' 测试用例3: OR运算
|
||||
Debug.Print "测试用例3: OR运算"
|
||||
Dim expr3 As String
|
||||
expr3 = "azxs=AT OR azxs=A0"
|
||||
Debug.Print " 表达式: " & expr3
|
||||
Debug.Print " 结果: " & evaluator.Evaluate(expr3, Conditions)
|
||||
AssertTrue "OR运算", evaluator.Evaluate(expr3, Conditions)
|
||||
Debug.Print ""
|
||||
|
||||
' 测试用例4: !=运算
|
||||
Debug.Print "测试用例4: !=运算"
|
||||
Dim expr4 As String
|
||||
expr4 = "azxs!=AH"
|
||||
Debug.Print " 表达式: " & expr4
|
||||
Debug.Print " 结果: " & evaluator.Evaluate(expr4, Conditions)
|
||||
AssertTrue "!=运算", evaluator.Evaluate(expr4, Conditions)
|
||||
Debug.Print ""
|
||||
|
||||
' 测试用例5: 复杂嵌套
|
||||
Debug.Print "测试用例5: 复杂嵌套"
|
||||
Dim expr5 As String
|
||||
expr5 = "(azxs=A0 OR azxs=AT) AND (bkxs=531 OR bkxs=541)"
|
||||
Debug.Print " 表达式: " & expr5
|
||||
Debug.Print " 结果: " & evaluator.Evaluate(expr5, Conditions)
|
||||
AssertTrue "复杂嵌套", evaluator.Evaluate(expr5, Conditions)
|
||||
Debug.Print ""
|
||||
|
||||
' 测试用例6: 不存在的变量(!=情况)
|
||||
Debug.Print "测试用例6: 不存在的变量(!=情况)"
|
||||
Dim expr6 As String
|
||||
expr6 = "tsyq!=SCRJ"
|
||||
Debug.Print " 表达式: " & expr6
|
||||
Debug.Print " 结果: " & evaluator.Evaluate(expr6, Conditions)
|
||||
AssertTrue "不存在的变量!=", evaluator.Evaluate(expr6, Conditions)
|
||||
Debug.Print ""
|
||||
|
||||
' 测试用例7: 实际BOM条件
|
||||
Debug.Print "测试用例7: 实际BOM条件"
|
||||
Dim expr7 As String
|
||||
expr7 = "gclj=M20 AND jycz=1 AND lcfw=M01 AND (azxs=A0 OR azxs=AT OR azxs=AH)"
|
||||
Debug.Print " 表达式: " & expr7
|
||||
Debug.Print " 结果: " & evaluator.Evaluate(expr7, Conditions)
|
||||
' 这个应该是False,因为jycz=3,不是1
|
||||
AssertFalse "实际BOM条件(应该False)", evaluator.Evaluate(expr7, Conditions)
|
||||
Debug.Print ""
|
||||
|
||||
Debug.Print "<<< ConditionEvaluator 测试完成"
|
||||
Debug.Print ""
|
||||
End Sub
|
||||
|
||||
'=====================================================================
|
||||
' 过程: TestBomExtractor
|
||||
' 功能: 测试BOM提取器(需要实际的工作表数据)
|
||||
'=====================================================================
|
||||
Public Sub TestBomExtractor()
|
||||
Debug.Print ">>> 测试 BomExtractor"
|
||||
Debug.Print ""
|
||||
|
||||
On Error Resume Next
|
||||
Dim bomSheet As Worksheet
|
||||
Set bomSheet = ThisWorkbook.Worksheets("平台配置清单")
|
||||
|
||||
If bomSheet Is Nothing Then
|
||||
Debug.Print "警告: 未找到'平台配置清单'工作表,跳过BomExtractor测试"
|
||||
Debug.Print ""
|
||||
Exit Sub
|
||||
End If
|
||||
On Error GoTo 0
|
||||
|
||||
Dim extractor As BomExtractor
|
||||
Set extractor = New BomExtractor
|
||||
extractor.SetWorksheet bomSheet
|
||||
|
||||
If Not extractor.LoadBomData Then
|
||||
Debug.Print "加载BOM数据失败: " & extractor.GetErrorSummary
|
||||
Debug.Print ""
|
||||
Exit Sub
|
||||
End If
|
||||
|
||||
Debug.Print "BOM数据加载成功"
|
||||
Debug.Print ""
|
||||
|
||||
' 测试用例: 提取BOM
|
||||
Debug.Print "测试用例: 提取BOM"
|
||||
Dim testConditions As Object
|
||||
Set testConditions = CreateObject("Scripting.Dictionary")
|
||||
testConditions.Add "azxs", "A0"
|
||||
testConditions.Add "bkxs", "531"
|
||||
testConditions.Add "gclj", "M20"
|
||||
testConditions.Add "jycz", "1"
|
||||
testConditions.Add "lcfw", "M01"
|
||||
|
||||
Dim matchedItems As collection
|
||||
Set matchedItems = extractor.ExtractBom(testConditions)
|
||||
|
||||
Debug.Print " 匹配到 " & matchedItems.Count & " 个物料"
|
||||
|
||||
If matchedItems.Count > 0 Then
|
||||
Debug.Print " 匹配的物料:"
|
||||
Dim item As BomItem
|
||||
Dim i As Long
|
||||
i = 1
|
||||
For Each item In matchedItems
|
||||
Debug.Print " " & i & ". " & item.ToString
|
||||
i = i + 1
|
||||
Next item
|
||||
End If
|
||||
|
||||
Dim errors As String
|
||||
errors = extractor.GetErrorSummary
|
||||
If errors <> "" Then
|
||||
Debug.Print " 错误信息: " & errors
|
||||
End If
|
||||
|
||||
Debug.Print ""
|
||||
Debug.Print "<<< BomExtractor 测试完成"
|
||||
Debug.Print ""
|
||||
End Sub
|
||||
|
||||
'=====================================================================
|
||||
' 辅助测试函数
|
||||
'=====================================================================
|
||||
|
||||
Private Sub AssertEquals(testName As String, expected As String, actual As String)
|
||||
If expected = actual Then
|
||||
Debug.Print " PASS: " & testName
|
||||
Else
|
||||
Debug.Print " FAIL: " & testName & " (期望:" & expected & ", 实际:" & actual & ")"
|
||||
End If
|
||||
End Sub
|
||||
|
||||
Private Sub AssertTrue(testName As String, value As Boolean)
|
||||
If value Then
|
||||
Debug.Print " PASS: " & testName
|
||||
Else
|
||||
Debug.Print " FAIL: " & testName & " (期望:True, 实际:False)"
|
||||
End If
|
||||
End Sub
|
||||
|
||||
Private Sub AssertFalse(testName As String, value As Boolean)
|
||||
If Not value Then
|
||||
Debug.Print " PASS: " & testName
|
||||
Else
|
||||
Debug.Print " FAIL: " & testName & " (期望:False, 实际:True)"
|
||||
End If
|
||||
End Sub
|
||||
|
||||
'=====================================================================
|
||||
' 过程: TestWithProvidedModels
|
||||
' 功能: 使用提供的测试型号进行测试
|
||||
'=====================================================================
|
||||
Public Sub TestWithProvidedModels()
|
||||
Debug.Print "=========================================="
|
||||
Debug.Print "使用提供的测试型号进行测试"
|
||||
Debug.Print "=========================================="
|
||||
Debug.Print ""
|
||||
|
||||
Dim testModels() As String
|
||||
testModels = Split( _
|
||||
"YTHN-100.A0.531.G123.M04.Y3|BP-088.2312.B09.0A3," & _
|
||||
"YTHN-100.BZ.531.M201.M09.Y3|BP-088.2312.M37.0A3," & _
|
||||
"YTHN-100.BZ.531.M201.M08.Y3|BP-088.2312.M08.0A3," & _
|
||||
"YTHN-100.A0.531.M201.M08.Y3|BP-088.2312.M08.0B3," & _
|
||||
"YTHN-100.A0.531.M203.M06.Y3|BP-088.2312.M06.0A3|LSG-1.14x2.M20F.M20.3^HDJ.M20F.BW.14×2×60.3^TSFJ^WHP.70X20X1.3," & _
|
||||
"YTHN-100.A0.531.M203.P21.Y3|BP-088.2312.M39.0A3|HDJ.M20F.BW.14×2×60.3^LSG-1.14x2.M20F.M20.3^TSFJ^WHP.70X20X1.3," & _
|
||||
"YTHN-100.A0.531.M201.M03.N1.Y3|BP-088.2312.M31.0A4," & _
|
||||
"YTHN-100.A0.531.M201.M04.Y3|BP-088.2312.M32.0A3," & _
|
||||
"YTHN-100.A0.531.Z121.M07.Y3|BP-088.2312.M07.0A3," & _
|
||||
"YTHN-100.A0.531.Z121.M08.Y3|BP-088.2312.M08.0A3", _
|
||||
",")
|
||||
|
||||
Dim parser As ProductModelParser
|
||||
Set parser = New ProductModelParser
|
||||
|
||||
Dim i As Long
|
||||
For i = LBound(testModels) To UBound(testModels)
|
||||
Debug.Print "型号 " & (i + 1) & ": " & testModels(i)
|
||||
|
||||
If parser.Parse(testModels(i)) Then
|
||||
Debug.Print " 解析成功"
|
||||
Debug.Print " 表头: " & parser.HeaderModel
|
||||
Debug.Print " 条件: " & parser.GetAllConditions
|
||||
Else
|
||||
Debug.Print " 解析失败: " & parser.ErrorMessage
|
||||
End If
|
||||
Debug.Print ""
|
||||
Next i
|
||||
|
||||
Debug.Print "=========================================="
|
||||
Debug.Print "测试完成"
|
||||
Debug.Print "=========================================="
|
||||
End Sub
|
||||
Reference in New Issue
Block a user