Move unit tests from specflow to NUnit

This commit is contained in:
Peter Butzhammer
2024-02-05 14:21:40 +01:00
parent e524d82784
commit 8d8d5617d1
5 changed files with 55 additions and 359 deletions

View File

@@ -1,50 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using TechTalk.SpecFlow;
using TechTalk.SpecFlow.Assist;
namespace Sharp7.Rx.Tests
{
[Binding]
public class ParsingS7VariableName
{
private S7VariableNameParser parser;
[Given(@"I have an Parser")]
public void GivenIHaveAnParser()
{
parser = new S7VariableNameParser();
}
[Given(@"I have the following variables")]
public void GivenIHaveTheFollowingVariables(Table table)
{
var names = table.CreateSet<Vars>();
ScenarioContext.Current.Set(names);
}
[When(@"I parse the var name")]
public void WhenIParseTheVarName()
{
var names = ScenarioContext.Current.Get<IEnumerable<Vars>>();
var addresses = names.Select(v => parser.Parse(v.VarName)).ToArray();
ScenarioContext.Current.Set(addresses);
}
[Then(@"the result should be")]
public void ThenTheResultShouldBe(Table table)
{
var addresses = ScenarioContext.Current.Get<S7VariableAddress[]>();
table.CompareToSet(addresses);
}
}
class Vars
{
public string VarName { get; set; }
}
}

View File

@@ -1,40 +0,0 @@
Feature: ParsingS7VariableName
@mytag
Scenario: Parsing variable name for bool
Given I have an Parser
And I have the following variables
| VarName |
| DB13.DBX3.1 |
| Db403.X5.2 |
| DB55DBX23.6 |
| DB1.S255 |
| DB1.S255.20 |
| DB5.String887.20 |
| DB506.B216 |
| DB506.DBB216.5 |
| DB506.D216 |
| DB506.DINT216 |
| DB506.INT216 |
| DB506.DBW216 |
| DB506.DUL216 |
| DB506.DULINT216 |
| DB506.DULONG216 |
When I parse the var name
Then the result should be
| Operand | DbNr | Start | Length | Bit | Type |
| Db | 13 | 3 | 1 | 1 | Bit |
| Db | 403 | 5 | 1 | 2 | Bit |
| Db | 55 | 23 | 1 | 6 | Bit |
| Db | 1 | 255 | 0 | 0 | String |
| Db | 1 | 255 | 20 | 0 | String |
| Db | 5 | 887 | 20 | 0 | String |
| Db | 506 | 216 | 1 | 0 | Byte |
| Db | 506 | 216 | 5 | 0 | Byte |
| Db | 506 | 216 | 4 | 0 | Double |
| Db | 506 | 216 | 4 | 0 | DInteger |
| Db | 506 | 216 | 2 | 0 | Integer |
| Db | 506 | 216 | 2 | 0 | Integer |
| Db | 506 | 216 | 8 | 0 | ULong |
| Db | 506 | 216 | 8 | 0 | ULong |
| Db | 506 | 216 | 8 | 0 | ULong |

View File

