From 2b34046efc29d465622afe8e32cd2c32401980f9 Mon Sep 17 00:00:00 2001 From: Michael Schimmel Date: Mon, 29 Sep 2025 12:00:56 +0200 Subject: [PATCH] Fixed unit tests --- Test/Test.Ast.Interpreter.Scope.pas | 23 +-- Test/TestDataPOD.pas | 219 +++++++--------------------- 2 files changed, 65 insertions(+), 177 deletions(-) diff --git a/Test/Test.Ast.Interpreter.Scope.pas b/Test/Test.Ast.Interpreter.Scope.pas index 3fb4055..fda6a22 100644 --- a/Test/Test.Ast.Interpreter.Scope.pas +++ b/Test/Test.Ast.Interpreter.Scope.pas @@ -46,13 +46,18 @@ implementation function TInterpreterScopeTests.Execute(const ANode: IAstNode): TDataValue; var - boundScope: IExecutionScope; - visitor: IAstVisitor; + binder: IAstBinder; + boundNode: IAstNode; + descriptor: IScopeDescriptor; + runtimeScope: IExecutionScope; + visitor: IEvaluatorVisitor; begin // Helper to encapsulate the Bind -> CreateScope -> Evaluate pattern. - boundScope := TAstBinder.Bind(ANode, FGlobalScope).CreateScope(FGlobalScope); - visitor := TEvaluatorVisitor.Create(boundScope); - Result := ANode.Accept(visitor); + binder := TAstBinder.Create(FGlobalScope); + boundNode := binder.Execute(ANode, descriptor); + runtimeScope := descriptor.CreateScope(FGlobalScope); + visitor := TEvaluatorVisitor.Create(runtimeScope); + Result := visitor.Execute(boundNode); end; procedure TInterpreterScopeTests.Setup; @@ -95,7 +100,7 @@ begin TAst.Identifier('x'), TAst.BinaryExpr( TAst.Identifier('x'), - boAdd, + TScalar.TBinaryOp.Add, TAst.Constant(TScalar.FromInt64(5)) ) ) @@ -138,7 +143,7 @@ begin [], // Inner closure that is returned TAst.Assign( TAst.Identifier('a'), - TAst.BinaryExpr(TAst.Identifier('a'), boAdd, TAst.Constant(TScalar.FromInt64(5))) + TAst.BinaryExpr(TAst.Identifier('a'), TScalar.TBinaryOp.Add, TAst.Constant(TScalar.FromInt64(5))) ) ) ) @@ -169,7 +174,7 @@ begin [TAst.Identifier('p')], // Parameter 'p' TAst.LambdaExpr( [], // Returned lambda captures 'p' - TAst.BinaryExpr(TAst.Identifier('p'), boMultiply, TAst.Constant(TScalar.FromInt64(2))) + TAst.BinaryExpr(TAst.Identifier('p'), TScalar.TBinaryOp.Multiply, TAst.Constant(TScalar.FromInt64(2))) ) ) ), @@ -225,7 +230,7 @@ var begin // Defines a variable in the global scope and ensures a simple script can access it. FGlobalScope.Define('g', TScalar.FromInt64(99)); - mainBlock := TAst.BinaryExpr(TAst.Identifier('g'), boAdd, TAst.Constant(TScalar.FromInt64(1))); + mainBlock := TAst.BinaryExpr(TAst.Identifier('g'), TScalar.TBinaryOp.Add, TAst.Constant(TScalar.FromInt64(1))); resultValue := Execute(mainBlock); Assert.AreEqual(100, resultValue.AsScalar.Value.AsInt64, 'Should be able to access variables from the parent scope.'); diff --git a/Test/TestDataPOD.pas b/Test/TestDataPOD.pas index 22c83fc..682573d 100644 --- a/Test/TestDataPOD.pas +++ b/Test/TestDataPOD.pas @@ -14,32 +14,14 @@ type TTestPOD = class public // Tests the TScalar factory methods using a wide range of values. - [TestCase('IntegerZero', '0,skInteger')] - [TestCase('IntegerPositive', '12345,skInteger')] - [TestCase('IntegerNegative', '-54321,skInteger')] - [TestCase('Int64Zero', '0,skInt64')] - [TestCase('Int64Positive', '9876543210,skInt64')] - [TestCase('Int64Negative', '-1234567890,skInt64')] - [TestCase('Int64Max', '9223372036854775807,skInt64')] - [TestCase('UInt64Zero', '0,skUInt64')] - [TestCase('UInt64Positive', '12345678901234567890,skUInt64')] - [TestCase('UInt64Max', '18446744073709551615,skUInt64')] - [TestCase('Single', '1.23,skSingle')] - [TestCase('Double', '-3.1415926535,skDouble')] - [TestCase('DateTime', '45896.75,skDateTime')] // 28.08.2025 18:00 - [TestCase('BooleanTrue', 'True,skBoolean')] - [TestCase('BooleanFalse', 'False,skBoolean')] - [TestCase('Char', 'Z,skChar')] - [TestCase('String', 'Hello W,skString')] // Max 7 chars - [TestCase('StringTruncated', 'This string is definitely longer than 7 chars,skString')] - procedure TestScalarCreation(const AValueStr: string; AExpectedKind: TScalarKind); - - [Test] - procedure TestFromBytes; - [Test] - procedure TestFromDecimal; - [Test] - procedure TestFromTimestamp; + [TestCase('Ordinal_Zero', '0,Ordinal')] + [TestCase('Ordinal_Positive', '9876543210,Ordinal')] + [TestCase('Ordinal_Negative', '-1234567890,Ordinal')] + [TestCase('Ordinal_Max', '9223372036854775807,Ordinal')] + [TestCase('Float_Zero', '0.0,Float')] + [TestCase('Float_Positive', '1.23,Float')] + [TestCase('Float_Negative', '-3.1415926535,Float')] + procedure TestScalarCreation(const AValueStr: string; AExpectedKind: TScalar.TKind); // More detailed tests for complex POD types. [Test] @@ -53,192 +35,74 @@ type implementation uses - System.DateUtils, System.Variants; { TTestPOD } -procedure TTestPOD.TestScalarCreation(const AValueStr: string; AExpectedKind: TScalarKind); +procedure TTestPOD.TestScalarCreation(const AValueStr: string; AExpectedKind: TScalar.TKind); var scalar: TScalar; - s: string; begin case AExpectedKind of - skInteger: - begin - scalar := TScalar.FromInteger(StrToInt(AValueStr)); - Assert.AreEqual(AExpectedKind, scalar.Kind, 'Kind should be skInteger'); - Assert.AreEqual(StrToInt(AValueStr), scalar.Value.AsInteger, 'Value mismatch for AsInteger'); - end; - skInt64: + TScalar.TKind.Ordinal: begin scalar := TScalar.FromInt64(StrToInt64(AValueStr)); - Assert.AreEqual(AExpectedKind, scalar.Kind, 'Kind should be skInt64'); + Assert.AreEqual(AExpectedKind, scalar.Kind, 'Kind should be Ordinal'); Assert.AreEqual(StrToInt64(AValueStr), scalar.Value.AsInt64, 'Value mismatch for AsInt64'); end; - skUInt64: - begin - scalar := TScalar.FromUInt64(StrToUInt64(AValueStr)); - Assert.AreEqual(AExpectedKind, scalar.Kind, 'Kind should be skUInt64'); - Assert.AreEqual(StrToUInt64(AValueStr), scalar.Value.AsUInt64, 'Value mismatch for AsUInt64'); - end; - skSingle: - begin - scalar := TScalar.FromSingle(StrToFloat(AValueStr)); - Assert.AreEqual(AExpectedKind, scalar.Kind, 'Kind should be skSingle'); - Assert.AreEqual(StrToFloat(AValueStr), scalar.Value.AsSingle, 1E-6, 'Value mismatch for AsSingle'); - end; - skDouble: + TScalar.TKind.Float: begin scalar := TScalar.FromDouble(StrToFloat(AValueStr)); - Assert.AreEqual(AExpectedKind, scalar.Kind, 'Kind should be skDouble'); + Assert.AreEqual(AExpectedKind, scalar.Kind, 'Kind should be Float'); Assert.AreEqual(StrToFloat(AValueStr), scalar.Value.AsDouble, 1E-12, 'Value mismatch for AsDouble'); end; - skDateTime: - begin - scalar := TScalar.FromDateTime(StrToFloat(AValueStr)); - Assert.AreEqual(AExpectedKind, scalar.Kind, 'Kind should be skDateTime'); - Assert.AreEqual(StrToFloat(AValueStr), scalar.Value.AsDateTime, 'Value mismatch for AsDateTime'); - end; - skBoolean: - begin - scalar := TScalar.FromBoolean(AValueStr = 'True'); - Assert.AreEqual(AExpectedKind, scalar.Kind, 'Kind should be skBoolean'); - Assert.AreEqual(AValueStr = 'True', scalar.Value.AsBoolean, 'Value mismatch for AsBoolean'); - end; - skChar: - begin - scalar := TScalar.FromChar(AValueStr[1]); - Assert.AreEqual(AExpectedKind, scalar.Kind, 'Kind should be skChar'); - Assert.AreEqual(AValueStr[1], scalar.Value.AsChar, 'Value mismatch for AsChar'); - end; - skString: - begin - scalar := TScalar.FromString(AValueStr); - s := Copy(AValueStr, 1, 7); // ShortString truncates to 7 chars - Assert.AreEqual(AExpectedKind, scalar.Kind, 'Kind should be skString'); - Assert.AreEqual(s, string(scalar.Value.AsString), 'Value mismatch for AsString'); - end; else - Assert.Fail('Unhandled TScalarKind in test'); + Assert.Fail('Unhandled TScalar.TKind in test'); end; end; -procedure TTestPOD.TestFromBytes; -var - bytes, val: TScalarBytes; - scalar: TScalar; -begin - bytes[0] := $DE; - bytes[1] := $AD; - bytes[2] := $BE; - bytes[3] := $EF; - bytes[4] := $FE; - bytes[5] := $ED; - bytes[6] := $FA; - bytes[7] := $CE; - - scalar := TScalar.FromBytes(bytes); - - Assert.AreEqual(skBytes, scalar.Kind, 'Kind should be skBytes'); - val := scalar.Value.AsBytes; - Assert.IsTrue(CompareMem(@bytes, @val, Length(bytes)), 'Byte array content mismatch'); -end; - -procedure TTestPOD.TestFromDecimal; -var - dec: TDecimal; - scalar: TScalar; -begin - dec := TDecimal.Create(12345, 2); // 123.45 - - scalar := TScalar.FromDecimal(dec); - - Assert.AreEqual(skDecimal, scalar.Kind, 'Kind should be skDecimal'); - Assert.AreEqual(dec, scalar.Value.AsDecimal, 'Decimal value mismatch'); -end; - -procedure TTestPOD.TestFromTimestamp; -var - dt: TDateTime; - ts: TTimestamp; - scalar: TScalar; -begin - dt := EncodeDateTime(2025, 8, 28, 12, 53, 2, 500); - ts := DateTimeToTimeStamp(dt); - - scalar := TScalar.FromTimestamp(ts); - - Assert.AreEqual(skTimestamp, scalar.Kind, 'Kind should be skTimestamp'); - Assert.AreEqual(ts.Date, scalar.Value.AsTimestamp.Date, 'Timestamp.Date mismatch'); - Assert.AreEqual(ts.Time, scalar.Value.AsTimestamp.Time, 'Timestamp.Time mismatch'); -end; - procedure TTestPOD.TestScalarArray; var arr: TScalarArray; - values: TArray; + values: TArray; begin // Test with values - values := [TScalar.FromInteger(10).Value, TScalar.FromInteger(20).Value, TScalar.FromInteger(30).Value]; - arr := TScalarArray.Create(skInteger, values); + values := [TScalar.FromInt64(10).Value, TScalar.FromInt64(20).Value, TScalar.FromInt64(30).Value]; + arr := TScalarArray.Create(TScalar.TKind.Ordinal, values); - Assert.AreEqual(skInteger, arr.Kind, 'Array kind mismatch'); + Assert.AreEqual(TScalar.TKind.Ordinal, arr.Kind, 'Array kind mismatch'); Assert.AreEqual(Int64(3), Length(arr.Items), 'Array length mismatch'); - Assert.AreEqual(20, arr.Items[1].AsInteger, 'Array item content mismatch'); + Assert.AreEqual(Int64(20), arr.Items[1].AsInt64, 'Array item content mismatch'); // Test empty - arr := TScalarArray.Create(skDouble, []); - Assert.AreEqual(skDouble, arr.Kind, 'Empty array kind mismatch'); + arr := TScalarArray.Create(TScalar.TKind.Float, []); + Assert.AreEqual(TScalar.TKind.Float, arr.Kind, 'Empty array kind mismatch'); Assert.AreEqual(Int64(0), Length(arr.Items), 'Empty array length should be zero'); end; -procedure TTestPOD.TestScalarTuple; -var - tuple: TScalarTuple; -begin - // A tuple is a heterogeneous array of TScalar - SetLength(tuple, 3); - tuple[0] := TScalar.FromString('Test'); - tuple[1] := TScalar.FromInteger(123); - tuple[2] := TScalar.FromBoolean(True); - - Assert.AreEqual(Int64(3), Length(tuple), 'Tuple length mismatch'); - Assert.AreEqual(skString, tuple[0].Kind, 'Tuple item 0 kind mismatch'); - Assert.AreEqual('Test', string(tuple[0].Value.AsString), 'Tuple item 0 value mismatch'); - Assert.AreEqual(skInteger, tuple[1].Kind, 'Tuple item 1 kind mismatch'); - Assert.AreEqual(123, tuple[1].Value.AsInteger, 'Tuple item 1 value mismatch'); - Assert.AreEqual(skBoolean, tuple[2].Kind, 'Tuple item 2 kind mismatch'); - Assert.AreEqual(True, tuple[2].Value.AsBoolean, 'Tuple item 2 value mismatch'); -end; - procedure TTestPOD.TestScalarRecordAndDefinition; var recDef: TScalarRecordDefinition; - values: TArray; + values: TArray; rec: TScalarRecord; - mismatchedValues: TArray; + mismatchedValues: TArray; begin // 1. Create a definition recDef := - TScalarRecordDefinition.Create( - [ - TScalarRecordField.Create('ID', skInt64), - TScalarRecordField.Create('Name', skString), - TScalarRecordField.Create('Price', skDecimal) - ] - ); - Assert.AreEqual(Int64(3), Length(recDef.Fields), 'Record definition field count mismatch'); - Assert.AreEqual('Name', recDef.Fields[1].Name, 'Record definition field name mismatch'); + TScalarRecordDefinition + .Create([TScalarRecordField.Create('ID', TScalar.TKind.Ordinal), TScalarRecordField.Create('Price', TScalar.TKind.Float)]); + Assert.AreEqual(Int64(2), Length(recDef.Fields), 'Record definition field count mismatch'); + Assert.AreEqual('Price', recDef.Fields[1].Name, 'Record definition field name mismatch'); // 2. Create a matching set of values - values := [TScalar.FromInt64(99).Value, TScalar.FromString('Product').Value, TScalar.FromDecimal(TDecimal.Create(1995, 2)).Value]; + values := [TScalar.FromInt64(99).Value, TScalar.FromDouble(19.95).Value]; // 3. Create the record rec := TScalarRecord.Create(recDef, values); - Assert.AreEqual(Int64(3), Length(rec.Fields), 'Record field count mismatch'); - Assert.AreEqual('Product', string(rec.Fields[1].AsString), 'Record field value mismatch'); - Assert.AreEqual(TDecimal.Create(1995, 2), rec.Fields[2].AsDecimal, 'Record decimal field value mismatch'); + Assert.AreEqual(Int64(2), Length(rec.Fields), 'Record field count mismatch'); + Assert.AreEqual(19.95, rec.Fields[1].AsDouble, 1E-12, 'Record field value mismatch'); + Assert.AreEqual(Int64(99), rec.Items['ID'].Value.AsInt64, 'Record item by name mismatch'); + Assert.AreEqual(TScalar.TKind.Float, rec.Items['Price'].Kind, 'Record item kind by name mismatch'); // 4. Test assertion for mismatched field count mismatchedValues := [TScalar.FromInt64(1).Value]; @@ -249,6 +113,25 @@ begin ); end; +procedure TTestPOD.TestScalarTuple; +var + tuple: TScalarTuple; +begin + // A tuple is a heterogeneous array of TScalar + SetLength(tuple, 3); + tuple[0] := TScalar.FromInt64(123); + tuple[1] := TScalar.FromDouble(3.14); + tuple[2] := TScalar.FromInt64(-456); + + Assert.AreEqual(Int64(3), Length(tuple), 'Tuple length mismatch'); + Assert.AreEqual(TScalar.TKind.Ordinal, tuple[0].Kind, 'Tuple item 0 kind mismatch'); + Assert.AreEqual(Int64(123), tuple[0].Value.AsInt64, 'Tuple item 0 value mismatch'); + Assert.AreEqual(TScalar.TKind.Float, tuple[1].Kind, 'Tuple item 1 kind mismatch'); + Assert.AreEqual(3.14, tuple[1].Value.AsDouble, 1E-12, 'Tuple item 1 value mismatch'); + Assert.AreEqual(TScalar.TKind.Ordinal, tuple[2].Kind, 'Tuple item 2 kind mismatch'); + Assert.AreEqual(Int64(-456), tuple[2].Value.AsInt64, 'Tuple item 2 value mismatch'); +end; + initialization FormatSettings.DecimalSeparator := '.'; TDUnitX.RegisterTestFixture(TTestPOD);