Skip to content

Commit a48eb32

Browse files
committed
test(buffer): Test constructors and slicing methods for the read-only string buffer
Signed-off-by: Matthew <matthew@oceanapocalypsestudios.org> Refs: #60
1 parent b8ad640 commit a48eb32

1 file changed

Lines changed: 161 additions & 9 deletions

File tree

tests/RSML.Tests/Sources/ReadOnlyStringBufferTests.cs

Lines changed: 161 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using System.Diagnostics;
2+
using System.Runtime.InteropServices;
3+
using System.Text;
24

35
using OceanApocalypseStudios.RSML.Exceptions;
46
using OceanApocalypseStudios.RSML.Sources;
@@ -820,12 +822,164 @@ public void GetSourceLocation_ThrowsIfEmpty(int lineNumber)
820822
Assert.Throws<BufferException>(() => buffer.GetSourceLocation(lineNumber));
821823
}
822824

823-
/* todo: test GetWord (all overloads)
824-
* todo: test non-string constructors
825-
* todo: test Slice (all overloads)
826-
* todo: test ToString
827-
* todo: test TryGetChar
828-
* todo: test TryGetLine */
825+
[Theory]
826+
#region String ends with newline
827+
[InlineData(TestString01, 0, 0, 0, 0, 3, 3, 0, 3)]
828+
[InlineData(TestString01, 3, 3, 0, 3, 5, 5, 1, 0)]
829+
[InlineData(TestString01, 4, 4, 0, 4, 33, 33, 6, 1)]
830+
[InlineData(TestString01, 5, 5, 1, 0, 6, 6, 1, 1)]
831+
[InlineData(TestString01, 6, 6, 1, 1, 15, 15, 3, 2)]
832+
[InlineData(TestString01, 13, 13, 3, 0, 16, 16, 3, 3)]
833+
[InlineData(TestString01, 15, 15, 3, 2, 22, 22, 4, 1)]
834+
[InlineData(TestString01, 28, 28, 4, 7, -5, 29, 4, 8)]
835+
[InlineData(TestString01, 30, 30, 5, 0, 32, 32, 6, 0)]
836+
[InlineData(TestString01, -3, 31, 5, 1, 33, 33, 6, 1)]
837+
#endregion
838+
#region String ends without newline
839+
[InlineData(TestString02, 0, 0, 0, 0, 3, 3, 0, 3)]
840+
[InlineData(TestString02, 4, 4, 0, 4, 5, 5, 1, 0)]
841+
[InlineData(TestString02, 6, 6, 1, 1, 22, 22, 4, 1)]
842+
[InlineData(TestString02, 13, 13, 3, 0, 15, 15, 3, 2)]
843+
[InlineData(TestString02, 29, 29, 4, 8, -2, 31, 5, 1)]
844+
[InlineData(TestString02, 30, 30, 5, 0, -1, 32, 6, 0)]
845+
#endregion
846+
public void GetSourceSpan(string data, int startIndex, int expectedStartIndex, int expectedStartLine, int expectedStartColumn, int endIndex, int expectedEndIndex, int expectedEndLine, int expectedEndColumn)
847+
{
848+
var buffer = new ReadOnlyStringBuffer(data);
849+
var span = buffer.GetSourceSpan(startIndex, endIndex);
850+
851+
Assert.Equal(expectedStartIndex, span.Start.Index);
852+
Assert.Equal(expectedStartLine, span.Start.Line);
853+
Assert.Equal(expectedStartColumn, span.Start.Column);
854+
855+
Assert.Equal(expectedEndIndex, span.End.Index);
856+
Assert.Equal(expectedEndLine, span.End.Line);
857+
Assert.Equal(expectedEndColumn, span.End.Column);
858+
}
859+
860+
[Theory]
861+
[InlineData(TestString02)]
862+
[InlineData(TestString03)]
863+
[InlineData(TestString04)]
864+
[InlineData(TestString06)]
865+
public void Constructor_CharArray(string data)
866+
{
867+
var array = data.ToCharArray();
868+
Assert.Equal(data, array);
869+
Assert.True(new ReadOnlyStringBuffer(array).Equals(data));
870+
}
871+
872+
[Theory]
873+
[InlineData(TestString01)]
874+
[InlineData(TestString02)]
875+
[InlineData(TestString03)]
876+
[InlineData(TestString04)]
877+
[InlineData(TestString05)]
878+
[InlineData(TestString06)]
879+
public unsafe void Constructor_BytePointer(string data)
880+
{
881+
var byteCount = Encoding.Default.GetByteCount(data);
882+
883+
fixed (byte* pointer = Encoding.Default.GetBytes(data))
884+
{
885+
Assert.Equal(data, Encoding.Default.GetString(pointer, byteCount));
886+
Assert.True(new ReadOnlyStringBuffer(pointer, byteCount).Equals(data));
887+
}
888+
}
889+
890+
[Theory]
891+
[InlineData(TestString02)]
892+
[InlineData(TestString03)]
893+
[InlineData(TestString04)]
894+
[InlineData(TestString06)]
895+
public void Constructor_ByteArray(string data)
896+
{
897+
var array = Encoding.Default.GetBytes(data);
898+
Assert.True(new ReadOnlyStringBuffer(array).Equals(data));
899+
}
900+
901+
[Theory]
902+
[InlineData(TestString02)]
903+
[InlineData(TestString03)]
904+
[InlineData(TestString04)]
905+
[InlineData(TestString06)]
906+
public void Constructor_ReadOnlySpan(string data)
907+
{
908+
ReadOnlySpan<char> span = data.AsSpan();
909+
Assert.True(new ReadOnlyStringBuffer(span).Equals(data));
910+
}
911+
912+
[Theory]
913+
#region String ends with newline
914+
[InlineData(TestString01, 0, 4, "Hey\r")]
915+
[InlineData(TestString01, 3, 2, "\r\n")]
916+
[InlineData(TestString01, 5, 1, "T")]
917+
[InlineData(TestString01, 7, 6, "is\rIs\u2029")]
918+
[InlineData(TestString01, 12, 8, "\u2029A Test ")]
919+
[InlineData(TestString01, 16, 4, "est ")]
920+
[InlineData(TestString01, 30, 3, "\r\n.")]
921+
[InlineData(TestString01, -3, 3, "\n.\u2028")]
922+
#endregion
923+
public void Slice_CharArray(string data, int index, int length, string expectedSlice)
924+
{
925+
var buffer = new ReadOnlyStringBuffer(data);
926+
var slice = buffer.Slice(index, length);
927+
928+
Assert.Equal(expectedSlice, new string(slice));
929+
}
930+
931+
[Theory]
932+
#region String ends with newline
933+
[InlineData(TestString01, 0, "Hey\r")]
934+
[InlineData(TestString01, 3, "\r\n")]
935+
[InlineData(TestString01, 5, "T")]
936+
[InlineData(TestString01, 7, "is\rIs\u2029")]
937+
[InlineData(TestString01, 12, "\u2029A Test ")]
938+
[InlineData(TestString01, 16, "est ")]
939+
[InlineData(TestString01, 30, "\r\n.")]
940+
[InlineData(TestString01, -3, "\n.\u2028")]
941+
#endregion
942+
public void Slice_CharSpan(string data, int index, string expectedSlice)
943+
{
944+
Span<char> span = stackalloc char[expectedSlice.Length];
945+
946+
var buffer = new ReadOnlyStringBuffer(data);
947+
buffer.Slice(index, span);
948+
949+
Assert.Equal(expectedSlice, span);
950+
}
951+
952+
[Theory]
953+
#region String ends with newline
954+
[InlineData(TestString01, 0, 4, "Hey\r")]
955+
[InlineData(TestString01, 3, 5, "\r\n")]
956+
[InlineData(TestString01, 5, 6, "T")]
957+
[InlineData(TestString01, 7, 13, "is\rIs\u2029")]
958+
[InlineData(TestString01, 12, 20, "\u2029A Test ")]
959+
[InlineData(TestString01, 16, 20, "est ")]
960+
[InlineData(TestString01, 30, 33, "\r\n.")]
961+
[InlineData(TestString01, 31, 34, "\n.\u2028")]
962+
#endregion
963+
public void Slice_SourceSpan(string data, int startIndex, int endIndex, string expectedSlice)
964+
{
965+
Span<char> span = stackalloc char[expectedSlice.Length];
966+
967+
var buffer = new ReadOnlyStringBuffer(data);
968+
buffer.Slice(new SourceSpan(new(startIndex, 0, 0), new(endIndex, 0, 0)), span);
969+
970+
Assert.Equal(expectedSlice, span);
971+
}
972+
973+
[Fact]
974+
public void Slice_SourceSpan_ThrowsIfSpanTooSmall()
975+
{
976+
var buffer = new ReadOnlyStringBuffer(TestString05);
977+
Assert.Throws<ArgumentException>(() => buffer.Slice(new SourceSpan(new(0, 0, 0), new(7, 0, 0)), stackalloc char[3]));
978+
}
979+
980+
// todo: test ToString
981+
// todo: test TryGetChar
982+
// todo: test TryGetLine
829983

830984
[Theory]
831985
#region String ends with newline
@@ -871,7 +1025,5 @@ public void TryGetLineFromIndex(string data, int index, string expectedLine)
8711025
Assert.Equal(expectedLine.Length, written);
8721026
}
8731027

874-
/* todo: further test TryGetLineFromIndex
875-
* todo: test TryGetWord (all overloads)
876-
*/
1028+
// todo: further test TryGetLineFromIndex
8771029
}

0 commit comments

Comments
 (0)