2024-04-04 21:25:11 +08:00
|
|
|
|
using Canon.Core.Enums;
|
|
|
|
|
using Canon.Core.LexicalParser;
|
|
|
|
|
using Xunit.Abstractions;
|
|
|
|
|
using Canon.Core.Exceptions;
|
2024-04-18 16:34:32 +08:00
|
|
|
|
using Canon.Core.Abstractions;
|
|
|
|
|
using Canon.Tests.Utils;
|
2024-04-04 21:25:11 +08:00
|
|
|
|
|
|
|
|
|
namespace Canon.Tests.LexicalParserTests
|
|
|
|
|
{
|
2024-05-04 13:57:14 +08:00
|
|
|
|
public class CharacterTypeTests(ITestOutputHelper testOutputHelper)
|
2024-04-04 21:25:11 +08:00
|
|
|
|
{
|
2024-04-18 16:34:32 +08:00
|
|
|
|
private readonly ILexer _lexer = new Lexer();
|
2024-04-04 21:25:11 +08:00
|
|
|
|
|
|
|
|
|
[Theory]
|
2024-05-04 13:57:14 +08:00
|
|
|
|
[InlineData("'a'", 'a')]
|
|
|
|
|
[InlineData("'+'", '+')]
|
|
|
|
|
public void TestCharacterType(string input, char expectedResult)
|
2024-04-04 21:25:11 +08:00
|
|
|
|
{
|
2024-04-18 16:34:32 +08:00
|
|
|
|
IEnumerable<SemanticToken> tokensEnumerable = _lexer.Tokenize(new StringSourceReader(input));
|
|
|
|
|
List<SemanticToken> tokens = tokensEnumerable.ToList();
|
2024-05-04 13:57:14 +08:00
|
|
|
|
|
|
|
|
|
testOutputHelper.WriteLine(tokens[0].LiteralValue);
|
|
|
|
|
Assert.Equal(SemanticTokenType.Character, tokens[0].TokenType);
|
|
|
|
|
Assert.Equal(expectedResult, tokens[0].Convert<CharacterSemanticToken>().ParseAsCharacter());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[InlineData("'Hello, world'", "Hello, world")]
|
|
|
|
|
[InlineData("'asdfasdf'", "asdfasdf")]
|
|
|
|
|
public void StringTypeTest(string input, string expect)
|
|
|
|
|
{
|
|
|
|
|
IEnumerable<SemanticToken> tokens = _lexer.Tokenize(new StringSourceReader(input));
|
|
|
|
|
SemanticToken token = tokens.First();
|
|
|
|
|
|
|
|
|
|
Assert.Equal(SemanticTokenType.String, token.TokenType);
|
|
|
|
|
Assert.Equal(expect, token.Convert<StringSemanticToken>().ParseAsString());
|
2024-04-04 21:25:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
//[InlineData("'\\x'", 1, 2, LexemeException.LexemeErrorType.InvalidEscapeSequence)]
|
2024-04-24 11:01:45 +08:00
|
|
|
|
[InlineData("'This is an unclosed string literal", 1, 35, LexemeErrorType.UnclosedStringLiteral)]
|
|
|
|
|
[InlineData("'This", 1, 5, LexemeErrorType.UnclosedStringLiteral)]
|
2024-04-04 21:25:11 +08:00
|
|
|
|
[InlineData("x @", 1, 3, LexemeErrorType.UnknownCharacterOrString)]
|
|
|
|
|
//[InlineData("\"x\'", 1, 3, LexemeException.LexemeErrorType.UnclosedStringLiteral)]
|
2024-05-04 13:57:14 +08:00
|
|
|
|
public void TestParseCharacterError(string input, uint expectedLine, uint expectedCharPosition,
|
|
|
|
|
LexemeErrorType expectedErrorType)
|
2024-04-04 21:25:11 +08:00
|
|
|
|
{
|
2024-04-18 16:34:32 +08:00
|
|
|
|
var ex = Assert.Throws<LexemeException>(() => _lexer.Tokenize(new StringSourceReader(input)).ToList());
|
2024-05-04 13:57:14 +08:00
|
|
|
|
testOutputHelper.WriteLine(ex.ToString());
|
2024-04-04 21:25:11 +08:00
|
|
|
|
Assert.Equal(expectedErrorType, ex.ErrorType);
|
|
|
|
|
Assert.Equal(expectedLine, ex.Line);
|
|
|
|
|
Assert.Equal(expectedCharPosition, ex.CharPosition);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|