Skip to content

Commit d0c56cd

Browse files
authored
Merge pull request #2 from TeamControlium/master-candidate-1.0.2
Master candidate 1.0.2
2 parents 9b76da0 + e697833 commit d0c56cd

File tree

9 files changed

+63
-166
lines changed

9 files changed

+63
-166
lines changed

Detokenizer.cs

Lines changed: 15 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using System.Globalization;
33
using System.Linq;
44
using System.Text;
5-
using System.Threading.Tasks;
65

76
namespace TeamControlium.Utilities
87
{
@@ -161,10 +160,10 @@ private static string ProcessToken(string token)
161160
}
162161
break;
163162
}
163+
164164
return processedToken;
165165
}
166166

167-
168167
private static string DoRandomToken(char delimiter, string TypeAndLength)
169168
{
170169
string[] typeAndLengthOrFormat = TypeAndLength.Split(new char[] { delimiter }, 2);
@@ -206,26 +205,20 @@ private static string DoRandomToken(char delimiter, string TypeAndLength)
206205
select = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890";
207206
break;
208207
case "acn":
209-
{
210-
string acn = ProcessTokensInString("{random;digits;9}");
211-
return acn;
212-
break;
213-
}
208+
return ProcessTokensInString("{random;digits;9}");
214209
case "abn":
215210
{
216211
string acn = ProcessTokensInString("{random;acn}");
217-
result = ProcessTokensInString($"{{ABNFromACN;{acn}}}");
218-
return result;
219-
break;
212+
return ProcessTokensInString($"{{ABNFromACN;{acn}}}");
220213
}
221214
default:
222215
throw new Exception($"Unrecognised random Type [{typeAndLengthOrFormat[0]}] - Expect letters, lowercaseletters, uppercaseletters digits or alphanumerics");
223216
}
224217
}
225-
int number;
226-
if (!int.TryParse(typeAndLengthOrFormat[1], out number)) throw new Exception($"Invalid number of characters in Random token {{random;<type>;<length>}}");
218+
if (!int.TryParse(typeAndLengthOrFormat[1], out int number)) throw new Exception($"Invalid number of characters in Random token {{random;<type>;<length>}}");
227219
result = new string(Enumerable.Repeat(select, number).Select(s => s[RandomGenerator.Next(s.Length)]).ToArray());
228220
}
221+
229222
return result;
230223
}
231224

@@ -237,6 +230,7 @@ private static string DoDateToken(char delimiter, string OffsetAndFormat)
237230
{
238231
throw new Exception("Date token does not have a format parameter; example: {date" + delimiter + "today" + delimiter + "dd-MM-yyyy}");
239232
}
233+
240234
DateTime dt;
241235
string verb = offsetAndFormat[0].ToLower().Trim();
242236
if (verb.StartsWith("random("))
@@ -284,31 +278,15 @@ private static string DoDateToken(char delimiter, string OffsetAndFormat)
284278
}
285279
}
286280
}
281+
287282
return dt.ToString(offsetAndFormat[1]);
288283
}
289284

