forked from libgit2/libgit2sharp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSetErrorFixture.cs
186 lines (148 loc) · 6.32 KB
/
SetErrorFixture.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
using System;
using System.IO;
using System.Text;
using LibGit2Sharp.Tests.TestHelpers;
using Xunit;
namespace LibGit2Sharp.Tests
{
public class SetErrorFixture : BaseFixture
{
private const string simpleExceptionMessage = "This is a simple exception message.";
private const string aggregateExceptionMessage = "This is aggregate exception.";
private const string outerExceptionMessage = "This is an outer exception.";
private const string innerExceptionMessage = "This is an inner exception.";
private const string innerExceptionMessage2 = "This is inner exception #2.";
private const string expectedInnerExceptionHeaderText = "Inner Exception:";
private const string expectedAggregateExceptionHeaderText = "Contained Exception:";
private const string expectedAggregateExceptionsHeaderText = "Contained Exceptions:";
[Fact]
public void FormatSimpleException()
{
Exception exceptionToThrow = new Exception(simpleExceptionMessage);
string expectedMessage = simpleExceptionMessage;
AssertExpectedExceptionMessage(expectedMessage, exceptionToThrow);
}
[Fact]
public void FormatExceptionWithInnerException()
{
Exception exceptionToThrow = new Exception(outerExceptionMessage, new Exception(innerExceptionMessage));
StringBuilder sb = new StringBuilder();
sb.AppendLine(outerExceptionMessage);
sb.AppendLine();
AppendIndentedLine(sb, expectedInnerExceptionHeaderText, 0);
AppendIndentedText(sb, innerExceptionMessage, 1);
string expectedMessage = sb.ToString();
AssertExpectedExceptionMessage(expectedMessage, exceptionToThrow);
}
[Fact]
public void FormatAggregateException()
{
Exception exceptionToThrow = new AggregateException(aggregateExceptionMessage, new Exception(innerExceptionMessage), new Exception(innerExceptionMessage2));
StringBuilder sb = new StringBuilder();
#if NETFRAMEWORK
sb.AppendLine(aggregateExceptionMessage);
#else
sb.AppendLine($"{aggregateExceptionMessage} ({innerExceptionMessage}) ({innerExceptionMessage2})");
#endif
sb.AppendLine();
AppendIndentedLine(sb, expectedAggregateExceptionsHeaderText, 0);
AppendIndentedLine(sb, innerExceptionMessage, 1);
sb.AppendLine();
AppendIndentedText(sb, innerExceptionMessage2, 1);
string expectedMessage = sb.ToString();
AssertExpectedExceptionMessage(expectedMessage, exceptionToThrow);
}
private void AssertExpectedExceptionMessage(string expectedMessage, Exception exceptionToThrow)
{
Exception thrownException = null;
ObjectId id = new ObjectId("deadbeefdeadbeefdeadbeefdeadbeefdeadbeef");
string repoPath = InitNewRepository();
using (var repo = new Repository(repoPath))
{
repo.ObjectDatabase.AddBackend(new ThrowingOdbBackend(exceptionToThrow), priority: 1);
try
{
repo.Lookup<Blob>(id);
}
catch (Exception ex)
{
thrownException = ex;
}
}
Assert.NotNull(thrownException);
Assert.Equal(expectedMessage, thrownException.Message);
}
private void AppendIndentedText(StringBuilder sb, string text, int indentLevel)
{
sb.AppendFormat("{0}{1}", IndentString(indentLevel), text);
}
private void AppendIndentedLine(StringBuilder sb, string text, int indentLevel)
{
sb.AppendFormat("{0}{1}{2}", IndentString(indentLevel), text, Environment.NewLine);
}
private string IndentString(int level)
{
return new string(' ', level * 4);
}
#region ThrowingOdbBackend
private class ThrowingOdbBackend : OdbBackend
{
private Exception exceptionToThrow;
public ThrowingOdbBackend(Exception exceptionToThrow)
{
this.exceptionToThrow = exceptionToThrow;
}
protected override OdbBackendOperations SupportedOperations
{
get
{
return OdbBackendOperations.Read |
OdbBackendOperations.ReadPrefix |
OdbBackendOperations.Write |
OdbBackendOperations.WriteStream |
OdbBackendOperations.Exists |
OdbBackendOperations.ExistsPrefix |
OdbBackendOperations.ForEach |
OdbBackendOperations.ReadHeader;
}
}
public override int Read(ObjectId oid, out UnmanagedMemoryStream data, out ObjectType objectType)
{
throw this.exceptionToThrow;
}
public override int ReadPrefix(string shortSha, out ObjectId id, out UnmanagedMemoryStream data, out ObjectType objectType)
{
throw this.exceptionToThrow;
}
public override int Write(ObjectId oid, Stream dataStream, long length, ObjectType objectType)
{
throw this.exceptionToThrow;
}
public override int WriteStream(long length, ObjectType objectType, out OdbBackendStream stream)
{
throw this.exceptionToThrow;
}
public override bool Exists(ObjectId oid)
{
throw this.exceptionToThrow;
}
public override int ExistsPrefix(string shortSha, out ObjectId found)
{
throw this.exceptionToThrow;
}
public override int ReadHeader(ObjectId oid, out int length, out ObjectType objectType)
{
throw this.exceptionToThrow;
}
public override int ReadStream(ObjectId oid, out OdbBackendStream stream)
{
throw this.exceptionToThrow;
}
public override int ForEach(ForEachCallback callback)
{
throw this.exceptionToThrow;
}
}
#endregion
}
}