commit initial
This commit is contained in:
86
tests/TinyCC.Tests/UnitTests.cs
Normal file
86
tests/TinyCC.Tests/UnitTests.cs
Normal file
@@ -0,0 +1,86 @@
|
||||
using TinyCC.Core;
|
||||
|
||||
namespace TinyCC.Tests;
|
||||
|
||||
public class LexerTests
|
||||
{
|
||||
private readonly IErrorReporter _errorReporter;
|
||||
|
||||
public LexerTests()
|
||||
{
|
||||
_errorReporter = new ErrorReporter();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Tokenize_SimpleExpression_ReturnsTokens()
|
||||
{
|
||||
var source = "int x = 3 + 4;";
|
||||
var lexer = new Lexer(source, "test.c", _errorReporter);
|
||||
var tokens = lexer.Tokenize().ToList();
|
||||
|
||||
Assert.False(_errorReporter.HasErrors);
|
||||
Assert.Contains(tokens, t => t.Type == TokenType.Int);
|
||||
Assert.Contains(tokens, t => t.Type == TokenType.Identifier && t.Lexeme == "x");
|
||||
Assert.Contains(tokens, t => t.Type == TokenType.Assign);
|
||||
Assert.Contains(tokens, t => t.Type == TokenType.IntLiteral && Convert.ToInt64(t.Value!) == 3);
|
||||
Assert.Contains(tokens, t => t.Type == TokenType.IntLiteral && Convert.ToInt64(t.Value!) == 4);
|
||||
Assert.Contains(tokens, t => t.Type == TokenType.Plus);
|
||||
Assert.Contains(tokens, t => t.Type == TokenType.Semicolon);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Tokenize_FunctionDefinition_ReturnsTokens()
|
||||
{
|
||||
var source = "int add(int a, int b) { return a + b; }";
|
||||
var lexer = new Lexer(source, "test.c", _errorReporter);
|
||||
var tokens = lexer.Tokenize().ToList();
|
||||
|
||||
Assert.False(_errorReporter.HasErrors);
|
||||
Assert.Contains(tokens, t => t.Type == TokenType.Int);
|
||||
Assert.Contains(tokens, t => t.Type == TokenType.Identifier && t.Lexeme == "add");
|
||||
Assert.Contains(tokens, t => t.Type == TokenType.LeftParen);
|
||||
Assert.Contains(tokens, t => t.Type == TokenType.RightParen);
|
||||
Assert.Contains(tokens, t => t.Type == TokenType.LeftBrace);
|
||||
Assert.Contains(tokens, t => t.Type == TokenType.Return);
|
||||
Assert.Contains(tokens, t => t.Type == TokenType.RightBrace);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Tokenize_SkipsComments()
|
||||
{
|
||||
var source = "int x; // this is a comment\nint y;";
|
||||
var lexer = new Lexer(source, "test.c", _errorReporter);
|
||||
var tokens = lexer.Tokenize().ToList();
|
||||
|
||||
Assert.False(_errorReporter.HasErrors);
|
||||
var identifiers = tokens.Where(t => t.Type == TokenType.Identifier).ToList();
|
||||
Assert.Equal(2, identifiers.Count);
|
||||
Assert.Equal("x", identifiers[0].Lexeme);
|
||||
Assert.Equal("y", identifiers[1].Lexeme);
|
||||
}
|
||||
}
|
||||
|
||||
public class ParserTests
|
||||
{
|
||||
private readonly IErrorReporter _errorReporter;
|
||||
|
||||
public ParserTests()
|
||||
{
|
||||
_errorReporter = new ErrorReporter();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Parse_SimpleFunction_ReturnsAst()
|
||||
{
|
||||
var source = "int add(int a, int b) { return a + b; }";
|
||||
var lexer = new Lexer(source, "test.c", _errorReporter);
|
||||
var tokens = lexer.Tokenize().ToList();
|
||||
var parser = new Parser(tokens, _errorReporter);
|
||||
|
||||
var ast = parser.Parse();
|
||||
|
||||
Assert.False(_errorReporter.HasErrors);
|
||||
Assert.NotNull(ast);
|
||||
Assert.IsType<ProgramNode>(ast);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user