mirror of
https://github.com/LucasVbr/croissant.git
synced 2026-05-14 01:22:06 +00:00
refactor: Syntax with unit tests
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
open Alcotest
|
||||
open Syntax.BinaryOperators
|
||||
|
||||
let test_pp_binary_operators () =
|
||||
let to_check =
|
||||
[
|
||||
("Should return \"Add\"", "Add", Add);
|
||||
("Should return \"Subtract\"", "Subtract", Subtract);
|
||||
("Should return \"Multiply\"", "Multiply", Multiply);
|
||||
("Should return \"Divide\"", "Divide", Divide);
|
||||
]
|
||||
in
|
||||
List.iter
|
||||
(fun (msg, expected, actual) ->
|
||||
check string msg expected (pp_binary_operators actual))
|
||||
to_check
|
||||
|
||||
let tests =
|
||||
( "BinaryOperators",
|
||||
[ test_case "pp_binary_operators" `Quick test_pp_binary_operators ] )
|
||||
@@ -0,0 +1,29 @@
|
||||
open Alcotest
|
||||
open Syntax
|
||||
|
||||
let test_pp_expressions () =
|
||||
let to_check =
|
||||
[
|
||||
( "Should return \"Literal(...)\"",
|
||||
"Literal(Integer(1))",
|
||||
Expressions.Literal (Literals.Integer 1) );
|
||||
( "Should return \"UnaryExpression(...)\"",
|
||||
"UnaryExpression(ArithmeticNegation, Literal(Integer(2)))",
|
||||
Expressions.UnaryExpression
|
||||
( UnaryOperators.ArithmeticNegation,
|
||||
Expressions.Literal (Literals.Integer 2) ) );
|
||||
( "Should return \"BinaryExpression(...)\"",
|
||||
"BinaryExpression(Add, Literal(Integer(5)), Literal(Integer(10)))",
|
||||
Expressions.BinaryExpression
|
||||
( BinaryOperators.Add,
|
||||
Expressions.Literal (Literals.Integer 5),
|
||||
Expressions.Literal (Literals.Integer 10) ) );
|
||||
]
|
||||
in
|
||||
List.iter
|
||||
(fun (msg, expected, actual) ->
|
||||
check string msg expected (Expressions.pp_expressions actual))
|
||||
to_check
|
||||
|
||||
let tests =
|
||||
("Expressions", [ test_case "pp_expressions" `Quick test_pp_expressions ])
|
||||
@@ -0,0 +1,22 @@
|
||||
open Alcotest
|
||||
open Syntax.Literals
|
||||
|
||||
let test_pp_literals () =
|
||||
let to_check =
|
||||
[
|
||||
("Should return \"Integer(1)\"", "Integer(1)", Integer 1);
|
||||
("Should return \"Float(1.000000)\"", "Float(1.000000)", Float 1.0);
|
||||
( "Should return \"String(\\\"Hello, World!\\\")\"",
|
||||
"String(\"Hello, World!\")",
|
||||
String "Hello, World!" );
|
||||
("Should return \"Character('c')\"", "Character('c')", Character 'c');
|
||||
("Should return \"Boolean(true)\"", "Boolean(true)", Boolean true);
|
||||
("Should return \"Boolean(false)\"", "Boolean(false)", Boolean false);
|
||||
]
|
||||
in
|
||||
List.iter
|
||||
(fun (msg, expected, actual) ->
|
||||
check string msg expected (pp_literals actual))
|
||||
to_check
|
||||
|
||||
let tests = ("Literals", [ test_case "pp_literals" `Quick test_pp_literals ])
|
||||
@@ -0,0 +1,24 @@
|
||||
open Alcotest
|
||||
open Syntax
|
||||
|
||||
let test_pp_source_files () =
|
||||
let to_check =
|
||||
[
|
||||
( "Should return \"SourceFile(...)\"",
|
||||
"SourceFile(ExpressionStatement(BinaryExpression(Add, \
|
||||
Literal(Integer(1)), Literal(Integer(2)))))",
|
||||
SourceFiles.SourceFile
|
||||
(Statements.ExpressionStatement
|
||||
(Expressions.BinaryExpression
|
||||
( BinaryOperators.Add,
|
||||
Expressions.Literal (Literals.Integer 1),
|
||||
Expressions.Literal (Literals.Integer 2) ))) );
|
||||
]
|
||||
in
|
||||
List.iter
|
||||
(fun (name, expected, actual) ->
|
||||
check string name expected (SourceFiles.pp_source_files actual))
|
||||
to_check
|
||||
|
||||
let tests =
|
||||
("SourceFiles", [ test_case "pp_source_files" `Quick test_pp_source_files ])
|
||||
@@ -0,0 +1,27 @@
|
||||
open Alcotest
|
||||
open Syntax
|
||||
|
||||
let test_pp_statements () =
|
||||
let to_check =
|
||||
[
|
||||
( "Should return \"ExpressionStatement(...)\"",
|
||||
"ExpressionStatement(Literal(Integer(1)))",
|
||||
Statements.ExpressionStatement
|
||||
(Expressions.Literal (Literals.Integer 1)) );
|
||||
( "Should return \"SequenceStatement(...)\"",
|
||||
"SequenceStatement(ExpressionStatement(Literal(Integer(1))), \
|
||||
ExpressionStatement(Literal(Float(1.000000))))",
|
||||
Statements.SequenceStatement
|
||||
( Statements.ExpressionStatement
|
||||
(Expressions.Literal (Literals.Integer 1)),
|
||||
Statements.ExpressionStatement
|
||||
(Expressions.Literal (Literals.Float 1.0)) ) );
|
||||
]
|
||||
in
|
||||
List.iter
|
||||
(fun (name, expected, actual) ->
|
||||
check string name expected (Statements.pp_statements actual))
|
||||
to_check
|
||||
|
||||
let tests =
|
||||
("Statements", [ test_case "pp_statements" `Quick test_pp_statements ])
|
||||
@@ -0,0 +1,13 @@
|
||||
open Alcotest
|
||||
|
||||
let () =
|
||||
run "Syntax"
|
||||
[
|
||||
TestTypes.tests;
|
||||
TestLiterals.tests;
|
||||
TestUnaryOperators.tests;
|
||||
TestBinaryOperators.tests;
|
||||
TestExpressions.tests;
|
||||
TestStatements.tests;
|
||||
TestSourceFiles.tests;
|
||||
]
|
||||
@@ -0,0 +1,18 @@
|
||||
open Alcotest
|
||||
open Syntax.Types
|
||||
|
||||
let test_pp_types () =
|
||||
let to_check =
|
||||
[
|
||||
("Should return \"IntegerType\"", "IntegerType", IntegerType);
|
||||
("Should return \"BooleanType\"", "BooleanType", BooleanType);
|
||||
("Should return \"StringType\"", "StringType", StringType);
|
||||
("Should return \"CharacterType\"", "CharacterType", CharacterType);
|
||||
("Should return \"VoidType\"", "VoidType", VoidType);
|
||||
]
|
||||
in
|
||||
List.iter
|
||||
(fun (msg, expected, input) -> check string msg expected (pp_types input))
|
||||
to_check
|
||||
|
||||
let tests = ("Types", [ test_case "pp_types" `Quick test_pp_types ])
|
||||
@@ -0,0 +1,19 @@
|
||||
open Alcotest
|
||||
open Syntax.UnaryOperators
|
||||
|
||||
let test_pp_unary_operators () =
|
||||
let to_check =
|
||||
[
|
||||
( "Should return \"ArithmeticNegation\"",
|
||||
ArithmeticNegation,
|
||||
"ArithmeticNegation" );
|
||||
]
|
||||
in
|
||||
List.iter
|
||||
(fun (msg, unary_operator, expected) ->
|
||||
check string msg expected (pp_unary_operators unary_operator))
|
||||
to_check
|
||||
|
||||
let tests =
|
||||
( "UnaryOperators",
|
||||
[ test_case "pp_unary_operators" `Quick test_pp_unary_operators ] )
|
||||
@@ -0,0 +1,4 @@
|
||||
(test
|
||||
(name TestSyntax)
|
||||
(libraries Syntax alcotest)
|
||||
)
|
||||
Reference in New Issue
Block a user