-
Notifications
You must be signed in to change notification settings - Fork 232
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove UTF7 encoding from EncodingProvider (#651)
Closes #649 Remove UTF7 encoding from EncodingProvider and add unit tests - Enabled nullable reference types. - Added XML documentation - Removed 7-bit methods - Refactored 8-bit and Base64 methods to remove try-catch blocks. - Modified Encode, DecodeString, and DecodeData to handle null values.
- Loading branch information
Showing
2 changed files
with
178 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// | ||
// Copyright ical.net project maintainers and contributors. | ||
// Licensed under the MIT license. | ||
// | ||
|
||
#nullable enable | ||
using System; | ||
using Ical.Net.Serialization; | ||
using NUnit.Framework; | ||
using EncodingProvider = Ical.Net.Serialization.EncodingProvider; | ||
|
||
namespace Ical.Net.Tests; | ||
|
||
[TestFixture] | ||
public class EncodingProviderTests | ||
{ | ||
private EncodingProvider GetEncodingProvider() => new EncodingProvider(new SerializationContext()); | ||
|
||
[Test] | ||
public void Encode_ShouldReturnEncodedString_WhenValidEncodingIsProvided() | ||
{ | ||
const string encoding = "8BIT"; | ||
var data = "Hello"u8.ToArray(); | ||
|
||
var result = GetEncodingProvider().Encode(encoding, data); | ||
|
||
Assert.That(result, Is.EqualTo("Hello")); | ||
} | ||
|
||
[Test] | ||
public void Encode_ShouldBeNull_WhenInvalidEncodingIsProvided() | ||
{ | ||
const string encoding = "Invalid-Encoding"; | ||
var data = "Hello"u8.ToArray(); | ||
|
||
Assert.That(GetEncodingProvider().Encode(encoding, data), Is.Null); | ||
} | ||
|
||
[Test] | ||
public void Decode_ShouldReturnDecodedByteArray_WhenValidEncodingIsProvided() | ||
{ | ||
const string encoding = "8BIT"; | ||
const string data = "Hello"; | ||
|
||
var result = GetEncodingProvider().DecodeString(encoding, data); | ||
|
||
Assert.That(result, Is.EqualTo("Hello"u8.ToArray())); | ||
} | ||
|
||
[Test] | ||
public void Decode_ShouldBeNull_WhenInvalidEncodingIsProvided() | ||
{ | ||
const string encoding = "Invalid-Encoding"; | ||
const string data = "Hello"; | ||
|
||
Assert.That(GetEncodingProvider().DecodeString(encoding, data), Is.Null); | ||
} | ||
|
||
[Test] | ||
public void Encode_ShouldReturnEncodedString_WithBase64Encoding() | ||
{ | ||
const string encoding = "BASE64"; | ||
var data = "Hello"u8.ToArray(); | ||
|
||
var result = GetEncodingProvider().Encode(encoding, data); | ||
|
||
Assert.That(result, Is.EqualTo("SGVsbG8=")); // "Hello" in Base64 | ||
} | ||
|
||
[Test] | ||
public void Decode_ShouldReturnDecodedByteArray_WithBase64Encoding() | ||
{ | ||
const string encoding = "BASE64"; | ||
const string data = "SGVsbG8="; // "Hello" in Base64 | ||
|
||
var result = GetEncodingProvider().DecodeString(encoding, data); | ||
|
||
Assert.That(result, Is.EqualTo("Hello"u8.ToArray())); | ||
} | ||
|
||
[Test] | ||
public void Decode_ShouldThrow_WithInvalidBase64String() | ||
{ | ||
const string encoding = "BASE64"; | ||
const string data = "InvalidBase64==="; // Invalid Base64 string | ||
|
||
Assert.Throws<FormatException>(() => GetEncodingProvider().DecodeString(encoding, data)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters