-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathSnapshotFullNameResolver.cs
118 lines (104 loc) · 4.32 KB
/
SnapshotFullNameResolver.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
using Snapshooter.Core;
using Snapshooter.Exceptions;
namespace Snapshooter
{
/// <summary>
/// Resolves the full snapshot name for a unit test. The full snapshot name
/// consists of the snapshot file name and the folder path.
/// </summary>
public class SnapshotFullNameResolver : ISnapshotFullNameResolver
{
private readonly ISnapshotFullNameReader _snapshotFullNameReader;
/// <summary>
/// Constructor of the class <see cref="SnapshotFullNameResolver"/>
/// initializes a new instance.
/// </summary>
/// <param name="snapshotFullNameReader">The filename.</param>
public SnapshotFullNameResolver(
ISnapshotFullNameReader snapshotFullNameReader)
{
_snapshotFullNameReader = snapshotFullNameReader;
}
/// <summary>
/// Resolves automatically the snapshot name for the running unit test.
/// </summary>
/// <returns>The full name of a snapshot.</returns>
public SnapshotFullName ResolveSnapshotFullName()
{
return ResolveSnapshotFullName(null);
}
/// <summary>
/// Resolves the snapshot name for the running unit test.
/// The default generated snapshot name can be overwritten
/// by the given snapshot name.
/// </summary>
/// <param name="snapshotName">
/// The snapshot name given by the user. This snapshot name will overwrite
/// the automatically generated snapshot name.
/// </param>
/// <returns>The full name of a snapshot.</returns>
public SnapshotFullName ResolveSnapshotFullName(
string snapshotName)
{
return ResolveSnapshotFullName(snapshotName, null);
}
/// <summary>
/// Resolves the snapshot name for the running unit test.
/// The default generated snapshot name can either be overwritten
/// with a given snapshot name, or can be extended by the snapshot name extensions,
/// or both.
/// </summary>
/// <param name="snapshotName">
/// The snapshot name given by the user, this snapshot name will overwrite
/// the automatically generated snapshot name.
/// </param>
/// <param name="snapshotNameExtension">
/// The snapshot name extension will extend the snapshot name with
/// this given extensions. It can be used to make a snapshot name even more
/// specific.
/// </param>
/// <returns>The full name of a snapshot.</returns>
public SnapshotFullName ResolveSnapshotFullName(
string snapshotName, string snapshotNameExtension)
{
SnapshotFullName snapshotFullName =
_snapshotFullNameReader.ReadSnapshotFullName();
if (snapshotFullName == null)
{
throw new SnapshotTestException(
"The snapshot full name could not be evaluated.");
}
if(string.IsNullOrWhiteSpace(snapshotFullName.Filename) &&
string.IsNullOrWhiteSpace(snapshotName))
{
throw new SnapshotTestException(
"No snapshot name could be resolved.");
}
if (string.IsNullOrWhiteSpace(snapshotName))
{
snapshotName = snapshotFullName.Filename;
}
snapshotName = AddSnapshotNameExtension(snapshotName, snapshotNameExtension);
snapshotName = EnsureSnapshotFileExtension(snapshotName);
return new SnapshotFullName(snapshotName, snapshotFullName.FolderPath);
}
private string EnsureSnapshotFileExtension(string snapshotName)
{
if (!snapshotName.EndsWith(FileNames.SnapshotFileExtension))
{
snapshotName = string.Concat(
snapshotName, FileNames.SnapshotFileExtension);
}
return snapshotName;
}
private string AddSnapshotNameExtension(
string snapshotName, string snapshotNameExtension)
{
if (snapshotNameExtension != null)
{
snapshotName = string.Concat(snapshotName, snapshotNameExtension);
}
return snapshotName;
}
}
}