Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/add awaiting assertion capability #179

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System.Collections.Generic;

namespace TcUnit.Verifier
{
class FB_HaltAssertionsWhenAwaitingAssertionsIsUsed : TestFunctionBlockAssert
{
public FB_HaltAssertionsWhenAwaitingAssertionsIsUsed(IEnumerable<ErrorList.Error> errors, string testFunctionBlockInstance = null)
: base(errors, testFunctionBlockInstance)
{
Test_AssertImmediatelyAfterTest();
Test_ShortHalt();
Test_LongHalt();
}

private void Test_AssertImmediatelyAfterTest()
{
string testMessage = CreateFailedTestMessage("Test_AssertImmediatelyAfterTest",
"TRUE",
"FALSE",
"this assertion should fail because test needs 2 second to have the real result");
AssertContainsMessage(testMessage, EnvDTE80.vsBuildErrorLevel.vsBuildErrorLevelHigh);
}

private void Test_ShortHalt()
{
string testMessage = CreateFailedTestMessage("Test_ShortHalt",
"TRUE",
"FALSE",
"this assertion should fail because test needs 2 second to have the real result but assertion halted for 1 second");
AssertContainsMessage("Test_ShortHalt", EnvDTE80.vsBuildErrorLevel.vsBuildErrorLevelHigh);
}

private void Test_LongHalt()
{
AssertDoesNotContainMessage("Test_LongHalt", EnvDTE80.vsBuildErrorLevel.vsBuildErrorLevelHigh);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Program
private static string tcUnitTargetNetId = "127.0.0.1.1.1";
private static VisualStudioInstance vsInstance = null;
private static ILog log = LogManager.GetLogger("TcUnit-Verifier");
private static int expectedNumberOfFailedTests = 116; // Update this if you add intentionally failing tests
private static int expectedNumberOfFailedTests = 118; // Update this if you add intentionally failing tests

[STAThread]
static void Main(string[] args)
Expand Down Expand Up @@ -197,6 +197,7 @@ static void Main(string[] args)
new FB_TestFinishedNamed(errors);
new FB_TestNumberOfAssertionsCalculation(errors);
new FB_EmptyAssertionMessage(errors);
new FB_HaltAssertionsWhenAwaitingAssertionsIsUsed(errors);

log.Info("Done.");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
<Compile Include="Constants.cs" />
<Compile Include="ErrorList.cs" />
<Compile Include="FB_EmptyAssertionMessage.cs" />
<Compile Include="FB_HaltAssertionsWhenAwaitingAssertionsIsUsed.cs" />
<Compile Include="FB_TestXmlControl.cs" />
<Compile Include="FB_TestFileControl.cs" />
<Compile Include="FB_TestStreamBuffer.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<TcSmProject xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://www.beckhoff.com/schemas/2012/07/TcSmProject" TcSmVersion="1.0" TcVersion="3.1.4022.32">
<Project ProjectGUID="{3B151CEE-1DB6-4543-9B44-7AFACBDFB147}" TargetNetId="127.0.0.1.1.1" Target64Bit="true" ShowHideConfigurations="#x3c7">
<System>
<Settings DontCheckTarget="127.0.0.1.1.1"/>
<Tasks>
<Task Id="3" Priority="20" CycleTime="100000" AmsPort="350" AdtTasks="true">
<Name>PlcTask</Name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@
<Compile Include="Test\FB_ExtendedTestInformation.TcPOU">
<SubType>Code</SubType>
</Compile>
<Compile Include="Test\FB_HaltAssertionsWhenAwaitingAssertionsIsUsed.TcPOU">
<SubType>Code</SubType>
</Compile>
<Compile Include="Test\FB_MultipleAssertWithSameParametersInDifferentCyclesAndInSameTest.TcPOU">
<SubType>Code</SubType>
</Compile>
Expand Down Expand Up @@ -144,11 +147,6 @@
<Namespace>TcUnit</Namespace>
</PlaceholderReference>
</ItemGroup>
<ItemGroup>
<PlaceholderResolution Include="TcUnit">
<Resolution>TcUnit, * (www.tcunit.org)</Resolution>
</PlaceholderResolution>
</ItemGroup>
<ItemGroup>
<None Include="TcUnitVerifier.tmc">
<SubType>Content</SubType>
Expand All @@ -159,6 +157,11 @@
<Namespace>IBaseLibrary</Namespace>
</LibraryReference>
</ItemGroup>
<ItemGroup>
<PlaceholderResolution Include="TcUnit">
<Resolution>TcUnit, * (www.tcunit.org)</Resolution>
</PlaceholderResolution>
</ItemGroup>
<ProjectExtensions>
<PlcProjectOptions>
<XmlArchive>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?xml version="1.0" encoding="utf-8"?>
<TcPlcObject Version="1.1.0.1" ProductVersion="3.1.4022.18">
<POU Name="FB_HaltAssertionsWhenAwaitingAssertionsIsUsed" Id="{b3457fc4-0c17-4e46-9834-9ec3368bf1b5}" SpecialFunc="None">
<Declaration><![CDATA[(*
Contains tests that require 2 seconds to get the real result :
- A normal test that asserts and finishes immediately so the assert statement fails
- A short halted test that halts assertion for 1 seconds so the assert statement fails
- A long halted test that halts assertion for 3 seconds so the assert statement passes
*)
FUNCTION_BLOCK FB_HaltAssertionsWhenAwaitingAssertionsIsUsed EXTENDS FB_TestSuite
VAR
// these timers are used to mock a function block which throws result after 2 second delay
ImmediateTestMockTimer : Tc2_Standard.TON := (PT := T#2S);
ShortTestMockTimer : Tc2_Standard.TON := (PT := T#2S);
LongTestMockTimer : Tc2_Standard.TON := (PT := T#2S);
END_VAR
]]></Declaration>
<Implementation>
<ST><![CDATA[Test_AssertImmediatelyAfterTest(ADR(ImmediateTestMockTimer));
Test_ShortHalt(ADR(ShortTestMockTimer));
Test_LongHalt(ADR(LongTestMockTimer));]]></ST>
</Implementation>
<Method Name="Test_AssertImmediatelyAfterTest" Id="{bb58d8b0-288a-4798-8d74-c58d960bafd2}">
<Declaration><![CDATA[METHOD Test_AssertImmediatelyAfterTest
VAR_INPUT
testMockTimer : POINTER TO Tc2_Standard.TON;
END_VAR
]]></Declaration>
<Implementation>
<ST><![CDATA[TEST('Test_AssertImmediatelyAfterTest');
testMockTimer^(IN:=TRUE);
AssertTrue(testMockTimer^.Q,'this assertion should fail because test needs 2 second to have the real result');
TEST_FINISHED();
]]></ST>
</Implementation>
</Method>
<Method Name="Test_LongHalt" Id="{ceb19f5f-1e0b-479e-9224-6630d1444303}">
<Declaration><![CDATA[METHOD Test_LongHalt
VAR_INPUT
testMockTimer : POINTER TO Tc2_Standard.TON;
END_VAR
]]></Declaration>
<Implementation>
<ST><![CDATA[TEST('Test_LongHalt');
testMockTimer^(IN:=TRUE);
AWAIT_ASSERTIONS(AwaitTime:=T#3S);
AssertTrue(testMockTimer^.Q,'this assertion should pass because test needs 2 second to have the real result and assertion halted for 3 seconds');
TEST_FINISHED();
]]></ST>
</Implementation>
</Method>
<Method Name="Test_ShortHalt" Id="{38e97dc0-6a5d-4249-86e6-920ea3c36382}">
<Declaration><![CDATA[METHOD Test_ShortHalt
VAR_INPUT
testMockTimer : POINTER TO Tc2_Standard.TON;
END_VAR
]]></Declaration>
<Implementation>
<ST><![CDATA[TEST('Test_ShortHalt');
testMockTimer^(IN:=TRUE);
AWAIT_ASSERTIONS(AwaitTime:=T#1S);
AssertTrue(testMockTimer^.Q,'this assertion should fail because test needs 2 second to have the real result but assertion halted for 1 second');
TEST_FINISHED();
]]></ST>
</Implementation>
</Method>
</POU>
</TcPlcObject>
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ VAR
TestStreamBuffer : FB_TestStreamBuffer;
TestFinishedNamed : FB_TestFinishedNamed;
EmptyAssertionMessage : FB_EmptyAssertionMessage;
HaltAssertionsWhenAwaitingAssertionsIsUsed : FB_HaltAssertionsWhenAwaitingAssertionsIsUsed;

(* The testsuite below is not active, as it will make TcUnit to abort. Uncomment if you want
to test the function of where a test with a name that doesn't exist is set to finished *)
Expand Down
5 changes: 4 additions & 1 deletion TcUnit/TcUnit/GVLs/GVL_TcUnit.TcGVL
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<TcPlcObject Version="1.1.0.1" ProductVersion="3.1.4022.18">
<TcPlcObject Version="1.1.0.1" ProductVersion="3.1.4024.9">
<GVL Name="GVL_TcUnit" Id="{78472b76-c9a4-4b27-a580-8e7898edc3d7}">
<Declaration><![CDATA[{attribute 'no_assign'}
{attribute 'qualified_only'}
Expand All @@ -11,6 +11,9 @@ VAR_GLOBAL

(* Pointer to current test suite being called *)
CurrentTestSuiteBeingCalled : POINTER TO FB_TestSuite;

(* Reference to current test being called *)
CurrentTestBeingCalled : REFERENCE TO FB_Test;

(* Current name of test being called *)
CurrentTestNameBeingCalled : Tc2_System.T_MaxString;
Expand Down
22 changes: 22 additions & 0 deletions TcUnit/TcUnit/POUs/FB_Test.TcPOU
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ VAR
TestName : Tc2_System.T_MaxString;
TestIsFinished : BOOL;
TestIsSkipped : BOOL; // This is set to true, if test is disabled (by putting the string "disabled_" in front of the test name
{attribute 'hide'}
_assertionIsAwaiting : BOOL; // This is set to true if assertions need some delay before execution (by using AWAIT_ASSERTIONS() method before assertions)
AwaitingTimer : TON ;
NumberOfAssertions : UINT;

(* In which order/sequence relative to the order tests should this test be executed/evaluated.
Expand All @@ -24,6 +27,25 @@ END_VAR]]></Declaration>
<Implementation>
<ST><![CDATA[]]></ST>
</Implementation>
<Property Name="AssertionIsAwaiting" Id="{b465ea5b-dea5-4ba3-8c94-16f5090457ff}">
<Declaration><![CDATA[PROPERTY INTERNAL AssertionIsAwaiting : bool]]></Declaration>
<Get Name="Get" Id="{88dabc4b-9750-4c59-8976-fd367fcfe9ca}">
<Declaration><![CDATA[VAR
END_VAR
]]></Declaration>
<Implementation>
<ST><![CDATA[AssertionIsAwaiting := _assertionIsAwaiting;]]></ST>
</Implementation>
</Get>
<Set Name="Set" Id="{b4944f5a-f816-49dc-a6bc-2041187026b6}">
<Declaration><![CDATA[VAR
END_VAR
]]></Declaration>
<Implementation>
<ST><![CDATA[_assertionIsAwaiting := AssertionIsAwaiting;]]></ST>
</Implementation>
</Set>
</Property>
<Method Name="GetAssertionMessage" Id="{273c4e89-faee-43b5-803d-428cdf75ac48}">
<Declaration><![CDATA[METHOD INTERNAL GetAssertionMessage : Tc2_System.T_MaxString]]></Declaration>
<Implementation>
Expand Down
Loading