-
Notifications
You must be signed in to change notification settings - Fork 5k
/
AppSettingStronglyTypedTest.cs
162 lines (135 loc) · 7.09 KB
/
AppSettingStronglyTypedTest.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
using Microsoft.Build.Framework;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace AppSettingStronglyTyped.Test
{
[TestClass]
public class AppSettingStronglyTypedTest
{
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
private Mock<IBuildEngine> buildEngine;
private List<BuildErrorEventArgs> errors;
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
[TestInitialize()]
public void Startup()
{
buildEngine = new Mock<IBuildEngine>();
errors = new List<BuildErrorEventArgs>();
buildEngine.Setup(x => x.LogErrorEvent(It.IsAny<BuildErrorEventArgs>())).Callback<BuildErrorEventArgs>(e => errors.Add(e));
}
[TestMethod]
public void EmptySettingFileList_EmptyClassGenerated()
{
//Arrange
var appSettingStronglyTyped = new AppSettingStronglyTyped { SettingClassName = "MySettingEmpty", SettingNamespaceName = "MyNamespace", SettingFiles = new ITaskItem[0] };
appSettingStronglyTyped.BuildEngine = buildEngine.Object;
//Act
var success = appSettingStronglyTyped.Execute();
//Assert
Assert.IsTrue(success);
Assert.AreEqual(0, errors.Count);
Assert.AreEqual("MySettingEmpty.generated.cs", appSettingStronglyTyped.ClassNameFile);
Assert.IsTrue(File.Exists(appSettingStronglyTyped.ClassNameFile));
Assert.IsTrue(File.ReadLines(appSettingStronglyTyped.ClassNameFile).SequenceEqual(File.ReadLines(".\\Resources\\empty-class.txt")));
//Cleanup
File.Delete(appSettingStronglyTyped.ClassNameFile);
}
[TestMethod]
public void SettingFileBadFormat_NotSuccess()
{
//Arrange
var item = new Mock<ITaskItem>();
item.Setup(x => x.GetMetadata("FullPath")).Returns(".\\Resources\\error-prop.setting");
var appSettingStronglyTyped = new AppSettingStronglyTyped { SettingClassName = "ErrorPropSetting", SettingNamespaceName = "MyNamespace", SettingFiles = new[] { item.Object } };
appSettingStronglyTyped.BuildEngine = buildEngine.Object;
//Act
var success = appSettingStronglyTyped.Execute();
//Assert
Assert.IsFalse(success);
Assert.AreEqual(errors.Count, 1);
Assert.AreEqual(null, appSettingStronglyTyped.ClassNameFile);
Assert.AreEqual(1, errors.Count);
var error = errors.First();
Assert.AreEqual("Incorrect line format. Valid format prop:type:defaultvalue", error.Message);
Assert.AreEqual(1, error.LineNumber);
}
[TestMethod]
public void SettingInvalidType_NotSuccess()
{
//Arrange
var item = new Mock<ITaskItem>();
item.Setup(x => x.GetMetadata("FullPath")).Returns(".\\Resources\\notvalidtype-prop.setting");
var appSettingStronglyTyped = new AppSettingStronglyTyped { SettingClassName = "ErrorPropSetting", SettingNamespaceName = "MyNamespace", SettingFiles = new[] { item.Object } };
appSettingStronglyTyped.BuildEngine = buildEngine.Object;
//Act
var success = appSettingStronglyTyped.Execute();
//Assert
Assert.IsFalse(success);
Assert.AreEqual(errors.Count, 1);
Assert.AreEqual(null, appSettingStronglyTyped.ClassNameFile);
Assert.AreEqual("Type not supported -> car", errors.First().Message);
}
[TestMethod]
public void SettingInvalidValue_NotSuccess()
{
//Arrange
var item = new Mock<ITaskItem>();
item.Setup(x => x.GetMetadata("FullPath")).Returns(".\\Resources\\notvalidvalue-prop.setting");
var appSettingStronglyTyped = new AppSettingStronglyTyped { SettingClassName = "ErrorPropSetting", SettingNamespaceName = "MyNamespace", SettingFiles = new[] { item.Object } };
appSettingStronglyTyped.BuildEngine = buildEngine.Object;
//Act
var success = appSettingStronglyTyped.Execute();
//Assert
Assert.IsFalse(success);
Assert.AreEqual(errors.Count, 1);
Assert.AreEqual(null, appSettingStronglyTyped.ClassNameFile);
Assert.AreEqual("It is not possible parse some value based on the type -> bool - awsome", errors.First().Message);
}
[DataTestMethod]
[DataRow("string")]
[DataRow("int")]
[DataRow("bool")]
[DataRow("guid")]
[DataRow("long")]
public void SettingFileWithProperty_ClassGeneratedWithOneProperty(string value)
{
//Arrange
var item = new Mock<ITaskItem>();
item.Setup(x => x.GetMetadata("FullPath")).Returns($".\\Resources\\{value}-prop.setting");
var appSettingStronglyTyped = new AppSettingStronglyTyped { SettingClassName = $"My{value}PropSetting", SettingNamespaceName = "MyNamespace", SettingFiles = new[] { item.Object } };
appSettingStronglyTyped.BuildEngine = buildEngine.Object;
//Act
var success = appSettingStronglyTyped.Execute();
//Assert
Assert.IsTrue(success);
Assert.AreEqual(errors.Count, 0);
Assert.AreEqual($"My{value}PropSetting.generated.cs", appSettingStronglyTyped.ClassNameFile);
Assert.IsTrue(File.Exists(appSettingStronglyTyped.ClassNameFile));
Assert.IsTrue(File.ReadLines(appSettingStronglyTyped.ClassNameFile).SequenceEqual(File.ReadLines($".\\Resources\\{value}-prop-class.txt")));
//Cleanup
File.Delete(appSettingStronglyTyped.ClassNameFile);
}
[DataTestMethod]
public void SettingFileWithMultipleProperty_ClassGeneratedWithMultipleProperty()
{
//Arrange
var item = new Mock<ITaskItem>();
item.Setup(x => x.GetMetadata("FullPath")).Returns($".\\Resources\\complete-prop.setting");
var appSettingStronglyTyped = new AppSettingStronglyTyped { SettingClassName = $"MyCompletePropSetting", SettingNamespaceName = "MyNamespace", SettingFiles = new[] { item.Object } };
appSettingStronglyTyped.BuildEngine = buildEngine.Object;
//Act
var success = appSettingStronglyTyped.Execute();
//Assert
Assert.IsTrue(success);
Assert.AreEqual(errors.Count, 0);
Assert.AreEqual($"MyCompletePropSetting.generated.cs", appSettingStronglyTyped.ClassNameFile);
Assert.IsTrue(File.Exists(appSettingStronglyTyped.ClassNameFile));
Assert.IsTrue(File.ReadLines(appSettingStronglyTyped.ClassNameFile).SequenceEqual(File.ReadLines(".\\Resources\\complete-prop-class.txt")));
//Cleanup
File.Delete(appSettingStronglyTyped.ClassNameFile);
}
}
}