Skip to content

Commit 0179232

Browse files
authored
chore(test): use mocking for timer (#33)
1 parent 4ca9f60 commit 0179232

4 files changed

Lines changed: 170 additions & 57 deletions

File tree

apax-lock.json

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
},
1313
"devDependencies": {
1414
"@ax/sdk": "^2504.0.0",
15-
"@simatic-ax/snippetscollection": "^1.0.0"
15+
"@simatic-ax/snippetscollection": "^1.0.0",
16+
"@ax/axunit-mocking": "^8.0.33"
1617
},
1718
"catalogs": {
1819
"@ax/simatic-ax": "^2504.0.0"
@@ -50,6 +51,17 @@
5051
"@ax/trace": "2.9.0"
5152
}
5253
},
54+
"@ax/axunit-mocking": {
55+
"name": "@ax/axunit-mocking",
56+
"version": "8.0.33",
57+
"integrity": "sha512-lv5kR6zmh1kBaAoeDwhx2/WiXkenf8QTRY04xVTv1vbM55V1eLq/B4L2Zp7muPGBmyhlck1rJ12IGiv0LuBi6Q==",
58+
"resolved": "https://registry.simatic-ax.siemens.io/@ax/axunit-mocking/-/axunit-mocking-8.0.33.tgz",
59+
"dependencies": {
60+
"@ax/target-mocking": "8.0.33",
61+
"@ax/mocking-library": "8.0.33",
62+
"@ax/mocking-ls-contrib": "8.0.33"
63+
}
64+
},
5365
"@ax/system-timer": {
5466
"name": "@ax/system-timer",
5567
"version": "10.0.24",
@@ -295,6 +307,30 @@
295307
"@ax/trace-linux-x64": "2.9.0"
296308
}
297309
},
310+
"@ax/target-mocking": {
311+
"name": "@ax/target-mocking",
312+
"version": "8.0.33",
313+
"integrity": "sha512-2lFVgj16QNoFgDVMoa+9vL4+i8+D47VIP73ENEChMfea+5UOBZ/zAcy4VGQiJ5wqLOirkk3NHLjyJCrp0e45iQ==",
314+
"resolved": "https://registry.simatic-ax.siemens.io/@ax/target-mocking/-/target-mocking-8.0.33.tgz",
315+
"dependencies": {
316+
"@ax/target-mocking-linux-x64": "8.0.33",
317+
"@ax/target-mocking-win-x64": "8.0.33"
318+
}
319+
},
320+
"@ax/mocking-library": {
321+
"name": "@ax/mocking-library",
322+
"version": "8.0.33",
323+
"integrity": "sha512-QO3YsqI4JTrYSmWaGC8Rl/X7ks97EVT9++Y3h4rTO3ZFp7GATKyN6KokQoq1QDwKdODIR7TLtqGymsBdwL0tzQ==",
324+
"resolved": "https://registry.simatic-ax.siemens.io/@ax/mocking-library/-/mocking-library-8.0.33.tgz",
325+
"dependencies": {}
326+
},
327+
"@ax/mocking-ls-contrib": {
328+
"name": "@ax/mocking-ls-contrib",
329+
"version": "8.0.33",
330+
"integrity": "sha512-Rhd89jqT/yPic9eCydHF/1F66xNNc+r+84aaoiSsx3Fyvey402ML0jsYcxwfx7SFoNJ8uOMCk1NPzljQd8Aqeg==",
331+
"resolved": "https://registry.simatic-ax.siemens.io/@ax/mocking-ls-contrib/-/mocking-ls-contrib-8.0.33.tgz",
332+
"dependencies": {}
333+
},
298334
"@ax/system-math": {
299335
"name": "@ax/system-math",
300336
"version": "10.0.24",
@@ -621,6 +657,32 @@
621657
],
622658
"dependencies": {}
623659
},
660+
"@ax/target-mocking-linux-x64": {
661+
"name": "@ax/target-mocking-linux-x64",
662+
"version": "8.0.33",
663+
"integrity": "sha512-q+q0t7I0dpNGQIaEPJaAws4L5IbXfPGed3y08vbVYT7I6TEvGW0Pu40ZOJikvRCQ8SgBwiLycKgYzpFgkwB+VQ==",
664+
"resolved": "https://registry.simatic-ax.siemens.io/@ax/target-mocking-linux-x64/-/target-mocking-linux-x64-8.0.33.tgz",
665+
"os": [
666+
"linux"
667+
],
668+
"cpu": [
669+
"x64"
670+
],
671+
"dependencies": {}
672+
},
673+
"@ax/target-mocking-win-x64": {
674+
"name": "@ax/target-mocking-win-x64",
675+
"version": "8.0.33",
676+
"integrity": "sha512-7UZ7LKBq4Pbw/PkbQ0C4+lwH1KKcsyUmOcYP7quXypJB7lbZpdkiuF+xF1osHs07iIJl1wAg3dQ1iuMsE0D66A==",
677+
"resolved": "https://registry.simatic-ax.siemens.io/@ax/target-mocking-win-x64/-/target-mocking-win-x64-8.0.33.tgz",
678+
"os": [
679+
"win32"
680+
],
681+
"cpu": [
682+
"x64"
683+
],
684+
"dependencies": {}
685+
},
624686
"@ax/st-docs": {
625687
"name": "@ax/st-docs",
626688
"version": "10.0.85",

apax.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ description: This library contains a collection of signal generators
88
repository:
99
type: git
1010
url: https://github.com/simatic-ax/generators
11-
11+
1212
# Environment variables
1313
variables:
1414
APAX_BUILD_ARGS:
@@ -19,13 +19,15 @@ targets:
1919
# Registry information, where the package will be published
2020
registries:
2121
'@simatic-ax': 'https://npm.pkg.github.com/'
22-
22+
2323
# Dependencies
2424
catalogs:
2525
"@ax/simatic-ax": ^2504.0.0
2626
devDependencies:
2727
"@ax/sdk": ^2504.0.0
2828
"@simatic-ax/snippetscollection": ^1.0.0
29+
"@ax/axunit-mocking": ^8.0.33
30+
# "@simatic-ax/mocks": ^3.0.0
2931
dependencies:
3032
"@simatic-ax/io": ^9.0.0
3133
"@ax/system-timer": ^10.0.24

test/OnDelayMockWithTwoTimers.st

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
NAMESPACE Simatic.Ax.Mocks
2+
/// This Mock can be used in Classes/FunctionBlocks where are two OnDelay instances are used
3+
{axcode:docs-v0:@simatic-ax/generators:OnDelayMockWithTwoTimers.md}
4+
FUNCTION_BLOCK OnDelayMock2Timer
5+
VAR
6+
iecTimer : ARRAY[0..27] of BYTE;
7+
END_VAR
8+
VAR_INPUT
9+
duration : LTIME;
10+
signal : BOOL;
11+
END_VAR
12+
VAR_OUTPUT
13+
output : BOOL;
14+
elapsedTime : LTIME;
15+
END_VAR
16+
VAR_TEMP
17+
state : REF_TO OnDelayMockState;
18+
END_VAR
19+
VAR PRIVATE
20+
END_VAR
21+
state ?= AxUnit.Mocking.GetPayload();
22+
output := TRUE;
23+
24+
IF (signal = FALSE) THEN
25+
output := FALSE;
26+
ELSE
27+
IF duration = T#1s THEN
28+
output := state^.StatusTimer1;
29+
END_IF;
30+
IF duration = T#2s THEN
31+
output := state^.StatusTimer2 ;
32+
END_IF;
33+
END_IF;
34+
35+
END_FUNCTION_BLOCK
36+
37+
/// The returned states for the Timer1 and Timer2
38+
{axcode:docs-v0:@simatic-ax/generators:OnDelayMockWithTwoTimers.md}
39+
CLASS OnDelayMockState IMPLEMENTS AxUnit.Mocking.IPayload
40+
VAR PUBLIC
41+
StatusTimer1 : BOOL;
42+
StatusTimer2 : BOOL;
43+
END_VAR
44+
END_CLASS
45+
END_NAMESPACE
46+
47+
48+
49+

test/TestOnCycle.st

Lines changed: 54 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,62 @@
1-
// USING Simatic.Ax.Generators;
2-
// USING Simatic.Ax.Mocks;
3-
4-
// NAMESPACE Simatic.Ax.Generators.Test
5-
// {TestFixture}
6-
// CLASS TestActiveWait
7-
// VAR
8-
// _state : OnDelayMockState;
9-
// testInstance : PulseGenerator;
10-
// END_VAR
1+
USING Simatic.Ax.Generators;
2+
USING Simatic.Ax.Mocks;
3+
USING System.Timer;
4+
NAMESPACE Simatic.Ax.Generators.Test
5+
{TestFixture}
6+
CLASS TestActiveWait
7+
VAR
8+
_state : OnDelayMockState;
9+
testInstance : PulseGenerator;
10+
END_VAR
1111

12-
// {Test}
13-
// METHOD PUBLIC Test_One_Clock_Cycle_Expect_True_False_True
14-
// VAR_TEMP
15-
// res : BOOL;
16-
// END_VAR
17-
// testInstance.PulseTime := T#1s; // give the mocked timer an value to distiguish in the mock
18-
// testInstance.PauseTime := T#2s;
19-
20-
// // Initial state --> Clock returns true, no timer is running
21-
// _state.StatusTimer1 := FALSE;
22-
// _state.StatusTimer2 := FALSE;
23-
24-
// AxUnit.Mocking.Mock('System.Timer.OnDelay', NAME_OF(OnDelayMock2Timer), THIS._state);
25-
// testInstance.Execute();
26-
// res := testInstance.Q();
27-
// AxUnit.Assert.Equal(TRUE, res);
12+
{Test}
13+
METHOD PUBLIC Test_One_Clock_Cycle_Expect_True_False_True
14+
VAR_TEMP
15+
res : BOOL;
16+
END_VAR
17+
testInstance.PulseTime := T#1s; // give the mocked timer an value to distiguish in the mock
18+
testInstance.PauseTime := T#2s;
19+
20+
// Initial state --> Clock returns true, no timer is running
21+
_state.StatusTimer1 := FALSE;
22+
_state.StatusTimer2 := FALSE;
23+
24+
AxUnit.Mocking.Mock(NAME_OF(OnDelay), NAME_OF(OnDelayMock2Timer), _state);
25+
testInstance.Execute();
26+
res := testInstance.Q();
27+
AxUnit.Assert.Equal(TRUE, res);
2828

29-
// // second cycle --> Clock returns still true
30-
// testInstance.Execute();
31-
32-
// res := testInstance.Q();
33-
// AxUnit.Assert.Equal(TRUE, res);
34-
35-
// // third cycle --> PulseTime has elapsed --> Clock returns false
36-
// _state.StatusTimer1 := TRUE;
37-
// _state.StatusTimer2 := FALSE;
38-
// testInstance.Execute();
39-
// res := testInstance.Q();
40-
// AxUnit.Assert.Equal(FALSE, res);
29+
// second cycle --> Clock returns still true
30+
testInstance.Execute();
31+
32+
res := testInstance.Q();
33+
AxUnit.Assert.Equal(TRUE, res);
34+
35+
// third cycle --> PulseTime has elapsed --> Clock returns false
36+
_state.StatusTimer1 := TRUE;
37+
_state.StatusTimer2 := FALSE;
38+
testInstance.Execute();
39+
res := testInstance.Q();
40+
AxUnit.Assert.Equal(FALSE, res);
4141

42-
// // 4th Cycle --> Clock returns still false. PauseTimer is running but is not elapsed
43-
// _state.StatusTimer1 := FALSE;
44-
// _state.StatusTimer2 := FALSE;
45-
// testInstance.Execute();
46-
// res := testInstance.Q();
47-
// AxUnit.Assert.Equal(FALSE, res);
48-
49-
// // 5th cycle --> PauseTime has elapsed --> Clock returns false
50-
// _state.StatusTimer1 := FALSE;
51-
// _state.StatusTimer2 := TRUE;
52-
// testInstance.Execute();
53-
// res := testInstance.Q();
54-
// AxUnit.Assert.Equal(TRUE, res);
55-
// END_METHOD
42+
// 4th Cycle --> Clock returns still false. PauseTimer is running but is not elapsed
43+
_state.StatusTimer1 := FALSE;
44+
_state.StatusTimer2 := FALSE;
45+
testInstance.Execute();
46+
res := testInstance.Q();
47+
AxUnit.Assert.Equal(FALSE, res);
48+
49+
// 5th cycle --> PauseTime has elapsed --> Clock returns false
50+
_state.StatusTimer1 := FALSE;
51+
_state.StatusTimer2 := TRUE;
52+
testInstance.Execute();
53+
res := testInstance.Q();
54+
AxUnit.Assert.Equal(TRUE, res);
55+
END_METHOD
5656

57-
// END_CLASS
57+
END_CLASS
5858

59-
// END_NAMESPACE
59+
END_NAMESPACE
6060

6161

6262

0 commit comments

Comments
 (0)