Skip to content

Commit 4bed99c

Browse files
add missing tests
1 parent 852c9c7 commit 4bed99c

4 files changed

Lines changed: 235 additions & 1 deletion

File tree

src/TestStack.BDDfy.Tests/Scanner/Examples/ExampleTableTests.cs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,58 @@ public void TableToStringWithAdditionalColumn()
5353
table.ToString(new[] { "Additional" }, new[] { new[] { "SomeAdditional Value" } })
5454
.ShouldMatchApproved();
5555
}
56+
57+
[Fact]
58+
public void Add_WhenColumnCountMismatch_Throws()
59+
{
60+
var table = new ExampleTable("A", "B");
61+
var ex = Should.Throw<ArgumentException>(() => table.Add(1, 2, 3));
62+
ex.Message.ShouldContain("Number of column values does not match");
63+
}
64+
65+
[Fact]
66+
public void CollectionOperations_WorkCorrectly()
67+
{
68+
var table = new ExampleTable("A");
69+
table.Add("val1");
70+
table.Add("val2");
71+
72+
table.Count.ShouldBe(2);
73+
table.IsReadOnly.ShouldBeFalse();
74+
75+
var first = table.First();
76+
table.Contains(first).ShouldBeTrue();
77+
table.Remove(first).ShouldBeTrue();
78+
table.Count.ShouldBe(1);
79+
80+
table.Clear();
81+
table.Count.ShouldBe(0);
82+
}
83+
84+
[Fact]
85+
public void CopyTo_CopiesElements()
86+
{
87+
var table = new ExampleTable("A");
88+
table.Add("x");
89+
table.Add("y");
90+
91+
var array = new Example[3];
92+
table.CopyTo(array, 1);
93+
94+
array[0].ShouldBeNull();
95+
array[1].ShouldNotBeNull();
96+
array[2].ShouldNotBeNull();
97+
}
98+
99+
[Theory]
100+
[InlineData("MyHeader", "my header", true)]
101+
[InlineData("MyHeader", "my_header", true)]
102+
[InlineData("MyHeader", "MYHEADER", true)]
103+
[InlineData("MyHeader", "Other", false)]
104+
[InlineData("MyHeader", null, false)]
105+
public void HeaderMatches_VariousCases(string header, string? name, bool expected)
106+
{
107+
ExampleTable.HeaderMatches(header, name).ShouldBe(expected);
108+
}
56109
}
57110
}

src/TestStack.BDDfy.Tests/Scanner/Examples/ExampleValueTests.cs

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Shouldly;
1+
using System;
2+
using Shouldly;
23
using Xunit;
34

