Skip to content

Commit 7ed0543

Browse files
authored
Merge pull request #47 from sharpcode-it/develop
Merge Develop into Master: Include Recent Features and Enhancements
2 parents 9ba05d0 + 803338f commit 7ed0543

File tree

9 files changed

+252
-32
lines changed

9 files changed

+252
-32
lines changed

README.md

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,30 @@
1-
[![Github license](mit.svg)](https://github.com/sharpcode-it/SharpHelpers/blob/master/LICENSE)
1+
# SharpHelpers (SharpCoding Community Library)
2+
3+
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
4+
[![Nuget](https://img.shields.io/nuget/v/SharpHelpers?style=plastic)](https://www.nuget.org/packages/SharpHelpers)
5+
[![issues - SharpHelpers](https://img.shields.io/github/issues/sharpcode-it/SharpHelpers)](https://github.com/sharpcode-it/SharpHelpers/issues)
6+
[![stars - SharpHelpers](https://img.shields.io/github/stars/sharpcode-it/SharpHelpers?style=social)](https://github.com/sharpcode-it/SharpHelpers)
27

38
|Version|Status|
49
|:-:|:-:|
510
|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)|
611
|Master|![.NET Core](https://github.com/sharpcode-it/SharpHelpers/workflows/.NET%20Core/badge.svg?branch=master)|
712
|v1.0|![.NET Core](https://github.com/sharpcode-it/SharpHelpers/workflows/.NET%20Core/badge.svg?branch=v1.0)|
813

9-
# SharpHelpers (SharpCoding Community Library)
1014
--------------------------------------
1115
## What is this?
1216

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

1519
Get SharpHelpers:
1620

17-
git clone git://github.com/sharpcodingIT/SharpHelpers/
21+
`git clone git://github.com/sharpcodingIT/SharpHelpers/`
1822

1923
--------------------------------------
2024
## What do i find?
2125

2226
The library contains a series of Helpers, under MIT license for use and consumption of any developer.
23-
The project contains several helpers for the manipulation and management of different types of primitive type:
27+
The project includes various helpers designed for the manipulation and management of different primitive types:
2428
- Boolean
2529
- Byte
2630
- Enum
@@ -35,6 +39,7 @@ The project contains several helpers for the manipulation and management of diff
3539
- DateTime
3640
- Regex
3741
- XmlDocument
42+
- DataTable
3843

3944
--------------------------------------
4045
## Contributing
@@ -46,6 +51,11 @@ Want to contribute? Great! Here are a few guidelines.
4651
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.
4752
4. Pull requests should have a single commit. If you have multiple commits, squash them into a single commit before requesting a pull.
4853
5. Try and follow the code styling already in place.
54+
55+
* [Setting up Git](https://docs.github.com/en/get-started/getting-started-with-git/set-up-git)
56+
* [Fork the repository](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/fork-a-repo)
57+
* [Open an issue](https://github.com/sharpcode-it/SharpHelpers/issues) if you encounter a bug or have a suggestion for improvements/features
58+
4959
--------------------------------------
5060
### License
5161

SharpHelpers/SharpHelpers.UnitTest/DateAndTime/DateTimeTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// (c) 2023 SharpCoding
1+
// (c) 2023 SharpCoding
22
// This code is licensed under MIT license (see LICENSE.txt for details)
33
using System;
44
using Microsoft.VisualStudio.TestTools.UnitTesting;

SharpHelpers/SharpHelpers.UnitTest/Dictionary/DictionaryTest.cs

Lines changed: 163 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,184 @@
44
using SharpCoding.SharpHelpers;
55
using System;
66
using System.Collections.Generic;
7-
using System.Linq;
87

98
namespace SharpHelpers.UnitTest.Dictionary
109
{
11-
1210
[TestClass]
1311
public class DictionaryTest
1412
{
1513
[TestMethod]
16-
public void TestAddFormat()
14+
public void AddFormat_ShouldAddFormattedStringToDictionary()
1715
{
18-
var dic = new Dictionary<int, DateTime>() { { 1, DateTime.Now }, { 2, DateTime.Now } };
19-
16+
// Arrange
17+
var dictionary = new Dictionary<int, string>();
18+
19+
// Act
20+
dictionary.AddFormat(1, "Hello, {0}!", "World");
21+
22+
// Assert
23+
Assert.AreEqual("Hello, World!", dictionary[1]);
2024
}
25+
2126
[TestMethod]
22-
public void TestRemoveAll()
27+
[ExpectedException(typeof(ArgumentNullException))]
28+
public void AddFormat_ShouldThrowArgumentNullException_WhenDictionaryIsNull()
2329
{
24-
var dic = new Dictionary<int, DateTime>() { { 1, DateTime.Now }, { 2, DateTime.Now } };
25-
dic.RemoveAll(a=> a.Key < 2 );
26-
Assert.IsTrue(dic.Count() == 1);
30+
// Arrange
31+
Dictionary<int, string> dictionary = null;
2732

33+
// Act
34+
dictionary.AddFormat(1, "Hello, {0}!", "World");
35+
36+
// Assert - [ExpectedException] handles the assertion
2837
}
38+
2939
[TestMethod]
30-
public void TestGetOrCreate()
40+
public void RemoveAll_ShouldRemoveItemsBasedOnCondition()
3141
{
32-
var dic = new Dictionary<int,DateTime>() { { 1, DateTime.Now }, { 2, DateTime.Now} };
33-
Assert.IsNotNull(dic.GetOrCreate(1));
34-
Assert.IsNotNull(dic.GetOrCreate(3));
42+
// Arrange
43+
var dictionary = new Dictionary<int, string>
44+
{
45+
{ 1, "Apple" },
46+
{ 2, "Banana" },
47+
{ 3, "Avocado" }
48+
};
3549

36-
50+
// Act
51+
dictionary.RemoveAll(kvp => kvp.Value.StartsWith("A"));
52+
53+
// Assert
54+
Assert.AreEqual(1, dictionary.Count);
55+
Assert.IsTrue(dictionary.ContainsKey(2));
3756
}
38-
}
3957

40-
}
58+
[TestMethod]
59+
[ExpectedException(typeof(ArgumentNullException))]
60+
public void RemoveAll_ShouldThrowArgumentNullException_WhenDictionaryIsNull()
61+
{
62+
// Arrange
63+
Dictionary<int, string> dictionary = null;
64+
65+
// Act
66+
dictionary.RemoveAll(kvp => kvp.Value.StartsWith("A"));
67+
68+
// Assert - [ExpectedException] handles the assertion
69+
}
70+
71+
[TestMethod]
72+
public void GetOrCreate_ShouldCreateAndReturnNewValue_WhenKeyDoesNotExist()
73+
{
74+
// Arrange
75+
var dictionary = new Dictionary<int, List<string>>();
76+
77+
// Act
78+
var result = dictionary.GetOrCreate(1);
79+
80+
// Assert
81+
Assert.IsNotNull(result);
82+
Assert.AreEqual(0, result.Count);
83+
Assert.IsTrue(dictionary.ContainsKey(1));
84+
}
85+
86+
[TestMethod]
87+
public void AddOrUpdate_ShouldAddNewItem_WhenKeyDoesNotExist()
88+
{
89+
// Arrange
90+
var dictionary = new Dictionary<int, string>();
91+
92+
// Act
93+
dictionary.AddOrUpdate(1, "NewValue");
94+
95+
// Assert
96+
Assert.AreEqual("NewValue", dictionary[1]);
97+
}
98+
99+
[TestMethod]
100+
public void AddOrUpdate_ShouldUpdateExistingItem_WhenKeyExists()
101+
{
102+
// Arrange
103+
var dictionary = new Dictionary<int, string>
104+
{
105+
{ 1, "OldValue" }
106+
};
107+
108+
// Act
109+
dictionary.AddOrUpdate(1, "UpdatedValue");
110+
111+
// Assert
112+
Assert.AreEqual("UpdatedValue", dictionary[1]);
113+
}
114+
115+
[TestMethod]
116+
public void RemoveIfExists_ShouldReturnTrueAndRemoveItem_WhenKeyExists()
117+
{
118+
// Arrange
119+
var dictionary = new Dictionary<int, string>
120+
{
121+
{ 1, "Value" }
122+
};
123+
124+
// Act
125+
var result = dictionary.RemoveIfExists(1);
126+
127+
// Assert
128+
Assert.IsTrue(result);
129+
Assert.IsFalse(dictionary.ContainsKey(1));
130+
}
131+
132+
[TestMethod]
133+
public void RemoveIfExists_ShouldReturnFalse_WhenKeyDoesNotExist()
134+
{
135+
// Arrange
136+
var dictionary = new Dictionary<int, string>();
137+
138+
// Act
139+
var result = dictionary.RemoveIfExists(1);
140+
141+
// Assert
142+
Assert.IsFalse(result);
143+
}
144+
145+
[TestMethod]
146+
public void Merge_ShouldMergeDictionariesAndUpdateValues()
147+
{
148+
// Arrange
149+
var dictionary = new Dictionary<int, string>
150+
{
151+
{ 1, "Value1" },
152+
{ 2, "Value2" }
153+
};
154+
155+
var otherDictionary = new Dictionary<int, string>
156+
{
157+
{ 2, "UpdatedValue2" },
158+
{ 3, "Value3" }
159+
};
160+
161+
// Act
162+
dictionary.Merge(otherDictionary);
163+
164+
// Assert
165+
Assert.AreEqual(3, dictionary.Count);
166+
Assert.AreEqual("UpdatedValue2", dictionary[2]);
167+
Assert.AreEqual("Value3", dictionary[3]);
168+
}
169+
170+
[TestMethod]
171+
public void ToReadableString_ShouldReturnFormattedStringRepresentationOfDictionary()
172+
{
173+
// Arrange
174+
var dictionary = new Dictionary<int, string>
175+
{
176+
{ 1, "Value1" },
177+
{ 2, "Value2" }
178+
};
179+
180+
// Act
181+
var result = dictionary.ToReadableString();
182+
183+
// Assert
184+
Assert.AreEqual("{1: Value1, 2: Value2}", result);
185+
}
186+
}
187+
}

SharpHelpers/SharpHelpers.UnitTest/SharpHelpers.UnitTest.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
10+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.0" />
1111
<PackageReference Include="MSTest.TestAdapter" Version="3.5.2" />
1212
<PackageReference Include="MSTest.TestFramework" Version="3.5.2" />
1313
</ItemGroup>

SharpHelpers/SharpHelpers/DateTimeHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,4 +156,4 @@ public static bool IsLeapYear(this DateTime dateTime)
156156
return DateTime.IsLeapYear(year);
157157
}
158158
}
159-
}
159+
}

SharpHelpers/SharpHelpers/DictionaryHelper.cs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,63 @@ public static TValue GetOrCreate<TKey, TValue>(this IDictionary<TKey, TValue> di
6363
dictionary[key] = ret;
6464
}
6565
return ret;
66+
}
67+
68+
/// <summary>
69+
/// Tries to add a key-value pair to the dictionary. If the key already exists, it updates the value.
70+
/// </summary>
71+
/// <param name="dictionary">The dictionary to operate on.</param>
72+
/// <param name="key">The key to add or update.</param>
73+
/// <param name="value">The value to associate with the key.</param>
74+
public static void AddOrUpdate<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key, TValue value)
75+
{
76+
if (dictionary.ContainsKey(key))
77+
{
78+
dictionary[key] = value;
79+
}
80+
else
81+
{
82+
dictionary.Add(key, value);
83+
}
84+
}
85+
86+
/// <summary>
87+
/// Removes the entry with the specified key if it exists in the dictionary, and returns a boolean indicating success.
88+
/// </summary>
89+
/// <param name="dictionary">The dictionary to operate on.</param>
90+
/// <param name="key">The key to remove.</param>
91+
/// <returns>True if the key was found and removed, otherwise false.</returns>
92+
public static bool RemoveIfExists<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key)
93+
{
94+
return dictionary.Remove(key);
95+
}
96+
97+
/// <summary>
98+
/// Merges the entries from another dictionary into the current dictionary. If a key already exists, its value is updated.
99+
/// </summary>
100+
/// <param name="dictionary">The dictionary to operate on.</param>
101+
/// <param name="otherDictionary">The dictionary to merge from.</param>
102+
public static void Merge<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, Dictionary<TKey, TValue> otherDictionary)
103+
{
104+
foreach (var kvp in otherDictionary)
105+
{
106+
dictionary.AddOrUpdate(kvp.Key, kvp.Value);
107+
}
108+
}
109+
110+
/// <summary>
111+
/// Converts the dictionary into a readable string format, useful for debugging.
112+
/// </summary>
113+
/// <param name="dictionary">The dictionary to convert to a string.</param>
114+
/// <returns>A string representation of the dictionary.</returns>
115+
public static string ToReadableString<TKey, TValue>(this Dictionary<TKey, TValue> dictionary)
116+
{
117+
var entries = new List<string>();
118+
foreach (var kvp in dictionary)
119+
{
120+
entries.Add($"{kvp.Key}: {kvp.Value}");
121+
}
122+
return "{" + string.Join(", ", entries) + "}";
66123
}
67124
}
68125
}

