Skip to content

Commit

Permalink
Adds support for rules
Browse files Browse the repository at this point in the history
  • Loading branch information
skjohansen committed Aug 21, 2024
1 parent bd84b24 commit 7e965bd
Show file tree
Hide file tree
Showing 7 changed files with 175 additions and 13 deletions.
38 changes: 31 additions & 7 deletions source/GenGurka/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

Console.WriteLine("Hello, Gurka!");

var testProject = new TestProject();

var gurka = new Testrun();
gurka.TestName = "DemoProject";
var gurkaProduct = new Product() { Name = "Test Product" };
Expand All @@ -17,7 +19,7 @@

// read gherkin
var gherkinReader = new GherkinFileReader();
var featureFile = gherkinReader.ReadGherkinFile(ConfigurationManager.AppSettings["gherkinfile"]);
var featureFile = gherkinReader.ReadGherkinFile(testProject.GherkinFile);
var gurkaFeature = new Feature() { Name = featureFile.Feature.Name };
foreach (var featurechild in featureFile.Feature.Children)
{
Expand All @@ -34,24 +36,50 @@
gurkaFeature.Scenarios.Add(gurkaScenario);
}

foreach (var featurechild in featureFile.Feature.Children)
{
if (featurechild is not Gherkin.Ast.Rule)
continue;

var g = (Gherkin.Ast.Rule)featurechild;
var gurkaRule = new Rule() { Name = g.Name };
foreach (var ruleChild in g.Children)
{
if (ruleChild is not Gherkin.Ast.Scenario)
continue;

var s = (Gherkin.Ast.Scenario)ruleChild;
var gurkaScenario = new Scenario() { Name = s.Name };

foreach (var step in s.Steps)
{
gurkaScenario.Steps.Add(new Step() { Text = step.Text });
}
gurkaRule.Scenarios.Add(gurkaScenario);
}
gurkaFeature.Rules.Add(gurkaRule);
}

gurkaProduct.Features.Add(gurkaFeature);
// read test dll
var assembly = Assembly.LoadFile(ConfigurationManager.AppSettings["assemblyfile"]);
var assembly = Assembly.LoadFile(testProject.AssemblyFile);
//within dll find all attributes of type Given
var givenMethods = assembly.GetTypes()
.SelectMany(t => t.GetMethods())
.Where(m => m.GetCustomAttributes(typeof(GivenAttribute), false).Length > 0)
.ToArray();

// read test result
TestRun testRun = TrxFileParser.TrxDeserializer.Deserialize(ConfigurationManager.AppSettings["trxfile"]);
TestRun testRun = TrxFileParser.TrxDeserializer.Deserialize(testProject.TestResultFile);

var featUnderTest = gurkaProduct.GetFeature("DemoProject");
bool featurePassed = true;
foreach (var utr in testRun.Results.UnitTestResults)
{

var sceUnderTest = featUnderTest.GetScenario(utr.TestName);
if(sceUnderTest == null)
continue;
bool outcome = utr.Outcome == "Passed" ? true : false;
sceUnderTest.TestPassed = outcome;
sceUnderTest.TestOutput = utr.Output.StdOut;
Expand Down Expand Up @@ -80,7 +108,3 @@

Gurka.WriteGurkaFile(ConfigurationManager.AppSettings["outputpath"], gurka);

Console.WriteLine("heelo");

// combine
// generate .gurka
21 changes: 21 additions & 0 deletions source/GenGurka/TestProject.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SpecGurka.GenGurka
{
internal class TestProject
{
public string GherkinFile { get { return ConfigurationManager.AppSettings["gherkinfile"]; } }
public string AssemblyFile { get { return ConfigurationManager.AppSettings["assemblyfile"]; } }

public string TestResultFile { get { return ConfigurationManager.AppSettings["trxfile"]; } }




}
}
30 changes: 26 additions & 4 deletions source/GurkaSpec/Feature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ public TimeSpan TestDurationTime {
{
testDuration = testDuration.Add(scenario.TestDurationTime);
}

foreach (var rule in Rules)
{
testDuration = testDuration.Add(rule.TestDurationTime);
}

return testDuration;
}
set { }
Expand All @@ -31,13 +37,29 @@ public TimeSpan TestDurationTime {
set { } }

public List<Scenario> Scenarios { get; set; } = new List<Scenario>();

// rules

public Scenario GetScenario(string name)
{
var scenario = Scenarios.FirstOrDefault(s => s.Name == name);
Scenario? scenario = Scenarios.FirstOrDefault(s => s.Name == name);
if(scenario == null)
{
foreach (var rule in Rules)
{
var ruleScenario = rule.GetScenario(name);
if (ruleScenario != null)
{
return ruleScenario;
}
}
}
return scenario;
}

public List<Rule> Rules { get; set; } = new List<Rule>();

public Rule GetRule(string name)
{
var rule = Rules.FirstOrDefault(s => s.Name == name);
return rule;
}
}
}
49 changes: 49 additions & 0 deletions source/GurkaSpec/Rule.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,62 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace SpecGurka.GurkaSpec
{
public class Rule
{
public string Name { get; set; }

public bool TestsPassed { get
{
bool testsPassed = true;
foreach(var scenario in Scenarios)
{
if (!scenario.TestPassed)
{
testsPassed = false;
break;
}
}

return testsPassed;
}
set { } }

private TimeSpan testDuration;
[XmlIgnore]
public TimeSpan TestDurationTime
{
get
{
testDuration = TimeSpan.Zero;
foreach (var scenario in Scenarios)
{
testDuration = testDuration.Add(scenario.TestDurationTime);
}

return testDuration;
}
set { }
}

public string TestDuration
{
get { return TestDurationTime.ToString("G"); }
set { }
}

public List<Scenario> Scenarios { get; set; } = new List<Scenario>();

public Scenario GetScenario(string name)
{
var scenario = Scenarios.FirstOrDefault(s => s.Name == name);
return scenario;
}
}
}
6 changes: 5 additions & 1 deletion source/ReqnrollDemo/DemoFunctionality.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ public class DemoFunctionality

public string Name2 { get; set; }

public string CombineName() { return Name1 + " " + Name2; }
public string CombineName() {
if (Name1.ToLower() == Name2.ToLower())
return Name1;
return Name1 + " " + Name2;
}



Expand Down
10 changes: 9 additions & 1 deletion tests/ReqnrollDemo.Spec/Features/DemoProject.feature
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,12 @@ Scenario: Combine firstname and lastname with error
Given firstname is "Otis"
And lastname is "Redding"
When the two names are combined
Then the result should be "Otis Reddin"
Then the result should be "Otis Reddin"

Rule: When firstname and lastname are alike should it only appear once

Scenario: Combine alike firstname and lastname
Given firstname is "Otis"
And lastname is "Otis"
When the two names are combined
Then the result should be "Otis"
34 changes: 34 additions & 0 deletions tests/ReqnrollDemo.Spec/Features/DemoProject.feature.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7e965bd

Please sign in to comment.