Skip to content

Commit b02a904

Browse files
committed
- added extensions to generate random items and numbers from collections
1 parent 4f21bb1 commit b02a904

File tree

7 files changed

+45
-11
lines changed

7 files changed

+45
-11
lines changed

ExtendFurther.Tests/CollectionExtensionTests.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,15 @@ class CollectionExtensionTests
1010
[TestCase(new string[] { "" }, true)]
1111
[TestCase(new string[] { "a", "b" }, true)]
1212
[TestCase(new string[] { "a", "b", "b" }, false)]
13-
public void ExtensionListExtensionDeterminesIfItemsInListAreDistinct(string[] s, bool isDistinct)
13+
public void Extension_ListExtension_IsDistinct_Determines_If_Items_In_List_Are_Distinct(string[] s, bool isDistinct)
1414
{
1515
Assert.That(s.ToList().IsDistinct, Is.EqualTo(isDistinct));
1616
}
17+
18+
[TestCase(new string[] { "a", "b", "c", "d" }, null)]
19+
public void Extension_ListExtension_Rand(string[] values, int i)
20+
{
21+
Assert.That(values.Contains(values.Rand()));
22+
}
1723
}
1824
}

ExtendFurther.Tests/NumericalExtensionTests.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,14 @@ public void Extension_NumericalExtension_Rnd_Rounds_To_Expected_Decimal_Places(d
4848
}
4949

5050
[Test]
51-
[TestCase(0, "This Month")]
52-
[TestCase(1, "Last Month")]
53-
[TestCase(12, "This Year")]
54-
[TestCase(24, "Last Year")]
55-
[TestCase(50, "Last 50 Months")]
56-
public void Extension_StringExtension_ToDateRangeFilterOption(int i, string expectedResult)
51+
[TestCase(0, 0)]
52+
[TestCase(0, 10)]
53+
[TestCase(0, 1)]
54+
[TestCase(0, 1)]
55+
[TestCase(5, 100)]
56+
public void Extension_NumericalExtension_Rand(int i, int j)
5757
{
58-
Assert.That(i.ToDateRangeFilterOption(), Is.EqualTo(expectedResult));
58+
Assert.That(i.RandPos(j), Is.GreaterThanOrEqualTo(i).And.LessThanOrEqualTo(j));
5959
}
6060
}
6161
}

ExtendFurther.Tests/StringExtensionTests.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,18 @@ public void Extension_StringExtension_Truncate(string s, int maxChars, string ex
9595
Assert.That(s.Truncate(maxChars), Is.EqualTo(expectedResult));
9696
}
9797

98+
99+
[Test]
100+
[TestCase(0, "This Month")]
101+
[TestCase(1, "Last Month")]
102+
[TestCase(12, "This Year")]
103+
[TestCase(24, "Last Year")]
104+
[TestCase(50, "Last 50 Months")]
105+
public void Extension_StringExtension_ToDateRangeFilterOption(int i, string expectedResult)
106+
{
107+
Assert.That(i.ToDateRangeFilterOption(), Is.EqualTo(expectedResult));
108+
}
109+
98110
}
99111
}
100112

ExtendFurther/CollectionExtensions.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,12 @@ public static List<T> DequeueMany<T>(this Queue<T> queue, int chunkSize)
4141

4242
return values;
4343
}
44+
4445
public static string Get(this Dictionary<string, object> d, string key)
4546
{
4647
return d.ContainsKey(key) ? d[key]?.ToString() : string.Empty;
4748
}
49+
4850
public static List<KeyValuePair<int, int>> ToKeyValuePair(this int[,] array)
4951
{
5052
var result = new List<KeyValuePair<int, int>>();
@@ -55,17 +57,25 @@ public static List<KeyValuePair<int, int>> ToKeyValuePair(this int[,] array)
5557

5658
return result;
5759
}
60+
5861
public static List<int> Enumerator(this int i)
5962
{
6063
return Enumerable.Range(0, Math.Abs(i)).ToList().Select(j => i < 0 ? j * -1 : j).ToList();
6164
}
65+
6266
public static bool IsDistinct(this List<string> s)
6367
{
6468
return s?.Distinct().Count() == s?.Count();
6569
}
70+
6671
public static IDictionary<TKey, TValue> AsReadOnly<TKey, TValue>(this IDictionary<TKey, TValue> dictionary)
6772
{
6873
return new ReadOnlyDictionary<TKey, TValue>(dictionary);
6974
}
75+
76+
public static string Rand(this IEnumerable<string> values)
77+
{
78+
return values.ElementAt(0.RandPos(values.Count() - 1));
79+
}
7080
}
7181
}

ExtendFurther/NumericalExtensions.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,15 @@ public static decimal Rnd(this decimal d, int i)
2828
{
2929
return Math.Round(d, i);
3030
}
31+
3132
public static string ToDateRangeFilterOption(this int i)
3233
{
3334
return i <= 0 ? "This Month" : i == 1 ? "Last Month" : i <= 12 ? "This Year" : i <= 24 ? "Last Year" : $"Last {i} Months";
3435
}
36+
37+
public static int RandPos(this int i, int j)
38+
{
39+
return (new Random(Guid.NewGuid().GetHashCode())).Next(i, j + 1);
40+
}
3541
}
3642
}

ExtendFurther/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@
3232
// You can specify all the values or you can default the Build and Revision Numbers
3333
// by using the '*' as shown below:
3434
// [assembly: AssemblyVersion("1.0.*")]
35-
[assembly: AssemblyVersion("1.0.2.*")]
36-
[assembly: AssemblyFileVersion("1.0.2.0")]
35+
[assembly: AssemblyVersion("1.0.3.*")]
36+
[assembly: AssemblyFileVersion("1.0.3.0")]

Package.nuspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<package >
33
<metadata>
44
<id>ExtendFurther</id>
5-
<version>1.0.2</version>
5+
<version>1.0.3</version>
66
<authors>itcombox@gmail.com</authors>
77
<owners>itcombox@gmail.com</owners>
88
<licenseUrl>https://github.com/itb2k13/ExtendFurther/blob/master/LICENSE</licenseUrl>

0 commit comments

Comments
 (0)