Skip to content
Merged
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
1 change: 1 addition & 0 deletions src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
53 changes: 53 additions & 0 deletions src/TestStack.BDDfy.Tests/Scanner/Examples/ExampleTableTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,58 @@ public void TableToStringWithAdditionalColumn()
table.ToString(new[] { "Additional" }, new[] { new[] { "SomeAdditional Value" } })
.ShouldMatchApproved();
}

[Fact]
public void Add_WhenColumnCountMismatch_Throws()
{
var table = new ExampleTable("A", "B");
var ex = Should.Throw<ArgumentException>(() => table.Add(1, 2, 3));
ex.Message.ShouldContain("Number of column values does not match");
}

[Fact]
public void CollectionOperations_WorkCorrectly()
{
var table = new ExampleTable("A");
table.Add("val1");
table.Add("val2");

table.Count.ShouldBe(2);
table.IsReadOnly.ShouldBeFalse();

var first = table.First();
table.Contains(first).ShouldBeTrue();
table.Remove(first).ShouldBeTrue();
table.Count.ShouldBe(1);

table.Clear();
table.Count.ShouldBe(0);
}

[Fact]
public void CopyTo_CopiesElements()
{
var table = new ExampleTable("A");
table.Add("x");
table.Add("y");

var array = new Example[3];
table.CopyTo(array, 1);

array[0].ShouldBeNull();
array[1].ShouldNotBeNull();
array[2].ShouldNotBeNull();
}

[Theory]
[InlineData("MyHeader", "my header", true)]
[InlineData("MyHeader", "my_header", true)]
[InlineData("MyHeader", "MYHEADER", true)]
[InlineData("MyHeader", "Other", false)]
[InlineData("MyHeader", null, false)]
public void HeaderMatches_VariousCases(string header, string? name, bool expected)
{
ExampleTable.HeaderMatches(header, name).ShouldBe(expected);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Shouldly;
using System;
using Shouldly;
using Xunit;

namespace TestStack.BDDfy.Tests.Scanner.Examples
Expand All @@ -13,5 +14,92 @@ public void CanFormatAsStringTests()
new ExampleValue("Header", new object(), () => 0).GetValueAsString().ShouldBe("System.Object");
new ExampleValue("Header", new[] {1, 2}, () => 0).GetValueAsString().ShouldBe("1, 2");
}

[Fact]
public void GetValue_WhenTargetTypeMatchesDirectly_ReturnsValue()
{
var value = new ExampleValue("Col", 42, () => 0);
value.GetValue(typeof(int)).ShouldBe(42);
value.ValueHasBeenUsed.ShouldBeTrue();
}

[Fact]
public void GetValue_WhenNullAndTargetIsNullable_ReturnsNull()
{
var value = new ExampleValue("Col", null, () => 0);
value.GetValue(typeof(int?)).ShouldBeNull();
value.ValueHasBeenUsed.ShouldBeTrue();
}

[Fact]
public void GetValue_WhenNullAndTargetIsReferenceType_ReturnsNull()
{
var value = new ExampleValue("Col", null, () => 0);
value.GetValue(typeof(string)).ShouldBeNull();
}

[Fact]
public void GetValue_WhenNullAndTargetIsValueType_Throws()
{
var value = new ExampleValue("Col", null, () => 2);
var ex = Should.Throw<ArgumentException>(() => value.GetValue(typeof(int)));
ex.Message.ShouldContain("Cannot convert <null> to Int32");
ex.Message.ShouldContain("Column: 'Col'");
ex.Message.ShouldContain("Row: 3");
}

[Theory]
[InlineData("123", typeof(int), 123)]
[InlineData("45.6", typeof(double), 45.6)]
[InlineData("true", typeof(bool), true)]
public void GetValue_UsesConvertChangeType(string input, Type targetType, object expected)
{
var value = new ExampleValue("Col", input, () => 0);
value.GetValue(targetType).ShouldBe(expected);
}

[Fact]
public void GetValue_WhenEnumString_ParsesEnum()
{
var value = new ExampleValue("Col", "Transition", () => 0);
value.GetValue(typeof(ExecutionOrder)).ShouldBe(ExecutionOrder.Transition);
}

[Fact]
public void GetValue_WhenDateTimeString_ParsesDateTime()
{
var value = new ExampleValue("Col", "2023-06-15", () => 0);
value.GetValue(typeof(DateTime)).ShouldBe(new DateTime(2023, 6, 15));
}

