Skip to content

Commit

Permalink
Merge branch '2.1'
Browse files Browse the repository at this point in the history
# Conflicts:
#	lib12/lib12.csproj
  • Loading branch information
kkalinowski committed Jul 7, 2018
2 parents 34f826d + f0918f1 commit 53aa81f
Show file tree
Hide file tree
Showing 12 changed files with 239 additions and 7 deletions.
36 changes: 36 additions & 0 deletions lib12.Tests/Checking/ObjectCheckingExtensionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,18 +85,54 @@ public void is_int_test()
4.IsNot(5, 6, 6).ShouldBeTrue();
}

[Fact]
public void is_string_test()
{
"test".Is("test").ShouldBeTrue();
}

[Fact]
public void is_handles_null_as_target_to_compare_test()
{
"test".Is((string)null).ShouldBeFalse();
}

[Fact]
public void is_handles_null_as_source_test()
{
((string)null).Is("test").ShouldBeFalse();
}

[Fact]
public void is_returns_true_if_both_source_and_target_are_null_test()
{
((string)null).Is((string)null).ShouldBeTrue();
}

[Fact]
public void in_test()
{
var array = new[] { 3, 4, 12 };
12.In(array).ShouldBeTrue();
}

[Fact]
public void if_in_collection_is_null_return_false_test()
{
12.In(null).ShouldBeFalse();
}

[Fact]
public void not_in_test()
{
var array = new[] { 3, 4, 12 };
11.NotIn(array).ShouldBeTrue();
}

[Fact]
public void if_notin_collection_is_null_return_true_test()
{
12.NotIn(null).ShouldBeTrue();
}
}
}
31 changes: 31 additions & 0 deletions lib12.Tests/Extensions/StringExtensionTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;
using lib12.Extensions;
using Shouldly;
using Xunit;
Expand Down Expand Up @@ -144,5 +145,35 @@ public void remove_diacritics_happy_path()

withDiacritics.RemoveDiacritics().ShouldBe(withoutDiacritics);
}

[Theory]
[InlineData(null, null, 0)]
[InlineData("", null, 0)]
[InlineData(null, "", 0)]
[InlineData("", "", 0)]
[InlineData("", "", 0)]
[InlineData("test", "t", 2)]
[InlineData("test", "ch", 0)]
[InlineData("It will be long and harsh winter! Better be prepared!", "be", 2)]
public void get_number_of_occurences_theory(string source, string text, int expectedResult)
{
source.GetNumberOfOccurrences(text).ShouldBe(expectedResult);
}

[Theory]
[InlineData(null, null)]
[InlineData("", null)]
[InlineData(null, "")]
[InlineData("", "")]
[InlineData("", "")]
[InlineData("test", "t", 0, 3)]
[InlineData("test", "ch")]
[InlineData("_be be", "be", 1, 4)]
public void get_all_occurences_theory(string source, string text, params int[] expectedResult)
{
var result = source.GetAllOccurrences(text);
var arr = result.ToArray();
result.ShouldBe(expectedResult);
}
}
}
21 changes: 21 additions & 0 deletions lib12.Tests/Mathematics/BoolExtensionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using lib12.Mathematics.Extensions;
using Shouldly;
using Xunit;

namespace lib12.Tests.Mathematics
{
public class BoolExtensionTests
{
[Fact]
public void to_int_returns_1_if_source_is_true()
{
true.ToInt().ShouldBe(1);
}

[Fact]
public void to_int_returns_0_if_source_is_false()
{
false.ToInt().ShouldBe(0);
}
}
}
21 changes: 21 additions & 0 deletions lib12.Tests/Mathematics/IntExtensionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using lib12.Mathematics.Extensions;
using Shouldly;
using Xunit;

namespace lib12.Tests.Mathematics
{
public class IntExtensionTests
{
[Fact]
public void to_bool_returns_false_if_given_zero()
{
0.ToBool().ShouldBeFalse();
}

[Fact]
public void to_bool_returns_true_if_given_non_zero()
{
12.ToBool().ShouldBeTrue();
}
}
}
24 changes: 24 additions & 0 deletions lib12.Tests/Reflection/TypeExtensionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using lib12.Reflection;
using Shouldly;
using Xunit;

namespace lib12.Tests.Reflection
{
public class TypeExtensionTests
{
private static class StaticClass { }
private class NonStaticClass { }

[Fact]
public void is_static_returns_true_for_static_class()
{
typeof(StaticClass).IsStatic().ShouldBeTrue();
}

[Fact]
public void is_static_returns_false_for_non_static_class()
{
typeof(NonStaticClass).IsStatic().ShouldBeFalse();
}
}
}
14 changes: 10 additions & 4 deletions lib12/Checking/ObjectCheckingExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static class ObjectCheckingExtension
/// <returns></returns>
public static bool Is<TSource>(this TSource source, TSource value)
{
return source.Equals(value);
return Equals(source, value);
}

/// <summary>
Expand All @@ -26,7 +26,7 @@ public static bool Is<TSource>(this TSource source, TSource value)
/// <returns></returns>
public static bool Is<TSource>(this TSource source, params TSource[] values)
{
return values.Any(x => source.Equals(x));
return values.Any(x => Equals(source, x));
}

/// <summary>
Expand All @@ -38,7 +38,7 @@ public static bool Is<TSource>(this TSource source, params TSource[] values)
/// <returns></returns>
public static bool IsNot<TSource>(this TSource source, TSource value)
{
return !source.Equals(value);
return !Equals(source, value);
}

/// <summary>
Expand All @@ -50,7 +50,7 @@ public static bool IsNot<TSource>(this TSource source, TSource value)
/// <returns></returns>
public static bool IsNot<TSource>(this TSource source, params TSource[] values)
{
return values.All(x => !source.Equals(x));
return values.All(x => !Equals(source, x));
}

/// <summary>
Expand All @@ -60,6 +60,9 @@ public static bool IsNot<TSource>(this TSource source, params TSource[] values)
/// <returns></returns>
public static bool In<TSource>(this TSource source, IEnumerable<TSource> collection)
{
if (collection == null)
return false;

return collection.Contains(source);
}

Expand All @@ -70,6 +73,9 @@ public static bool In<TSource>(this TSource source, IEnumerable<TSource> collect
/// <returns></returns>
public static bool NotIn<TSource>(this TSource source, IEnumerable<TSource> collection)
{
if (collection == null)
return true;

return !collection.Contains(source);
}
}
Expand Down
8 changes: 8 additions & 0 deletions lib12/Collections/IEnumerableExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@ public static class IEnumerableExtension
/// Determines whether the specified enumerable is empty.
/// </summary>
/// <param name="source">The source to check</param>
/// <exception cref="ArgumentNullException">If source is null</exception>
/// <returns>True if enumerable is empty</returns>
public static bool IsEmpty<TSource>(this IEnumerable<TSource> source)
{
if(source == null)
throw new ArgumentNullException(nameof(source));

return !source.Any();
}

Expand All @@ -31,9 +35,13 @@ public static bool IsNullOrEmpty<TSource>(this IEnumerable<TSource> source)
/// Determines whether enumerable is not empty
/// </summary>
/// <param name="source">The source to check</param>
/// <exception cref="ArgumentNullException">If source is null</exception>
/// <returns>True if enumerable is not empty</returns>
public static bool IsNotEmpty<TSource>(this IEnumerable<TSource> source)
{
if (source == null)
throw new ArgumentNullException(nameof(source));

return source.Any();
}

Expand Down
44 changes: 44 additions & 0 deletions lib12/Extensions/StringExtension.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;

Expand Down Expand Up @@ -105,5 +106,48 @@ public static string RemoveDiacritics(this string text)

return stringBuilder.ToString().Normalize(NormalizationForm.FormC);
}

/// <summary>
/// Get number of the occurrences of given string in source
/// </summary>
/// <param name="source">The source string</param>
/// <param name="text">The text to search for</param>
/// <param name="stringComparison">The string comparison method</param>
/// <returns></returns>
public static int GetNumberOfOccurrences(this string source, string text, StringComparison stringComparison = StringComparison.Ordinal)
{
if (source.IsNullOrEmpty() || text.IsNullOrEmpty())
return 0;

var count = 0;
var position = 0;
while ((position = source.IndexOf(text, position, stringComparison)) != -1)
{
position += text.Length;
count += 1;
}

return count;
}

/// <summary>
/// Get all occurrences of given string in source
/// </summary>
/// <param name="source">The source string</param>
/// <param name="text">The text to search for</param>
/// <param name="stringComparison">The string comparison method</param>
/// <returns></returns>
public static IEnumerable<int> GetAllOccurrences(this string source, string text, StringComparison stringComparison = StringComparison.Ordinal)
{
if (source.IsNullOrEmpty() || text.IsNullOrEmpty())
yield break;

var position = 0;
while ((position = source.IndexOf(text, position, stringComparison)) != -1)
{
yield return position;
position += text.Length;
}
}
}
}
15 changes: 15 additions & 0 deletions lib12/Mathematics/Extensions/BoolExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace lib12.Mathematics.Extensions
{
public static class BoolExtension
{
/// <summary>
/// Converts bool to int
/// </summary>
/// <param name="source">The source bool</param>
/// <returns>1 if true, 0 if false</returns>
public static int ToInt(this bool source)
{
return source ? 1 : 0;
}
}
}
15 changes: 15 additions & 0 deletions lib12/Mathematics/Extensions/IntExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace lib12.Mathematics.Extensions
{
public static class IntExtension
{
/// <summary>
/// Converts int to bool
/// </summary>
/// <param name="source">The source int</param>
/// <returns>True if not zero</returns>
public static bool ToBool(this int source)
{
return source != 0;
}
}
}
12 changes: 12 additions & 0 deletions lib12/Reflection/TypeExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ public static bool IsNullable(this Type type)
return type.GetTypeInfo().IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>);
}

/// <summary>
/// Determines whether the specified type is static
/// </summary>
/// <param name="type">The type to check</param>
/// <returns></returns>
public static bool IsStatic(this Type type)
{
//according to https://stackoverflow.com/questions/1175888/determine-if-a-type-is-static
var typeInfo = type.GetTypeInfo();
return typeInfo.IsAbstract && typeInfo.IsSealed;
}

/// <summary>
/// Gets the attribute decorating given type
/// </summary>
Expand Down
5 changes: 2 additions & 3 deletions lib12/lib12.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
<Authors>Krzysztof Kalinowski</Authors>
<Company />
<Description>lib12 is set of useful classes and extension created for .NET framework</Description>
<AssemblyVersion>2.0.1.0</AssemblyVersion>
<FileVersion>2.0.1.0</FileVersion>
<Version>2.0.1</Version>
<Version>2.1.0.0</Version>
<AssemblyVersion>2.1.0.0</AssemblyVersion>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageLicenseUrl>https://github.com/kkalinowski/lib12/blob/master/LICENSE.md</PackageLicenseUrl>
<PackageProjectUrl>https://github.com/kkalinowski/lib12</PackageProjectUrl>
Expand Down

0 comments on commit 53aa81f

Please sign in to comment.