1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573
|
unit tests.rtti;
{$ifdef fpc}
{$mode objfpc}{$H+}
{$modeswitch advancedrecords}
{$endif}
interface
uses
{$IFDEF FPC}
fpcunit,testregistry, testutils,
{$ELSE FPC}
TestFramework,
{$ENDIF FPC}
Classes, SysUtils, typinfo,
Rtti;
type
{ TTestCase1 }
TTestCase1= class(TTestCase)
published
//procedure GetTypes;
procedure GetTypeInteger;
procedure GetTypePointer;
procedure GetClassProperties;
procedure GetClassPropertiesValue;
procedure TestTRttiTypeProperties;
procedure TestPropGetValueString;
procedure TestPropGetValueInteger;
procedure TestPropGetValueBoolean;
procedure TestPropGetValueShortString;
procedure TestPropGetValueProcString;
procedure TestPropGetValueProcInteger;
procedure TestPropGetValueProcBoolean;
procedure TestPropGetValueProcShortString;
procedure TestPropGetValueObject;
procedure TestPropGetValueInterface;
procedure TestPropGetValueFloat;
procedure TestPropGetValueDynArray;
procedure TestPropGetValueEnumeration;
procedure TestPropGetValueChars;
procedure TestPropSetValueString;
procedure TestPropSetValueInteger;
procedure TestPropSetValueBoolean;
procedure TestPropSetValueShortString;
procedure TestPropSetValueObject;
procedure TestPropSetValueInterface;
procedure TestPropSetValueFloat;
procedure TestPropSetValueDynArray;
procedure TestPropSetValueEnumeration;
procedure TestPropSetValueChars;
procedure TestGetValueStringCastError;
procedure TestGetIsReadable;
procedure TestIsWritable;
procedure TestIsType;
procedure TestMakeNil;
procedure TestMakeObject;
procedure TestMakeArrayDynamic;
procedure TestMakeArrayStatic;
{$ifdef fpc}
procedure TestMakeArrayOpen;
{$endif}
procedure TestMakeSingle;
procedure TestMakeDouble;
procedure TestMakeExtended;
procedure TestMakeCurrency;
procedure TestMakeComp;
procedure TestMakeEnum;
procedure TestMakeAnsiChar;
procedure TestMakeWideChar;
procedure TestFromOrdinal;
procedure TestDataSize;
procedure TestDataSizeEmpty;
procedure TestReferenceRawData;
procedure TestReferenceRawDataEmpty;
procedure TestIsManaged;
{$ifdef fpc}
procedure TestOpenArrayToDyn;
{$endif}
procedure TestInterface;
{$ifdef fpc}
procedure TestInterfaceRaw;
{$endif}
procedure TestProcVar;
procedure TestMethod;
procedure TestRawThunk;
private
procedure MakeFromOrdinalTObject;
procedure MakeFromOrdinalSet;
procedure MakeFromOrdinalString;
procedure MakeFromOrdinalNil;
end;
implementation
uses
Tests.Rtti.Util;
type
{$M+}
TGetClassProperties = class
private
FPubPropRO: integer;
FPubPropRW: integer;
published
property PubPropRO: integer read FPubPropRO;
property PubPropRW: integer read FPubPropRW write FPubPropRW;
property PubPropSetRO: integer read FPubPropRO;
property PubPropSetRW: integer read FPubPropRW write FPubPropRW;
end;
TGetClassPropertiesSub = class(TGetClassProperties)
end;
TTestDynArray = array of Integer;
TTestEnumeration = (en1, en2, en3, en4);
{$M-}
{ TTestValueClass }
{$M+}
TTestValueClass = class
private
FAArray: TTestDynArray;
FAChar: AnsiChar;
FAComp: Comp;
FACurrency: Currency;
FADouble: Double;
FAEnumeration: TTestEnumeration;
FAExtended: Extended;
FAInteger: integer;
FAObject: TObject;
FASingle: Single;
FAString: string;
FABoolean: boolean;
FAShortString: ShortString;
FAUnknown: IUnknown;
FAWideChar: WideChar;
function GetAInteger: integer;
function GetAString: string;
function GetABoolean: boolean;
function GetAShortString: ShortString;
procedure SetWriteOnly(AValue: integer);
published
property AArray: TTestDynArray read FAArray write FAArray;
property AEnumeration: TTestEnumeration read FAEnumeration write FAEnumeration;
property AInteger: Integer read FAInteger write FAInteger;
property AString: string read FAString write FAString;
property ASingle: Single read FASingle write FASingle;
property ADouble: Double read FADouble write FADouble;
property AExtended: Extended read FAExtended write FAExtended;
property ACurrency: Currency read FACurrency write FACurrency;
property AObject: TObject read FAObject write FAObject;
property AUnknown: IUnknown read FAUnknown write FAUnknown;
property AComp: Comp read FAComp write FAComp;
property ABoolean: boolean read FABoolean write FABoolean;
property AShortString: ShortString read FAShortString write FAShortString;
property AGetInteger: Integer read GetAInteger;
property AGetString: string read GetAString;
property AGetBoolean: boolean read GetABoolean;
property AGetShortString: ShortString read GetAShortString;
property AWriteOnly: integer write SetWriteOnly;
property AChar: AnsiChar read FAChar write FAChar;
property AWideChar: WideChar read FAWideChar write FAWideChar;
end;
{$M-}
{$M+}
ITestInterface = interface
procedure Test;
function Test2: LongInt;
procedure Test3(aArg1: LongInt; const aArg2: AnsiString; var aArg3: Boolean; out aArg4: Word);
function Test4(aArg1: array of LongInt; aArg2: array of const): AnsiString;
end;
{$M-}
TManagedRec = record
s: string;
end;
{$ifdef fpc}
TManagedRecOp = record
class operator AddRef(var a: TManagedRecOp);
end;
{$endif}
TNonManagedRec = record
i: Integer;
end;
TManagedObj = object
i: IInterface;
end;
TNonManagedObj = object
d: double;
end;
TTestEnum = (te1, te2, te3, te4, te5);
TTestSet = set of TTestEnum;
TTestProc = procedure;
TTestFunc1 = function: LongInt;
TTestFunc2 = function(aArg1: LongInt; aArg2: array of LongInt): String;
TTestMethod = procedure of object;
TTestMethod1 = function: LongInt of object;
TTestMethod2 = function(aArg1: LongInt; aArg2: array of LongInt): String of object;
TTestHelper = class helper for TObject
end;
TArrayOfString = array[0..0] of string;
TArrayOfManagedRec = array[0..0] of TManagedRec;
TArrayOfNonManagedRec = array[0..0] of TNonManagedRec;
TArrayOfByte = array[0..0] of byte;
TArrayOfLongintDyn = array of LongInt;
TArrayOfLongintStatic = array[0..3] of LongInt;
TTestRecord = record
Value1: LongInt;
Value2: String;
end;
PTestRecord = ^TTestRecord;
{$ifdef fpc}
{$PUSH}
{$INTERFACES CORBA}
ICORBATest = interface
end;
{$POP}
{$endif}
{$ifdef fpc}
class operator TManagedRecOp.AddRef(var a: TManagedRecOp);
begin
end;
{$endif}
{ TTestValueClass }
function TTestValueClass.GetAInteger: integer;
begin
result := FAInteger;
end;
function TTestValueClass.GetAString: string;
begin
result := FAString;
end;
function TTestValueClass.GetABoolean: boolean;
begin
result := FABoolean;
end;
function TTestValueClass.GetAShortString: ShortString;
begin
Result := FAShortString;
end;
procedure TTestValueClass.SetWriteOnly(AValue: integer);
begin
// Do nothing
end;
{ Note: GetTypes currently only returns those types that had been acquired using
GetType, so GetTypes itself can't be really tested currently }
(*procedure TTestCase1.GetTypes;
var
LContext: TRttiContext;
LType: TRttiType;
IsTestCaseClassFound: boolean;
begin
LContext := TRttiContext.Create;
{ Enumerate all types declared in the application }
for LType in LContext.GetTypes() do
begin
if LType.Name='TTestCase1' then
IsTestCaseClassFound:=true;
end;
LContext.Free;
CheckTrue(IsTestCaseClassFound, 'RTTI information does not contain class of testcase.');
end;*)
procedure TTestCase1.TestGetValueStringCastError;
var
ATestClass : TTestValueClass;
c: TRttiContext;
ARttiType: TRttiType;
AValue: TValue;
i: integer;
HadException: boolean;
begin
c := TRttiContext.Create;
try
ATestClass := TTestValueClass.Create;
ATestClass.AString := '12';
try
ARttiType := c.GetType(ATestClass.ClassInfo);
AValue := ARttiType.GetProperty('astring').GetValue(ATestClass);
HadException := false;
try
i := AValue.AsInteger;
except
on E: Exception do
if E.ClassType=EInvalidCast then
HadException := true;
end;
Check(HadException, 'No or invalid exception on invalid cast');
finally
AtestClass.Free;
end;
finally
c.Free;
end;
end;
procedure TTestCase1.TestMakeNil;
var
value: TValue;
begin
TValue.Make(Nil, Nil, value);
CheckTrue(value.Kind = tkUnknown);
CheckTrue(value.IsEmpty);
CheckTrue(value.IsObject);
CheckTrue(value.IsClass);
CheckTrue(value.IsOrdinal);
CheckFalse(value.IsArray);
CheckTrue(value.AsObject = Nil);
CheckTrue(value.AsClass = Nil);
CheckTrue(value.AsInterface = Nil);
CheckEquals(0, value.AsOrdinal);
TValue.Make(Nil, TypeInfo(TObject), value);
CheckTrue(value.IsEmpty);
CheckTrue(value.IsObject);
CheckTrue(value.IsClass);
CheckTrue(value.IsOrdinal);
CheckFalse(value.IsArray);
CheckTrue(value.AsObject=Nil);
CheckTrue(value.AsClass=Nil);
CheckTrue(value.AsInterface=Nil);
CheckEquals(0, value.AsOrdinal);
TValue.Make(Nil, TypeInfo(TClass), value);
CheckTrue(value.IsEmpty);
CheckTrue(value.IsClass);
CheckTrue(value.IsOrdinal);
CheckFalse(value.IsArray);
CheckTrue(value.AsObject=Nil);
CheckTrue(value.AsClass=Nil);
CheckTrue(value.AsInterface=Nil);
CheckEquals(0, value.AsOrdinal);
TValue.Make(Nil, TypeInfo(LongInt), value);
CheckTrue(value.IsOrdinal);
CheckFalse(value.IsEmpty);
CheckFalse(value.IsClass);
CheckFalse(value.IsObject);
CheckFalse(value.IsArray);
CheckEquals(0, value.AsOrdinal);
CheckEquals(0, value.AsInteger);
CheckEquals(0, value.AsInt64);
CheckEquals(0, value.AsUInt64);
TValue.Make(Nil, TypeInfo(String), value);
CheckFalse(value.IsEmpty);
CheckFalse(value.IsObject);
CheckFalse(value.IsClass);
CheckFalse(value.IsArray);
CheckEquals('', value.AsString);
end;
procedure TTestCase1.TestMakeObject;
var
AValue: TValue;
ATestClass: TTestValueClass;
begin
ATestClass := TTestValueClass.Create;
ATestClass.AInteger := 54329;
TValue.Make(@ATestClass, TypeInfo(TTestValueClass),AValue);
CheckEquals(AValue.IsClass, False);
CheckEquals(AValue.IsObject, True);
Check(AValue.AsObject=ATestClass);
Check(PPointer(AValue.GetReferenceToRawData)^ = Pointer(ATestClass));
CheckEquals(TTestValueClass(AValue.AsObject).AInteger, 54329);
ATestClass.Free;
end;
procedure TTestCase1.TestMakeArrayDynamic;
var
arr: TArrayOfLongintDyn;
value: TValue;
begin
SetLength(arr, 2);
arr[0] := 42;
arr[1] := 21;
TValue.Make(@arr, TypeInfo(TArrayOfLongintDyn), value);
CheckEquals(value.IsArray, True);
CheckEquals(value.IsObject, False);
CheckEquals(value.IsOrdinal, False);
CheckEquals(value.IsClass, False);
CheckEquals(value.GetArrayLength, 2);
CheckEquals(value.GetArrayElement(0).AsInteger, 42);
CheckEquals(value.GetArrayElement(1).AsInteger, 21);
Check(PPointer(value.GetReferenceToRawData)^ = Pointer(arr));
value.SetArrayElement(0, 84);
CheckEquals(arr[0], 84);
end;
procedure TTestCase1.TestMakeArrayStatic;
type
TArrStat = array[0..1] of LongInt;
TArrStat2D = array[0..1, 0..1] of LongInt;
var
arr: TArrStat;
arr2D: TArrStat2D;
value: TValue;
begin
arr[0] := 42;
arr[1] := 21;
TValue.Make(@arr, TypeInfo(TArrStat), value);
CheckEquals(value.IsArray, True);
CheckEquals(value.IsObject, False);
CheckEquals(value.IsOrdinal, False);
CheckEquals(value.IsClass, False);
CheckEquals(value.GetArrayLength, 2);
CheckEquals(value.GetArrayElement(0).AsInteger, 42);
CheckEquals(value.GetArrayElement(1).AsInteger, 21);
value.SetArrayElement(0, 84);
{ since this is a static array the original array isn't touched! }
CheckEquals(arr[0], 42);
arr2D[0, 0] := 42;
arr2D[0, 1] := 21;
arr2D[1, 0] := 84;
arr2D[1, 1] := 63;
TValue.Make(@arr2D, TypeInfo(TArrStat2D), value);
CheckEquals(value.IsArray, True);
CheckEquals(value.GetArrayLength, 4);
CheckEquals(value.GetArrayElement(0).AsInteger, 42);
CheckEquals(value.GetArrayElement(1).AsInteger, 21);
CheckEquals(value.GetArrayElement(2).AsInteger, 84);
CheckEquals(value.GetArrayElement(3).AsInteger, 63);
end;
{$ifdef fpc}
procedure TTestCase1.TestMakeArrayOpen;
procedure TestOpenArrayValueCopy(aArr: array of LongInt);
var
value: TValue;
begin
TValue.MakeOpenArray(@aArr[0], Length(aArr), PTypeInfo(TypeInfo(aArr)), value);
CheckEquals(value.IsArray, True);
CheckEquals(value.IsOpenArray, True);
CheckEquals(value.IsObject, False);
CheckEquals(value.IsOrdinal, False);
CheckEquals(value.IsClass, False);
CheckEquals(value.GetArrayLength, 2);
CheckEquals(value.GetArrayElement(0).AsInteger, 42);
CheckEquals(value.GetArrayElement(1).AsInteger, 21);
value.SetArrayElement(0, 84);
{ since this is an open array the original array is modified! }
CheckEquals(aArr[0], 84);
end;
procedure TestOpenArrayValueVar(var aArr: array of LongInt);
var
value: TValue;
begin
TValue.MakeOpenArray(@aArr[0], Length(aArr), PTypeInfo(TypeInfo(aArr)), value);
CheckEquals(value.IsArray, True);
CheckEquals(value.IsOpenArray, True);
CheckEquals(value.IsObject, False);
CheckEquals(value.IsOrdinal, False);
CheckEquals(value.IsClass, False);
CheckEquals(value.GetArrayLength, 2);
CheckEquals(value.GetArrayElement(0).AsInteger, 42);
CheckEquals(value.GetArrayElement(1).AsInteger, 21);
value.SetArrayElement(0, 84);
{ since this is an open array the original array is modified! }
CheckEquals(aArr[0], 84);
end;
procedure TestOpenArrayValueOut(var aArr: array of LongInt);
var
value: TValue;
begin
TValue.MakeOpenArray(@aArr[0], Length(aArr), PTypeInfo(TypeInfo(aArr)), value);
CheckEquals(value.IsArray, True);
CheckEquals(value.IsOpenArray, True);
CheckEquals(value.IsObject, False);
CheckEquals(value.IsOrdinal, False);
CheckEquals(value.IsClass, False);
CheckEquals(value.GetArrayLength, 2);
CheckEquals(value.GetArrayElement(0).AsInteger, 42);
CheckEquals(value.GetArrayElement(1).AsInteger, 21);
value.SetArrayElement(0, 84);
value.SetArrayElement(1, 128);
{ since this is an open array the original array is modified! }
CheckEquals(aArr[0], 84);
CheckEquals(aArr[1], 128);
CheckEquals(value.GetArrayElement(0).AsInteger, 84);
CheckEquals(value.GetArrayElement(1).AsInteger, 128);
end;
var
arr: array of LongInt;
begin
TestOpenArrayValueCopy([42, 21]);
arr := [42, 21];
TestOpenArrayValueVar(arr);
CheckEquals(arr[0], 84);
CheckEquals(arr[1], 21);
arr := [42, 21];
TestOpenArrayValueOut(arr);
CheckEquals(arr[0], 84);
CheckEquals(arr[1], 128);
end;
{$endif}
procedure TTestCase1.TestMakeSingle;
var
fs: Single;
v: TValue;
hadexcept: Boolean;
begin
fs := 3.14;
TValue.Make(@fs, TypeInfo(fs), v);
CheckEquals(v.IsClass, False);
CheckEquals(v.IsObject, False);
CheckEquals(v.IsOrdinal, False);
Check(v.AsExtended=fs);
Check(v.GetReferenceToRawData <> @fs);
try
hadexcept := False;
v.AsInt64;
except
hadexcept := True;
end;
CheckTrue(hadexcept, 'No signed type conversion exception');
try
hadexcept := False;
v.AsUInt64;
except
hadexcept := True;
end;
CheckTrue(hadexcept, 'No unsigned type conversion exception');
end;
procedure TTestCase1.TestMakeDouble;
var
fd: Double;
v: TValue;
hadexcept: Boolean;
begin
fd := 3.14;
TValue.Make(@fd, TypeInfo(fd), v);
CheckEquals(v.IsClass, False);
CheckEquals(v.IsObject, False);
CheckEquals(v.IsOrdinal, False);
Check(v.AsExtended=fd);
Check(v.GetReferenceToRawData <> @fd);
try
hadexcept := False;
v.AsInt64;
except
hadexcept := True;
end;
CheckTrue(hadexcept, 'No signed type conversion exception');
try
hadexcept := False;
v.AsUInt64;
except
hadexcept := True;
end;
CheckTrue(hadexcept, 'No unsigned type conversion exception');
end;
procedure TTestCase1.TestMakeExtended;
var
fe: Extended;
v: TValue;
hadexcept: Boolean;
begin
fe := 3.14;
TValue.Make(@fe, TypeInfo(fe), v);
CheckEquals(v.IsClass, False);
CheckEquals(v.IsObject, False);
CheckEquals(v.IsOrdinal, False);
Check(v.AsExtended=fe);
Check(v.GetReferenceToRawData <> @fe);
try
hadexcept := False;
v.AsInt64;
except
hadexcept := True;
end;
CheckTrue(hadexcept, 'No signed type conversion exception');
try
hadexcept := False;
v.AsUInt64;
except
hadexcept := True;
end;
CheckTrue(hadexcept, 'No unsigned type conversion exception');
end;
procedure TTestCase1.TestMakeCurrency;
var
fcu: Currency;
v: TValue;
hadexcept: Boolean;
begin
fcu := 3.14;
TValue.Make(@fcu, TypeInfo(fcu), v);
CheckEquals(v.IsClass, False);
CheckEquals(v.IsObject, False);
CheckEquals(v.IsOrdinal, False);
Check(v.AsExtended=Extended(fcu));
Check(v.AsCurrency=fcu);
Check(v.GetReferenceToRawData <> @fcu);
try
hadexcept := False;
v.AsInt64;
except
hadexcept := True;
end;
CheckTrue(hadexcept, 'No signed type conversion exception');
try
hadexcept := False;
v.AsUInt64;
except
hadexcept := True;
end;
CheckTrue(hadexcept, 'No unsigned type conversion exception');
end;
procedure TTestCase1.TestMakeComp;
var
fco: Comp;
v: TValue;
hadexcept: Boolean;
begin
fco := 314;
TValue.Make(@fco, TypeInfo(fco), v);
if v.Kind <> tkFloat then
Exit;
CheckEquals(v.IsClass, False);
CheckEquals(v.IsObject, False);
CheckEquals(v.IsOrdinal, False);
Check(v.AsExtended=Extended(fco));
Check(v.GetReferenceToRawData <> @fco);
try
hadexcept := False;
CheckEquals(v.AsInt64, 314);
except
hadexcept := True;
end;
CheckFalse(hadexcept, 'Had signed type conversion exception');
try
hadexcept := False;
CheckEquals(v.AsUInt64, 314);
except
hadexcept := True;
end;
CheckFalse(hadexcept, 'Had unsigned type conversion exception');
end;
procedure TTestCase1.TestMakeEnum;
var
e: TTestEnum;
v: TValue;
begin
e := te1;
TValue.Make(@e, TypeInfo(e), v);
Check(not v.IsClass);
Check(not v.IsArray);
Check(not v.IsEmpty);
Check(not v.IsOpenArray);
Check(not v.IsObject);
Check(v.IsOrdinal);
Check(v.GetReferenceToRawData <> @e);
Check(TTestEnum(v.AsOrdinal) = te1);
end;
procedure TTestCase1.TestMakeAnsiChar;
var
c: AnsiChar;
v: TValue;
begin
c := #20;
TValue.Make(@c, TypeInfo(c), v);
Check(not v.IsClass);
Check(not v.IsArray);
Check(not v.IsEmpty);
Check(not v.IsOpenArray);
Check(not v.IsObject);
Check(v.IsOrdinal);
Check(v.GetReferenceToRawData <> @c);
Check(AnsiChar(v.AsOrdinal) = #20);
Check(v.AsAnsiChar = #20);
end;
procedure TTestCase1.TestMakeWideChar;
var
c: WideChar;
v: TValue;
begin
c := #$1234;
TValue.Make(@c, TypeInfo(c), v);
Check(not v.IsClass);
Check(not v.IsArray);
Check(not v.IsEmpty);
Check(not v.IsOpenArray);
Check(not v.IsObject);
Check(v.IsOrdinal);
Check(v.GetReferenceToRawData <> @c);
Check(WideChar(v.AsOrdinal) = #$1234);
Check(v.AsWideChar = #$1234);
end;
procedure TTestCase1.MakeFromOrdinalTObject;
begin
TValue.FromOrdinal(TypeInfo(TObject), 42);
end;
procedure TTestCase1.MakeFromOrdinalSet;
begin
TValue.FromOrdinal(TypeInfo(TTestSet), 42);
end;
procedure TTestCase1.MakeFromOrdinalString;
begin
TValue.FromOrdinal(TypeInfo(AnsiString), 42);
end;
procedure TTestCase1.MakeFromOrdinalNil;
begin
TValue.FromOrdinal(Nil, 42);
end;
procedure TTestCase1.TestFromOrdinal;
var
v: TValue;
begin
v := TValue.FromOrdinal(TypeInfo(LongInt), 42);
Check(v.IsOrdinal);
CheckEquals(v.AsOrdinal, 42);
v := TValue.FromOrdinal(TypeInfo(Boolean), Ord(True));
Check(v.IsOrdinal);
CheckEquals(v.AsOrdinal, Ord(True));
v := TValue.FromOrdinal(TypeInfo(Int64), $1234123412341234);
Check(v.IsOrdinal);
CheckEquals(v.AsOrdinal, $1234123412341234);
v := TValue.FromOrdinal(TypeInfo(QWord), $1234123412341234);
Check(v.IsOrdinal);
CheckEquals(v.AsOrdinal, $1234123412341234);
v := TValue.FromOrdinal(TypeInfo(LongBool), Ord(True));
Check(v.IsOrdinal);
CheckEquals(v.AsOrdinal, Ord(True));
v := TValue.FromOrdinal(TypeInfo(TTestEnum), Ord(te1));
Check(v.IsOrdinal);
CheckEquals(v.AsOrdinal, Ord(te1));
v := TValue.FromOrdinal(TypeInfo(AnsiChar), Ord(#20));
Check(v.IsOrdinal);
CheckEquals(v.AsOrdinal, Ord(#20));
v := TValue.FromOrdinal(TypeInfo(WideChar), Ord(#$1234));
Check(v.IsOrdinal);
CheckEquals(v.AsOrdinal, Ord(#$1234));
CheckException({$ifdef fpc}@{$endif}MakeFromOrdinalNil, EInvalidCast);
CheckException({$ifdef fpc}@{$endif}MakeFromOrdinalTObject, EInvalidCast);
CheckException({$ifdef fpc}@{$endif}MakeFromOrdinalSet, EInvalidCast);
CheckException({$ifdef fpc}@{$endif}MakeFromOrdinalString, EInvalidCast);
end;
procedure TTestCase1.TestGetIsReadable;
var
c: TRttiContext;
ARttiType: TRttiType;
AProperty: TRttiProperty;
begin
c := TRttiContext.Create;
try
ARttiType := c.GetType(TTestValueClass);
AProperty := ARttiType.GetProperty('aBoolean');
CheckEquals(AProperty.IsReadable, true);
AProperty := ARttiType.GetProperty('aGetBoolean');
CheckEquals(AProperty.IsReadable, true);
AProperty := ARttiType.GetProperty('aWriteOnly');
CheckEquals(AProperty.IsReadable, False);
finally
c.Free;
end;
end;
procedure TTestCase1.TestIsWritable;
var
c: TRttiContext;
ARttiType: TRttiType;
AProperty: TRttiProperty;
begin
c := TRttiContext.Create;
try
ARttiType := c.GetType(TTestValueClass);
AProperty := ARttiType.GetProperty('aBoolean');
CheckEquals(AProperty.IsWritable, true);
AProperty := ARttiType.GetProperty('aGetBoolean');
CheckEquals(AProperty.IsWritable, false);
AProperty := ARttiType.GetProperty('aWriteOnly');
CheckEquals(AProperty.IsWritable, True);
finally
c.Free;
end;
end;
procedure TTestCase1.TestIsType;
type
TMyLongInt = type LongInt;
var
v: TValue;
l: LongInt;
ml: TMyLongInt;
begin
l := 42;
ml := 42;
TValue.Make(@l, TypeInfo(l), v);
Check(v.IsType(TypeInfo(l)));
Check(not v.IsType(TypeInfo(ml)));
Check(not v.IsType(TypeInfo(String)));
Check(v.specialize IsType<LongInt>);
Check(not v.specialize IsType<TMyLongInt>);
Check(not v.specialize IsType<String>);
TValue.Make(@ml, TypeInfo(ml), v);
Check(v.IsType(TypeInfo(ml)));
Check(not v.IsType(TypeInfo(l)));
Check(not v.IsType(TypeInfo(String)));
Check(v.specialize IsType<TMyLongInt>);
Check(not v.specialize IsType<LongInt>);
Check(not v.specialize IsType<String>);
end;
procedure TTestCase1.TestPropGetValueBoolean;
var
ATestClass : TTestValueClass;
c: TRttiContext;
ARttiType: TRttiType;
AProperty: TRttiProperty;
AValue: TValue;
begin
c := TRttiContext.Create;
try
ATestClass := TTestValueClass.Create;
ATestClass.ABoolean := true;
try
ARttiType := c.GetType(ATestClass.ClassInfo);
Check(assigned(ARttiType));
AProperty := ARttiType.GetProperty('aBoolean');
AValue := AProperty.GetValue(ATestClass);
CheckEquals(true,AValue.AsBoolean);
ATestClass.ABoolean := false;
CheckEquals(true, AValue.AsBoolean);
CheckEquals('True', AValue.ToString);
CheckEquals(True, AValue.IsOrdinal);
CheckEquals(1, AValue.AsOrdinal);
finally
AtestClass.Free;
end;
CheckEquals(True,AValue.AsBoolean);
finally
c.Free;
end;
end;
procedure TTestCase1.TestPropGetValueShortString;
var
ATestClass : TTestValueClass;
c: TRttiContext;
ARttiType: TRttiType;
AProperty: TRttiProperty;
AValue: TValue;
begin
c := TRttiContext.Create;
try
ATestClass := TTestValueClass.Create;
ATestClass.AShortString := 'Hello World';
try
ARttiType := c.GetType(ATestClass.ClassInfo);
Check(assigned(ARttiType));
AProperty := ARttiType.GetProperty('aShortString');
AValue := AProperty.GetValue(ATestClass);
CheckEquals('Hello World',AValue.AsString);
ATestClass.AShortString := 'Foobar';
CheckEquals('Hello World', AValue.AsString);
CheckEquals(False, AValue.IsOrdinal);
CheckEquals(False, AValue.IsObject);
CheckEquals(False, AValue.IsArray);
CheckEquals(False, AValue.IsClass);
finally
AtestClass.Free;
end;
CheckEquals('Hello World',AValue.AsString);
finally
c.Free;
end;
end;
procedure TTestCase1.TestPropGetValueInteger;
var
ATestClass : TTestValueClass;
c: TRttiContext;
ARttiType: TRttiType;
AProperty: TRttiProperty;
AValue: TValue;
begin
c := TRttiContext.Create;
try
ATestClass := TTestValueClass.Create;
ATestClass.AInteger := 472349;
try
ARttiType := c.GetType(ATestClass.ClassInfo);
Check(assigned(ARttiType));
AProperty := ARttiType.GetProperty('ainteger');
AValue := AProperty.GetValue(ATestClass);
CheckEquals(472349,AValue.AsInteger);
ATestClass.AInteger := 12;
CheckEquals(472349, AValue.AsInteger);
CheckEquals('472349', AValue.ToString);
CheckEquals(True, AValue.IsOrdinal);
finally
AtestClass.Free;
end;
CheckEquals(472349,AValue.AsInteger);
finally
c.Free;
end;
end;
procedure TTestCase1.TestPropGetValueString;
var
ATestClass : TTestValueClass;
c: TRttiContext;
ARttiType: TRttiType;
AProperty: TRttiProperty;
AValue: TValue;
i: int64;
begin
c := TRttiContext.Create;
try
ATestClass := TTestValueClass.Create;
ATestClass.AString := 'Hello World';
try
ARttiType := c.GetType(ATestClass.ClassInfo);
Check(assigned(ARttiType));
AProperty := ARttiType.GetProperty('astring');
AValue := AProperty.GetValue(ATestClass);
CheckEquals('Hello World',AValue.AsString);
ATestClass.AString := 'Goodbye World';
CheckEquals('Hello World',AValue.AsString);
CheckEquals('Hello World',AValue.ToString);
Check(TypeInfo(string)=AValue.TypeInfo);
Check(AValue.TypeData=GetTypeData(AValue.TypeInfo));
Check(AValue.IsEmpty=false);
Check(AValue.IsObject=false);
Check(AValue.IsClass=false);
CheckEquals(AValue.IsOrdinal, false);
CheckEquals(AValue.TryAsOrdinal(i), false);
CheckEquals(AValue.IsType(TypeInfo(string)), true);
CheckEquals(AValue.IsType(TypeInfo(integer)), false);
CheckEquals(AValue.IsArray, false);
finally
AtestClass.Free;
end;
CheckEquals('Hello World',AValue.AsString);
finally
c.Free;
end;
end;
procedure TTestCase1.TestPropGetValueProcBoolean;
var
ATestClass : TTestValueClass;
c: TRttiContext;
ARttiType: TRttiType;
AProperty: TRttiProperty;
AValue: TValue;
begin
c := TRttiContext.Create;
try
ATestClass := TTestValueClass.Create;
ATestClass.ABoolean := true;
try
ARttiType := c.GetType(ATestClass.ClassInfo);
Check(assigned(ARttiType));
AProperty := ARttiType.GetProperty('aGetBoolean');
AValue := AProperty.GetValue(ATestClass);
CheckEquals(true,AValue.AsBoolean);
finally
AtestClass.Free;
end;
CheckEquals(True,AValue.AsBoolean);
finally
c.Free;
end;
end;
procedure TTestCase1.TestPropGetValueProcShortString;
var
ATestClass : TTestValueClass;
c: TRttiContext;
ARttiType: TRttiType;
AProperty: TRttiProperty;
AValue: TValue;
begin
c := TRttiContext.Create;
try
ATestClass := TTestValueClass.Create;
ATestClass.AShortString := 'Hello World';
try
ARttiType := c.GetType(ATestClass.ClassInfo);
Check(assigned(ARttiType));
AProperty := ARttiType.GetProperty('aGetShortString');
AValue := AProperty.GetValue(ATestClass);
CheckEquals('Hello World',AValue.AsString);
finally
AtestClass.Free;
end;
CheckEquals('Hello World',AValue.AsString);
finally
c.Free;
end;
end;
procedure TTestCase1.TestPropGetValueObject;
var
ATestClass : TTestValueClass;
c: TRttiContext;
ARttiType: TRttiType;
AProperty: TRttiProperty;
AValue: TValue;
O: TObject;
begin
c := TRttiContext.Create;
O := TObject.Create;
try
ATestClass := TTestValueClass.Create;
ATestClass.AObject := O;
try
ARttiType := c.GetType(ATestClass.ClassInfo);
Check(assigned(ARttiType));
AProperty := ARttiType.GetProperty('AObject');
AValue := AProperty.GetValue(ATestClass);
CheckEquals(O.GetHashCode, AValue.AsObject.GetHashCode);
finally
AtestClass.Free;
end;
CheckEquals(O.GetHashCode, AValue.AsObject.GetHashCode);
finally
c.Free;
O.Free;
end;
end;
procedure TTestCase1.TestPropGetValueInterface;
var
ATestClass : TTestValueClass;
c: TRttiContext;
ARttiType: TRttiType;
AProperty: TRttiProperty;
AValue: TValue;
i: IInterface;
begin
c := TRttiContext.Create;
i := TInterfacedObject.Create;
try
ATestClass := TTestValueClass.Create;
ATestClass.AUnknown := i;
try
ARttiType := c.GetType(ATestClass.ClassInfo);
Check(assigned(ARttiType));
AProperty := ARttiType.GetProperty('AUnknown');
AValue := AProperty.GetValue(ATestClass);
Check(i = AValue.AsInterface);
finally
AtestClass.Free;
end;
Check(i = AValue.AsInterface);
finally
c.Free;
end;
end;
procedure TTestCase1.TestPropGetValueFloat;
var
ATestClass : TTestValueClass;
c: TRttiContext;
ARttiType: TRttiType;
AProperty: TRttiProperty;
AValueS, AValueD, AValueE, AValueC, AValueCm: TValue;
begin
c := TRttiContext.Create;
try
ATestClass := TTestValueClass.Create;
ATestClass.ASingle := 1.1;
ATestClass.ADouble := 2.2;
ATestClass.AExtended := 3.3;
ATestClass.ACurrency := 4;
ATestClass.AComp := 5;
try
ARttiType := c.GetType(ATestClass.ClassInfo);
Check(assigned(ARttiType));
AProperty := ARttiType.GetProperty('ASingle');
AValueS := AProperty.GetValue(ATestClass);
CheckEquals(1.1, AValueS.AsExtended, 0.001);
AProperty := ARttiType.GetProperty('ADouble');
AValueD := AProperty.GetValue(ATestClass);
CheckEquals(2.2, AValueD.AsExtended, 0.001);
AProperty := ARttiType.GetProperty('AExtended');
AValueE := AProperty.GetValue(ATestClass);
CheckEquals(3.3, AValueE.AsExtended, 0.001);
AProperty := ARttiType.GetProperty('ACurrency');
AValueC := AProperty.GetValue(ATestClass);
CheckEquals(4.0, AValueC.AsExtended, 0.001);
AProperty := ARttiType.GetProperty('AComp');
AValueCm := AProperty.GetValue(ATestClass);
CheckEquals(5.0, AValueCm.AsExtended, 0.001);
finally
AtestClass.Free;
end;
CheckEquals(1.1, AValueS.AsExtended, 0.001);
CheckEquals(2.2, AValueD.AsExtended, 0.001);
CheckEquals(3.3, AValueE.AsExtended, 0.001);
CheckEquals(4.0, AValueC.AsExtended, 0.001);
CheckEquals(5.0, AValueCm.AsExtended, 0.001);
finally
c.Free;
end;
end;
procedure TTestCase1.TestPropGetValueDynArray;
var
ATestClass : TTestValueClass;
c: TRttiContext;
ARttiType: TRttiType;
AProperty: TRttiProperty;
AValue: TValue;
A: TTestDynArray;
begin
c := TRttiContext.Create;
A := [1, 2, 3, 4];
try
ATestClass := TTestValueClass.Create;
ATestClass.AArray := A;
try
ARttiType := c.GetType(ATestClass.ClassInfo);
Check(assigned(ARttiType));
AProperty := ARttiType.GetProperty('AArray');
AValue := AProperty.GetValue(ATestClass);
CheckEquals(A[0], AValue.GetArrayElement(0).AsInteger);
CheckEquals(A[1], AValue.GetArrayElement(1).AsInteger);
CheckEquals(A[2], AValue.GetArrayElement(2).AsInteger);
CheckEquals(A[3], AValue.GetArrayElement(3).AsInteger);
finally
AtestClass.Free;
end;
finally
c.Free;
end;
end;
procedure TTestCase1.TestPropGetValueEnumeration;
var
ATestClass : TTestValueClass;
c: TRttiContext;
ARttiType: TRttiType;
AProperty: TRttiProperty;
AValue: TValue;
begin
c := TRttiContext.Create;
try
ATestClass := TTestValueClass.Create;
ATestClass.AEnumeration := en3;
try
ARttiType := c.GetType(ATestClass.ClassInfo);
Check(assigned(ARttiType));
AProperty := ARttiType.GetProperty('AEnumeration');
AValue := AProperty.GetValue(ATestClass);
CheckEquals(Ord(en3),AValue.AsOrdinal);
ATestClass.AEnumeration := en1;
CheckEquals(Ord(en3), AValue.AsOrdinal);
CheckEquals('en3', AValue.ToString);
CheckEquals(True, AValue.IsOrdinal);
finally
AtestClass.Free;
end;
CheckEquals(Ord(en3),AValue.AsOrdinal);
finally
c.Free;
end;
end;
procedure TTestCase1.TestPropGetValueChars;
var
ATestClass : TTestValueClass;
c: TRttiContext;
ARttiType: TRttiType;
AProperty: TRttiProperty;
AValueC, AValueW: TValue;
begin
c := TRttiContext.Create;
try
ATestClass := TTestValueClass.Create;
ATestClass.AChar := 'C';
ATestClass.AWideChar := 'W';
try
ARttiType := c.GetType(ATestClass.ClassInfo);
Check(assigned(ARttiType));
AProperty := ARttiType.GetProperty('AChar');
AValueC := AProperty.GetValue(ATestClass);
CheckEquals('C',AValueC.AsAnsiChar);
ATestClass.AChar := 'N';
CheckEquals('C', AValueC.AsAnsiChar);
CheckEquals('C', AValueC.ToString);
CheckEquals(True, AValueC.IsOrdinal);
AProperty := ARttiType.GetProperty('AWideChar');
AValueW := AProperty.GetValue(ATestClass);
CheckEquals('W',AValueW.AsWideChar);
ATestClass.AWideChar := 'Z';
CheckEquals('W', AValueW.AsWideChar);
CheckEquals('W', AValueW.ToString);
CheckEquals(True, AValueW.IsOrdinal);
finally
AtestClass.Free;
end;
CheckEquals('C',AValueC.AsAnsiChar);
CheckEquals('W',AValueW.AsWideChar);
finally
c.Free;
end;
end;
procedure TTestCase1.TestPropSetValueString;
var
ATestClass : TTestValueClass;
c: TRttiContext;
ARttiType: TRttiType;
AProperty: TRttiProperty;
AValue: TValue;
s: string;
begin
c := TRttiContext.Create;
try
ATestClass := TTestValueClass.Create;
try
ARttiType := c.GetType(ATestClass.ClassInfo);
AProperty := ARttiType.GetProperty('astring');
s := 'ipse lorem or something like that';
TValue.Make(@s, TypeInfo(string), AValue);
AProperty.SetValue(ATestClass, AValue);
CheckEquals(ATestClass.AString, s);
s := 'Another string';
CheckEquals(ATestClass.AString, 'ipse lorem or something like that');
finally
AtestClass.Free;
end;
finally
c.Free;
end;
end;
procedure TTestCase1.TestPropSetValueInteger;
var
ATestClass : TTestValueClass;
c: TRttiContext;
ARttiType: TRttiType;
AProperty: TRttiProperty;
AValue: TValue;
i: integer;
begin
c := TRttiContext.Create;
try
ATestClass := TTestValueClass.Create;
try
ARttiType := c.GetType(ATestClass.ClassInfo);
AProperty := ARttiType.GetProperty('aInteger');
i := -43573;
TValue.Make(@i, TypeInfo(Integer), AValue);
AProperty.SetValue(ATestClass, AValue);
CheckEquals(ATestClass.AInteger, i);
i := 1;
CheckEquals(ATestClass.AInteger, -43573);
finally
AtestClass.Free;
end;
finally
c.Free;
end;
end;
procedure TTestCase1.TestPropSetValueBoolean;
var
ATestClass : TTestValueClass;
c: TRttiContext;
ARttiType: TRttiType;
AProperty: TRttiProperty;
AValue: TValue;
b: boolean;
begin
c := TRttiContext.Create;
try
ATestClass := TTestValueClass.Create;
try
ARttiType := c.GetType(ATestClass.ClassInfo);
AProperty := ARttiType.GetProperty('aboolean');
b := true;
TValue.Make(@b, TypeInfo(Boolean), AValue);
AProperty.SetValue(ATestClass, AValue);
CheckEquals(ATestClass.ABoolean, b);
b := false;
CheckEquals(ATestClass.ABoolean, true);
TValue.Make(@b, TypeInfo(Boolean), AValue);
AProperty.SetValue(ATestClass, AValue);
CheckEquals(ATestClass.ABoolean, false);
finally
AtestClass.Free;
end;
finally
c.Free;
end;
end;
procedure TTestCase1.TestPropSetValueShortString;
var
ATestClass : TTestValueClass;
c: TRttiContext;
ARttiType: TRttiType;
AProperty: TRttiProperty;
AValue: TValue;
s: string;
ss: ShortString;
begin
c := TRttiContext.Create;
try
ATestClass := TTestValueClass.Create;
try
ARttiType := c.GetType(ATestClass.ClassInfo);
AProperty := ARttiType.GetProperty('aShortString');
s := 'ipse lorem or something like that';
TValue.Make(@s, TypeInfo(String), AValue);
AProperty.SetValue(ATestClass, AValue);
CheckEquals(ATestClass.AShortString, s);
s := 'Another string';
CheckEquals(ATestClass.AShortString, 'ipse lorem or something like that');
ss := 'Hello World';
TValue.Make(@ss, TypeInfo(ShortString), AValue);
AProperty.SetValue(ATestClass, AValue);
CheckEquals(ATestClass.AShortString, ss);
ss := 'Foobar';
CheckEquals(ATestClass.AShortString, 'Hello World');
AProperty.SetValue(ATestClass, 'Another string');
CheckEquals(ATestClass.AShortString, 'Another string');
finally
AtestClass.Free;
end;
finally
c.Free;
end;
end;
procedure TTestCase1.TestPropSetValueObject;
var
ATestClass : TTestValueClass;
c: TRttiContext;
ARttiType: TRttiType;
AProperty: TRttiProperty;
AValue: TValue;
O: TObject;
TypeInfo: PTypeInfo;
begin
c := TRttiContext.Create;
try
ATestClass := TTestValueClass.Create;
try
ARttiType := c.GetType(ATestClass.ClassInfo);
AProperty := ARttiType.GetProperty('AObject');
TypeInfo := GetPropInfo(ATestClass, 'AObject')^.PropType;
O := TPersistent.Create;
TValue.Make(@O, TypeInfo, AValue);
AProperty.SetValue(ATestClass, AValue);
CheckEquals(ATestClass.AObject.GetHashCode, O.GetHashCode);
O.Free;
O := TPersistent.Create;
AProperty.SetValue(ATestClass, O);
CheckEquals(ATestClass.AObject.GetHashCode, O.GetHashCode);
O.Free;
finally
AtestClass.Free;
end;
finally
c.Free;
end;
end;
procedure TTestCase1.TestPropSetValueInterface;
var
ATestClass : TTestValueClass;
c: TRttiContext;
ARttiType: TRttiType;
AProperty: TRttiProperty;
AValue: TValue;
TypeInfo: PTypeInfo;
i: IInterface;
begin
c := TRttiContext.Create;
try
ATestClass := TTestValueClass.Create;
try
ARttiType := c.GetType(ATestClass.ClassInfo);
AProperty := ARttiType.GetProperty('AUnknown');
TypeInfo := GetPropInfo(ATestClass, 'AUnknown')^.PropType;
i := TInterfacedObject.Create;
TValue.Make(@i, TypeInfo, AValue);
AProperty.SetValue(ATestClass, AValue);
Check(ATestClass.AUnknown = i);
i := TInterfacedObject.Create;
AProperty.SetValue(ATestClass, i);
Check(ATestClass.AUnknown = i);
finally
AtestClass.Free;
end;
finally
c.Free;
end;
end;
procedure TTestCase1.TestPropSetValueFloat;
var
ATestClass : TTestValueClass;
c: TRttiContext;
ARttiType: TRttiType;
AProperty: TRttiProperty;
AValue: TValue;
TypeInfo: PTypeInfo;
S: Single;
D: Double;
E: Extended;
Cur: Currency;
Cmp: Comp;
begin
c := TRttiContext.Create;
try
ATestClass := TTestValueClass.Create;
try
ARttiType := c.GetType(ATestClass.ClassInfo);
AProperty := ARttiType.GetProperty('ASingle');
TypeInfo := GetPropInfo(ATestClass, 'ASingle')^.PropType;
S := 1.1;
TValue.Make(@S, TypeInfo, AValue);
AProperty.SetValue(ATestClass, AValue);
CheckEquals(S, ATestClass.ASingle, 0.001);
S := 1.2;
AProperty.SetValue(ATestClass, S);
CheckEquals(S, ATestClass.ASingle, 0.001);
AProperty := ARttiType.GetProperty('ADouble');
TypeInfo := GetPropInfo(ATestClass, 'ADouble')^.PropType;
D := 2.1;
TValue.Make(@D, TypeInfo, AValue);
AProperty.SetValue(ATestClass, AValue);
CheckEquals(D, ATestClass.ADouble, 0.001);
D := 2.2;
AProperty.SetValue(ATestClass, D);
CheckEquals(D, ATestClass.ADouble, 0.001);
AProperty := ARttiType.GetProperty('AExtended');
TypeInfo := GetPropInfo(ATestClass, 'AExtended')^.PropType;
E := 3.1;
TValue.Make(@E, TypeInfo, AValue);
AProperty.SetValue(ATestClass, AValue);
CheckEquals(E, ATestClass.AExtended, 0.001);
E := 3.2;
AProperty.SetValue(ATestClass, E);
CheckEquals(E, ATestClass.AExtended, 0.001);
AProperty := ARttiType.GetProperty('ACurrency');
TypeInfo := GetPropInfo(ATestClass, 'ACurrency')^.PropType;
Cur := 40;
TValue.Make(@Cur, TypeInfo, AValue);
AProperty.SetValue(ATestClass, AValue);
CheckEquals(Cur, ATestClass.ACurrency, 0.001);
Cur := 41;
AProperty.SetValue(ATestClass, Cur);
CheckEquals(Cur, ATestClass.ACurrency, 0.001);
AProperty := ARttiType.GetProperty('AComp');
TypeInfo := GetPropInfo(ATestClass, 'AComp')^.PropType;
Cmp := 50;
TValue.Make(@Cmp, TypeInfo, AValue);
AProperty.SetValue(ATestClass, AValue);
CheckEquals(Cmp, ATestClass.AComp, 0.001);
Cmp := 51;
AProperty.SetValue(ATestClass, Cmp);
CheckEquals(Cmp, ATestClass.AComp, 0.001);
finally
AtestClass.Free;
end;
finally
c.Free;
end;
end;
procedure TTestCase1.TestPropSetValueDynArray;
var
ATestClass : TTestValueClass;
c: TRttiContext;
ARttiType: TRttiType;
AProperty: TRttiProperty;
AValue: TValue;
A: TTestDynArray;
TypeInfo: PTypeInfo;
i: Integer;
begin
c := TRttiContext.Create;
try
ATestClass := TTestValueClass.Create;
try
ARttiType := c.GetType(ATestClass.ClassInfo);
AProperty := ARttiType.GetProperty('AArray');
TypeInfo := GetPropInfo(ATestClass, 'AArray')^.PropType;
A := [1, 2, 3, 4, 5];
TValue.Make(@A, TypeInfo, AValue);
AProperty.SetValue(ATestClass, AValue);
for i := 0 to High(A) do
CheckEquals(A[i], ATestClass.AArray[i]);
finally
AtestClass.Free;
end;
finally
c.Free;
end;
end;
procedure TTestCase1.TestPropSetValueEnumeration;
var
ATestClass : TTestValueClass;
c: TRttiContext;
ARttiType: TRttiType;
AProperty: TRttiProperty;
AValue: TValue;
E: TTestEnumeration;
begin
c := TRttiContext.Create;
try
ATestClass := TTestValueClass.Create;
try
ARttiType := c.GetType(ATestClass.ClassInfo);
AProperty := ARttiType.GetProperty('AEnumeration');
E := en2;
TValue.Make(@E, TypeInfo(TTestEnumeration), AValue);
AProperty.SetValue(ATestClass, AValue);
CheckEquals(Ord(E), Ord(ATestClass.AEnumeration));
finally
AtestClass.Free;
end;
finally
c.Free;
end;
end;
procedure TTestCase1.TestPropSetValueChars;
var
ATestClass : TTestValueClass;
c: TRttiContext;
ARttiType: TRttiType;
AProperty: TRttiProperty;
AValueC, AValueW: TValue;
begin
c := TRttiContext.Create;
try
ATestClass := TTestValueClass.Create;
ATestClass.AChar := 'C';
ATestClass.AWideChar := 'W';
try
ARttiType := c.GetType(ATestClass.ClassInfo);
Check(assigned(ARttiType));
AProperty := ARttiType.GetProperty('AChar');
AValueC := AProperty.GetValue(ATestClass);
CheckEquals('C', AValueC.AsAnsiChar);
AProperty := ARttiType.GetProperty('AWideChar');
AValueW := AProperty.GetValue(ATestClass);
CheckEquals('W', AValueW.AsWideChar);
finally
AtestClass.Free;
end;
CheckEquals('C', AValueC.AsAnsiChar);
CheckEquals('W', AValueW.AsWideChar);
finally
c.Free;
end;
end;
procedure TTestCase1.TestPropGetValueProcInteger;
var
ATestClass : TTestValueClass;
c: TRttiContext;
ARttiType: TRttiType;
AProperty: TRttiProperty;
AValue: TValue;
begin
c := TRttiContext.Create;
try
ATestClass := TTestValueClass.Create;
ATestClass.AInteger := 472349;
try
ARttiType := c.GetType(ATestClass.ClassInfo);
Check(assigned(ARttiType));
AProperty := ARttiType.GetProperty('agetinteger');
AValue := AProperty.GetValue(ATestClass);
CheckEquals(472349,AValue.AsInteger);
finally
AtestClass.Free;
end;
CheckEquals(472349,AValue.AsInteger);
finally
c.Free;
end;
end;
procedure TTestCase1.TestPropGetValueProcString;
var
ATestClass : TTestValueClass;
c: TRttiContext;
ARttiType: TRttiType;
AProperty: TRttiProperty;
AValue: TValue;
begin
c := TRttiContext.Create;
try
ATestClass := TTestValueClass.Create;
ATestClass.AString := 'Hello World';
try
ARttiType := c.GetType(ATestClass.ClassInfo);
Check(assigned(ARttiType));
AProperty := ARttiType.GetProperty('agetstring');
AValue := AProperty.GetValue(ATestClass);
CheckEquals('Hello World',AValue.AsString);
finally
AtestClass.Free;
end;
CheckEquals('Hello World',AValue.AsString);
finally
c.Free;
end;
end;
procedure TTestCase1.TestTRttiTypeProperties;
var
c: TRttiContext;
ARttiType: TRttiType;
begin
c := TRttiContext.Create;
try
ARttiType := c.GetType(TTestValueClass);
Check(assigned(ARttiType));
CheckEquals(ARttiType.Name,'TTestValueClass');
Check(ARttiType.TypeKind=tkClass);
// CheckEquals(ARttiType.IsPublicType,false);
CheckEquals(ARttiType.TypeSize,SizeOf(TObject));
CheckEquals(ARttiType.IsManaged,false);
CheckEquals(ARttiType.BaseType.classname,'TRttiInstanceType');
CheckEquals(ARttiType.IsInstance,True);
CheckEquals(ARttiType.AsInstance.DeclaringUnitName,'tests.rtti');
Check(ARttiType.BaseType.Name='TObject');
Check(ARttiType.AsInstance.BaseType.Name='TObject');
CheckEquals(ARttiType.IsOrdinal,False);
CheckEquals(ARttiType.IsRecord,False);
CheckEquals(ARttiType.IsSet,False);
finally
c.Free;
end;
end;
procedure TTestCase1.GetTypeInteger;
var
LContext: TRttiContext;
LType: TRttiType;
begin
LContext := TRttiContext.Create;
LType := LContext.GetType(TypeInfo(integer));
{$ifdef fpc}
CheckEquals(LType.Name, 'LongInt');
{$else}
CheckEquals(LType.Name, 'Integer');
{$endif}
LContext.Free;
end;
procedure TTestCase1.GetTypePointer;
var
context: TRttiContext;
t: TRttiType;
p: TRttiPointerType absolute t;
begin
context := TRttiContext.Create;
try
t := context.GetType(TypeInfo(Pointer));
Assert(t is TRttiPointerType, 'Type of Pointer is not a TRttiPointerType');
Assert(not Assigned(p.ReferredType), 'ReferredType of Pointer is not Nil');
t := context.GetType(TypeInfo(PLongInt));
Assert(t is TRttiPointerType, 'Type of Pointer is not a TRttiPointerType');
Assert(Assigned(p.ReferredType), 'ReferredType of PLongInt is Nil');
Assert(p.ReferredType = context.GetType(TypeInfo(LongInt)), 'ReferredType of PLongInt is not a LongInt');
t := context.GetType(TypeInfo(PWideChar));
Assert(t is TRttiPointerType, 'Type of Pointer is not a TRttiPointerType');
Assert(Assigned(p.ReferredType), 'ReferredType of PWideChar is Nil');
Assert(p.ReferredType = context.GetType(TypeInfo(WideChar)), 'ReferredType of PWideChar is not a WideChar');
finally
context.Free;
end;
end;
procedure TTestCase1.GetClassProperties;
var
LContext: TRttiContext;
LType: TRttiType;
PropList, PropList2: {$ifdef fpc}specialize{$endif} TArray<TRttiProperty>;
i: LongInt;
begin
LContext := TRttiContext.Create;
LType := LContext.GetType(TypeInfo(TGetClassProperties));
PropList := LType.GetProperties;
CheckEquals(4, length(PropList));
CheckEquals('PubPropRO', PropList[0].Name);
CheckEquals('PubPropRW', PropList[1].Name);
CheckEquals('PubPropSetRO', PropList[2].Name);
CheckEquals('PubPropSetRW', PropList[3].Name);
LType := LContext.GetType(TypeInfo(TGetClassPropertiesSub));
PropList2 := LType.GetProperties;
CheckEquals(Length(PropList), Length(PropList2));
for i := 0 to High(PropList) do
Check(PropList[i] = PropList2[i], 'Property instances are not equal');
LContext.Free;
end;
procedure TTestCase1.GetClassPropertiesValue;
var
AGetClassProperties: TGetClassProperties;
LContext: TRttiContext;
LType: TRttiType;
AValue: TValue;
begin
LContext := TRttiContext.Create;
LType := LContext.GetType(TGetClassProperties);
AGetClassProperties := TGetClassProperties.Create;
try
AGetClassProperties.PubPropRW:=12345;
AValue := LType.GetProperty('PubPropRW').GetValue(AGetClassProperties);
CheckEquals(12345, AValue.AsInteger);
finally
AGetClassProperties.Free;
end;
LContext.Free;
end;
procedure TTestCase1.TestReferenceRawData;
var
value: TValue;
str: String;
intf: IInterface;
i: LongInt;
test: TTestRecord;
arrdyn: TArrayOfLongintDyn;
arrstat: TArrayOfLongintStatic;
begin
str := 'Hello World';
UniqueString(str);
TValue.Make(@str, TypeInfo(String), value);
Check(PPointer(value.GetReferenceToRawData)^ = Pointer(str), 'Reference to string data differs');
intf := TInterfacedObject.Create;
TValue.Make(@intf, TypeInfo(IInterface), value);
Check(PPointer(value.GetReferenceToRawData)^ = Pointer(intf), 'Reference to interface data differs');
i := 42;
TValue.Make(@i, TypeInfo(LongInt), value);
Check(value.GetReferenceToRawData <> @i, 'Reference to longint is equal');
Check(PLongInt(value.GetReferenceToRawData)^ = PLongInt(@i)^, 'Reference to longint data differs');
test.value1 := 42;
test.value2 := 'Hello World';
TValue.Make(@test, TypeInfo(TTestRecord), value);
Check(value.GetReferenceToRawData <> @test, 'Reference to record is equal');
Check(PTestRecord(value.GetReferenceToRawData)^.value1 = PTestRecord(@test)^.value1, 'Reference to record data value1 differs');
Check(PTestRecord(value.GetReferenceToRawData)^.value2 = PTestRecord(@test)^.value2, 'Reference to record data value2 differs');
SetLength(arrdyn, 3);
arrdyn[0] := 42;
arrdyn[1] := 23;
arrdyn[2] := 49;
TValue.Make(@arrdyn, TypeInfo(TArrayOfLongintDyn), value);
Check(PPointer(value.GetReferenceToRawData)^ = Pointer(arrdyn), 'Reference to dynamic array data differs');
arrstat[0] := 42;
arrstat[1] := 23;
arrstat[2] := 49;
arrstat[3] := 59;
TValue.Make(@arrstat, TypeInfo(TArrayOfLongintStatic), value);
Check(value.GetReferenceToRawData <> @arrstat, 'Reference to static array is equal');
Check(PLongInt(value.GetReferenceToRawData)^ = PLongInt(@arrstat)^, 'Reference to static array data differs');
end;
procedure TTestCase1.TestReferenceRawDataEmpty;
var
value: TValue;
begin
TValue.Make(Nil, TypeInfo(String), value);
Check(Assigned(value.GetReferenceToRawData()), 'Reference to empty String is not assigned');
Check(not Assigned(PPointer(value.GetReferenceToRawData)^), 'Empty String data is assigned');
TValue.Make(Nil, TypeInfo(IInterface), value);
Check(Assigned(value.GetReferenceToRawData()), 'Reference to empty interface is not assigned');
Check(not Assigned(PPointer(value.GetReferenceToRawData)^), 'Empty interface data is assigned');
TValue.Make(Nil, TypeInfo(LongInt), value);
Check(Assigned(value.GetReferenceToRawData()), 'Reference to empty LongInt is not assigned');
Check(PLongInt(value.GetReferenceToRawData)^ = 0, 'Empty longint data is not 0');
TValue.Make(Nil, TypeInfo(TTestRecord), value);
Check(Assigned(value.GetReferenceToRawData()), 'Reference to empty record is not assigned');
Check(PTestRecord(value.GetReferenceToRawData)^.value1 = 0, 'Empty record data value1 is not 0');
Check(PTestRecord(value.GetReferenceToRawData)^.value2 = '', 'Empty record data value2 is not empty');
TValue.Make(Nil, TypeInfo(TArrayOfLongintDyn), value);
Check(Assigned(value.GetReferenceToRawData()), 'Reference to empty dynamic array is not assigned');
Check(not Assigned(PPointer(value.GetReferenceToRawData)^), 'Empty dynamic array data is assigned');
TValue.Make(Nil, TypeInfo(TArrayOfLongintStatic), value);
Check(Assigned(value.GetReferenceToRawData()), 'Reference to empty static array is not assigned');
Check(PLongInt(value.GetReferenceToRawData)^ = 0, 'Empty static array data is not 0');
end;
procedure TTestCase1.TestDataSize;
var
u8: UInt8;
u16: UInt16;
u32: UInt32;
u64: UInt64;
s8: Int8;
s16: Int16;
s32: Int32;
s64: Int64;
f32: Single;
f64: Double;
{$ifdef FPC_HAS_TYPE_EXTENDED}
f80: Extended;
{$endif}
fco: Comp;
fcu: Currency;
ss: ShortString;
sa: AnsiString;
su: UnicodeString;
sw: WideString;
o: TObject;
c: TClass;
i: IInterface;
ad: TArrayOfLongintDyn;
_as: TArrayOfLongintStatic;
b8: Boolean;
{$ifdef fpc}
b16: Boolean16;
b32: Boolean32;
b64: Boolean64;
{$endif}
bl8: ByteBool;
bl16: WordBool;
bl32: LongBool;
{$ifdef fpc}
bl64: QWordBool;
{$endif}
e: TTestEnum;
s: TTestSet;
t: TTestRecord;
p: Pointer;
proc: TTestProc;
method: TTestMethod;
value: TValue;
begin
u8:=245;
TValue.Make(@u8, TypeInfo(UInt8), value);
CheckEquals(1, value.DataSize, 'Size of UInt8 differs');
u16:=789;
TValue.Make(@u16, TypeInfo(UInt16), value);
CheckEquals(2, value.DataSize, 'Size of UInt16 differs');
u32:=568789;
TValue.Make(@u32, TypeInfo(UInt32), value);
CheckEquals(4, value.DataSize, 'Size of UInt32 differs');
u64:=$abdcefadbcef;
TValue.Make(@u64, TypeInfo(UInt64), value);
CheckEquals(8, value.DataSize, 'Size of UInt64 differs');
s8:=-32;
TValue.Make(@s8, TypeInfo(Int8), value);
CheckEquals(1, value.DataSize, 'Size of Int8 differs');
s16:=-5345;
TValue.Make(@s16, TypeInfo(Int16), value);
CheckEquals(2, value.DataSize, 'Size of Int16 differs');
s32:=-234567;
TValue.Make(@s32, TypeInfo(Int32), value);
CheckEquals(4, value.DataSize, 'Size of Int32 differs');
s64:=23456789012;
TValue.Make(@s64, TypeInfo(Int64), value);
CheckEquals(8, value.DataSize, 'Size of Int64 differs');
b8:=false;
TValue.Make(@b8, TypeInfo(Boolean), value);
CheckEquals(1, value.DataSize, 'Size of Boolean differs');
{$ifdef fpc}
b16:=true;
TValue.Make(@b16, TypeInfo(Boolean16), value);
CheckEquals(2, value.DataSize, 'Size of Boolean16 differs');
b32:=false;
TValue.Make(@b32, TypeInfo(Boolean32), value);
CheckEquals(4, value.DataSize, 'Size of Boolean32 differs');
b64:=true;
TValue.Make(@b64, TypeInfo(Boolean64), value);
CheckEquals(8, value.DataSize, 'Size of Boolean64 differs');
{$endif}
bl8:=true;
TValue.Make(@bl8, TypeInfo(ByteBool), value);
CheckEquals(1, value.DataSize, 'Size of ByteBool differs');
bl16:=false;
TValue.Make(@bl16, TypeInfo(WordBool), value);
CheckEquals(2, value.DataSize, 'Size of WordBool differs');
bl32:=false;
TValue.Make(@bl32, TypeInfo(LongBool), value);
CheckEquals(4, value.DataSize, 'Size of LongBool differs');
{$ifdef fpc}
bl64:=true;
TValue.Make(@bl64, TypeInfo(QWordBool), value);
CheckEquals(8, value.DataSize, 'Size of QWordBool differs');
{$endif}
f32:=4.567;
TValue.Make(@f32, TypeInfo(Single), value);
CheckEquals(4, value.DataSize, 'Size of Single differs');
f64:=-3456.678;
TValue.Make(@f64, TypeInfo(Double), value);
CheckEquals(8, value.DataSize, 'Size of Double differs');
{$ifdef FPC_HAS_TYPE_EXTENDED}
f80:=-2345.678;
TValue.Make(@f80, TypeInfo(Extended), value);
CheckEquals(10, value.DataSize, 'Size of Extended differs');
{$endif}
fcu:=56.78;
TValue.Make(@fcu, TypeInfo(Currency), value);
CheckEquals(SizeOf(Currency), value.DataSize, 'Size of Currency differs');
fco:=456;
TValue.Make(@fco, TypeInfo(Comp), value);
CheckEquals(SizeOf(Comp), value.DataSize, 'Size of Comp differs');
ss := '';
TValue.Make(@ss, TypeInfo(ShortString), value);
CheckEquals(254, value.DataSize, 'Size ofShortString differs');
sa:= '';
TValue.Make(@sa, TypeInfo(AnsiString), value);
CheckEquals(SizeOf(Pointer), value.DataSize, 'Size of AnsiString differs');
sw := '';
TValue.Make(@sw, TypeInfo(WideString), value);
CheckEquals(SizeOf(Pointer), value.DataSize, 'Size of WideString differs');
su:='';
TValue.Make(@su, TypeInfo(UnicodeString), value);
CheckEquals(SizeOf(Pointer), value.DataSize, 'Size of UnicodeString differs');
o := TTestValueClass.Create;
TValue.Make(@o, TypeInfo(TObject), value);
CheckEquals(SizeOf(Pointer), value.DataSize, 'Size of TObject differs');
o.Free;
c := TObject;
TValue.Make(@c, TypeInfo(TClass), value);
CheckEquals(SizeOf(Pointer), value.DataSize, 'Size of TClass differs');
i := Nil;
TValue.Make(@i, TypeInfo(IInterface), value);
CheckEquals(SizeOf(Pointer), value.DataSize, 'Size of IInterface differs');
TValue.Make(@t, TypeInfo(TTestRecord), value);
CheckEquals(SizeOf(TTestRecord), value.DataSize, 'Size of TTestRecord differs');
proc := Nil;
TValue.Make(@proc, TypeInfo(TTestProc), value);
CheckEquals(SizeOf(TTestProc), value.DataSize, 'Size of TTestProc differs');
method := Nil;
TValue.Make(@method, TypeInfo(TTestMethod), value);
CheckEquals(SizeOf(TTestMethod), value.DataSize, 'Size of TTestMethod differs');
TValue.Make(@_as, TypeInfo(TArrayOfLongintStatic), value);
CheckEquals(SizeOf(TArrayOfLongintStatic), value.DataSize, 'Size of TArrayOfLongintStatic differs');
TValue.Make(@ad, TypeInfo(TArrayOfLongintDyn), value);
CheckEquals(SizeOf(TArrayOfLongintDyn), value.DataSize, 'Size of TArrayOfLongintDyn differs');
e:=low(TTestEnum);
TValue.Make(@e, TypeInfo(TTestEnum), value);
CheckEquals(SizeOf(TTestEnum), value.DataSize, 'Size of TTestEnum differs');
s:=[low(TTestEnum),high(TTestEnum)];
TValue.Make(@s, TypeInfo(TTestSet), value);
CheckEquals(SizeOf(TTestSet), value.DataSize, 'Size of TTestSet differs');
p := Nil;
TValue.Make(@p, TypeInfo(Pointer), value);
CheckEquals(SizeOf(Pointer), value.DataSize, 'Size of Pointer differs');
end;
procedure TTestCase1.TestDataSizeEmpty;
var
value: TValue;
begin
TValue.Make(Nil, TypeInfo(UInt8), value);
CheckEquals(1, value.DataSize, 'Size of UInt8 differs');
TValue.Make(Nil, TypeInfo(UInt16), value);
CheckEquals(2, value.DataSize, 'Size of UInt16 differs');
TValue.Make(Nil, TypeInfo(UInt32), value);
CheckEquals(4, value.DataSize, 'Size of UInt32 differs');
TValue.Make(Nil, TypeInfo(UInt64), value);
CheckEquals(8, value.DataSize, 'Size of UInt64 differs');
TValue.Make(Nil, TypeInfo(Int8), value);
CheckEquals(1, value.DataSize, 'Size of Int8 differs');
TValue.Make(Nil, TypeInfo(Int16), value);
CheckEquals(2, value.DataSize, 'Size of Int16 differs');
TValue.Make(Nil, TypeInfo(Int32), value);
CheckEquals(4, value.DataSize, 'Size of Int32 differs');
TValue.Make(Nil, TypeInfo(Int64), value);
CheckEquals(8, value.DataSize, 'Size of Int64 differs');
TValue.Make(Nil, TypeInfo(Boolean), value);
CheckEquals(1, value.DataSize, 'Size of Boolean differs');
{$ifdef fpc}
TValue.Make(Nil, TypeInfo(Boolean16), value);
CheckEquals(2, value.DataSize, 'Size of Boolean16 differs');
TValue.Make(Nil, TypeInfo(Boolean32), value);
CheckEquals(4, value.DataSize, 'Size of Boolean32 differs');
TValue.Make(Nil, TypeInfo(Boolean64), value);
CheckEquals(8, value.DataSize, 'Size of Boolean64 differs');
{$endif}
TValue.Make(Nil, TypeInfo(ByteBool), value);
CheckEquals(1, value.DataSize, 'Size of ByteBool differs');
TValue.Make(Nil, TypeInfo(WordBool), value);
CheckEquals(2, value.DataSize, 'Size of WordBool differs');
TValue.Make(Nil, TypeInfo(LongBool), value);
CheckEquals(4, value.DataSize, 'Size of LongBool differs');
{$ifdef fpc}
TValue.Make(Nil, TypeInfo(QWordBool), value);
CheckEquals(8, value.DataSize, 'Size of QWordBool differs');
{$endif}
TValue.Make(Nil, TypeInfo(Single), value);
CheckEquals(4, value.DataSize, 'Size of Single differs');
TValue.Make(Nil, TypeInfo(Double), value);
CheckEquals(8, value.DataSize, 'Size of Double differs');
{$ifdef FPC_HAS_TYPE_EXTENDED}
TValue.Make(Nil, TypeInfo(Extended), value);
CheckEquals(10, value.DataSize, 'Size of Extended differs');
{$endif}
TValue.Make(Nil, TypeInfo(Currency), value);
CheckEquals(SizeOf(Currency), value.DataSize, 'Size of Currency differs');
TValue.Make(Nil, TypeInfo(Comp), value);
CheckEquals(SizeOf(Comp), value.DataSize, 'Size of Comp differs');
TValue.Make(Nil, TypeInfo(ShortString), value);
CheckEquals(254, value.DataSize, 'Size of ShortString differs');
TValue.Make(Nil, TypeInfo(AnsiString), value);
CheckEquals(SizeOf(Pointer), value.DataSize, 'Size of Pointer differs');
TValue.Make(Nil, TypeInfo(WideString), value);
CheckEquals(SizeOf(Pointer), value.DataSize, 'Size of WideString differs');
TValue.Make(Nil, TypeInfo(UnicodeString), value);
CheckEquals(SizeOf(Pointer), value.DataSize, 'Size of UnicodeString differs');
TValue.Make(Nil, TypeInfo(TObject), value);
CheckEquals(SizeOf(Pointer), value.DataSize, 'Size of TObject differs');
TValue.Make(Nil, TypeInfo(TClass), value);
CheckEquals(SizeOf(Pointer), value.DataSize, 'Size of TClass differs');
TValue.Make(Nil, TypeInfo(IInterface), value);
CheckEquals(SizeOf(Pointer), value.DataSize, 'Size of IInterface differs');
TValue.Make(Nil, TypeInfo(TTestRecord), value);
CheckEquals(SizeOf(TTestRecord), value.DataSize, 'Size of TTestRecord differs');
TValue.Make(Nil, TypeInfo(TTestProc), value);
CheckEquals(SizeOf(TTestProc), value.DataSize, 'Size of TTestProc differs');
TValue.Make(Nil, TypeInfo(TTestMethod), value);
CheckEquals(SizeOf(TTestMethod), value.DataSize, 'Size of TTestMethod differs');
TValue.Make(Nil, TypeInfo(TArrayOfLongintStatic), value);
CheckEquals(SizeOf(TArrayOfLongintStatic), value.DataSize, 'Size of TArrayOfLongintStatic differs');
TValue.Make(Nil, TypeInfo(TArrayOfLongintDyn), value);
CheckEquals(SizeOf(TArrayOfLongintDyn), value.DataSize, 'Size of TArrayOfLongintDyn differs');
TValue.Make(Nil, TypeInfo(TTestEnum), value);
CheckEquals(SizeOf(TTestEnum), value.DataSize, 'Size of TTestEnum differs');
TValue.Make(Nil, TypeInfo(TTestSet), value);
CheckEquals(SizeOf(TTestSet), value.DataSize, 'Size of TTestSet differs');
TValue.Make(Nil, TypeInfo(Pointer), value);
CheckEquals(SizeOf(Pointer), value.DataSize, 'Size of Pointer differs');
end;
procedure TTestCase1.TestIsManaged;
begin
CheckEquals(true, IsManaged(TypeInfo(ansistring)), 'IsManaged for tkAString');
CheckEquals(true, IsManaged(TypeInfo(widestring)), 'IsManaged for tkWString');
CheckEquals(true, IsManaged(TypeInfo(Variant)), 'IsManaged for tkVariant');
CheckEquals(true, IsManaged(TypeInfo(TArrayOfManagedRec)),
'IsManaged for tkArray (with managed ElType)');
CheckEquals(true, IsManaged(TypeInfo(TArrayOfString)),
'IsManaged for tkArray (with managed ElType)');
CheckEquals(true, IsManaged(TypeInfo(TManagedRec)), 'IsManaged for tkRecord');
{$ifdef fpc}
CheckEquals(true, IsManaged(TypeInfo(TManagedRecOp)), 'IsManaged for tkRecord');
{$endif}
CheckEquals(true, IsManaged(TypeInfo(IInterface)), 'IsManaged for tkInterface');
CheckEquals(true, IsManaged(TypeInfo(TManagedObj)), 'IsManaged for tkObject');
{$ifdef fpc}
CheckEquals(true, IsManaged(TypeInfo(specialize TArray<byte>)), 'IsManaged for tkDynArray');
{$else}
CheckEquals(true, IsManaged(TypeInfo(TArray<byte>)), 'IsManaged for tkDynArray');
{$endif}
CheckEquals(true, IsManaged(TypeInfo(unicodestring)), 'IsManaged for tkUString');
CheckEquals(false, IsManaged(TypeInfo(shortstring)), 'IsManaged for tkSString');
CheckEquals(false, IsManaged(TypeInfo(Byte)), 'IsManaged for tkInteger');
CheckEquals(false, IsManaged(TypeInfo(Char)), 'IsManaged for tkChar');
CheckEquals(false, IsManaged(TypeInfo(TTestEnum)), 'IsManaged for tkEnumeration');
CheckEquals(false, IsManaged(TypeInfo(Single)), 'IsManaged for tkFloat');
CheckEquals(false, IsManaged(TypeInfo(TTestSet)), 'IsManaged for tkSet');
{$ifdef fpc}
CheckEquals(false, IsManaged(TypeInfo(TTestMethod)), 'IsManaged for tkMethod');
{$else}
{ Delphi bug (or sabotage). For some reason Delphi considers method pointers to be managed (only in newer versions, probably since XE7) :/ }
CheckEquals({$if RTLVersion>=28}true{$else}false{$endif}, IsManaged(TypeInfo(TTestMethod)), 'IsManaged for tkMethod');
{$endif}
CheckEquals(false, IsManaged(TypeInfo(TArrayOfByte)),
'IsManaged for tkArray (with non managed ElType)');
CheckEquals(false, IsManaged(TypeInfo(TArrayOfNonManagedRec)),
'IsManaged for tkArray (with non managed ElType)');
CheckEquals(false, IsManaged(TypeInfo(TNonManagedRec)), 'IsManaged for tkRecord');
CheckEquals(false, IsManaged(TypeInfo(TObject)), 'IsManaged for tkClass');
CheckEquals(false, IsManaged(TypeInfo(TNonManagedObj)), 'IsManaged for tkObject');
CheckEquals(false, IsManaged(TypeInfo(WideChar)), 'IsManaged for tkWChar');
CheckEquals(false, IsManaged(TypeInfo(Boolean)), 'IsManaged for tkBool');
CheckEquals(false, IsManaged(TypeInfo(Int64)), 'IsManaged for tkInt64');
CheckEquals(false, IsManaged(TypeInfo(UInt64)), 'IsManaged for tkQWord');
{$ifdef fpc}
CheckEquals(false, IsManaged(TypeInfo(ICORBATest)), 'IsManaged for tkInterfaceRaw');
{$endif}
CheckEquals(false, IsManaged(TypeInfo(TTestProc)), 'IsManaged for tkProcVar');
CheckEquals(false, IsManaged(TypeInfo(TTestHelper)), 'IsManaged for tkHelper');
{$ifdef fpc}
CheckEquals(false, IsManaged(TypeInfo(file)), 'IsManaged for tkFile');
{$endif}
CheckEquals(false, IsManaged(TypeInfo(TClass)), 'IsManaged for tkClassRef');
CheckEquals(false, IsManaged(TypeInfo(Pointer)), 'IsManaged for tkPointer');
CheckEquals(false, IsManaged(nil), 'IsManaged for nil');
end;
{$ifdef fpc}
procedure TTestCase1.TestOpenArrayToDyn;
procedure OpenArrayProc(aArr: array of LongInt);
var
value: TValue;
begin
{$ifndef InLazIDE}
value := specialize OpenArrayToDynArrayValue<LongInt>(aArr);
{$endif}
CheckEquals(value.IsArray, True);
CheckEquals(value.IsOpenArray, False);
CheckEquals(value.IsObject, False);
CheckEquals(value.IsOrdinal, False);
CheckEquals(value.IsClass, False);
CheckEquals(value.GetArrayLength, 2);
CheckEquals(value.GetArrayElement(0).AsInteger, 42);
CheckEquals(value.GetArrayElement(1).AsInteger, 84);
value.SetArrayElement(0, 21);
{ since this is a copy the original array is not modified! }
CheckEquals(aArr[0], 42);
end;
begin
OpenArrayProc([42, 84]);
end;
{$endif}
procedure TTestCase1.TestInterface;
var
context: TRttiContext;
t: TRttiType;
ti1, ti2: TRttiInterfaceType;
methods: {$ifdef fpc}specialize{$endif} TArray<TRttiMethod>;
params: {$ifdef fpc}specialize{$endif} TArray<TRttiParameter>;
method: TRttiMethod;
param: TRttiParameter;
flag: TParamFlag;
begin
context := TRttiContext.Create;
try
t := context.GetType(TypeInfo(IInterface));
Check(t is TRttiInterfaceType, 'Type is not an interface type');
Check(not Assigned(t.BaseType), 'Base type is assigned');
ti1 := TRttiInterfaceType(t);
Check(not Assigned(ti1.BaseType), 'Base type is assigned');
methods := t.GetMethods;
CheckEquals(0, Length(methods), 'Overall method count does not match');
methods := t.GetDeclaredMethods;
CheckEquals(0, Length(methods), 'Declared method conut does not match');
t := context.GetType(TypeInfo(ITestInterface));
Check(t is TRttiInterfaceType, 'Type is not an interface type');
Check(Assigned(t.BaseType), 'Base type is not assigned');
Check(t.BaseType = TRttiType(ti1), 'Base type does not match');
ti2 := TRttiInterfaceType(t);
Check(Assigned(ti2.BaseType), 'Base type is not assigned');
Check(ti2.BaseType = ti1, 'Base type does not match');
methods := t.GetMethods;
CheckEquals(4, Length(methods), 'Overall method count does not match');
methods := t.GetDeclaredMethods;
CheckEquals(4, Length(methods), 'Declared method count does not match');
method := methods[0];
CheckEquals(method.Name, 'Test', 'Method name of Test does not match');
Check(method.CallingConvention = DefaultCC, 'Calling convention of Test does not match');
Check(method.MethodKind = mkProcedure, 'Method kind of Test does not match');
Check(method.DispatchKind = dkInterface, 'Dispatch kind of Test does not match');
Check(not Assigned(method.CodeAddress), 'Code address of Test is not Nil');
CheckEquals(method.VirtualIndex, 3, 'Virtual index of Test does not match');
Check(not Assigned(method.ReturnType), 'Return type of Test is not Nil');
params := method.GetParameters;
CheckEquals(0, Length(params), 'Parameter count of Test does not match');
method := methods[1];
CheckEquals(method.Name, 'Test2', 'Method name of Test2 does not match');
Check(method.CallingConvention = DefaultCC, 'Calling convention of Test2 does not match');
Check(method.MethodKind = mkFunction, 'Method kind of Test2 does not match');
Check(method.DispatchKind = dkInterface, 'Dispatch kind of Test2 does not match');
Check(not Assigned(method.CodeAddress), 'Code address of Test2 is not Nil');
CheckEquals(method.VirtualIndex, 4, 'Virtual index of Test2 does not match');
Check(Assigned(method.ReturnType), 'Return type of Test2 is Nil');
Check(method.ReturnType.TypeKind = tkInteger, 'Return type of Test2 is not an ordinal');
params := method.GetParameters;
CheckEquals(0, Length(params), 'Parameter count of Test2 does not match');
method := methods[2];
CheckEquals(method.Name, 'Test3', 'Method name of Test3 does not match');
Check(method.CallingConvention = DefaultCC, 'Calling convention of Test3 does not match');
Check(method.MethodKind = mkProcedure, 'Method kind of Test3 does not match');
Check(method.DispatchKind = dkInterface, 'Dispatch kind of Test3 does not match');
Check(not Assigned(method.CodeAddress), 'Code address of Test3 is not Nil');
CheckEquals(method.VirtualIndex, 5, 'Virtual index of Test3 does not match');
Check(not Assigned(method.ReturnType), 'Return type of Test3 is not Nil');
params := method.GetParameters;
CheckEquals(4, Length(params), 'Parameter count of Test3 does not match');
param := params[0];
CheckEquals(param.Name, 'aArg1', 'Parameter name of Test3.aArg1 does not match');
Check(param.Flags = [], 'Parameter flags of Test3.aArg1 do not match');
Check(Assigned(param.ParamType), 'Parameter type of Test3.aArg1 is Nil');
Check(param.ParamType.TypeKind = tkInteger, 'Parameter type of Test3.aArg1 is not an ordinal');
param := params[1];
CheckEquals(param.Name, 'aArg2', 'Parameter name of Test3.aArg2 does not match');
Check(param.Flags = [pfConst], 'Parameter flags of Test3.aArg2 do not match');
Check(Assigned(param.ParamType), 'Parameter type of Test3.aArg2 is Nil');
Check(param.ParamType.TypeKind = tkAnsiString, 'Parameter type of Test3.aArg2 is not a string');
param := params[2];
CheckEquals(param.Name, 'aArg3', 'Parameter name of Test3.aArg3 does not match');
Check(param.Flags = [pfVar], 'Parameter flags of Test3.aArg3 do not match');
Check(Assigned(param.ParamType), 'Parameter type of Test3.aArg3 is Nil');
Check(param.ParamType.TypeKind = {$ifdef fpc}tkBool{$else}tkEnumeration{$endif}, 'Parameter type of Test3.aArg3 is not a boolean');
param := params[3];
CheckEquals(param.Name, 'aArg4', 'Parameter name of Test3.aArg4 does not match');
Check(param.Flags = [pfOut], 'Parameter flags of Test3.aArg4 do not match');
Check(Assigned(param.ParamType), 'Parameter type of Test3.aArg4 is Nil');
Check(param.ParamType.TypeKind = tkInteger, 'Parameter type of Test3.aArg4 is not a string');
method := methods[3];
CheckEquals(method.Name, 'Test4', 'Method name of Test4 does not match');
Check(method.CallingConvention = DefaultCC, 'Calling convention of Test4 does not match');
Check(method.MethodKind = mkFunction, 'Method kind of Test4 does not match');
Check(method.DispatchKind = dkInterface, 'Dispatch kind of Test4 does not match');
Check(not Assigned(method.CodeAddress), 'Code address of Test4 is not Nil');
CheckEquals(method.VirtualIndex, 6, 'Virtual index of Test4 does not match');
Check(Assigned(method.ReturnType), 'Return type of Test4 is not Nil');
Check(method.ReturnType.TypeKind = tkAnsiString, 'Return type of Test4 is not a string');
params := method.GetParameters;
CheckEquals(2, Length(params), 'Parameter count of Test4 does not match');
param := params[0];
CheckEquals(param.Name, 'aArg1', 'Parameter name of Test4.aArg1 does not match');
Check(param.Flags = [pfArray, pfReference], 'Parameter flags of Test4.aArg1 do not match');
Check(Assigned(param.ParamType), 'Parameter type of Test4.aArg1 is Nil');
Check(param.ParamType.TypeKind = tkInteger, 'Parameter type of Test4.aArg1 is not an ordinal');
param := params[1];
CheckEquals(param.Name, 'aArg2', 'Parameter name of Test4.aArg2 does not match');
Check(param.Flags = [pfArray, pfReference], 'Parameter flags of Test4.aArg2 do not match');
Check(Assigned(param.ParamType), 'Parameter type of Test4.aArg2 is Nil');
Check(param.ParamType.TypeKind = tkRecord, 'Parameter type of Test4.aArg2 is not a record');
finally
context.Free;
end;
end;
procedure TTestCase1.TestRawThunk;
var
intf: IInterface;
begin
{ we test the raw thunking by instantiating a TVirtualInterface of IInterface }
{ this does not require a function call manager as the thunking is implemented
directly inside the RTTI unit }
try
intf := TVirtualInterface.Create(PTypeInfo(TypeInfo(IInterface))) as IInterface;
except
on e: ENotImplemented do
Ignore('RawThunk not implemented');
end;
{ if all went well QueryInterface and _AddRef were called and now we call
_Release as well }
intf := Nil;
end;
{$ifdef fpc}
procedure TTestCase1.TestInterfaceRaw;
var
context: TRttiContext;
t: TRttiType;
ti: TRttiInterfaceType;
begin
context := TRttiContext.Create;
try
t := context.GetType(TypeInfo(ICORBATest));
Check(t is TRttiInterfaceType, 'Type is not a raw interface type');
Check(not Assigned(t.BaseType), 'Base type is assigned');
ti := TRttiInterfaceType(t);
Check(not Assigned(ti.BaseType), 'Base type is assigned');
finally
context.Free;
end;
end;
{$endif}
procedure TTestCase1.TestProcVar;
var
context: TRttiContext;
t: TRttiType;
p: TRttiProcedureType;
params: {$ifdef fpc}specialize{$endif} TArray<TRttiParameter>;
begin
context := TRttiContext.Create;
try
t := context.GetType(PTypeInfo(TypeInfo(TTestProc)));
Check(Assigned(t), 'Rtti Type is Nil');
Check(t is TRttiInvokableType, 'Rtti Type is not an invokeable');
Check(t is TRttiProcedureType, 'Rtti Type is not a procedure type');
p := t as TRttiProcedureType;
Check(p.CallingConvention = DefaultCC, 'Calling convention does not match');
Check(not Assigned(p.ReturnType), 'Return type is assigned');
CheckEquals(0, Length(p.GetParameters), 'Procedure variable has parameters');
t := context.GetType(PTypeInfo(TypeInfo(TTestFunc1)));
Check(Assigned(t), 'Rtti Type is Nil');
Check(t is TRttiInvokableType, 'Rtti Type is not an invokeable');
Check(t is TRttiProcedureType, 'Rtti Type is not a procedure type');
p := t as TRttiProcedureType;
Check(p.CallingConvention = DefaultCC, 'Calling convention does not match');
Check(Assigned(p.ReturnType), 'Return type is not assigned');
//Check(p.ReturnType is TRttiOrdinalType, 'Return type is not an ordinal type');
CheckEquals(0, Length(p.GetParameters), 'Procedure variable has parameters');
t := context.GetType(PTypeInfo(TypeInfo(TTestFunc2)));
Check(Assigned(t), 'Rtti Type is Nil');
Check(t is TRttiInvokableType, 'Rtti Type is not an invokeable');
Check(t is TRttiProcedureType, 'Rtti Type is not a procedure type');
p := t as TRttiProcedureType;
Check(p.CallingConvention = DefaultCC, 'Calling convention does not match');
Check(Assigned(p.ReturnType), 'Return type is not assigned');
Check(p.ReturnType is TRttiStringType, 'Return type is not a string type');
params := p.GetParameters;
CheckEquals(2, Length(params), 'Procedure variable has incorrect amount of parameters');
Check(params[0].ParamType.TypeKind in [tkInteger, tkInt64], 'Parameter 1 is not an ordinal type');
//Check(params[0].ParamType is TRttiOrdinalType, 'Parameter 1 is not an ordinal type');
Check(pfArray in params[1].Flags, 'Parameter 2 is not an array');
Check(params[1].ParamType.TypeKind in [tkInteger, tkInt64], 'Parameter 2 is not an ordinal array');
finally
context.Free;
end;
end;
procedure TTestCase1.TestMethod;
var
context: TRttiContext;
t: TRttiType;
m: TRttiMethodType;
params: {$ifdef fpc}specialize{$endif} TArray<TRttiParameter>;
begin
context := TRttiContext.Create;
try
t := context.GetType(PTypeInfo(TypeInfo(TTestMethod)));
Check(Assigned(t), 'Rtti Type is Nil');
Check(t is TRttiInvokableType, 'Rtti Type is not an invokeable');
Check(t is TRttiMethodType, 'Rtti Type is not a method type');
m := t as TRttiMethodType;
Check(m.CallingConvention = DefaultCC, 'Calling convention does not match');
Check(not Assigned(m.ReturnType), 'Return type is assigned');
CheckEquals(0, Length(m.GetParameters), 'Method variable has parameters');
t := context.GetType(PTypeInfo(TypeInfo(TTestMethod1)));
Check(Assigned(t), 'Rtti Type is Nil');
Check(t is TRttiInvokableType, 'Rtti Type is not an invokeable');
Check(t is TRttiMethodType, 'Rtti Type is not a method type');
m := t as TRttiMethodType;
Check(m.CallingConvention = DefaultCC, 'Calling convention does not match');
Check(Assigned(m.ReturnType), 'Return type is not assigned');
//Check(p.ReturnType is TRttiOrdinalType, 'Return type is not an ordinal type');
CheckEquals(0, Length(m.GetParameters), 'Method variable has parameters');
t := context.GetType(PTypeInfo(TypeInfo(TTestMethod2)));
Check(Assigned(t), 'Rtti Type is Nil');
Check(t is TRttiInvokableType, 'Rtti Type is not an invokeable');
Check(t is TRttiMethodType, 'Rtti Type is not a method type');
m := t as TRttiMethodType;
Check(m.CallingConvention = DefaultCC, 'Calling convention does not match');
Check(Assigned(m.ReturnType), 'Return type is not assigned');
Check(m.ReturnType is TRttiStringType, 'Return type is not a string type');
params := m.GetParameters;
CheckEquals(2, Length(params), 'Method variable has incorrect amount of parameters');
Check(params[0].ParamType.TypeKind in [tkInteger, tkInt64], 'Parameter 1 is not an ordinal type');
//Check(params[0].ParamType is TRttiOrdinalType, 'Parameter 1 is not an ordinal type');
Check(pfArray in params[1].Flags, 'Parameter 2 is not an array');
Check(params[1].ParamType.TypeKind in [tkInteger, tkInt64], 'Parameter 2 is not an ordinal array');
finally
context.Free;
end;
end;
initialization
{$ifdef fpc}
RegisterTest(TTestCase1);
{$else fpc}
RegisterTest(TTestCase1.Suite);
{$endif fpc}
end.
|