2024-04-15 18:02:11 +08:00
|
|
|
|
using Canon.Core.Abstractions;
|
|
|
|
|
|
|
|
|
|
namespace Canon.Tests.Utils;
|
|
|
|
|
|
|
|
|
|
public class StringSourceReaderTests
|
|
|
|
|
{
|
|
|
|
|
[Fact]
|
|
|
|
|
public void LineFeedTest()
|
|
|
|
|
{
|
|
|
|
|
ISourceReader reader = new StringSourceReader("program Main;\nbegin\nend.\n");
|
2024-04-18 16:34:32 +08:00
|
|
|
|
reader.MoveNext();
|
2024-04-15 18:02:11 +08:00
|
|
|
|
|
2024-04-18 16:34:32 +08:00
|
|
|
|
CheckLine(reader, "program Main;", 1);
|
|
|
|
|
reader.MoveNext();
|
|
|
|
|
CheckLine(reader, "begin", 2);
|
|
|
|
|
reader.MoveNext();
|
|
|
|
|
CheckLine(reader, "end.", 3);
|
|
|
|
|
}
|
2024-04-15 18:02:11 +08:00
|
|
|
|
|
2024-04-18 16:34:32 +08:00
|
|
|
|
[Fact]
|
|
|
|
|
public void CarriageReturnLineFeedTest()
|
|
|
|
|
{
|
|
|
|
|
ISourceReader reader = new StringSourceReader("program Main;\r\nbegin\r\nend.\r\n");
|
|
|
|
|
reader.MoveNext();
|
2024-04-15 18:02:11 +08:00
|
|
|
|
|
2024-04-18 16:34:32 +08:00
|
|
|
|
CheckLine(reader, "program Main;", 1);
|
|
|
|
|
reader.MoveNext();
|
|
|
|
|
reader.MoveNext();
|
|
|
|
|
CheckLine(reader, "begin", 2);
|
|
|
|
|
reader.MoveNext();
|
|
|
|
|
reader.MoveNext();
|
|
|
|
|
CheckLine(reader, "end.", 3);
|
|
|
|
|
}
|
2024-04-15 18:02:11 +08:00
|
|
|
|
|
2024-04-18 16:34:32 +08:00
|
|
|
|
[Fact]
|
|
|
|
|
public void RetractTest()
|
|
|
|
|
{
|
|
|
|
|
ISourceReader reader = new StringSourceReader("test");
|
|
|
|
|
reader.MoveNext();
|
2024-04-15 18:02:11 +08:00
|
|
|
|
|
2024-04-18 16:34:32 +08:00
|
|
|
|
Assert.Equal('t', reader.Current);
|
|
|
|
|
Assert.True(reader.MoveNext());
|
|
|
|
|
Assert.Equal('e', reader.Current);
|
|
|
|
|
Assert.True(reader.Retract());
|
|
|
|
|
Assert.Equal('t', reader.Current);
|
|
|
|
|
Assert.False(reader.Retract());
|
2024-04-15 18:02:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
2024-04-18 16:34:32 +08:00
|
|
|
|
public void PeekTest()
|
2024-04-15 18:02:11 +08:00
|
|
|
|
{
|
2024-04-18 16:34:32 +08:00
|
|
|
|
ISourceReader reader = new StringSourceReader("peek");
|
|
|
|
|
reader.MoveNext();
|
2024-04-15 18:02:11 +08:00
|
|
|
|
|
2024-04-18 16:34:32 +08:00
|
|
|
|
Assert.Equal('p', reader.Current);
|
|
|
|
|
Assert.True(reader.TryPeekChar(out char? c));
|
|
|
|
|
Assert.Equal('e', c);
|
|
|
|
|
Assert.Equal('p', reader.Current);
|
|
|
|
|
}
|
2024-04-15 18:02:11 +08:00
|
|
|
|
|
2024-04-18 16:34:32 +08:00
|
|
|
|
private static void CheckLine(ISourceReader reader, string line, uint lineNumber)
|
|
|
|
|
{
|
|
|
|
|
foreach ((char value, uint index) in line.WithIndex())
|
2024-04-15 18:02:11 +08:00
|
|
|
|
{
|
2024-04-18 16:34:32 +08:00
|
|
|
|
Assert.Equal(value, reader.Current);
|
|
|
|
|
Assert.Equal(lineNumber, reader.Line);
|
2024-04-15 18:02:11 +08:00
|
|
|
|
Assert.Equal(index + 1, reader.Pos);
|
2024-04-18 16:34:32 +08:00
|
|
|
|
reader.MoveNext();
|
2024-04-15 18:02:11 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|