[Fact]
public void GetValue_WhenInvalidCast_ThrowsUnassignableExampleException()
{
var value = new ExampleValue("Col", new object(), () => 1);
var ex = Should.Throw<UnassignableExampleException>(() => value.GetValue(typeof(int)));
ex.Message.ShouldContain("cannot be assigned to Int32");
ex.Message.ShouldContain("Column: 'Col'");
ex.Message.ShouldContain("Row: 2");
}

[Theory]
[InlineData("myHeader", "myHeader", true)]
[InlineData("my header", "myHeader", true)]
[InlineData("my_header", "myHeader", true)]
[InlineData("MyHeader", "myheader", true)]
[InlineData("Header1", "Header2", false)]
[InlineData("Header", null, false)]
public void MatchesName_VariousInputs(string header, string? name, bool expected)
{
var value = new ExampleValue(header, "x", () => 0);
value.MatchesName(name).ShouldBe(expected);
}

[Fact]
public void Row_ReturnsOneBasedIndex()
{
var value = new ExampleValue("Col", "x", () => 4);
value.Row.ShouldBe(5);
}
}
}
60 changes: 60 additions & 0 deletions src/TestStack.BDDfy.Tests/TypeExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System;
using Shouldly;
using Xunit;

namespace TestStack.BDDfy.Tests
{
public class TypeExtensionsTests
{
[Theory]
[InlineData(typeof(int), true)]
[InlineData(typeof(string), false)]
[InlineData(typeof(DateTime), true)]
[InlineData(typeof(object), false)]
public void IsValueType_ReturnsExpected(Type type, bool expected)
{
type.IsValueType().ShouldBe(expected);
}

[Theory]
[InlineData(typeof(DayOfWeek), true)]
[InlineData(typeof(int), false)]
[InlineData(typeof(string), false)]
public void IsEnum_ReturnsExpected(Type type, bool expected)
{
type.IsEnum().ShouldBe(expected);
}

[Theory]
[InlineData(typeof(int?), true)]
[InlineData(typeof(System.Collections.Generic.List<int>), true)]
[InlineData(typeof(int), false)]
[InlineData(typeof(string), false)]
public void IsGenericType_ReturnsExpected(Type type, bool expected)
{
type.IsGenericType().ShouldBe(expected);
}

[Fact]
public void IsInstanceOfType_ReturnsCorrectly()
{
typeof(string).IsInstanceOfType("hello").ShouldBeTrue();
typeof(int).IsInstanceOfType("hello").ShouldBeFalse();
typeof(object).IsInstanceOfType(42).ShouldBeTrue();
}

[Fact]
public void Assembly_ReturnsCorrectAssembly()
{
typeof(string).Assembly().ShouldBe(typeof(string).Assembly);
}

[Fact]
public void GetCustomAttributes_ReturnsAttributes()
{
var attrs = typeof(FlagsAttribute).GetCustomAttributes(typeof(AttributeUsageAttribute), true);
attrs.ShouldNotBeEmpty();
attrs[0].ShouldBeOfType<AttributeUsageAttribute>();
}
}
}
33 changes: 33 additions & 0 deletions src/TestStack.BDDfy.Tests/WithTagsExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System.Linq;
using Shouldly;
using Xunit;

namespace TestStack.BDDfy.Tests
{
public class WithTagsExtensionsTests
{
[Fact]
public void WithTags_AddsTags_ToTestContext()
{
var testObject = new TestClass();
var result = testObject.WithTags("tag1", "tag2");

result.ShouldBeSameAs(testObject);
var context = TestContext.GetContext(testObject);
context.Tags.ShouldContain("tag1");
context.Tags.ShouldContain("tag2");
}

[Fact]
public void WithTags_CanBeCalledMultipleTimes()
{
var testObject = new TestClass();
testObject.WithTags("a").WithTags("b", "c");

var context = TestContext.GetContext(testObject);
context.Tags.Count.ShouldBe(3);
}

private class TestClass { }
}
}
2 changes: 1 addition & 1 deletion src/TestStack.BDDfy/Processors/AsyncTestRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace TestStack.BDDfy.Processors
{
public static class AsyncTestRunner
{
public static void Run(Func<object> performStep)
public static void Run(Func<object?> performStep)
{
var oldSyncContext = SynchronizationContext.Current;
try
Expand Down
1 change: 0 additions & 1 deletion src/TestStack.BDDfy/TestStack.BDDfy.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<EmbeddedResource Include="**\Scripts\*.*" Exclude="bin\**;obj\**;**\*.xproj;packages\**;@(EmbeddedResource)" />
Expand Down
Loading