290-
//private static string DoSeleniumKey(string KeyName)
291-
//{
292-
// try
293-
// {
294-
// //
295-
// // Selenium keys are static fields in the WebDriver Keys class.
296-
// //
297-
// return (string)typeof(Keys).GetField(KeyName).GetValue(null);
298-
// }
299-
// catch (Exception ex)
300-
// {
301-
// throw new ArgumentException($"[{KeyName ?? "null!!"}] not found as a field in Selenium Keys class", "KeyName");
302-
// }
303-
//}
304-
305285
private static string DoFinancialYearToken(char delimiter, string DateToWorkFromAndFormat, bool Start)
306286
{
307287
string[] dateToWorkFromAndFormat = DateToWorkFromAndFormat.Split(new char[] { delimiter }, 2);
308288

309-
310-
DateTime dateToWorkFrom;
311-
if (!DateTime.TryParseExact(dateToWorkFromAndFormat[0], new string[] { "dd/MM/yyyy", "d/MM/yyyy", "dd/M/yyyy", "d/M/yyyy", "dd/MM/yy", "d/MM/yy", "dd/M/yy", "d/M/yy" }, CultureInfo.InvariantCulture, DateTimeStyles.None, out dateToWorkFrom))
289+
if (!DateTime.TryParseExact(dateToWorkFromAndFormat[0], new string[] { "dd/MM/yyyy", "d/MM/yyyy", "dd/M/yyyy", "d/M/yyyy", "dd/MM/yy", "d/MM/yy", "dd/M/yy", "d/M/yy" }, CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime dateToWorkFrom))
312290
{
313291
throw new ArgumentException("Cannot parse date. Must be in format d/M/y", "DateToWorkFromAndFormat (first element)");
314292
}
@@ -329,16 +307,15 @@ static private float DoRandomFloat(string MaxAndMinFloats)
329307
string[] MaxAndMin = MaxAndMinFloats.Split(delimiter);
330308
if (MaxAndMin.Length != 2)
331309
throw new Exception($"Invalid Maximum and Minimum floats. Expect {{random.float(min;max),<format>}}. Max/min was: [{MaxAndMinFloats}]");
332-
float Min;
333-
float Max;
334310

335-
if (!float.TryParse(MaxAndMin[0], out Min))
311+
if (!float.TryParse(MaxAndMin[0], out float Min))
336312
throw new Exception($"Invalid Minimum float. Expect {{random.float(min;max),<format>}}. Max/min was: [{MaxAndMinFloats}]");
337-
if (!float.TryParse(MaxAndMin[1], out Max))
313+
if (!float.TryParse(MaxAndMin[1], out float Max))
338314
throw new Exception($"Invalid Maximum float. Expect {{random.float(min;max),<format>}}. Max/min was: [{MaxAndMinFloats}]");
339315

340316
return DoRandomFloat(Min, Max);
341317
}
318+
342319
static public float DoRandomFloat(float MinFloat, float MaxFloat)
343320
{
344321
if (MinFloat >= MaxFloat)
@@ -352,21 +329,20 @@ static private DateTime DoRandomDate(string MaxAndMinDates)
352329
string[] MaxAndMin = MaxAndMinDates.Split(delimiter);
353330
if (MaxAndMin.Length != 2)
354331
throw new Exception($"Invalid Maximum and Minimum dates. Expect {{random;date(dd-MM-yyyy,dd-MM-yyyy);<format>}}. Max/min was: [{MaxAndMinDates}]");
355-
DateTime Min;
356-
DateTime Max;
357332

358-
if (!DateTime.TryParseExact(MaxAndMin[0], "d-M-yyyy", CultureInfo.InstalledUICulture, DateTimeStyles.None, out Min))
333+
if (!DateTime.TryParseExact(MaxAndMin[0], "d-M-yyyy", CultureInfo.InstalledUICulture, DateTimeStyles.None, out DateTime Min))
359334
throw new Exception($"Invalid Minimum date. Expect {{random;date(dd-MM-yyyy,dd-MM-yyyy);<format>}}. Max/min was: [{MaxAndMinDates}]");
360-
if (!DateTime.TryParseExact(MaxAndMin[1], "d-M-yyyy", CultureInfo.InstalledUICulture, DateTimeStyles.None, out Max))
335+
if (!DateTime.TryParseExact(MaxAndMin[1], "d-M-yyyy", CultureInfo.InstalledUICulture, DateTimeStyles.None, out DateTime Max))
361336
throw new Exception($"Invalid Maximum date. Expect {{random;date(dd-MM-yyyy,dd-MM-yyyy);<format>}}. Max/min was: [{MaxAndMinDates}]");
362337

363338
return DoRandomDate(Min, Max);
364339
}
340+
365341
static public DateTime DoRandomDate(DateTime MinDate, DateTime MaxDate)
366342
{
367343
if (MinDate > MaxDate)
368344
throw new Exception($"Maximum date earlier than Maximum date! Expect {{random;date(dd-MM-yyyy,dd-MM-yyyy);<format>}} Mindate = {MinDate.ToString("dd/MM/yyyy")}, Maxdate = {MaxDate.ToString("dd/MM/yyyy")}");
369345
return MinDate.AddDays(RandomGenerator.Next((MaxDate - MinDate).Days));
370346
}
371347
}
372-
}
348+
}

General.cs

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,12 @@
22
using System;
33
using System.Collections.Generic;
44
using System.Linq;
5-
using System.Text;
65
using System.Text.RegularExpressions;
76

