1- using Shouldly ;
1+ using System ;
2+ using Shouldly ;
23using Xunit ;
34
45namespace 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}
0 commit comments