Skip to content

Commit

Permalink
chore(examples/[no ci): Add examples
Browse files Browse the repository at this point in the history
  • Loading branch information
densogiaichned committed Feb 11, 2024
1 parent df48f25 commit 9b987f5
Show file tree
Hide file tree
Showing 11 changed files with 127 additions and 27 deletions.
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,37 @@ During the assertion phase, Snappy performs the following steps:
This discrepancy could indicate either an unexpected change or the need to update the reference snapshot to reflect the new result
4. The validated files can the be added to [source control](#source-control-received-and-verified-files).


## Example using `TcUnit`
```
FUNCTION_BLOCK FB_TcUnitExample EXTENDS FB_TestSuite
VAR
hr : HRESULT;
fbTcUnitAdapter : FB_TcUnitAdapter;
stActual : ST_DemoDataType;
{attribute 'analysis' := '-33'}
stResult : ST_VerificationResult;
END_VAR
```
```
TEST('Test some stuff with TcHaxx.Snappy');
// ARRANGE
// ACT
stActual := F_CreateDemoData();
// ASSERT
hr := fbTcUnitAdapter.Verify(anyArg:= stActual);
IF NOT PENDING(hr) THEN
TEST_FINISHED_NAMED('Test some stuff with TcHaxx.Snappy');
END_IF;
```

> Find more examples in the `examples` PLC project.
## Install

Snappy consists of two parts:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
sString: Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy,
nNumber: 3735928559,
nFloat: -1.2345678,
fFloat: -1.2345678,
aBuffer: [
1,
2,
Expand All @@ -16,7 +16,7 @@
11,
12,
13,
12,
14,
15,
16
]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
sString: Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy,
nNumber: 3735928559,
fFloat: -1.2345678,
aBuffer: [
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16
]
}
2 changes: 1 addition & 1 deletion examples/examples/DUTs/ST_DemoDataType.TcDUT
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
STRUCT
sString : STRING;
nNumber : UDINT;
nFloat : REAL;
fFloat : REAL;
aBuffer : ARRAY[0..15] OF BYTE;
END_STRUCT
END_TYPE
Expand Down
15 changes: 5 additions & 10 deletions examples/examples/POUs/FB_TcUnitExample.TcPOU
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,28 @@
<TcPlcObject Version="1.1.0.1" ProductVersion="3.1.4024.12">
<POU Name="FB_TcUnitExample" Id="{2328aed7-3c8d-44a5-a37e-d87d39d21ab0}" SpecialFunc="None">
<Declaration><![CDATA[FUNCTION_BLOCK FB_TcUnitExample EXTENDS FB_TestSuite
VAR_INPUT
END_VAR
VAR_OUTPUT
END_VAR
VAR
hr : HRESULT;
fbTcUnitAdapter : FB_TcUnitAdapter;
stDemoDT : ST_DemoDataType;
stActual : ST_DemoDataType;
{attribute 'analysis' := '-33'}
stResult : ST_VerificationResult;
END_VAR
]]></Declaration>
<Implementation>
<ST><![CDATA[
TEST('Test some stuff');
<ST><![CDATA[TEST('Test some stuff with TcHaxx.Snappy');
// ARRANGE
stDemoDT := F_CreateDemoData();
// ACT
stActual := F_CreateDemoData();
// ASSERT
hr := fbTcUnitAdapter.Verify(anyArg:= stDemoDT);
hr := fbTcUnitAdapter.Verify(anyArg:= stActual);
IF NOT PENDING(hr) THEN
TEST_FINISHED_NAMED('Test some stuff');
TEST_FINISHED_NAMED('Test some stuff with TcHaxx.Snappy');
END_IF;]]></ST>
</Implementation>
</POU>
Expand Down
43 changes: 43 additions & 0 deletions examples/examples/POUs/FB_TcUnitWithoutSnappyExample.TcPOU
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?xml version="1.0" encoding="utf-8"?>
<TcPlcObject Version="1.1.0.1" ProductVersion="3.1.4024.12">
<POU Name="FB_TcUnitWithoutSnappyExample" Id="{43b4abf0-a389-4bd6-9155-50172d18c287}" SpecialFunc="None">
<Declaration><![CDATA[FUNCTION_BLOCK FB_TcUnitWithoutSnappyExample EXTENDS FB_TestSuite
VAR
stActual : ST_DemoDataType;
stActual2 : ST_DemoDataType;
END_VAR
VAR CONSTANT
cStringExpected : STRING := 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy';
cBufferExpected : ARRAY[0..15] OF BYTE := [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16];
END_VAR
]]></Declaration>
<Implementation>
<ST><![CDATA[
TEST('Test some stuff without TcHaxx.Snappy');
// ARRANGE
// ACT
stActual := F_CreateDemoData();
// ASSERT
AssertEquals_STRING(Expected:= cStringExpected,
Actual:= stActual.sString,
Message:= 'Field sString differs.');
AssertEquals_UDINT(Expected:= 16#DEADBEEF,
Actual:= stActual.nNumber,
Message:= 'Field nNumber differs.');
AssertEquals_REAL(Expected:= -1.23456789,
Actual:= stActual.fFloat,
Delta:= 0.00001,
Message:= 'Field fFloat differs.');
AssertArrayEquals_BYTE(Expecteds:= cBufferExpected,
Actuals:= stActual.aBuffer,
Message:= 'Field aBuffer differs.');
stActual2 := stActual;
stActual2.aBuffer[3]:= 55;
AssertEquals(stActual2, stActual, 'Oh snap!');
TEST_FINISHED_NAMED('Test some stuff without TcHaxx.Snappy');]]></ST>
</Implementation>
</POU>
</TcPlcObject>
2 changes: 1 addition & 1 deletion examples/examples/POUs/F_CreateDemoData.TcPOU
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ VAR
END_VAR
]]></Declaration>
<Implementation>
<ST><![CDATA[F_CreateDemoData.nFloat := -1.23456789;
<ST><![CDATA[F_CreateDemoData.fFloat := -1.23456789;
F_CreateDemoData.nNumber := 16#DEADBEEF;
F_CreateDemoData.sString := 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy';
FOR i:= 0 TO SIZEOF(F_CreateDemoData.aBuffer) DO
Expand Down
1 change: 1 addition & 0 deletions examples/examples/POUs/PRG_TcUnit.TcPOU
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
PROGRAM PRG_TcUnit
VAR
fbTcunitExample : FB_TcUnitExample;
fbNoSnappy : FB_TcUnitWithoutSnappyExample;
END_VAR
]]></Declaration>
<Implementation>
Expand Down
23 changes: 13 additions & 10 deletions examples/examples/examples.plcproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
<Compile Include="POUs\FB_TcUnitExample.TcPOU">
<SubType>Code</SubType>
</Compile>
<Compile Include="POUs\FB_TcUnitWithoutSnappyExample.TcPOU">
<SubType>Code</SubType>
</Compile>
<Compile Include="POUs\F_CreateDemoData.TcPOU">
<SubType>Code</SubType>
</Compile>
Expand Down Expand Up @@ -88,8 +91,8 @@
<ProjectExtensions>
<PlcProjectOptions>
<XmlArchive>
<Data>
<o xml:space="preserve" t="OptionKey">
<Data>
<o xml:space="preserve" t="OptionKey">
<v n="Name">"&lt;ProjectRoot&gt;"</v>
<d n="SubKeys" t="Hashtable" ckt="String" cvt="OptionKey">
<v>{192FAD59-8248-4824-A8DE-9177C94C195A}</v>
Expand Down Expand Up @@ -178,14 +181,14 @@
</d>
<d n="Values" t="Hashtable" />
</o>
</Data>
<TypeList>
<Type n="Boolean">System.Boolean</Type>
<Type n="Hashtable">System.Collections.Hashtable</Type>
<Type n="OptionKey">{54dd0eac-a6d8-46f2-8c27-2f43c7e49861}</Type>
<Type n="String">System.String</Type>
</TypeList>
</XmlArchive>
</Data>
<TypeList>
<Type n="Boolean">System.Boolean</Type>
<Type n="Hashtable">System.Collections.Hashtable</Type>
<Type n="OptionKey">{54dd0eac-a6d8-46f2-8c27-2f43c7e49861}</Type>
<Type n="String">System.String</Type>
</TypeList>
</XmlArchive>
</PlcProjectOptions>
</ProjectExtensions>
</Project>
6 changes: 5 additions & 1 deletion src/TcHaxx.Snappy/TcHaxx.Snappy.tsproj
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,16 @@
</Variant>
<Variant>
<Name>LIB</Name>
<Disable>0</Disable>
<Defines></Defines>
</Variant>
<Variant>
<Name>UNIT_TEST</Name>
<Disable>0</Disable>
<Defines/>
</Variant>
</ProjectVariant>
<Instance Id="#x08502000" TcSmClass="TComPlcObjDef" KeepUnrestoredLinks="2" TmcPath="snappy\snappy.tmc" TmcHash="{934AA734-D6F0-B942-FF86-A5DF414A8682}">
<Instance Id="#x08502000" TcSmClass="TComPlcObjDef" KeepUnrestoredLinks="2" TmcPath="snappy\snappy.tmc" TmcHash="{724D2834-43F6-6772-1E35-993D38D07869}">
<Name>snappy Instance</Name>
<CLSID ClassFactory="TcPlc30">{08500001-0000-0000-F000-000000000064}</CLSID>
<Contexts>
Expand Down
4 changes: 2 additions & 2 deletions src/TcHaxx.Snappy/_Config/PLC/examples.xti
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<Variant>
<Name>EXAMPLES</Name>
<Disable>0</Disable>
<Defines/>
<Defines></Defines>
</Variant>
<Variant>
<Name>LIB</Name>
Expand All @@ -14,7 +14,7 @@
<Defines/>
</Variant>
</ProjectVariant>
<Instance Id="#x08502080" TcSmClass="TComPlcObjDef" KeepUnrestoredLinks="2" TmcPath="..\..\examples\examples\examples.tmc" TmcHash="{E2F6A659-B1F9-2F24-31B0-69C562451188}">
<Instance Id="#x08502080" TcSmClass="TComPlcObjDef" KeepUnrestoredLinks="2" TmcPath="..\..\examples\examples\examples.tmc" TmcHash="{48A3FC13-FA74-F622-EB16-F0E26A79BE42}">
<Name>examples Instance</Name>
<CLSID ClassFactory="TcPlc30">{08500001-0000-0000-F000-000000000064}</CLSID>
<Contexts>
Expand Down

0 comments on commit 9b987f5

Please sign in to comment.