87
namespace TeamControlium.Utilities
98
{
109
public class General
1110
{
12-
/// <summary>
13-
/// Delegate for processing Tokens.
14-
/// </summary>
15-
public static Func<string, string> TokenProcessor;
1611

1712
/// <summary>
1813
/// If object is a string and <see cref="Settings.TokenProcessor"/> has been set, tokens with the string are processed
@@ -23,14 +18,6 @@ public class General
2318
/// <returns>Processed object</returns>
2419
public static T DetokeniseString<T>(T ObjectToProcess)
2520
{
26-
//
27-
// Make sure we are thread-safe. There is a possibility that the framework is in a multi-threaded apartment (MTA) and it is possible
28-
// for TokenProcessor to become null between checking and use ( if (TokenProcessor!=null) option = (T)(object)TokenProcessor((string)(object)option); ).
29-
// Using the processor temporary variable forces .NET to make a copy of the handler.
30-
// See https://blogs.msdn.microsoft.com/ericlippert/2009/04/29/events-and-races/ for details.
31-
//
32-
Func<string, string> processor = null;
33-
3421
// Only do any processing if the object is a string.
3522
if (typeof(T) == typeof(String))
3623
{
@@ -44,18 +31,9 @@ public static T DetokeniseString<T>(T ObjectToProcess)
4431
while (!StringToProcess.Equals(ProcessedString))
4532
{
4633
StringToProcess = string.Copy(ProcessedString);
47-
processor = TokenProcessor;
48-
if (processor != null)
49-
{
50-
ProcessedString = processor(StringToProcess);
51-
Logger.WriteLine(Logger.LogLevels.FrameworkDebug, "Processed [{0}] to [{1}]", StringToProcess, ProcessedString ?? string.Empty);
52-
}
53-
else
54-
{
55-
Logger.WriteLine(Logger.LogLevels.FrameworkDebug, "No Token Processor active. String not processed");
56-
ProcessedString = StringToProcess;
57-
}
58-
processor = null;
34+
35+
ProcessedString = Detokenizer.ProcessTokensInString(StringToProcess);
36+
Logger.WriteLine(Logger.LogLevels.FrameworkDebug, "Processed [{0}] to [{1}]", StringToProcess, ProcessedString ?? string.Empty);
5937
}
6038
return (T)(object)ProcessedString;
6139
}
@@ -74,8 +52,7 @@ public static T DetokeniseString<T>(T ObjectToProcess)
7452
public static bool IsValueTrue(string Value)
7553
{
7654
if (string.IsNullOrEmpty(Value)) return false;
77-
int i;
78-
if (int.TryParse(Value, out i))
55+
if (int.TryParse(Value, out int i))
7956
if (i > 0) return true; else return false;
8057
return Value.ToLower().StartsWith("t") || Value.ToLower().StartsWith("y") || Value.ToLower().StartsWith("on");
8158
}
@@ -147,6 +124,5 @@ public static string GetTextFromHTML(string HtmlData)
147124

148125
return document.DocumentNode.InnerHtml;
149126
}
150-
151127
}
152128
}

Logger.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ static public void LogException(Exception ex, string text, params Object[] args)
188188
static public void Write(LogLevels logLevel, string textString, params Object[] args)
189189
{
190190
StackFrame stackFrame = new StackFrame(1, true);
191-
DoWrite((stackFrame == null) ? null : stackFrame.GetMethod(), logLevel, string.Format(textString, args));
191+
DoWrite(stackFrame?.GetMethod(), logLevel, string.Format(textString, args));
192192
}
193193

