2025-08-30 19:41:10 +02:00
|
|
|
|
using FluentResults;
|
|
|
|
|
|
using pgLabII.PgUtils.ConnectionStrings;
|
|
|
|
|
|
using OneOf;
|
2024-11-24 12:48:12 +01:00
|
|
|
|
|
|
|
|
|
|
namespace pgLabII.PgUtils.Tests.ConnectionStrings;
|
|
|
|
|
|
|
2025-08-30 19:41:10 +02:00
|
|
|
|
using Elem = OneOf<PqToken, string, IError>;
|
|
|
|
|
|
|
2024-11-24 12:48:12 +01:00
|
|
|
|
internal class UnitTestTokenizer : IPqConnectionStringTokenizer
|
|
|
|
|
|
{
|
2025-08-30 19:41:10 +02:00
|
|
|
|
private readonly List<Elem> _tokens = new();
|
2024-11-24 12:48:12 +01:00
|
|
|
|
private int _position = 0;
|
|
|
|
|
|
|
2025-08-30 19:41:10 +02:00
|
|
|
|
public UnitTestTokenizer AddString(string output)
|
2024-11-24 12:48:12 +01:00
|
|
|
|
{
|
2025-08-30 19:41:10 +02:00
|
|
|
|
_tokens.Add(output);
|
2024-11-24 12:48:12 +01:00
|
|
|
|
return this;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-30 19:41:10 +02:00
|
|
|
|
public UnitTestTokenizer AddError(Error error)
|
2024-11-24 12:48:12 +01:00
|
|
|
|
{
|
2025-08-30 19:41:10 +02:00
|
|
|
|
_tokens.Add(error);
|
2024-11-24 12:48:12 +01:00
|
|
|
|
return this;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public UnitTestTokenizer AddEquals()
|
|
|
|
|
|
{
|
2025-08-30 19:41:10 +02:00
|
|
|
|
_tokens.Add(PqToken.Equals);
|
2024-11-24 12:48:12 +01:00
|
|
|
|
return this;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-30 19:41:10 +02:00
|
|
|
|
public void AddEof()
|
|
|
|
|
|
{
|
|
|
|
|
|
_tokens.Add(PqToken.Eof);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-24 12:48:12 +01:00
|
|
|
|
// note we do no whitespace at end tests here
|
2025-08-30 19:41:10 +02:00
|
|
|
|
public bool IsEof
|
|
|
|
|
|
{
|
|
|
|
|
|
get
|
|
|
|
|
|
{
|
|
|
|
|
|
EnsureNotEol();
|
|
|
|
|
|
return _tokens[_position].IsT0 && _tokens[_position].AsT0 == PqToken.Eof;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-11-24 12:48:12 +01:00
|
|
|
|
|
2025-08-30 19:41:10 +02:00
|
|
|
|
public Result<string> GetKeyword()
|
2024-11-24 12:48:12 +01:00
|
|
|
|
{
|
2025-08-30 19:41:10 +02:00
|
|
|
|
EnsureNotEol();
|
2024-11-24 12:48:12 +01:00
|
|
|
|
var elem = Consume();
|
2025-08-30 19:41:10 +02:00
|
|
|
|
return elem.Match<Result<string>>(
|
|
|
|
|
|
token => throw new Exception("Unexpected call to GetKeyword"),
|
|
|
|
|
|
str => str,
|
|
|
|
|
|
error => Result.Fail(error));
|
2024-11-24 12:48:12 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-30 19:41:10 +02:00
|
|
|
|
public Result ConsumeEquals()
|
2024-11-24 12:48:12 +01:00
|
|
|
|
{
|
2025-08-30 19:41:10 +02:00
|
|
|
|
EnsureNotEol();
|
2024-11-24 12:48:12 +01:00
|
|
|
|
var elem = Consume();
|
|
|
|
|
|
|
2025-08-30 19:41:10 +02:00
|
|
|
|
return elem.Match<Result>(
|
|
|
|
|
|
token =>
|
|
|
|
|
|
{
|
|
|
|
|
|
if (token != PqToken.Equals)
|
|
|
|
|
|
throw new Exception("Unexpected call to GetKeyword");
|
|
|
|
|
|
return Result.Ok();
|
|
|
|
|
|
},
|
|
|
|
|
|
str => throw new Exception("Unexpected call to ConsumeEquals"),
|
|
|
|
|
|
error => Result.Fail(error));
|
2024-11-24 12:48:12 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-30 19:41:10 +02:00
|
|
|
|
public Result<string> GetValue()
|
2024-11-24 12:48:12 +01:00
|
|
|
|
{
|
2025-08-30 19:41:10 +02:00
|
|
|
|
EnsureNotEol();
|
2024-11-24 12:48:12 +01:00
|
|
|
|
var elem = Consume();
|
2025-08-30 19:41:10 +02:00
|
|
|
|
return elem.Match<Result<string>>(
|
|
|
|
|
|
token => throw new Exception("Unexpected call to GetValue"),
|
|
|
|
|
|
str => str,
|
|
|
|
|
|
error => Result.Fail(error));
|
2024-11-24 12:48:12 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private Elem Consume()
|
|
|
|
|
|
{
|
2025-08-30 19:41:10 +02:00
|
|
|
|
return _tokens[_position++];
|
2024-11-24 12:48:12 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-30 19:41:10 +02:00
|
|
|
|
private void EnsureNotEol()
|
2024-11-24 12:48:12 +01:00
|
|
|
|
{
|
2025-08-30 19:41:10 +02:00
|
|
|
|
if (_position >= _tokens.Count)
|
|
|
|
|
|
throw new Exception("unexpected end of list in test, wrong parser call?");
|
2024-11-24 12:48:12 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|