-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
CodeCatchClauseTests.cs
111 lines (96 loc) · 4.97 KB
/
CodeCatchClauseTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.Generic;
using System.Linq;
using Xunit;
namespace System.CodeDom.Tests
{
public class CodeCatchClauseTests : CodeDomTestBase
{
[Fact]
public void Ctor_Default()
{
var catchClause = new CodeCatchClause();
Assert.Empty(catchClause.LocalName);
Assert.Empty(catchClause.Statements);
Assert.Equal(new CodeTypeReference(typeof(Exception)).BaseType, catchClause.CatchExceptionType.BaseType);
}
[Theory]
[MemberData(nameof(String_TestData))]
public void Ctor_String(string localName)
{
var catchClause = new CodeCatchClause(localName);
Assert.Equal(localName ?? string.Empty, catchClause.LocalName);
Assert.Equal(typeof(Exception).ToString(), catchClause.CatchExceptionType.BaseType);
Assert.Empty(catchClause.Statements);
}
public static IEnumerable<object[]> Ctor_String_CodeTypeReference_TestData()
{
yield return new object[] { null, null };
yield return new object[] { "", new CodeTypeReference() };
yield return new object[] { "Value1", new CodeTypeReference(typeof(ArgumentException)) };
}
[Theory]
[MemberData(nameof(Ctor_String_CodeTypeReference_TestData))]
public void Ctor_String_CodeTypeReference(string localName, CodeTypeReference catchExceptionType)
{
var catchClause = new CodeCatchClause(localName, catchExceptionType);
Assert.Equal(localName ?? string.Empty, catchClause.LocalName);
Assert.Equal((catchExceptionType ?? new CodeTypeReference(typeof(Exception))).BaseType, catchClause.CatchExceptionType.BaseType);
Assert.Empty(catchClause.Statements);
}
public static IEnumerable<object[]> Ctor_String_CodeTypeReference_ParamsCodeStatement_TestData()
{
yield return new object[] { null, null, new CodeStatement[0] };
yield return new object[] { "", new CodeTypeReference(), new CodeStatement[] { new CodeCommentStatement("") } };
yield return new object[] { "Value1", new CodeTypeReference(typeof(ArgumentException)), new CodeStatement[] { new CodeCommentStatement("") } };
}
[Theory]
[MemberData(nameof(Ctor_String_CodeTypeReference_ParamsCodeStatement_TestData))]
public void Ctor_String_CodeTypeReference_ParamsCodeStatement(string localName, CodeTypeReference catchExceptionType, CodeStatement[] statements)
{
var catchClause = new CodeCatchClause(localName, catchExceptionType, statements);
Assert.Equal(localName ?? string.Empty, catchClause.LocalName);
Assert.Equal((catchExceptionType ?? new CodeTypeReference(typeof(Exception))).BaseType, catchClause.CatchExceptionType.BaseType);
Assert.Equal(statements, catchClause.Statements.Cast<CodeStatement>());
}
[Fact]
public void Ctor_NullStatements_ThrowsArgumentNullException()
{
AssertExtensions.Throws<ArgumentNullException>("value", () => new CodeCatchClause("name", new CodeTypeReference(typeof(void)), null));
}
[Fact]
public void Ctor_NullObjectInStatements_ThrowsArgumentNullException()
{
CodeStatement[] statements = new CodeStatement[] { null };
AssertExtensions.Throws<ArgumentNullException>("value", () => new CodeCatchClause("name", new CodeTypeReference(typeof(void)), statements));
}
[Theory]
[MemberData(nameof(String_TestData))]
public void LocalName_Set_Get_ReturnsExpected(string value)
{
var catchClause = new CodeCatchClause();
catchClause.LocalName = value;
Assert.Equal(value ?? string.Empty, catchClause.LocalName);
}
[Theory]
[MemberData(nameof(CodeTypeReference_TestData))]
public void CatchExceptionType_Set_Get_ReturnsExpected(CodeTypeReference value)
{
var catchClause = new CodeCatchClause();
catchClause.CatchExceptionType = value;
Assert.Equal((value ?? new CodeTypeReference(typeof(Exception))).BaseType, catchClause.CatchExceptionType.BaseType);
}
[Fact]
public void Statements_AddMultiple_ReturnsExpected()
{
var catchClause = new CodeCatchClause();
CodeStatement statement1 = new CodeCommentStatement("Value1");
catchClause.Statements.Add(statement1);
Assert.Equal(new CodeStatement[] { statement1 }, catchClause.Statements.Cast<CodeStatement>());
CodeStatement statement2 = new CodeCommentStatement("Value1");
catchClause.Statements.Add(statement2);
Assert.Equal(new CodeStatement[] { statement1, statement2 }, catchClause.Statements.Cast<CodeStatement>());
}
}
}