/
MSTest.cs
316 lines (312 loc) · 11.5 KB
/
MSTest.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MoodAnalyse;
using System;
namespace MoodAnalyserTest
{
/// <summary>
/// MS Testing class
/// </summary>
[TestClass]
public class MSTest
{
/// <summary>
/// Given the happy mood message as parameter should return HAPPY.
/// </summary>
[TestMethod]
public void Given_HappyMoodMessage_Should_Return_HAPPY()
{
///Arrange
string expected = "HAPPY";
MoodAnalyser moodAnalyser = new MoodAnalyser("i am very happy".ToUpper());
///Act
string mood = moodAnalyser.AnalyseMood();
///Assert
Assert.AreEqual(expected, mood);
}
/// <summary>
/// Given the sad mood message as parameter should return SAD.
/// </summary>
[TestMethod]
public void Given_SadMoodMessage_Should_Return_SAD()
{
///Arrange
string expected = "SAD";
MoodAnalyser moodAnalyser = new MoodAnalyser("i am very sad".ToUpper());
///Act
string mood = moodAnalyser.AnalyseMood();
///Assert
Assert.AreEqual(expected, mood);
}
/// <summary>
/// Given the null as input should return mood should not be null
/// </summary>
/// <param name="message">The message.</param>
[TestMethod]
[DataRow(null)]
public void Given_Null_AsInput_Should_Return_MOOD_SHOULD_NOT_BE_NULL(string message)
{
try
{
//Arrange
MoodAnalyser moodAnalyser = new MoodAnalyser(message);
///Act
var mood = moodAnalyser.AnalyseMood();
}
catch (MoodAnalyserCustomException e)
{
///Assert
Assert.AreEqual("mood should not be null", e.Message);
}
}
/// <summary>
/// Given Empty message as input parameter should throw exception as
/// moos should not be empty
/// </summary>
[TestMethod]
public void Given_Empty_AsInput_Should_Return_MOOD_SHOULD_NOT_BE_EMPTY()
{
try
{
/// Arrange
string message = "";
///string expected = "mood should not be empty";
MoodAnalyser moodAnalyser = new MoodAnalyser(message);
///Act
var mood = moodAnalyser.AnalyseMood();
}
catch (MoodAnalyserCustomException e)
{
///Assert
Assert.AreEqual("mood should not be empty", e.Message);
}
}
///UC 4
/// <summary>
/// Given the mood analyse class name should return mood analyse object.
/// </summary>
[TestMethod]
public void GivenMoodAnalyseClassName_ShouldReturnMoodAnalyseObject()
{
object expected = new MoodAnalyser();
object obj = MoodAnalyseFactory.CreateMoodAnalyserObject("MoodAnalyse.MoodAnalyser", "MoodAnalyser");
expected.Equals(obj);
}
/// <summary>
/// Given the mood analyse class name should return mood analyse object.
/// </summary>
[TestMethod]
public void Given_MoodAnalyser_Class_Name_Should_Return_MoodAnalyser_Object()
{
//Arrange
string className = "MoodAnalyse.MoodAnalyser";
string constructorName = "MoodAnalyser";
//Act
object expected = new MoodAnalyser();
object resultObj = MoodAnalyseFactory.CreateMoodAnalyserObject(className, constructorName);
//Assert
expected.Equals(resultObj);
}
/// <summary>
/// Given improper class name should throw exception No such class found
/// </summary>
[TestMethod]
public void Given_Improper_Class_Name_Should_Throw_MoodAnalysisException_Indicating_No_Such_Class()
{
try
{
//Arrange
string className = "DemoNamespace.MoodAnalyser";
string constructorName = "MoodAnalyser";
//Act
object resultObj = MoodAnalyseFactory.CreateMoodAnalyserObject(className, constructorName);
}
catch (MoodAnalyserCustomException e)
{
//Assert
Assert.AreEqual("class not found", e.Message);
}
}
/// <summary>
/// Given improper constructor name should throw constructor not found exception
/// </summary>
[TestMethod]
public void Given_Improper_Constructor_Name_Should_Throw_MoodAnalysisException_Indicating_No_Such_Constructor()
{
try
{
//Arrange
string className = "MoodAnalyse.MoodAnalyser";
string constructorName = "SampleConstructorName";
//Act
object resultObj = MoodAnalyseFactory.CreateMoodAnalyserObject(className, constructorName);
}
catch (MoodAnalyserCustomException e)
{
//Assert
Assert.AreEqual("constructor not found", e.Message);
}
}
///UC 5
/// <summary>
/// Givens the mood analyser class name should return mood analyser object using parametrized constructor.
/// </summary>
[TestMethod]
public void Given_MoodAnalyser_Class_Name_Should_Return_MoodAnalyser_Object_Using_Parametrized_Constructor()
{
//Arrange
string className = "MoodAnalyse.MoodAnalyser";
string constructorName = "MoodAnalyser";
MoodAnalyser expectedObj = new MoodAnalyser("HAPPY");
//Act
object resultObj = MoodAnalyseFactory.CreateMoodAnalyserUsingParameterizedConstructor(className, constructorName, "HAPPY");
//Assert
expectedObj.Equals(resultObj);
}
/// <summary>
/// Given improper class name for parameterized constructor should throw exception No such class found
/// </summary>
[TestMethod]
public void Given_Improper_Class_Name_Should_Throw_MoodAnalysisException_For_Parameterized_Constructor()
{
try
{
//Arrange
string className = "DemoNamespace.MoodAnalyser";
string constructorName = "MoodAnalyser";
MoodAnalyser expectedObj = new MoodAnalyser("SAD");
//Act
object resultObj = MoodAnalyseFactory.CreateMoodAnalyserUsingParameterizedConstructor(className, constructorName, "HAPPY");
}
catch (MoodAnalyserCustomException e)
{
//Assert
Assert.AreEqual("class not found", e.Message);
}
}
/// <summary>
/// Given improper constructor name for parameterized constructor
/// Should throw constructor not found exception
/// </summary>
[TestMethod]
public void Given_Improper_Constructor_Name_Should_Throw_MoodAnalysisException_For_Parameterized_Constructor()
{
try
{
//Arrange
string className = "MoodAnalyse.MoodAnalyser";
string constructorName = "SampleConstructorName";
//Act
object resultObj = MoodAnalyseFactory.CreateMoodAnalyserUsingParameterizedConstructor(className, constructorName, "GOOD");
}
catch (MoodAnalyserCustomException e)
{
//Assert
Assert.AreEqual("constructor not found", e.Message);
}
}
///TC6.1
/// <summary>
/// Given happy message using reflection when proper should retutn happy mood
/// </summary>
[TestMethod]
public void Given_Happy_Message_Using_Reflection_When_Proper_Should_Return_Happy()
{
//Arrange
string message = "HAPPY";
string methodName = "AnalyseMood";
//Act
string mood = MoodAnalyseFactory.InvokeAnalyseMood(message, methodName);
//Assert
Assert.AreEqual("HAPPY", mood);
}
/// <summary>
/// Given sad message using reflection when proper should retutn sad mood
/// </summary>
[TestMethod]
public void Given_Sad_Message_Using_Reflection_When_Proper_Should_Return_SAD()
{
//Arrange
string message = "SAD";
string methodName = "AnalyseMood";
//Act
string mood = MoodAnalyseFactory.InvokeAnalyseMood(message, methodName);
//Assert
Assert.AreEqual("SAD", mood);
}
///TC 6.2
/// <summary>
/// Given improper method name to throw exception that no such method
/// </summary>
[TestMethod]
public void Given_Improper_Method_Name_Should_Throw_MoodAnalyserCustomException_Indicating_No_Such_Method()
{
try
{
//Arrange
string message = "HAPPY";
string methodName = "SampleMethodName";
//Act
string actual = MoodAnalyseFactory.InvokeAnalyseMood(message, methodName);
}
catch (MoodAnalyserCustomException e)
{
//Assert
Assert.AreEqual("no such method", e.Message);
}
}
/// <summary>
/// TC 7.1 When given proper fieldName and a mood message for happy mood then should return HAPPY
/// </summary>
[TestMethod]
public void Given_ProperFieldName_And_HappyMoodMessage_Should_Return_HAPPY()
{
///Arrange
string mood = "I am happy today";
string fieldName = "message";
/// Act
object actual = MoodAnalyseFactory.SetFieldValue(mood, fieldName);
/// Assert
Assert.AreEqual("HAPPY", actual);
}
/// <summary>
/// TC 7.2 When given wrong fieldName and a happy mood message then should throw exception
/// </summary>
[TestMethod]
public void Given_Improper_FieldName_And_HappyMoodMessage_Should_Return_NoSuchFieldFound()
{
try
{
///Arrange
string mood = "I am in happy mood today";
string fieldName = "InvalidField";
/// Act
object actual = MoodAnalyseFactory.SetFieldValue(mood, fieldName);
}
catch (MoodAnalyserCustomException e)
{
/// Assert
Assert.AreEqual("No such field found", e.Message);
}
}
/// <summary>
/// TC 7.3 When given correct fieldName and passing a null mood message then throw error that Mood should not be NULL
/// </summary>
[TestMethod]
public void Given_Proper_FieldName_And_NULL_Should_Return_MoodShouldNotBeNULL()
{
try
{
///Arrange
string mood = null;
string fieldName = "message";
/// Act
object actual = MoodAnalyseFactory.SetFieldValue(mood, fieldName);
}
catch (MoodAnalyserCustomException e)
{
/// Assert
Assert.AreEqual("Mood should not be NULL", e.Message);
}
}
}
}