@@ -1,242 +0,0 @@
// ------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by SpecFlow (http://www.specflow.org/).
// SpecFlow Version:2.4.0.0
// SpecFlow Generator Version:2.4.0.0
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
// ------------------------------------------------------------------------------
#region Designer generated code
#pragma warning disable
namespace Sharp7.Rx.Tests
{
using TechTalk.SpecFlow;
[System.CodeDom.Compiler.GeneratedCodeAttribute("TechTalk.SpecFlow", "2.4.0.0")]
[System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
[NUnit.Framework.TestFixtureAttribute()]
[NUnit.Framework.DescriptionAttribute("ParsingS7VariableName")]
public partial class ParsingS7VariableNameFeature
{
private TechTalk.SpecFlow.ITestRunner testRunner;
#line 1 "ParsingS7VariableName.feature"
#line hidden
[NUnit.Framework.OneTimeSetUpAttribute()]
public virtual void FeatureSetup()
{
testRunner = TechTalk.SpecFlow.TestRunnerManager.GetTestRunner();
TechTalk.SpecFlow.FeatureInfo featureInfo = new TechTalk.SpecFlow.FeatureInfo(new System.Globalization.CultureInfo("en-US"), "ParsingS7VariableName", null, ProgrammingLanguage.CSharp, ((string[])(null)));
testRunner.OnFeatureStart(featureInfo);
}
[NUnit.Framework.OneTimeTearDownAttribute()]
public virtual void FeatureTearDown()
{
testRunner.OnFeatureEnd();
testRunner = null;
}
[NUnit.Framework.SetUpAttribute()]
public virtual void TestInitialize()
{
}
[NUnit.Framework.TearDownAttribute()]
public virtual void ScenarioTearDown()
{
testRunner.OnScenarioEnd();
}
public virtual void ScenarioInitialize(TechTalk.SpecFlow.ScenarioInfo scenarioInfo)
{
testRunner.OnScenarioInitialize(scenarioInfo);
testRunner.ScenarioContext.ScenarioContainer.RegisterInstanceAs<NUnit.Framework.TestContext>(NUnit.Framework.TestContext.CurrentContext);
}
public virtual void ScenarioStart()
{
testRunner.OnScenarioStart();
}
public virtual void ScenarioCleanup()
{
testRunner.CollectScenarioErrors();
}
[NUnit.Framework.TestAttribute()]
[NUnit.Framework.DescriptionAttribute("Parsing variable name for bool")]
[NUnit.Framework.CategoryAttribute("mytag")]
public virtual void ParsingVariableNameForBool()
{
TechTalk.SpecFlow.ScenarioInfo scenarioInfo = new TechTalk.SpecFlow.ScenarioInfo("Parsing variable name for bool", null, new string[] {
"mytag"});
#line 4
this.ScenarioInitialize(scenarioInfo);
this.ScenarioStart();
#line 5
testRunner.Given("I have an Parser", ((string)(null)), ((TechTalk.SpecFlow.Table)(null)), "Given ");
#line hidden
TechTalk.SpecFlow.Table table1 = new TechTalk.SpecFlow.Table(new string[] {
"VarName"});
table1.AddRow(new string[] {
"DB13.DBX3.1"});
table1.AddRow(new string[] {
"Db403.X5.2"});
table1.AddRow(new string[] {
"DB55DBX23.6"});
table1.AddRow(new string[] {
"DB1.S255"});
table1.AddRow(new string[] {
"DB1.S255.20"});
table1.AddRow(new string[] {
"DB5.String887.20"});
table1.AddRow(new string[] {
"DB506.B216"});
table1.AddRow(new string[] {
"DB506.DBB216.5"});
table1.AddRow(new string[] {
"DB506.D216"});
table1.AddRow(new string[] {
"DB506.DINT216"});
table1.AddRow(new string[] {
"DB506.INT216"});
table1.AddRow(new string[] {
"DB506.DBW216"});
table1.AddRow(new string[] {
"DB506.DUL216"});
table1.AddRow(new string[] {
"DB506.DULINT216"});
table1.AddRow(new string[] {
"DB506.DULONG216"});
#line 6
testRunner.And("I have the following variables", ((string)(null)), table1, "And ");
#line 23
testRunner.When("I parse the var name", ((string)(null)), ((TechTalk.SpecFlow.Table)(null)), "When ");
#line hidden
TechTalk.SpecFlow.Table table2 = new TechTalk.SpecFlow.Table(new string[] {
"Operand",
"DbNr",
"Start",
"Length",
"Bit",
"Type"});
table2.AddRow(new string[] {
"Db",
"13",
"3",
"1",
"1",
"Bit"});
table2.AddRow(new string[] {
"Db",
"403",
"5",
"1",
"2",
"Bit"});
table2.AddRow(new string[] {
"Db",
"55",
"23",
"1",
"6",
"Bit"});
table2.AddRow(new string[] {
"Db",
"1",
"255",
"0",
"0",
"String"});
table2.AddRow(new string[] {
"Db",
"1",
"255",
"20",
"0",
"String"});
table2.AddRow(new string[] {
"Db",
"5",
"887",
"20",
"0",
"String"});
table2.AddRow(new string[] {
"Db",
"506",
"216",
"1",
"0",
"Byte"});
table2.AddRow(new string[] {
"Db",
"506",
"216",
"5",
"0",
"Byte"});
table2.AddRow(new string[] {
"Db",
"506",
"216",
"4",
"0",
"Double"});
table2.AddRow(new string[] {
"Db",
"506",
"216",
"4",
"0",
"DInteger"});
table2.AddRow(new string[] {
"Db",
"506",
"216",
"2",
"0",
"Integer"});
table2.AddRow(new string[] {
"Db",
"506",
"216",
"2",
"0",
"Integer"});
table2.AddRow(new string[] {
"Db",
"506",
"216",
"8",
"0",
"ULong"});
table2.AddRow(new string[] {
"Db",
"506",
"216",
"8",
"0",
"ULong"});
table2.AddRow(new string[] {
"Db",
"506",
"216",
"8",
"0",
"ULong"});
#line 24
testRunner.Then("the result should be", ((string)(null)), table2, "Then ");
#line hidden
this.ScenarioCleanup();
}
}
}
#pragma warning restore
#endregion

View File

@@ -0,0 +1,42 @@
using System.Collections.Generic;
using DeepEqual.Syntax;
using NUnit.Framework;
using Sharp7.Rx.Enums;
namespace Sharp7.Rx.Tests;
[TestFixture]
internal class S7VariableNameParserTests
{
[TestCaseSource(nameof(GetTestCases))]
public void Run(TestCase tc)
{
var parser = new S7VariableNameParser();
var resp = parser.Parse(tc.Input);
resp.ShouldDeepEqual(tc.Expected);
}
public static IEnumerable<TestCase> GetTestCases()
{
yield return new TestCase("DB13.DBX3.1", new S7VariableAddress {Operand = Operand.Db, DbNr = 13, Start = 3, Length = 1, Bit = 1, Type = DbType.Bit});
yield return new TestCase("Db403.X5.2", new S7VariableAddress {Operand = Operand.Db, DbNr = 403, Start = 5, Length = 1, Bit = 2, Type = DbType.Bit});
yield return new TestCase("DB55DBX23.6", new S7VariableAddress {Operand = Operand.Db, DbNr = 55, Start = 23, Length = 1, Bit = 6, Type = DbType.Bit});
yield return new TestCase("DB1.S255", new S7VariableAddress {Operand = Operand.Db, DbNr = 1, Start = 255, Length = 0, Bit = 0, Type = DbType.String});
yield return new TestCase("DB1.S255.20", new S7VariableAddress {Operand = Operand.Db, DbNr = 1, Start = 255, Length = 20, Bit = 0, Type = DbType.String});
yield return new TestCase("DB5.String887.20", new S7VariableAddress {Operand = Operand.Db, DbNr = 5, Start = 887, Length = 20, Bit = 0, Type = DbType.String});
yield return new TestCase("DB506.B216", new S7VariableAddress {Operand = Operand.Db, DbNr = 506, Start = 216, Length = 1, Bit = 0, Type = DbType.Byte});
yield return new TestCase("DB506.DBB216.5", new S7VariableAddress {Operand = Operand.Db, DbNr = 506, Start = 216, Length = 5, Bit = 0, Type = DbType.Byte});
yield return new TestCase("DB506.D216", new S7VariableAddress {Operand = Operand.Db, DbNr = 506, Start = 216, Length = 4, Bit = 0, Type = DbType.Double});
yield return new TestCase("DB506.DINT216", new S7VariableAddress {Operand = Operand.Db, DbNr = 506, Start = 216, Length = 4, Bit = 0, Type = DbType.DInteger});
yield return new TestCase("DB506.INT216", new S7VariableAddress {Operand = Operand.Db, DbNr = 506, Start = 216, Length = 2, Bit = 0, Type = DbType.Integer});
yield return new TestCase("DB506.DBW216", new S7VariableAddress {Operand = Operand.Db, DbNr = 506, Start = 216, Length = 2, Bit = 0, Type = DbType.Integer});
yield return new TestCase("DB506.DUL216", new S7VariableAddress {Operand = Operand.Db, DbNr = 506, Start = 216, Length = 8, Bit = 0, Type = DbType.ULong});
yield return new TestCase("DB506.DULINT216", new S7VariableAddress {Operand = Operand.Db, DbNr = 506, Start = 216, Length = 8, Bit = 0, Type = DbType.ULong});
yield return new TestCase("DB506.DULONG216", new S7VariableAddress {Operand = Operand.Db, DbNr = 506, Start = 216, Length = 8, Bit = 0, Type = DbType.ULong});
}
public record TestCase(string Input, S7VariableAddress Expected)
{
public override string ToString() => Input;
}
}

View File

@@ -1,32 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net461</TargetFramework> <TargetFramework>net8.0</TargetFramework>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" /> <PackageReference Include="DeepEqual" Version="4.2.1" />
<PackageReference Include="NUnit" Version="3.11.0" /> <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
<PackageReference Include="SpecFlow" Version="2.4.0" /> <PackageReference Include="NUnit" Version="3.11.0" />
</ItemGroup> <PackageReference Include="Shouldly" Version="4.2.1" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\Sharp7.Rx\Sharp7.Rx.csproj" /> <ProjectReference Include="..\Sharp7.Rx\Sharp7.Rx.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup> </Project>
<Compile Update="ParsingS7VariableName.feature.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>ParsingS7VariableName.feature</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup>
<None Update="ParsingS7VariableName.feature">
<Generator>SpecFlowSingleFileGenerator</Generator>
<LastGenOutput>ParsingS7VariableName.feature.cs</LastGenOutput>
</None>
</ItemGroup>
</Project>