SharpHelpers/SharpHelpers/ObjectExtensions/ObjectSerializationhelper.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using System.IO;
44
using System.Xml;
55
using System.Xml.Serialization;
6-
using Newtonsoft.Json;
6+
using System.Text.Json;
77

88
namespace SharpCoding.SharpHelpers.ObjectExtensions
99
{
@@ -16,7 +16,7 @@ public static class ObjectSerializationHelper
1616
/// <returns></returns>
1717
public static string SerializeToJson(this object istance)
1818
{
19-
return istance == null ? string.Empty : JsonConvert.SerializeObject(istance);
19+
return istance == null ? string.Empty : JsonSerializer.Serialize(istance);
2020
}
2121

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

3333
/// <summary>

SharpHelpers/SharpHelpers/SharpHelpers.csproj

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@
2020
<PackageIcon>ico.png</PackageIcon>
2121
<PackageIconUrl />
2222
</PropertyGroup>
23-
<ItemGroup>
24-
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
25-
</ItemGroup>
2623

2724
<ItemGroup>
2825
<None Include="..\..\LICENSE">
@@ -38,4 +35,8 @@
3835
<ItemGroup>
3936
<Compile Remove="RegistryHelpers.cs" />
4037
</ItemGroup>
38+
39+
<ItemGroup>
40+
<PackageReference Include="System.Text.Json" Version="8.0.4" />
41+
</ItemGroup>
4142
</Project>

0 commit comments

Comments
 (0)