Skip to content

Commit

Permalink
AAA pattern in unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
raman-m committed Apr 12, 2024
1 parent 1389f50 commit a9ab2a3
Show file tree
Hide file tree
Showing 4 changed files with 189 additions and 276 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
using Ocelot.Configuration.Creator;
using Ocelot.Configuration.File;
using Ocelot.Values;
using Shouldly;
using System.Collections.Generic;
using TestStack.BDDfy;
using Xunit;

namespace Ocelot.UnitTests.Configuration;

Expand All @@ -22,23 +18,27 @@ public UpstreamHeaderTemplatePatternCreatorTests()
[Fact]
public void Should_create_pattern_without_placeholders()
{
// Arrange
var fileRoute = new FileRoute
{
UpstreamHeaderTemplates = new Dictionary<string, string>
{
["country"] = "a text without placeholders",
},
};
GivenTheFollowingFileRoute(fileRoute);

this.Given(x => x.GivenTheFollowingFileRoute(fileRoute))
.When(x => x.WhenICreateTheTemplatePattern())
.Then(x => x.ThenTheFollowingIsReturned("country", "^(?i)a text without placeholders$"))
.BDDfy();
// Act
WhenICreateTheTemplatePattern();

// Assert
ThenTheFollowingIsReturned("country", "^(?i)a text without placeholders$");
}

[Fact]
public void Should_create_pattern_case_sensitive()
{
// Arrange
var fileRoute = new FileRoute
{
RouteIsCaseSensitive = true,
Expand All @@ -47,50 +47,59 @@ public void Should_create_pattern_case_sensitive()
["country"] = "a text without placeholders",
},
};
GivenTheFollowingFileRoute(fileRoute);

// Act
WhenICreateTheTemplatePattern();

this.Given(x => x.GivenTheFollowingFileRoute(fileRoute))
.When(x => x.WhenICreateTheTemplatePattern())
.Then(x => x.ThenTheFollowingIsReturned("country", "^a text without placeholders$"))
.BDDfy();
// Assert
ThenTheFollowingIsReturned("country", "^a text without placeholders$");
}

[Fact]
public void Should_create_pattern_with_placeholder_in_the_beginning()
{
// Arrange
var fileRoute = new FileRoute
{
UpstreamHeaderTemplates = new Dictionary<string, string>
{
["country"] = "{header:start}rest of the text",
},
};
GivenTheFollowingFileRoute(fileRoute);

this.Given(x => x.GivenTheFollowingFileRoute(fileRoute))
.When(x => x.WhenICreateTheTemplatePattern())
.Then(x => x.ThenTheFollowingIsReturned("country", "^(?i)(?<start>.+)rest of the text$"))
.BDDfy();
// Act
WhenICreateTheTemplatePattern();

// Assert
ThenTheFollowingIsReturned("country", "^(?i)(?<start>.+)rest of the text$");
}

[Fact]
public void Should_create_pattern_with_placeholder_at_the_end()
{
// Arrange
var fileRoute = new FileRoute
{
UpstreamHeaderTemplates = new Dictionary<string, string>
{
["country"] = "rest of the text{header:end}",
},
};
GivenTheFollowingFileRoute(fileRoute);

// Act
WhenICreateTheTemplatePattern();

this.Given(x => x.GivenTheFollowingFileRoute(fileRoute))
.When(x => x.WhenICreateTheTemplatePattern())
.Then(x => x.ThenTheFollowingIsReturned("country", "^(?i)rest of the text(?<end>.+)$"))
.BDDfy();
// Assert
ThenTheFollowingIsReturned("country", "^(?i)rest of the text(?<end>.+)$");
}

[Fact]
public void Should_create_pattern_with_placeholder_only()
{
// Arrange
var fileRoute = new FileRoute
{
UpstreamHeaderTemplates = new Dictionary<string, string>
Expand All @@ -99,27 +108,33 @@ public void Should_create_pattern_with_placeholder_only()
},
};

this.Given(x => x.GivenTheFollowingFileRoute(fileRoute))
.When(x => x.WhenICreateTheTemplatePattern())
.Then(x => x.ThenTheFollowingIsReturned("country", "^(?i)(?<countrycode>.+)$"))
.BDDfy();
GivenTheFollowingFileRoute(fileRoute);

// Act
WhenICreateTheTemplatePattern();

// Assert
ThenTheFollowingIsReturned("country", "^(?i)(?<countrycode>.+)$");
}

[Fact]
public void Should_crate_pattern_with_more_placeholders()
{
// Arrange
var fileRoute = new FileRoute
{
UpstreamHeaderTemplates = new Dictionary<string, string>
{
["country"] = "any text {header:cc} and other {header:version} and {header:bob} the end",
},
};
GivenTheFollowingFileRoute(fileRoute);

// Act
WhenICreateTheTemplatePattern();

this.Given(x => x.GivenTheFollowingFileRoute(fileRoute))
.When(x => x.WhenICreateTheTemplatePattern())
.Then(x => x.ThenTheFollowingIsReturned("country", "^(?i)any text (?<cc>.+) and other (?<version>.+) and (?<bob>.+) the end$"))
.BDDfy();
// Assert
ThenTheFollowingIsReturned("country", "^(?i)any text (?<cc>.+) and other (?<version>.+) and (?<bob>.+) the end$");
}

private void GivenTheFollowingFileRoute(FileRoute fileRoute)
Expand Down
Loading

0 comments on commit a9ab2a3

Please sign in to comment.