Skip to content

Commit fe5c223

Browse files
committed
Add a test for property throwing an exception
1 parent 39901f7 commit fe5c223

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

tests/CommandLine.Tests/CommandLine.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
<Compile Include="Fakes\Options_With_Default_Set_To_Sequence.cs" />
6262
<Compile Include="Fakes\Options_With_Guid.cs" />
6363
<Compile Include="Fakes\Options_With_Option_And_Value_Of_String_Type.cs" />
64+
<Compile Include="Fakes\Options_With_Property_Throwing_Exception.cs" />
6465
<Compile Include="Fakes\Options_With_SetName_That_Ends_With_Previous_SetName.cs" />
6566
<Compile Include="Fakes\Options_With_Shuffled_Index_Values.cs" />
6667
<Compile Include="Fakes\Options_With_Uri_And_SimpleType.cs" />
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace CommandLine.Tests.Fakes
8+
{
9+
class Options_With_Property_Throwing_Exception
10+
{
11+
private string optValue;
12+
13+
[Option('e')]
14+
public string OptValue
15+
{
16+
get
17+
{
18+
return optValue;
19+
}
20+
set
21+
{
22+
if (value != "good")
23+
throw new ArgumentException("Invalid value, only accept 'good' value");
24+
25+
optValue = value;
26+
}
27+
}
28+
}
29+
}

tests/CommandLine.Tests/Unit/Core/InstanceBuilderTests.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -984,6 +984,23 @@ public void Parse_to_type_with_single_string_ctor_builds_up_correct_instance()
984984
// Teardown
985985
}
986986

987+
[Fact]
988+
public void Parse_option_with_exception_thrown_from_setter_generates_SetValueExceptionError()
989+
{
990+
// Fixture setup
991+
var expectedResult = new[] { new SetValueExceptionError(new NameInfo("e", ""), new ArgumentException(), "bad") };
992+
993+
// Exercize system
994+
var result = InvokeBuild<Options_With_Property_Throwing_Exception>(
995+
new[] { "-e", "bad" });
996+
997+
// Verify outcome
998+
((NotParsed<Options_With_Property_Throwing_Exception>)result).Errors.ShouldBeEquivalentTo(expectedResult);
999+
1000+
// Teardown
1001+
}
1002+
1003+
9871004
[Theory]
9881005
[InlineData(new[] { "--stringvalue", "x-" }, "x-")]
9891006
[InlineData(new[] { "--stringvalue", "x--" }, "x--")]

0 commit comments

Comments
 (0)