From 3e3a9ba989de2bc19331c7df0983fce1223b6760 Mon Sep 17 00:00:00 2001 From: sjuergen Date: Thu, 8 May 2025 23:27:59 +0200 Subject: [PATCH] chore(test): use mocking for timer --- apax-lock.json | 64 +++++++++++++++++- apax.yml | 6 +- test/OnDelayMockWithTwoTimers.st | 49 ++++++++++++++ test/TestOnCycle.st | 108 +++++++++++++++---------------- 4 files changed, 170 insertions(+), 57 deletions(-) create mode 100644 test/OnDelayMockWithTwoTimers.st diff --git a/apax-lock.json b/apax-lock.json index 1035c52..dafe066 100644 --- a/apax-lock.json +++ b/apax-lock.json @@ -12,7 +12,8 @@ }, "devDependencies": { "@ax/sdk": "^2504.0.0", - "@simatic-ax/snippetscollection": "^1.0.0" + "@simatic-ax/snippetscollection": "^1.0.0", + "@ax/axunit-mocking": "^8.0.33" }, "catalogs": { "@ax/simatic-ax": "^2504.0.0" @@ -50,6 +51,17 @@ "@ax/trace": "2.9.0" } }, + "@ax/axunit-mocking": { + "name": "@ax/axunit-mocking", + "version": "8.0.33", + "integrity": "sha512-lv5kR6zmh1kBaAoeDwhx2/WiXkenf8QTRY04xVTv1vbM55V1eLq/B4L2Zp7muPGBmyhlck1rJ12IGiv0LuBi6Q==", + "resolved": "https://registry.simatic-ax.siemens.io/@ax/axunit-mocking/-/axunit-mocking-8.0.33.tgz", + "dependencies": { + "@ax/target-mocking": "8.0.33", + "@ax/mocking-library": "8.0.33", + "@ax/mocking-ls-contrib": "8.0.33" + } + }, "@ax/system-timer": { "name": "@ax/system-timer", "version": "10.0.24", @@ -295,6 +307,30 @@ "@ax/trace-linux-x64": "2.9.0" } }, + "@ax/target-mocking": { + "name": "@ax/target-mocking", + "version": "8.0.33", + "integrity": "sha512-2lFVgj16QNoFgDVMoa+9vL4+i8+D47VIP73ENEChMfea+5UOBZ/zAcy4VGQiJ5wqLOirkk3NHLjyJCrp0e45iQ==", + "resolved": "https://registry.simatic-ax.siemens.io/@ax/target-mocking/-/target-mocking-8.0.33.tgz", + "dependencies": { + "@ax/target-mocking-linux-x64": "8.0.33", + "@ax/target-mocking-win-x64": "8.0.33" + } + }, + "@ax/mocking-library": { + "name": "@ax/mocking-library", + "version": "8.0.33", + "integrity": "sha512-QO3YsqI4JTrYSmWaGC8Rl/X7ks97EVT9++Y3h4rTO3ZFp7GATKyN6KokQoq1QDwKdODIR7TLtqGymsBdwL0tzQ==", + "resolved": "https://registry.simatic-ax.siemens.io/@ax/mocking-library/-/mocking-library-8.0.33.tgz", + "dependencies": {} + }, + "@ax/mocking-ls-contrib": { + "name": "@ax/mocking-ls-contrib", + "version": "8.0.33", + "integrity": "sha512-Rhd89jqT/yPic9eCydHF/1F66xNNc+r+84aaoiSsx3Fyvey402ML0jsYcxwfx7SFoNJ8uOMCk1NPzljQd8Aqeg==", + "resolved": "https://registry.simatic-ax.siemens.io/@ax/mocking-ls-contrib/-/mocking-ls-contrib-8.0.33.tgz", + "dependencies": {} + }, "@ax/system-math": { "name": "@ax/system-math", "version": "10.0.24", @@ -621,6 +657,32 @@ ], "dependencies": {} }, + "@ax/target-mocking-linux-x64": { + "name": "@ax/target-mocking-linux-x64", + "version": "8.0.33", + "integrity": "sha512-q+q0t7I0dpNGQIaEPJaAws4L5IbXfPGed3y08vbVYT7I6TEvGW0Pu40ZOJikvRCQ8SgBwiLycKgYzpFgkwB+VQ==", + "resolved": "https://registry.simatic-ax.siemens.io/@ax/target-mocking-linux-x64/-/target-mocking-linux-x64-8.0.33.tgz", + "os": [ + "linux" + ], + "cpu": [ + "x64" + ], + "dependencies": {} + }, + "@ax/target-mocking-win-x64": { + "name": "@ax/target-mocking-win-x64", + "version": "8.0.33", + "integrity": "sha512-7UZ7LKBq4Pbw/PkbQ0C4+lwH1KKcsyUmOcYP7quXypJB7lbZpdkiuF+xF1osHs07iIJl1wAg3dQ1iuMsE0D66A==", + "resolved": "https://registry.simatic-ax.siemens.io/@ax/target-mocking-win-x64/-/target-mocking-win-x64-8.0.33.tgz", + "os": [ + "win32" + ], + "cpu": [ + "x64" + ], + "dependencies": {} + }, "@ax/st-docs": { "name": "@ax/st-docs", "version": "10.0.85", diff --git a/apax.yml b/apax.yml index 89b717e..f4ce4d8 100644 --- a/apax.yml +++ b/apax.yml @@ -8,7 +8,7 @@ description: This library contains a collection of signal generators repository: type: git url: https://github.com/simatic-ax/generators - + # Environment variables variables: APAX_BUILD_ARGS: @@ -19,13 +19,15 @@ targets: # Registry information, where the package will be published registries: '@simatic-ax': 'https://npm.pkg.github.com/' - + # Dependencies catalogs: "@ax/simatic-ax": ^2504.0.0 devDependencies: "@ax/sdk": ^2504.0.0 "@simatic-ax/snippetscollection": ^1.0.0 + "@ax/axunit-mocking": ^8.0.33 + # "@simatic-ax/mocks": ^3.0.0 dependencies: "@simatic-ax/io": ^9.0.0 "@ax/system-timer": ^10.0.24 diff --git a/test/OnDelayMockWithTwoTimers.st b/test/OnDelayMockWithTwoTimers.st new file mode 100644 index 0000000..ad3beb6 --- /dev/null +++ b/test/OnDelayMockWithTwoTimers.st @@ -0,0 +1,49 @@ +NAMESPACE Simatic.Ax.Mocks + /// This Mock can be used in Classes/FunctionBlocks where are two OnDelay instances are used + {axcode:docs-v0:@simatic-ax/generators:OnDelayMockWithTwoTimers.md} + FUNCTION_BLOCK OnDelayMock2Timer + VAR + iecTimer : ARRAY[0..27] of BYTE; + END_VAR + VAR_INPUT + duration : LTIME; + signal : BOOL; + END_VAR + VAR_OUTPUT + output : BOOL; + elapsedTime : LTIME; + END_VAR + VAR_TEMP + state : REF_TO OnDelayMockState; + END_VAR + VAR PRIVATE + END_VAR + state ?= AxUnit.Mocking.GetPayload(); + output := TRUE; + + IF (signal = FALSE) THEN + output := FALSE; + ELSE + IF duration = T#1s THEN + output := state^.StatusTimer1; + END_IF; + IF duration = T#2s THEN + output := state^.StatusTimer2 ; + END_IF; + END_IF; + + END_FUNCTION_BLOCK + + /// The returned states for the Timer1 and Timer2 + {axcode:docs-v0:@simatic-ax/generators:OnDelayMockWithTwoTimers.md} + CLASS OnDelayMockState IMPLEMENTS AxUnit.Mocking.IPayload + VAR PUBLIC + StatusTimer1 : BOOL; + StatusTimer2 : BOOL; + END_VAR + END_CLASS +END_NAMESPACE + + + + diff --git a/test/TestOnCycle.st b/test/TestOnCycle.st index 6e7de82..53d13c5 100644 --- a/test/TestOnCycle.st +++ b/test/TestOnCycle.st @@ -1,62 +1,62 @@ -// USING Simatic.Ax.Generators; -// USING Simatic.Ax.Mocks; - -// NAMESPACE Simatic.Ax.Generators.Test -// {TestFixture} -// CLASS TestActiveWait -// VAR -// _state : OnDelayMockState; -// testInstance : PulseGenerator; -// END_VAR +USING Simatic.Ax.Generators; +USING Simatic.Ax.Mocks; +USING System.Timer; +NAMESPACE Simatic.Ax.Generators.Test + {TestFixture} + CLASS TestActiveWait + VAR + _state : OnDelayMockState; + testInstance : PulseGenerator; + END_VAR -// {Test} -// METHOD PUBLIC Test_One_Clock_Cycle_Expect_True_False_True -// VAR_TEMP -// res : BOOL; -// END_VAR -// testInstance.PulseTime := T#1s; // give the mocked timer an value to distiguish in the mock -// testInstance.PauseTime := T#2s; - -// // Initial state --> Clock returns true, no timer is running -// _state.StatusTimer1 := FALSE; -// _state.StatusTimer2 := FALSE; - -// AxUnit.Mocking.Mock('System.Timer.OnDelay', NAME_OF(OnDelayMock2Timer), THIS._state); -// testInstance.Execute(); -// res := testInstance.Q(); -// AxUnit.Assert.Equal(TRUE, res); + {Test} + METHOD PUBLIC Test_One_Clock_Cycle_Expect_True_False_True + VAR_TEMP + res : BOOL; + END_VAR + testInstance.PulseTime := T#1s; // give the mocked timer an value to distiguish in the mock + testInstance.PauseTime := T#2s; + + // Initial state --> Clock returns true, no timer is running + _state.StatusTimer1 := FALSE; + _state.StatusTimer2 := FALSE; + + AxUnit.Mocking.Mock(NAME_OF(OnDelay), NAME_OF(OnDelayMock2Timer), _state); + testInstance.Execute(); + res := testInstance.Q(); + AxUnit.Assert.Equal(TRUE, res); -// // second cycle --> Clock returns still true -// testInstance.Execute(); - -// res := testInstance.Q(); -// AxUnit.Assert.Equal(TRUE, res); - -// // third cycle --> PulseTime has elapsed --> Clock returns false -// _state.StatusTimer1 := TRUE; -// _state.StatusTimer2 := FALSE; -// testInstance.Execute(); -// res := testInstance.Q(); -// AxUnit.Assert.Equal(FALSE, res); + // second cycle --> Clock returns still true + testInstance.Execute(); + + res := testInstance.Q(); + AxUnit.Assert.Equal(TRUE, res); + + // third cycle --> PulseTime has elapsed --> Clock returns false + _state.StatusTimer1 := TRUE; + _state.StatusTimer2 := FALSE; + testInstance.Execute(); + res := testInstance.Q(); + AxUnit.Assert.Equal(FALSE, res); -// // 4th Cycle --> Clock returns still false. PauseTimer is running but is not elapsed -// _state.StatusTimer1 := FALSE; -// _state.StatusTimer2 := FALSE; -// testInstance.Execute(); -// res := testInstance.Q(); -// AxUnit.Assert.Equal(FALSE, res); - -// // 5th cycle --> PauseTime has elapsed --> Clock returns false -// _state.StatusTimer1 := FALSE; -// _state.StatusTimer2 := TRUE; -// testInstance.Execute(); -// res := testInstance.Q(); -// AxUnit.Assert.Equal(TRUE, res); -// END_METHOD + // 4th Cycle --> Clock returns still false. PauseTimer is running but is not elapsed + _state.StatusTimer1 := FALSE; + _state.StatusTimer2 := FALSE; + testInstance.Execute(); + res := testInstance.Q(); + AxUnit.Assert.Equal(FALSE, res); + + // 5th cycle --> PauseTime has elapsed --> Clock returns false + _state.StatusTimer1 := FALSE; + _state.StatusTimer2 := TRUE; + testInstance.Execute(); + res := testInstance.Q(); + AxUnit.Assert.Equal(TRUE, res); + END_METHOD -// END_CLASS + END_CLASS -// END_NAMESPACE +END_NAMESPACE