Types-JSON

This commit is contained in:
Michael Schimmel
2025-08-27 18:00:01 +02:00
parent dc4066097c
commit afb8f459c6
7 changed files with 830 additions and 133 deletions
+23 -22
View File
@@ -8,7 +8,7 @@ uses
type
[TestFixture]
[IgnoreMemoryLeaks]
TMyTestObject = class
TTestDataTypes = class
public
[Test]
procedure TestRecords;
@@ -48,9 +48,10 @@ uses
System.Rtti,
System.TypInfo,
Myc.Data.Types,
Myc.Data.Types.RTTI;
Myc.Data.Types.RTTI,
Myc.Data.Types.JSON;
procedure TMyTestObject.TestRecords;
procedure TTestDataTypes.TestRecords;
var
intType: TDataType.TOrdinal;
floatType: TDataType.TFloat;
@@ -129,7 +130,7 @@ begin
);
end;
procedure TMyTestObject.TestRecords_Generic;
procedure TTestDataTypes.TestRecords_Generic;
var
dataType: TDataType.TRecord;
dataValue: TDataType.TRecord.TValue;
@@ -159,7 +160,7 @@ begin
Assert.AreEqual('<A: 1, B: two, C: 3>', IDataValue(dataValue).AsString, 'Generic record AsString is incorrect');
end;
procedure TMyTestObject.TestTuples;
procedure TTestDataTypes.TestTuples;
var
intValue: TDataType.TOrdinal.TValue;
floatValue: TDataType.TFloat.TValue;
@@ -193,7 +194,7 @@ begin
Assert.AreEqual('Tuple<Integer, Float>', type1.Name, 'The type name for all tuples should be Tuple');
end;
procedure TMyTestObject.TestTuples_Generic;
procedure TTestDataTypes.TestTuples_Generic;
var
tupleValue: TDataType.TTuple.TValue;
item: TDataType.TOrdinal.TValue;
@@ -219,7 +220,7 @@ begin
Assert.AreEqual('(1, 2, 3, 4, 5, 6)', IDataValue(tupleValue).AsString, 'Generic tuple AsString is incorrect');
end;
procedure TMyTestObject.TestArrays;
procedure TTestDataTypes.TestArrays;
var
intType: TDataType.TOrdinal;
floatType: TDataType.TFloat;
@@ -283,7 +284,7 @@ begin
);
end;
procedure TMyTestObject.TestArrays_OptimizedAndGeneric;
procedure TTestDataTypes.TestArrays_OptimizedAndGeneric;
var
arrayType: TDataType.TArray;
empty1, empty2, single, generic: TDataType.TArray.TValue;
@@ -316,7 +317,7 @@ begin
Assert.AreEqual('[1, 2, 3]', IDataValue(generic).AsString, 'Generic array AsString is incorrect');
end;
procedure TMyTestObject.TestTexts;
procedure TTestDataTypes.TestTexts;
var
textType: TDataType.TText;
textValue1, textValue2, castedValue: TDataType.TText.TValue;
@@ -354,7 +355,7 @@ begin
);
end;
procedure TMyTestObject.TestTexts_OptimizedEmpty;
procedure TTestDataTypes.TestTexts_OptimizedEmpty;
var
empty1, empty2: TDataType.TText.TValue;
begin
@@ -368,7 +369,7 @@ begin
Assert.AreEqual('', IDataValue(empty1).AsString, 'AsString of empty text should be empty');
end;
procedure TMyTestObject.TestFloats_OptimizedAndGeneric;
procedure TTestDataTypes.TestFloats_OptimizedAndGeneric;
var
zero1, zero2, nan1, nan2, generic: TDataType.TFloat.TValue;
begin
@@ -395,7 +396,7 @@ begin
Assert.AreEqual(123.45, generic.Value, 'Value of generic float is incorrect');
end;
procedure TMyTestObject.TestTimestamps;
procedure TTestDataTypes.TestTimestamps;
var
tsType: TDataType.TTimestamp;
tsValue1, tsValue2, castedValue: TDataType.TTimestamp.TValue;
@@ -435,7 +436,7 @@ begin
);
end;
procedure TMyTestObject.TestEnums;
procedure TTestDataTypes.TestEnums;
var
colorType: TDataType.TEnum;
red, green, blue: TDataType.TEnum.TValue;
@@ -490,7 +491,7 @@ begin
);
end;
procedure TMyTestObject.TestAsString;
procedure TTestDataTypes.TestAsString;
var
ordinalValue: TDataType.TOrdinal.TValue;
floatValue: TDataType.TFloat.TValue;
@@ -542,7 +543,7 @@ begin
Assert.AreEqual('(10, Tuple)', IDataValue(tupleValue).AsString, 'Tuple AsString incorrect');
end;
procedure TMyTestObject.TestAsTValue;
procedure TTestDataTypes.TestAsTValue;
var
dataValue: TDataType.TValue; // Keep as generic interface to test AsTValue
now: TDateTime;
@@ -551,21 +552,21 @@ begin
// This test is specifically for the IDataValue.AsTValue method. No changes needed.
// --- Ordinal ---
dataValue := TDataType.Ordinal.CreateValue(123);
var tv := dataValue.AsTValue;
var tv := AsTValue(dataValue);
Assert.IsFalse(tv.IsEmpty, 'Ordinal TValue should not be empty');
Assert.AreEqual(tkInt64, tv.Kind, 'Ordinal TValue kind should be tkInt64');
Assert.AreEqual(Int64(123), tv.AsInt64, 'Ordinal TValue content is incorrect');
// --- Float ---
dataValue := TDataType.Float.CreateValue(45.67);
tv := dataValue.AsTValue;
tv := AsTValue(dataValue);
Assert.IsFalse(tv.IsEmpty, 'Float TValue should not be empty');
Assert.AreEqual(tkFloat, tv.Kind, 'Float TValue kind should be tkFloat');
Assert.AreEqual(45.67, tv.AsExtended, 'Float TValue content is incorrect');
// --- Text ---
dataValue := TDataType.Text.CreateValue('Hello');
tv := dataValue.AsTValue;
tv := AsTValue(dataValue);
Assert.IsFalse(tv.IsEmpty, 'Text TValue should not be empty');
Assert.AreEqual(tkUString, tv.Kind, 'Text TValue kind should be tkUString');
Assert.AreEqual('Hello', tv.AsString, 'Text TValue content is incorrect');
@@ -573,7 +574,7 @@ begin
// --- Timestamp ---
now := System.SysUtils.Now;
dataValue := TDataType.Timestamp.CreateValue(now);
tv := dataValue.AsTValue;
tv := AsTValue(dataValue);
Assert.IsFalse(tv.IsEmpty, 'Timestamp TValue should not be empty');
Assert.AreEqual(tkFloat, tv.Kind, 'Timestamp TValue kind is incorrect');
Assert.AreEqual(now, tv.AsType<TDateTime>, 'Timestamp TValue content is incorrect');
@@ -581,13 +582,13 @@ begin
// --- Enum ---
colorType := TDataType.EnumOf('Color', ['Red', 'Green', 'Blue']);
dataValue := colorType.CreateValue('Green');
tv := dataValue.AsTValue;
tv := AsTValue(dataValue);
Assert.IsFalse(tv.IsEmpty, 'Enum TValue should not be empty');
Assert.AreEqual(tkInteger, tv.Kind, 'Enum TValue kind should be tkInteger');
Assert.AreEqual(1, tv.AsInteger, 'Enum TValue content is incorrect');
end;
procedure TMyTestObject.TestMethods;
procedure TTestDataTypes.TestMethods;
var
ordinalType: TDataType.TOrdinal;
textType: TDataType.TText;
@@ -658,7 +659,7 @@ begin
// --- 5. Test AsString and AsTValue ---
Assert.AreEqual('<METHOD>', IDataValue(methodValue).AsString, 'Method AsString is incorrect');
tv := TDataType.TValue(methodValue).AsTValue;
tv := AsTValue(methodValue);
Assert.IsFalse(tv.IsEmpty, 'Method TValue should not be empty');
Assert.AreEqual(tkInterface, tv.Kind, 'TValue kind for a TMethodProc should be tkInterface, as it is a reference-to-function');
Assert.IsTrue(TypeInfo(TDataMethodProc) = tv.TypeInfo, 'TValue should hold TMethodProc type info');