Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public void Reset()
_offset = 0;
_nestedDataItems?.Clear();
_currentMajorType = null;
_definiteLength = null;
_definiteLength = AllowMultipleRootLevelValues ? null : (int?)1;
_itemsWritten = 0;
_frameOffset = 0;
_isTagContext = false;
Expand All @@ -120,6 +120,7 @@ public void Reset()
_keysRequireSorting = false;
_keyValuePairEncodingRanges?.Clear();
_keyEncodingRanges?.Clear();
_currentIndefiniteLengthStringRanges?.Clear();
}
}

Expand Down
27 changes: 27 additions & 0 deletions src/libraries/System.Formats.Cbor/tests/Writer/CborWriterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,33 @@ public static void Reset_NonTrivialWriter_HappyPath()
Assert.Equal(new byte[] { 0x18, 0x2a }, writer.Encode());
}

[Fact]
public static void Reset_PreservesMultipleRootLevels()
{
var writer = new CborWriter(allowMultipleRootLevelValues: false);
writer.WriteBoolean(true);
writer.Reset();
writer.WriteInt32(6);
Assert.Throws<InvalidOperationException>(() => writer.WriteInt32(7));
}

[Fact]
public static void Reset_ClearsIndefiniteLengthRanges()
{
var writer = new CborWriter(convertIndefiniteLengthEncodings: true);

writer.WriteStartIndefiniteLengthByteString();
writer.WriteByteString([1, 2, 3]);
writer.Reset();

writer.WriteStartIndefiniteLengthByteString();
writer.WriteByteString([1, 2, 3]);
writer.WriteEndIndefiniteLengthByteString();
ReadOnlySpan<byte> encoded = writer.Encode();
ReadOnlySpan<byte> expected = [0x43, 0x01, 0x02, 0x03];
AssertExtensions.SequenceEqual(expected, encoded);
}

[Fact]
public static void ConformanceMode_DefaultValue_ShouldEqualStrict()
{
Expand Down
Loading