|
| 1 | +using FluentAssertions; |
| 2 | +using Xunit; |
| 3 | +using Sharpy.Compiler.Parser.Ast; |
| 4 | +using LexerNs = Sharpy.Compiler.Lexer; |
| 5 | +using ParserNs = Sharpy.Compiler.Parser; |
| 6 | +using ParserError = Sharpy.Compiler.Parser.ParserError; |
| 7 | + |
| 8 | +namespace Sharpy.Compiler.Tests.Parser; |
| 9 | + |
| 10 | +/// <summary> |
| 11 | +/// Parser tests: Function calls, conditionals, lambdas, and type annotations |
| 12 | +/// </summary> |
| 13 | +public partial class ParserTests |
| 14 | +{ |
| 15 | + #region Function Calls |
| 16 | + |
| 17 | + [Fact] |
| 18 | + public void ParseFunctionCall() |
| 19 | + { |
| 20 | + var module = Parse("foo()"); |
| 21 | + var exprStmt = module.Body[0].Should().BeOfType<ExpressionStatement>().Subject; |
| 22 | + var call = exprStmt.Expression.Should().BeOfType<FunctionCall>().Subject; |
| 23 | + call.Function.Should().BeOfType<Identifier>().Which.Name.Should().Be("foo"); |
| 24 | + call.Arguments.Should().BeEmpty(); |
| 25 | + } |
| 26 | + |
| 27 | + [Fact] |
| 28 | + public void ParseFunctionCallWithArgs() |
| 29 | + { |
| 30 | + var module = Parse("foo(1, 2)"); |
| 31 | + var exprStmt = module.Body[0].Should().BeOfType<ExpressionStatement>().Subject; |
| 32 | + var call = exprStmt.Expression.Should().BeOfType<FunctionCall>().Subject; |
| 33 | + call.Arguments.Should().HaveCount(2); |
| 34 | + call.Arguments[0].Should().BeOfType<IntegerLiteral>().Which.Value.Should().Be("1"); |
| 35 | + } |
| 36 | + |
| 37 | + [Fact] |
| 38 | + public void ParseMethodCall() |
| 39 | + { |
| 40 | + var module = Parse("obj.method(42)"); |
| 41 | + var exprStmt = module.Body[0].Should().BeOfType<ExpressionStatement>().Subject; |
| 42 | + var call = exprStmt.Expression.Should().BeOfType<FunctionCall>().Subject; |
| 43 | + var member = call.Function.Should().BeOfType<MemberAccess>().Subject; |
| 44 | + member.Member.Should().Be("method"); |
| 45 | + call.Arguments.Should().HaveCount(1); |
| 46 | + } |
| 47 | + |
| 48 | + #endregion |
| 49 | + |
| 50 | + #region Conditional and Lambda |
| 51 | + |
| 52 | + [Fact] |
| 53 | + public void ParseConditionalExpression() |
| 54 | + { |
| 55 | + var module = Parse("1 if True else 2"); |
| 56 | + var exprStmt = module.Body[0].Should().BeOfType<ExpressionStatement>().Subject; |
| 57 | + var cond = exprStmt.Expression.Should().BeOfType<ConditionalExpression>().Subject; |
| 58 | + cond.ThenValue.Should().BeOfType<IntegerLiteral>().Which.Value.Should().Be("1"); |
| 59 | + cond.Test.Should().BeOfType<BooleanLiteral>().Which.Value.Should().BeTrue(); |
| 60 | + cond.ElseValue.Should().BeOfType<IntegerLiteral>().Which.Value.Should().Be("2"); |
| 61 | + } |
| 62 | + |
| 63 | + [Fact] |
| 64 | + public void ParseLambdaExpression() |
| 65 | + { |
| 66 | + var module = Parse("lambda x: x + 1"); |
| 67 | + var exprStmt = module.Body[0].Should().BeOfType<ExpressionStatement>().Subject; |
| 68 | + var lambda = exprStmt.Expression.Should().BeOfType<LambdaExpression>().Subject; |
| 69 | + lambda.Parameters.Should().HaveCount(1); |
| 70 | + lambda.Parameters[0].Name.Should().Be("x"); |
| 71 | + lambda.Body.Should().BeOfType<BinaryOp>(); |
| 72 | + } |
| 73 | + |
| 74 | + [Fact] |
| 75 | + public void ParseLambdaNoParams() |
| 76 | + { |
| 77 | + var module = Parse("lambda: 42"); |
| 78 | + var exprStmt = module.Body[0].Should().BeOfType<ExpressionStatement>().Subject; |
| 79 | + var lambda = exprStmt.Expression.Should().BeOfType<LambdaExpression>().Subject; |
| 80 | + lambda.Parameters.Should().BeEmpty(); |
| 81 | + lambda.Body.Should().BeOfType<IntegerLiteral>().Which.Value.Should().Be("42"); |
| 82 | + } |
| 83 | + |
| 84 | + #endregion |
| 85 | + |
| 86 | + #region Type Annotations and Casts |
| 87 | + |
| 88 | + [Fact] |
| 89 | + public void ParseTypeAnnotation() |
| 90 | + { |
| 91 | + var module = Parse("x: int"); |
| 92 | + var varDecl = module.Body[0].Should().BeOfType<VariableDeclaration>().Subject; |
| 93 | + varDecl.Name.Should().Be("x"); |
| 94 | + varDecl.Type.Should().NotBeNull(); |
| 95 | + varDecl.Type.Name.Should().Be("int"); |
| 96 | + varDecl.Type.IsNullable.Should().BeFalse(); |
| 97 | + } |
| 98 | + |
| 99 | + [Fact] |
| 100 | + public void ParseNullableTypeAnnotation() |
| 101 | + { |
| 102 | + var module = Parse("x: int?"); |
| 103 | + var varDecl = module.Body[0].Should().BeOfType<VariableDeclaration>().Subject; |
| 104 | + varDecl.Type.IsNullable.Should().BeTrue(); |
| 105 | + varDecl.Type.Name.Should().Be("int"); |
| 106 | + } |
| 107 | + |
| 108 | + [Fact] |
| 109 | + public void ParseTypeCast() |
| 110 | + { |
| 111 | + var module = Parse("x as int"); |
| 112 | + var exprStmt = module.Body[0].Should().BeOfType<ExpressionStatement>().Subject; |
| 113 | + var cast = exprStmt.Expression.Should().BeOfType<TypeCast>().Subject; |
| 114 | + cast.Value.Should().BeOfType<Identifier>().Which.Name.Should().Be("x"); |
| 115 | + cast.TargetType.Name.Should().Be("int"); |
| 116 | + } |
| 117 | + |
| 118 | + [Fact] |
| 119 | + public void ParseTypeCheck() |
| 120 | + { |
| 121 | + var module = Parse("x is int"); |
| 122 | + var exprStmt = module.Body[0].Should().BeOfType<ExpressionStatement>().Subject; |
| 123 | + var check = exprStmt.Expression.Should().BeOfType<TypeCheck>().Subject; |
| 124 | + check.Value.Should().BeOfType<Identifier>().Which.Name.Should().Be("x"); |
| 125 | + check.CheckType.Name.Should().Be("int"); |
| 126 | + } |
| 127 | + |
| 128 | + [Fact] |
| 129 | + public void ParseNullableTypeInFunctionParameter() |
| 130 | + { |
| 131 | + var module = Parse(@" |
| 132 | +def greet(name: str?): |
| 133 | + pass |
| 134 | +"); |
| 135 | + var funcDef = module.Body[0].Should().BeOfType<FunctionDef>().Subject; |
| 136 | + funcDef.Parameters.Should().HaveCount(1); |
| 137 | + funcDef.Parameters[0].Type.Should().NotBeNull(); |
| 138 | + funcDef.Parameters[0].Type.Name.Should().Be("str"); |
| 139 | + funcDef.Parameters[0].Type.IsNullable.Should().BeTrue(); |
| 140 | + } |
| 141 | + |
| 142 | + [Fact] |
| 143 | + public void ParseNullableReturnType() |
| 144 | + { |
| 145 | + var module = Parse(@" |
| 146 | +def find_user(id: int) -> User?: |
| 147 | + pass |
| 148 | +"); |
| 149 | + var funcDef = module.Body[0].Should().BeOfType<FunctionDef>().Subject; |
| 150 | + funcDef.ReturnType.Should().NotBeNull(); |
| 151 | + funcDef.ReturnType.Name.Should().Be("User"); |
| 152 | + funcDef.ReturnType.IsNullable.Should().BeTrue(); |
| 153 | + } |
| 154 | + |
| 155 | + [Fact] |
| 156 | + public void ParseNullableDictType() |
| 157 | + { |
| 158 | + var module = Parse("mapping: dict[str, int?]?"); |
| 159 | + var varDecl = module.Body[0].Should().BeOfType<VariableDeclaration>().Subject; |
| 160 | + varDecl.Type.Should().NotBeNull(); |
| 161 | + varDecl.Type.Name.Should().Be("dict"); |
| 162 | + varDecl.Type.IsNullable.Should().BeTrue(); |
| 163 | + varDecl.Type.TypeArguments.Should().HaveCount(2); |
| 164 | + varDecl.Type.TypeArguments[0].Name.Should().Be("str"); |
| 165 | + varDecl.Type.TypeArguments[0].IsNullable.Should().BeFalse(); |
| 166 | + varDecl.Type.TypeArguments[1].Name.Should().Be("int"); |
| 167 | + varDecl.Type.TypeArguments[1].IsNullable.Should().BeTrue(); |
| 168 | + } |
| 169 | + |
| 170 | + #endregion |
| 171 | + |
| 172 | +} |
0 commit comments