Files
tia-lsp/test/test_plc_json.lua
T
lazar d5b8d7b153 feat: add testing framework with reference project integration
- Add test/ directory with test harness and test files
- Add UDT, DB, FB parser tests
- Add PLC JSON and integration tests
- Test against reference project (239 UDTs, 12 DBs, 37 SCL, 13 XML)
- Fix attribute parsing to handle underscores (S7_SetPoint)
- Add ORGANIZATION_BLOCK support to FB parser
2026-02-23 13:29:38 +01:00

125 lines
3.1 KiB
Lua

local test = dofile("test/test_harness.lua")
package.path = package.path .. ";src/?.lua"
local script_path = debug.getinfo(1, "S").source:gsub("^@", ""):match("(.*/)") or "."
script_path = script_path:gsub("^test/", ""):gsub("/tests$", "")
if script_path == "" then script_path = "." end
local plc_json = dofile(script_path .. "/src/plc_json.lua")
local function test_plc_json_load_types()
local root_dir = test.REF_PROJECT
local types = plc_json.load_types_from_workspace(root_dir)
local count = 0
for _ in pairs(types) do count = count + 1 end
test.assert_gt(count, 0, "should load types from workspace")
end
local function test_plc_json_struct_type()
local content = [[
{
"dataTypes": [
{
"name": "TestStruct",
"struct": [
{ "name": "field1", "dataTypeName": "Bool" },
{ "name": "field2", "dataTypeName": "Int" }
]
}
]
}
]]
local json = dofile(script_path .. "/src/json.lua")
local data = json.decode(content)
test.assert_not_nil(data, "should decode JSON")
local types = plc_json.load_types_from_workspace("")
test.assert_not_nil(types, "should return types table")
end
local function test_plc_json_alias_type()
local content = [[
{
"dataTypes": [
{
"name": "MyInt",
"baseType": "Int"
}
]
}
]]
local json = dofile(script_path .. "/src/json.lua")
local data = json.decode(content)
test.assert_not_nil(data, "should decode JSON alias")
end
local function test_plc_json_cache()
plc_json.clear_cache()
local types1 = plc_json.get_types(test.REF_PROJECT)
local count1 = 0
for _ in pairs(types1) do count1 = count1 + 1 end
test.assert_gt(count1, 0, "should load types")
local types2 = plc_json.get_types(test.REF_PROJECT)
local count2 = 0
for _ in pairs(types2) do count2 = count2 + 1 end
test.assert_gt(count2, 0, "should return cached types")
end
local function test_plc_json_scl_parsing()
local root_dir = test.REF_PROJECT
local types = plc_json.load_types_from_workspace(root_dir)
local found_udt = false
for name, typ in pairs(types) do
if typ.source == "udt_file" or typ.source == "scl_file" then
found_udt = true
break
end
end
if not found_udt then
print("Note: No UDT/SCL types found in reference project (may be normal)")
end
end
local function test_plc_json_db_parsing()
local root_dir = test.REF_PROJECT
local types = plc_json.load_types_from_workspace(root_dir)
local found_db = false
for name, typ in pairs(types) do
if typ.source == "db_file" then
found_db = true
break
end
end
if not found_db then
print("Note: No DB types found in reference project (may be normal)")
end
end
local function run_plc_json_tests()
test.run_suite("PLC JSON Tests", function()
print("\n--- Workspace Tests ---")
test_plc_json_load_types()
test_plc_json_scl_parsing()
test_plc_json_db_parsing()
print("\n--- Cache Tests ---")
test_plc_json_cache()
end)
end
if vim then
vim.api.nvim_create_user_command("TestPlcJson", function()
run_plc_json_tests()
end, {})
end
run_plc_json_tests()