Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 119 additions & 0 deletions test/Nerdbank.Streams.Tests/ExtensionHeaderTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
using MessagePack;
using System;
using Xunit;

namespace MessagePack.UnitTests
{
/// <summary>
/// Unit tests for the <see cref="ExtensionHeader"/> struct.
/// </summary>
public class ExtensionHeaderTests
{
/// <summary>
/// Tests that the constructor ExtensionHeader(sbyte, uint) correctly sets the TypeCode and Length properties.
/// </summary>
[Fact]
public void Ctor_WithUintLength_SetsProperties()
{
// Arrange
sbyte typeCode = 5;
uint length = 100;

// Act
var header = new ExtensionHeader(typeCode, length);

// Assert
Assert.Equal(typeCode, header.TypeCode);
Assert.Equal(length, header.Length);
}

/// <summary>
/// Tests that the constructor ExtensionHeader(sbyte, int) correctly sets the TypeCode and Length properties when given a positive integer.
/// </summary>
[Fact]
public void Ctor_WithIntLength_PositiveValue_SetsProperties()
{
// Arrange
sbyte typeCode = -10;
int length = 200;

// Act
var header = new ExtensionHeader(typeCode, length);

// Assert
Assert.Equal(typeCode, header.TypeCode);
Assert.Equal((uint)length, header.Length);
}

/// <summary>
/// Tests that the constructor ExtensionHeader(sbyte, int) correctly handles a negative integer by converting it to uint.
/// This edge case demonstrates the conversion of a negative integer to its corresponding unsigned value.
/// </summary>
[Fact]
public void Ctor_WithIntLength_NegativeValue_ConvertsToUint()
{
// Arrange
sbyte typeCode = 1;
int negativeLength = -1;
uint expectedLength = unchecked((uint)negativeLength);

// Act
var header = new ExtensionHeader(typeCode, negativeLength);

// Assert
Assert.Equal(typeCode, header.TypeCode);
Assert.Equal(expectedLength, header.Length);
}

/// <summary>
/// Tests that the Equals method returns true when comparing two ExtensionHeader instances with identical TypeCode and Length.
/// </summary>
[Fact]
public void Equals_SameValues_ReturnsTrue()
{
// Arrange
var header1 = new ExtensionHeader(10, 500);
var header2 = new ExtensionHeader(10, 500);

// Act
bool areEqual = header1.Equals(header2);

// Assert
Assert.True(areEqual, "Equals should return true for headers with identical values.");
}

/// <summary>
/// Tests that the Equals method returns false when comparing two ExtensionHeader instances with different TypeCode values.
/// </summary>
[Fact]
public void Equals_DifferentTypeCode_ReturnsFalse()
{
// Arrange
var header1 = new ExtensionHeader(10, 500);
var header2 = new ExtensionHeader(11, 500);

// Act
bool areEqual = header1.Equals(header2);

// Assert
Assert.False(areEqual, "Equals should return false for headers with different TypeCode values.");
}

/// <summary>
/// Tests that the Equals method returns false when comparing two ExtensionHeader instances with different Length values.
/// </summary>
[Fact]
public void Equals_DifferentLength_ReturnsFalse()
{
// Arrange
var header1 = new ExtensionHeader(10, 500);
var header2 = new ExtensionHeader(10, 600);

// Act
bool areEqual = header1.Equals(header2);

// Assert
Assert.False(areEqual, "Equals should return false for headers with different Length values.");
}
}
}
102 changes: 102 additions & 0 deletions test/Nerdbank.Streams.Tests/ExtensionResultTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
using System;
using System.Buffers;
using MessagePack;
using Xunit;

namespace MessagePack.UnitTests
{
/// <summary>
/// Unit tests for the <see cref="ExtensionResult"/> struct.
/// </summary>
public class ExtensionResultTests
{
/// <summary>
/// Tests that the constructor accepting Memory&lt;byte&gt; sets the properties correctly.
/// </summary>
[Theory]
[InlineData(-1)]
[InlineData(0)]
[InlineData(127)]
public void Constructor_WithMemoryByte_SetsPropertiesCorrectly(sbyte typeCode)
{
// Arrange
byte[] testData = { 1, 2, 3, 4, 5 };
Memory<byte> memoryData = new Memory<byte>(testData);

// Act
var result = new ExtensionResult(typeCode, memoryData);

// Assert
Assert.Equal(typeCode, result.TypeCode);
// Comparing lengths as ReadOnlySequence<byte> does not expose array directly, so check the length.
Assert.Equal(testData.Length, result.Data.Length);
}

/// <summary>
/// Tests that the constructor accepting ReadOnlySequence&lt;byte&gt; sets the properties correctly.
/// </summary>
[Theory]
[InlineData(-10)]
[InlineData(5)]
public void Constructor_WithReadOnlySequence_SetsPropertiesCorrectly(sbyte typeCode)
{
// Arrange
byte[] testData = { 10, 20, 30 };
ReadOnlySequence<byte> sequenceData = new ReadOnlySequence<byte>(testData);

// Act
var result = new ExtensionResult(typeCode, sequenceData);

// Assert
Assert.Equal(typeCode, result.TypeCode);
Assert.Equal(testData.Length, result.Data.Length);
}

/// <summary>
/// Tests that the Header property returns an ExtensionHeader with the correct type code and length.
/// </summary>
[Theory]
[InlineData(1)]
[InlineData(-5)]
public void HeaderProperty_WhenInvoked_ReturnsCorrectExtensionHeader(sbyte typeCode)
{
// Arrange
byte[] testData = { 100, 101, 102, 103 };
Memory<byte> memoryData = new Memory<byte>(testData);
var result = new ExtensionResult(typeCode, memoryData);
uint expectedLength = (uint)testData.Length;

// Act
var header = result.Header;

// Assert
// Since ExtensionHeader is constructed as new ExtensionHeader(typeCode, length)
// and we do not have its internal implementation, we assume it exposes the TypeCode and Length properties.
// We perform the check by comparing the values with a newly constructed header.
var expectedHeader = new ExtensionHeader(typeCode, expectedLength);
Assert.Equal(expectedHeader.TypeCode, header.TypeCode);
Assert.Equal(expectedHeader.Length, header.Length);
}

/// <summary>
/// Tests the behavior when handling an empty Memory&lt;byte&gt;.
/// </summary>
[Fact]
public void Constructor_WithEmptyMemory_SetsPropertiesCorrectly()
{
// Arrange
sbyte typeCode = 10;
Memory<byte> emptyMemory = Memory<byte>.Empty;

// Act
var result = new ExtensionResult(typeCode, emptyMemory);

// Assert
Assert.Equal(typeCode, result.TypeCode);
Assert.Equal(0, result.Data.Length);
var header = result.Header;
Assert.Equal(typeCode, header.TypeCode);
Assert.Equal((uint)0, header.Length);
}
}
}
Loading
Loading