Skip to content

Commit

Permalink
Merge pull request #47 from sharpcode-it/develop
Browse files Browse the repository at this point in the history
Merge Develop into Master: Include Recent Features and Enhancements
  • Loading branch information
iscifoni authored Nov 25, 2024
2 parents 9ba05d0 + 803338f commit 7ed0543
Show file tree
Hide file tree
Showing 9 changed files with 252 additions and 32 deletions.
18 changes: 14 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
[![Github license](mit.svg)](https://github.com/sharpcode-it/SharpHelpers/blob/master/LICENSE)
# SharpHelpers (SharpCoding Community Library)

[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
[![Nuget](https://img.shields.io/nuget/v/SharpHelpers?style=plastic)](https://www.nuget.org/packages/SharpHelpers)
[![issues - SharpHelpers](https://img.shields.io/github/issues/sharpcode-it/SharpHelpers)](https://github.com/sharpcode-it/SharpHelpers/issues)
[![stars - SharpHelpers](https://img.shields.io/github/stars/sharpcode-it/SharpHelpers?style=social)](https://github.com/sharpcode-it/SharpHelpers)

|Version|Status|
|:-:|:-:|
|Develop|[![.NET Core V2](https://github.com/sharpcode-it/SharpHelpers/actions/workflows/wfnetcorev2.yaml/badge.svg?branch=develop)](https://github.com/sharpcode-it/SharpHelpers/actions/workflows/wfnetcorev2.yaml)|
|Master|![.NET Core](https://github.com/sharpcode-it/SharpHelpers/workflows/.NET%20Core/badge.svg?branch=master)|
|v1.0|![.NET Core](https://github.com/sharpcode-it/SharpHelpers/workflows/.NET%20Core/badge.svg?branch=v1.0)|

# SharpHelpers (SharpCoding Community Library)
--------------------------------------
## What is this?

SharpHelpers is a collections of some handy code packages and tutorials to make developer's life easier.

Get SharpHelpers:

git clone git://github.com/sharpcodingIT/SharpHelpers/
`git clone git://github.com/sharpcodingIT/SharpHelpers/`

--------------------------------------
## What do i find?

The library contains a series of Helpers, under MIT license for use and consumption of any developer.
The project contains several helpers for the manipulation and management of different types of primitive type:
The project includes various helpers designed for the manipulation and management of different primitive types:
- Boolean
- Byte
- Enum
Expand All @@ -35,6 +39,7 @@ The project contains several helpers for the manipulation and management of diff
- DateTime
- Regex
- XmlDocument
- DataTable

--------------------------------------
## Contributing
Expand All @@ -46,6 +51,11 @@ Want to contribute? Great! Here are a few guidelines.
3. All code should have a unit test. If you make a feature, there should be significant tests around the feature. If you do a bug fix, there should be a test specific to that bug so it doesn't happen again.
4. Pull requests should have a single commit. If you have multiple commits, squash them into a single commit before requesting a pull.
5. Try and follow the code styling already in place.

* [Setting up Git](https://docs.github.com/en/get-started/getting-started-with-git/set-up-git)
* [Fork the repository](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/fork-a-repo)
* [Open an issue](https://github.com/sharpcode-it/SharpHelpers/issues) if you encounter a bug or have a suggestion for improvements/features

--------------------------------------
### License

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// (c) 2023 SharpCoding
// (c) 2023 SharpCoding
// This code is licensed under MIT license (see LICENSE.txt for details)
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
Expand Down
179 changes: 163 additions & 16 deletions SharpHelpers/SharpHelpers.UnitTest/Dictionary/DictionaryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,184 @@
using SharpCoding.SharpHelpers;
using System;
using System.Collections.Generic;
using System.Linq;

namespace SharpHelpers.UnitTest.Dictionary
{

[TestClass]
public class DictionaryTest
{
[TestMethod]
public void TestAddFormat()
public void AddFormat_ShouldAddFormattedStringToDictionary()
{
var dic = new Dictionary<int, DateTime>() { { 1, DateTime.Now }, { 2, DateTime.Now } };

// Arrange
var dictionary = new Dictionary<int, string>();

// Act
dictionary.AddFormat(1, "Hello, {0}!", "World");

// Assert
Assert.AreEqual("Hello, World!", dictionary[1]);
}

[TestMethod]
public void TestRemoveAll()
[ExpectedException(typeof(ArgumentNullException))]
public void AddFormat_ShouldThrowArgumentNullException_WhenDictionaryIsNull()
{
var dic = new Dictionary<int, DateTime>() { { 1, DateTime.Now }, { 2, DateTime.Now } };
dic.RemoveAll(a=> a.Key < 2 );
Assert.IsTrue(dic.Count() == 1);
// Arrange
Dictionary<int, string> dictionary = null;

// Act
dictionary.AddFormat(1, "Hello, {0}!", "World");

// Assert - [ExpectedException] handles the assertion
}

[TestMethod]
public void TestGetOrCreate()
public void RemoveAll_ShouldRemoveItemsBasedOnCondition()
{
var dic = new Dictionary<int,DateTime>() { { 1, DateTime.Now }, { 2, DateTime.Now} };
Assert.IsNotNull(dic.GetOrCreate(1));
Assert.IsNotNull(dic.GetOrCreate(3));
// Arrange
var dictionary = new Dictionary<int, string>
{
{ 1, "Apple" },
{ 2, "Banana" },
{ 3, "Avocado" }
};


// Act
dictionary.RemoveAll(kvp => kvp.Value.StartsWith("A"));

// Assert
Assert.AreEqual(1, dictionary.Count);
Assert.IsTrue(dictionary.ContainsKey(2));
}
}

}
[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void RemoveAll_ShouldThrowArgumentNullException_WhenDictionaryIsNull()
{
// Arrange
Dictionary<int, string> dictionary = null;

// Act
dictionary.RemoveAll(kvp => kvp.Value.StartsWith("A"));

// Assert - [ExpectedException] handles the assertion
}

[TestMethod]
public void GetOrCreate_ShouldCreateAndReturnNewValue_WhenKeyDoesNotExist()
{
// Arrange
var dictionary = new Dictionary<int, List<string>>();

// Act
var result = dictionary.GetOrCreate(1);

// Assert
Assert.IsNotNull(result);
Assert.AreEqual(0, result.Count);
Assert.IsTrue(dictionary.ContainsKey(1));
}

[TestMethod]
public void AddOrUpdate_ShouldAddNewItem_WhenKeyDoesNotExist()
{
// Arrange
var dictionary = new Dictionary<int, string>();

// Act
dictionary.AddOrUpdate(1, "NewValue");

// Assert
Assert.AreEqual("NewValue", dictionary[1]);
}

[TestMethod]
public void AddOrUpdate_ShouldUpdateExistingItem_WhenKeyExists()
{
// Arrange
var dictionary = new Dictionary<int, string>
{
{ 1, "OldValue" }
};

// Act
dictionary.AddOrUpdate(1, "UpdatedValue");

// Assert
Assert.AreEqual("UpdatedValue", dictionary[1]);
}

[TestMethod]
public void RemoveIfExists_ShouldReturnTrueAndRemoveItem_WhenKeyExists()
{
// Arrange
var dictionary = new Dictionary<int, string>
{
{ 1, "Value" }
};

// Act
var result = dictionary.RemoveIfExists(1);

// Assert
Assert.IsTrue(result);
Assert.IsFalse(dictionary.ContainsKey(1));
}

[TestMethod]
public void RemoveIfExists_ShouldReturnFalse_WhenKeyDoesNotExist()
{
// Arrange
var dictionary = new Dictionary<int, string>();

// Act
var result = dictionary.RemoveIfExists(1);

// Assert
Assert.IsFalse(result);
}

[TestMethod]
public void Merge_ShouldMergeDictionariesAndUpdateValues()
{
// Arrange
var dictionary = new Dictionary<int, string>
{
{ 1, "Value1" },
{ 2, "Value2" }
};

var otherDictionary = new Dictionary<int, string>
{
{ 2, "UpdatedValue2" },
{ 3, "Value3" }
};

// Act
dictionary.Merge(otherDictionary);

// Assert
Assert.AreEqual(3, dictionary.Count);
Assert.AreEqual("UpdatedValue2", dictionary[2]);
Assert.AreEqual("Value3", dictionary[3]);
}

[TestMethod]
public void ToReadableString_ShouldReturnFormattedStringRepresentationOfDictionary()
{
// Arrange
var dictionary = new Dictionary<int, string>
{
{ 1, "Value1" },
{ 2, "Value2" }
};

// Act
var result = dictionary.ToReadableString();

// Assert
Assert.AreEqual("{1: Value1, 2: Value2}", result);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.5.2" />
<PackageReference Include="MSTest.TestFramework" Version="3.5.2" />
</ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion SharpHelpers/SharpHelpers/DateTimeHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,4 @@ public static bool IsLeapYear(this DateTime dateTime)
return DateTime.IsLeapYear(year);
}
}
}
}
57 changes: 57 additions & 0 deletions SharpHelpers/SharpHelpers/DictionaryHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,63 @@ public static TValue GetOrCreate<TKey, TValue>(this IDictionary<TKey, TValue> di
dictionary[key] = ret;
}
return ret;
}

/// <summary>
/// Tries to add a key-value pair to the dictionary. If the key already exists, it updates the value.
/// </summary>
/// <param name="dictionary">The dictionary to operate on.</param>
/// <param name="key">The key to add or update.</param>
/// <param name="value">The value to associate with the key.</param>
public static void AddOrUpdate<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key, TValue value)
{
if (dictionary.ContainsKey(key))
{
dictionary[key] = value;
}
else
{
dictionary.Add(key, value);
}
}

/// <summary>
/// Removes the entry with the specified key if it exists in the dictionary, and returns a boolean indicating success.
/// </summary>
/// <param name="dictionary">The dictionary to operate on.</param>
/// <param name="key">The key to remove.</param>
/// <returns>True if the key was found and removed, otherwise false.</returns>
public static bool RemoveIfExists<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key)
{
return dictionary.Remove(key);
}

/// <summary>
/// Merges the entries from another dictionary into the current dictionary. If a key already exists, its value is updated.
/// </summary>
/// <param name="dictionary">The dictionary to operate on.</param>
/// <param name="otherDictionary">The dictionary to merge from.</param>
public static void Merge<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, Dictionary<TKey, TValue> otherDictionary)
{
foreach (var kvp in otherDictionary)
{
dictionary.AddOrUpdate(kvp.Key, kvp.Value);
}
}

/// <summary>
/// Converts the dictionary into a readable string format, useful for debugging.
/// </summary>
/// <param name="dictionary">The dictionary to convert to a string.</param>
/// <returns>A string representation of the dictionary.</returns>
public static string ToReadableString<TKey, TValue>(this Dictionary<TKey, TValue> dictionary)
{
var entries = new List<string>();
foreach (var kvp in dictionary)
{
entries.Add($"{kvp.Key}: {kvp.Value}");
}
return "{" + string.Join(", ", entries) + "}";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using Newtonsoft.Json;
using System.Text.Json;

namespace SharpCoding.SharpHelpers.ObjectExtensions
{
Expand All @@ -16,7 +16,7 @@ public static class ObjectSerializationHelper
/// <returns></returns>
public static string SerializeToJson(this object istance)
{
return istance == null ? string.Empty : JsonConvert.SerializeObject(istance);
return istance == null ? string.Empty : JsonSerializer.Serialize(istance);
}

/// <summary>
Expand All @@ -27,7 +27,7 @@ public static string SerializeToJson(this object istance)
/// <returns></returns>
public static T DeserializeFromJson<T>(this string istance) where T : class
{
return string.IsNullOrEmpty(istance) ? default : JsonConvert.DeserializeObject<T>(istance);
return string.IsNullOrEmpty(istance) ? default : JsonSerializer.Deserialize<T>(istance);
}

/// <summary>
Expand Down
7 changes: 4 additions & 3 deletions SharpHelpers/SharpHelpers/SharpHelpers.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@
<PackageIcon>ico.png</PackageIcon>
<PackageIconUrl />
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>

<ItemGroup>
<None Include="..\..\LICENSE">
Expand All @@ -38,4 +35,8 @@
<ItemGroup>
<Compile Remove="RegistryHelpers.cs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="System.Text.Json" Version="8.0.4" />
</ItemGroup>
</Project>
Loading

0 comments on commit 7ed0543

Please sign in to comment.