194194
/// <summary>
@@ -206,8 +206,7 @@ static public void Write(LogLevels logLevel, string textString, params Object[]
206206
static public void WriteLine(LogLevels logLevel, string textString, params Object[] args)
207207
{
208208
StackFrame stackFrame = new StackFrame(1, true);
209-
DoWriteLine((stackFrame == null) ? null : stackFrame.GetMethod(), logLevel,
210-
string.Format(textString, args));
209+
DoWriteLine(stackFrame?.GetMethod(), logLevel, string.Format(textString, args));
211210
}
212211

213212
/// <summary>

TestArguments.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Collections.Specialized;
4-
using System.Linq;
5-
using System.Text;
1+
using System.Collections.Specialized;
62
using System.Text.RegularExpressions;
73

84
namespace TeamControlium.Utilities
95
{
106
/// <summary>
117
/// Processes test command-line arguments and presents them to the test script as a string array
128
/// </summary>
9+
/// <remarks>
10+
/// Thanks to Mike Burns (https://www.linkedin.com/in/maddogmikeb) for original
11+
/// </remarks>
1312
public class TestArguments
1413
{
1514
// Variables

TestData.cs

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
63

74
namespace TeamControlium.Utilities
85
{
96
public class TestData
107
{
11-
private static TestDataRepository<dynamic> dataRepository = new TestDataRepository<dynamic>();
128
private static Dictionary<string, Dictionary<string, dynamic>> testData = new Dictionary<string, Dictionary<string, dynamic>>();
139

1410
/// <summary>
@@ -31,15 +27,8 @@ public class TestData
3127
/// string myString = Utilities.TestData.Repository["MyCategory","MyName"];
3228
/// </code>
3329
/// </example>
34-
public static TestDataRepository<dynamic> Repository
35-
{
36-
get
37-
{
38-
return dataRepository;
39-
}
40-
}
41-
42-
30+
public static TestDataRepository<dynamic> Repository { get; } = new TestDataRepository<dynamic>();
31+
4332
/// <summary>
4433
/// Underlying TestData repository
4534
/// </summary>
@@ -87,9 +76,10 @@ public bool HasCategory(string key)
8776
throw new ArgumentException(string.Format("Cannot be null or empty ({0})", name == null ? "Is Null" : "Is empty"), "name");
8877
if (!this[category].ContainsKey(name))
8978
throw new ArgumentOutOfRangeException("name", name, string.Format("Category [{0}] does not have item named", category));
79+
9080
// Get item named from category and return it
9181
dynamic obtainedObject = (this[category])[name];
92-
// Do logging
82+
9383
Logger.Write(Logger.LogLevels.FrameworkDebug, "Got ");
9484
if (obtainedObject is string)
9585
{
@@ -132,7 +122,7 @@ public bool HasCategory(string key)
132122
// Add Category if we dont already have it
133123
if (!testData.ContainsKey(category))
134124
testData.Add(category, new Dictionary<string, dynamic>());
135-
// Select Category
125+
136126
Dictionary<string, dynamic> wholeCategory = testData[category];
137127
// Add Name if we dont already have it in the current category, otherwise change contents of name
138128
if (wholeCategory.ContainsKey(name))
@@ -157,6 +147,7 @@ public bool HasCategory(string key)
157147
throw new ArgumentException(string.Format("Cannot be null or empty ({0})", category == null ? "Is Null" : "Is empty"), "Category");
158148
if (!testData.ContainsKey(category))
159149
throw new ArgumentOutOfRangeException("category", category, "Category does not exist");
150+
160151
var wholeCategory = testData[category];
161152
Logger.WriteLine(Logger.LogLevels.FrameworkDebug, "Got category [{0}] ({1} items)", category, wholeCategory.Count);
162153
return wholeCategory;
@@ -168,6 +159,7 @@ public bool HasCategory(string key)
168159
// Add Category if we dont already have it
169160
if (!testData.ContainsKey(category))
170161
testData.Add(category, new Dictionary<string, dynamic>());
162+
171163
Logger.WriteLine(Logger.LogLevels.FrameworkDebug, "Setting category [{0}] ({1} items)", category, value.Count);
172164
testData[category] = value;
173165
}
@@ -180,9 +172,11 @@ public bool HasCategory(string key)
180172
/// <returns>Object of Category and Name</returns>
181173
public U GetItem<U>(string category, string name)
182174
{
175+
dynamic obtainedObject;
176+
183177
try
184178
{
185-
dynamic obtainedObject = this[category, name];
179+
obtainedObject = this[category, name];
186180

187181
if (obtainedObject is U)
188182
{
@@ -206,15 +200,14 @@ public U GetItem<U>(string category, string name)
206200
}
207201
catch { }
208202
}
209-
210-
throw new Exception(string.Format("Expected type [{0}] but got type [{1}].", typeof(U).Name, obtainedObject.GetType()));
211203
}
212204
catch (Exception ex)
213205
{
214206
throw new Exception(string.Format("Exception getting Category.Name ([{0}].[{1}])", category, name), ex);
215207
}
216-
}
217208

209+
throw new Exception(string.Format("Expected type [{0}] but got type [{1}].", typeof(U).Name, obtainedObject.GetType()));
210+
}
218211

219212
/// <summary>Returns named option from named category</summary>
220213
/// <param name="category">Category to obtain option from</param>
@@ -275,8 +268,6 @@ public Dictionary<string, dynamic> GetCategoryItems(string Category)
275268
throw new Exception(string.Format("Exception getting itms in Category ([{0}])", Category), ex);
276269
}
277270
}
278-
279271
}
280272
}
281-
282-
}
273+
}

0 commit comments

Comments
 (0)