45
namespace TestStack.BDDfy.Tests.Scanner.Examples
@@ -13,5 +14,92 @@ public void CanFormatAsStringTests()
1314
new ExampleValue("Header", new object(), () => 0).GetValueAsString().ShouldBe("System.Object");
1415
new ExampleValue("Header", new[] {1, 2}, () => 0).GetValueAsString().ShouldBe("1, 2");
1516
}
17+
18+
[Fact]
19+
public void GetValue_WhenTargetTypeMatchesDirectly_ReturnsValue()
20+
{
21+
var value = new ExampleValue("Col", 42, () => 0);
22+
value.GetValue(typeof(int)).ShouldBe(42);
23+
value.ValueHasBeenUsed.ShouldBeTrue();
24+
}
25+
26+
[Fact]
27+
public void GetValue_WhenNullAndTargetIsNullable_ReturnsNull()
28+
{
29+
var value = new ExampleValue("Col", null, () => 0);
30+
value.GetValue(typeof(int?)).ShouldBeNull();
31+
value.ValueHasBeenUsed.ShouldBeTrue();
32+
}
33+
34+
[Fact]
35+
public void GetValue_WhenNullAndTargetIsReferenceType_ReturnsNull()
36+
{
37+
var value = new ExampleValue("Col", null, () => 0);
38+
value.GetValue(typeof(string)).ShouldBeNull();
39+
}
40+
41+
[Fact]
42+
public void GetValue_WhenNullAndTargetIsValueType_Throws()
43+
{
44+
var value = new ExampleValue("Col", null, () => 2);
45+
var ex = Should.Throw<ArgumentException>(() => value.GetValue(typeof(int)));
46+
ex.Message.ShouldContain("Cannot convert <null> to Int32");
47+
ex.Message.ShouldContain("Column: 'Col'");
48+
ex.Message.ShouldContain("Row: 3");
49+
}
50+
51+
[Theory]
52+
[InlineData("123", typeof(int), 123)]
53+
[InlineData("45.6", typeof(double), 45.6)]
54+
[InlineData("true", typeof(bool), true)]
55+
public void GetValue_UsesConvertChangeType(string input, Type targetType, object expected)
56+
{
57+
var value = new ExampleValue("Col", input, () => 0);
58+
value.GetValue(targetType).ShouldBe(expected);
59+
}
60+
61+
[Fact]
62+
public void GetValue_WhenEnumString_ParsesEnum()
63+
{
64+
var value = new ExampleValue("Col", "Transition", () => 0);
65+
value.GetValue(typeof(ExecutionOrder)).ShouldBe(ExecutionOrder.Transition);
66+
}
67+
68+
[Fact]
69+
public void GetValue_WhenDateTimeString_ParsesDateTime()
70+
{
71+
var value = new ExampleValue("Col", "2023-06-15", () => 0);
72+
value.GetValue(typeof(DateTime)).ShouldBe(new DateTime(2023, 6, 15));
73+
}
74+
75+
[Fact]
76+
public void GetValue_WhenInvalidCast_ThrowsUnassignableExampleException()
77+
{
78+
var value = new ExampleValue("Col", new object(), () => 1);
79+
var ex = Should.Throw<UnassignableExampleException>(() => value.GetValue(typeof(int)));
80+
ex.Message.ShouldContain("cannot be assigned to Int32");
81+
ex.Message.ShouldContain("Column: 'Col'");
82+
ex.Message.ShouldContain("Row: 2");
83+
}
84+
85+
[Theory]
86+
[InlineData("myHeader", "myHeader", true)]
87+
[InlineData("my header", "myHeader", true)]
88+
[InlineData("my_header", "myHeader", true)]
89+
[InlineData("MyHeader", "myheader", true)]
90+
[InlineData("Header1", "Header2", false)]
91+
[InlineData("Header", null, false)]
92+
public void MatchesName_VariousInputs(string header, string? name, bool expected)
93+
{
94+
var value = new ExampleValue(header, "x", () => 0);
95+
value.MatchesName(name).ShouldBe(expected);
96+
}
97+
98+
[Fact]
99+
public void Row_ReturnsOneBasedIndex()
100+
{
101+
var value = new ExampleValue("Col", "x", () => 4);
102+
value.Row.ShouldBe(5);
103+
}
16104
}
17105
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using System;
2+
using Shouldly;
3+
using Xunit;
4+
5+
namespace TestStack.BDDfy.Tests
6+
{
7+
public class TypeExtensionsTests
8+
{
9+
[Theory]
10+
[InlineData(typeof(int), true)]
11+
[InlineData(typeof(string), false)]
12+
[InlineData(typeof(DateTime), true)]
13+
[InlineData(typeof(object), false)]
14+
public void IsValueType_ReturnsExpected(Type type, bool expected)
15+
{
16+
type.IsValueType().ShouldBe(expected);
17+
}
18+
19+
[Theory]
20+
[InlineData(typeof(DayOfWeek), true)]
21+
[InlineData(typeof(int), false)]
22+
[InlineData(typeof(string), false)]
23+
public void IsEnum_ReturnsExpected(Type type, bool expected)
24+
{
25+
type.IsEnum().ShouldBe(expected);
26+
}
27+
28+
[Theory]
29+
[InlineData(typeof(int?), true)]
30+
[InlineData(typeof(System.Collections.Generic.List<int>), true)]
31+
[InlineData(typeof(int), false)]
32+
[InlineData(typeof(string), false)]
33+
public void IsGenericType_ReturnsExpected(Type type, bool expected)
34+
{
35+
type.IsGenericType().ShouldBe(expected);
36+
}
37+
38+
[Fact]
39+
public void IsInstanceOfType_ReturnsCorrectly()
40+
{
41+
typeof(string).IsInstanceOfType("hello").ShouldBeTrue();
42+
typeof(int).IsInstanceOfType("hello").ShouldBeFalse();
43+
typeof(object).IsInstanceOfType(42).ShouldBeTrue();
44+
}
45+
46+
[Fact]
47+
public void Assembly_ReturnsCorrectAssembly()
48+
{
49+
typeof(string).Assembly().ShouldBe(typeof(string).Assembly);
50+
}
51+
52+
[Fact]
53+
public void GetCustomAttributes_ReturnsAttributes()
54+
{
55+
var attrs = typeof(FlagsAttribute).GetCustomAttributes(typeof(AttributeUsageAttribute), true);
56+
attrs.ShouldNotBeEmpty();
57+
attrs[0].ShouldBeOfType<AttributeUsageAttribute>();
58+
}
59+
}
60+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System.Linq;
2+
using Shouldly;
3+
using Xunit;
4+
5+
namespace TestStack.BDDfy.Tests
6+
{
7+
public class WithTagsExtensionsTests
8+
{
9+
[Fact]
10+
public void WithTags_AddsTags_ToTestContext()
11+
{
12+
var testObject = new TestClass();
13+
var result = testObject.WithTags("tag1", "tag2");
14+
15+
result.ShouldBeSameAs(testObject);
16+
var context = TestContext.GetContext(testObject);
17+
context.Tags.ShouldContain("tag1");
18+
context.Tags.ShouldContain("tag2");
19+
}
20+
21+
[Fact]
22+
public void WithTags_CanBeCalledMultipleTimes()
23+
{
24+
var testObject = new TestClass();
25+
testObject.WithTags("a").WithTags("b", "c");
26+
27+
var context = TestContext.GetContext(testObject);
28+
context.Tags.Count.ShouldBe(3);
29+
}
30+
31+
private class TestClass { }
32+
}
33+
}

0 commit comments

Comments
 (0)