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
// vim: tw=80
use super::*;
use quote::ToTokens;
/// Convert a trait object reference into a reference to a Boxed trait
///
/// # Returns
///
/// Returns `true` if it was necessary to box the type.
fn dedynify(ty: &mut Type) -> bool {
if let Type::Reference(ref mut tr) = ty {
if let Type::TraitObject(ref tto) = tr.elem.as_ref() {
if let Some(lt) = &tr.lifetime {
if lt.ident == "static" {
// For methods that return 'static references, the user can
// usually actually supply one, unlike nonstatic references.
// dedynify is unneeded and harmful in such cases.
//
// But we do need to add parens to prevent parsing errors
// when methods like returning add a `+ Send` to the output
// type.
*tr.elem = parse2(quote!((#tto))).unwrap();
return false;
}
}
*tr.elem = parse2(quote!(Box<#tto>)).unwrap();
return true;
}
}
false
}
/// Convert a special reference type like "&str" into a reference to its owned
/// type like "&String".
fn destrify(ty: &mut Type) {
if let Type::Reference(ref mut tr) = ty {
if let Some(lt) = &tr.lifetime {
if lt.ident == "static" {
// For methods that return 'static references, the user can
// usually actually supply one, unlike nonstatic references.
// destrify is unneeded and harmful in such cases.
return;
}
}
let path_ty: TypePath = parse2(quote!(Path)).unwrap();
let pathbuf_ty: Type = parse2(quote!(::std::path::PathBuf)).unwrap();
let str_ty: TypePath = parse2(quote!(str)).unwrap();
let string_ty: Type = parse2(quote!(::std::string::String)).unwrap();
let cstr_ty: TypePath = parse2(quote!(CStr)).unwrap();
let cstring_ty: Type = parse2(quote!(::std::ffi::CString)).unwrap();
let osstr_ty: TypePath = parse2(quote!(OsStr)).unwrap();
let osstring_ty: Type = parse2(quote!(::std::ffi::OsString)).unwrap();
match tr.elem.as_ref() {
Type::Path(ref path) if *path == cstr_ty =>
*tr.elem = cstring_ty,
Type::Path(ref path) if *path == osstr_ty =>
*tr.elem = osstring_ty,
Type::Path(ref path) if *path == path_ty =>
*tr.elem = pathbuf_ty,
Type::Path(ref path) if *path == str_ty =>
*tr.elem = string_ty,
Type::Slice(ts) => {
let inner = (*ts.elem).clone();
let mut segments = Punctuated::new();
segments.push(format_ident!("std").into());
segments.push(format_ident!("vec").into());
let mut v: PathSegment = format_ident!("Vec").into();
let mut abga_args = Punctuated::new();
abga_args.push(GenericArgument::Type(inner));
v.arguments = PathArguments::AngleBracketed(
AngleBracketedGenericArguments {
colon2_token: None,
lt_token: Token![<](Span::call_site()),
args: abga_args,
gt_token: Token![>](Span::call_site()),
}
);
segments.push(v);
*tr.elem = Type::Path(TypePath {
qself: None,
path: Path {
leading_colon: Some(Token![::](Span::call_site())),
segments
}
});
},
_ => (), // Nothing to do
};
}
}
/// Return the owned version of the input.
fn ownify(ty: &Type) -> Type {
if let Type::Reference(ref tr) = &ty {
if tr.lifetime.as_ref().map_or(false, |lt| lt.ident == "static")
{
// Just a static expectation
ty.clone()
} else {
*tr.elem.clone()
}
} else {
ty.clone()
}
}
/// Add Send + Sync to a where clause
fn send_syncify(wc: &mut Option<WhereClause>, bounded_ty: Type) {
let mut bounds = Punctuated::new();
bounds.push(TypeParamBound::Trait(TraitBound {
paren_token: None,
modifier: TraitBoundModifier::None,
lifetimes: None,
path: Path::from(format_ident!("Send"))
}));
bounds.push(TypeParamBound::Trait(TraitBound {
paren_token: None,
modifier: TraitBoundModifier::None,
lifetimes: None,
path: Path::from(format_ident!("Sync"))
}));
if wc.is_none() {
*wc = Some(WhereClause {
where_token: <Token![where]>::default(),
predicates: Punctuated::new()
});
}
wc.as_mut().unwrap()
.predicates.push(
WherePredicate::Type(
PredicateType {
lifetimes: None,
bounded_ty,
colon_token: Default::default(),
bounds
}
)
);
}
/// Build a MockFunction.
#[derive(Clone, Copy, Debug)]
pub(crate) struct Builder<'a> {
attrs: &'a [Attribute],
call_levels: Option<usize>,
levels: usize,
parent: Option<&'a Ident>,
sig: &'a Signature,
struct_: Option<&'a Ident>,
struct_generics: Option<&'a Generics>,
trait_: Option<&'a Ident>,
vis: &'a Visibility
}
impl<'a> Builder<'a> {
pub fn attrs(&mut self, attrs: &'a[Attribute]) -> &mut Self {
self.attrs = attrs;
self
}
pub fn build(self) -> MockFunction {
let mut argnames = Vec::new();
let mut argty = Vec::new();
let mut is_static = true;
let mut predexprs = Vec::new();
let mut predty = Vec::new();
let mut refpredty = Vec::new();
let (mut declosured_generics, declosured_inputs, call_exprs) =
declosurefy(&self.sig.generics, &self.sig.inputs);
for fa in declosured_inputs.iter() {
if let FnArg::Typed(pt) = fa {
let argname = (*pt.pat).clone();
if pat_is_self(&argname) {
// A weird receiver like `Box<Self>`
is_static = false;
continue;
}
let aty = supersuperfy(&pt.ty, self.levels);
if let Type::Reference(ref tr) = aty {
predexprs.push(quote!(#argname));
predty.push((*tr.elem).clone());
let tr2 = Type::Reference(TypeReference {
and_token: tr.and_token,
lifetime: None,
mutability: None,
elem: tr.elem.clone()
});
refpredty.push(tr2);
} else {
predexprs.push(quote!(&#argname));
predty.push(aty.clone());
let tr = TypeReference {
and_token: Token![&](Span::call_site()),
lifetime: None,
mutability: None,
elem: Box::new(aty.clone())
};
refpredty.push(Type::Reference(tr));
};
argnames.push(argname);
argty.push(aty.clone());
} else {
is_static = false;
}
}
let (output, boxed) = match self.sig.output {
ReturnType::Default => (
Type::Tuple(TypeTuple {
paren_token: token::Paren::default(),
elems: Punctuated::new(),
}),
false,
),
ReturnType::Type(_, ref ty) => {
let mut output_ty = supersuperfy(ty, self.levels);
destrify(&mut output_ty);
let boxed = dedynify(&mut output_ty);
(output_ty, boxed)
}
};
supersuperfy_generics(&mut declosured_generics, self.levels);
let owned_output = ownify(&output);
let mut return_ref = false;
let mut return_refmut = false;
if let Type::Reference(ref tr) = &output {
if tr.lifetime.as_ref().map_or(true, |lt| lt.ident != "static")
{
if tr.mutability.is_none() {
return_ref = true;
} else {
return_refmut = true;
}
}
};
if is_static && (return_ref || return_refmut) {
compile_error(self.sig.span(),
"Mockall cannot mock static methods that return non-'static references. It's unclear what the return value's lifetime should be.");
}
let struct_generics = self.struct_generics.cloned()
.unwrap_or_default();
let (type_generics, salifetimes, srlifetimes) = split_lifetimes(
struct_generics.clone(),
&declosured_inputs,
&ReturnType::Type(<Token![->]>::default(),
Box::new(owned_output.clone()))
);
let srltg = lifetimes_to_generics(&srlifetimes);
let (call_generics, malifetimes, mrlifetimes) = split_lifetimes(
declosured_generics,
&declosured_inputs,
&ReturnType::Type(<Token![->]>::default(),
Box::new(owned_output.clone()))
);
let mrltg = lifetimes_to_generics(&mrlifetimes);
let cgenerics = merge_generics(&type_generics, &call_generics);
let egenerics = merge_generics(
&merge_generics(&cgenerics, &srltg),
&mrltg);
let alifetimes = salifetimes.into_iter()
.collect::<HashSet<LifetimeDef>>()
.union(&malifetimes.into_iter().collect::<HashSet<_>>())
.into_iter()
.cloned()
.collect();
let fn_params = egenerics.type_params()
.map(|tp| tp.ident.clone())
.collect();
let call_levels = self.call_levels.unwrap_or(self.levels);
MockFunction {
alifetimes,
argnames,
argty,
attrs: self.attrs.to_vec(),
call_exprs,
call_generics,
call_vis: expectation_visibility(self.vis, call_levels),
egenerics,
cgenerics,
fn_params,
is_static,
mod_ident: self.parent.unwrap_or(&Ident::new("FIXME", Span::call_site())).clone(),
output,
owned_output,
boxed,
predexprs,
predty,
refpredty,
return_ref,
return_refmut,
sig: self.sig.clone(),
struct_: self.struct_.cloned(),
struct_generics,
trait_: self.trait_.cloned(),
type_generics,
privmod_vis: expectation_visibility(self.vis, self.levels)
}
}
/// How many levels of modules beneath the original function this one is
/// nested.
pub fn call_levels(&mut self, levels: usize) -> &mut Self {
self.call_levels = Some(levels);
self
}
/// How many levels of modules beneath the original function this one's
/// private module is nested.
pub fn levels(&mut self, levels: usize) -> &mut Self {
self.levels = levels;
self
}
/// # Arguments
///
/// * sig: The signature of the mockable function
/// * v: The visibility of the mockable function
pub fn new(sig: &'a Signature, vis: &'a Visibility) -> Self {
Builder {
attrs: &[],
levels: 0,
call_levels: None,
parent: None,
sig,
struct_: None,
struct_generics: None,
trait_: None,
vis
}
}
/// Supply the name of the parent module
pub fn parent(&mut self, ident: &'a Ident) -> &mut Self {
self.parent = Some(ident);
self
}
/// Supply the name of the parent struct, if any
pub fn struct_(&mut self, ident: &'a Ident) -> &mut Self {
self.struct_= Some(ident);
self
}
/// Supply the Generics of the parent struct, if any
pub fn struct_generics(&mut self, generics: &'a Generics) -> &mut Self {
self.struct_generics = Some(generics);
self
}
/// Supply the name of the method's trait, if any
pub fn trait_(&mut self, ident: &'a Ident) -> &mut Self {
self.trait_ = Some(ident);
self
}
}
#[derive(Clone)]
pub(crate) struct MockFunction {
/// Lifetimes of the mocked method that relate to the arguments but not the
/// return value
alifetimes: Punctuated<LifetimeDef, token::Comma>,
/// Names of the method arguments
argnames: Vec<Pat>,
/// Types of the method arguments
argty: Vec<Type>,
/// any attributes on the original function, like #[inline]
pub attrs: Vec<Attribute>,
/// Expressions that should be used for Expectation::call's arguments
call_exprs: Vec<TokenStream>,
/// Generics used for the expectation call
call_generics: Generics,
/// Visibility of the mock function itself
call_vis: Visibility,
/// Generics of the Expectation object
egenerics: Generics,
/// Generics of the Common object
cgenerics: Generics,
/// The mock function's generic types as a list of types
fn_params: Vec<Ident>,
/// Is this for a static method or free function?
is_static: bool,
/// name of the function's parent module
mod_ident: Ident,
/// Output type of the Method, supersuperfied.
output: Type,
/// Owned version of the output type of the Method, supersuperfied.
///
/// If the real output type is a non-'static reference, then it will differ
/// from this field.
owned_output: Type,
/// True if the `owned_type` is boxed by `Box<>`.
boxed: bool,
/// Expressions that create the predicate arguments from the call arguments
predexprs: Vec<TokenStream>,
/// Types used for Predicates. Will be almost the same as args, but every
/// type will be a non-reference type.
predty: Vec<Type>,
/// Does the function return a non-'static reference?
return_ref: bool,
/// Does the function return a mutable reference?
return_refmut: bool,
/// References to every type in `predty`.
refpredty: Vec<Type>,
/// The signature of the mockable function
sig: Signature,
/// Name of the parent structure, if any
struct_: Option<Ident>,
/// Generics of the parent structure
struct_generics: Generics,
/// Name of this method's trait, if the method comes from a trait
trait_: Option<Ident>,
/// Type generics of the mock structure
type_generics: Generics,
/// Visibility of the expectation and its methods
privmod_vis: Visibility
}
impl MockFunction {
/// Return the mock function itself
///
/// # Arguments
///
/// * `modname`: Name of the parent struct's private module
// Supplying modname is an unfortunately hack. Ideally MockFunction
// wouldn't need to know that.
pub fn call(&self, modname: Option<&Ident>) -> impl ToTokens {
let attrs = AttrFormatter::new(&self.attrs).format();
let call_exprs = &self.call_exprs;
let (_, tg, _) = if self.is_method_generic() || self.is_static() {
&self.egenerics
} else {
&self.call_generics
}.split_for_impl();
let tbf = tg.as_turbofish();
let name = self.name();
let desc = self.desc();
let no_match_msg = quote!(std::format!(
"{}: No matching expectation found", #desc));
let sig = &self.sig;
let (vis, dead_code) = if self.trait_.is_some() {
(&Visibility::Inherited, quote!())
} else {
let dead_code = if let Visibility::Inherited = self.call_vis {
// This private method may be a helper only used by the struct's
// other methods, which we are mocking. If so, the mock method
// will be dead code. But we can't simply eliminate it, because
// it might also be used by other code in the same module.
quote!(#[allow(dead_code)])
} else {
quote!()
};
(&self.call_vis, dead_code)
};
let substruct_obj = if let Some(trait_) = &self.trait_ {
let ident = format_ident!("{}_expectations", trait_);
quote!(#ident.)
} else {
quote!()
};
let call = if self.return_refmut {
Ident::new("call_mut", Span::call_site())
} else {
Ident::new("call", Span::call_site())
};
let mut deref = quote!();
if self.boxed {
if self.return_ref {
deref = quote!(&**);
} else if self.return_refmut {
deref = quote!(&mut **);
}
}
if self.is_static {
let outer_mod_path = self.outer_mod_path(modname);
quote!(
// Don't add a doc string. The original is included in #attrs
#(#attrs)*
#dead_code
#vis #sig {
let no_match_msg = #no_match_msg;
#deref {
let __mockall_guard = #outer_mod_path::EXPECTATIONS
.lock().unwrap();
/*
* TODO: catch panics, then gracefully release the mutex
* so it won't be poisoned. This requires bounding any
* generic parameters with UnwindSafe
*/
/* std::panic::catch_unwind(|| */
__mockall_guard.#call#tbf(#(#call_exprs,)*)
/*)*/
}.expect(&no_match_msg)
}
)
} else {
quote!(
// Don't add a doc string. The original is included in #attrs
#(#attrs)*
#dead_code
#vis #sig {
let no_match_msg = #no_match_msg;
#deref self.#substruct_obj #name.#call#tbf(#(#call_exprs,)*)
.expect(&no_match_msg)
}
)
}
}
/// Return this method's contribution to its parent's checkpoint method
pub fn checkpoint(&self) -> impl ToTokens {
let attrs = AttrFormatter::new(&self.attrs)
.doc(false)
.format();
let inner_mod_ident = self.inner_mod_ident();
if self.is_static {
quote!(
#(#attrs)*
{
let __mockall_timeses = #inner_mod_ident::EXPECTATIONS.lock()
.unwrap()
.checkpoint()
.collect::<Vec<_>>();
}
)
} else {
let name = &self.name();
quote!(#(#attrs)* { self.#name.checkpoint(); })
}
}
/// Return a function that creates a Context object for this function
///
/// # Arguments
///
/// * `modname`: Name of the parent struct's private module
// Supplying modname is an unfortunately hack. Ideally MockFunction
// wouldn't need to know that.
pub fn context_fn(&self, modname: Option<&Ident>) -> impl ToTokens {
let attrs = AttrFormatter::new(&self.attrs)
.doc(false)
.format();
let context_docstr = format!("Create a [`Context`]({}{}/struct.Context.html) for mocking the `{}` method",
modname.map(|m| format!("{}/", m)).unwrap_or_default(),
self.inner_mod_ident(),
self.name());
let context_ident = format_ident!("{}_context", self.name());
let (_, tg, _) = self.type_generics.split_for_impl();
let outer_mod_path = self.outer_mod_path(modname);
let v = &self.call_vis;
quote!(
#(#attrs)*
#[doc = #context_docstr]
#v fn #context_ident() -> #outer_mod_path::Context #tg
{
#outer_mod_path::Context::default()
}
)
}
/// Generate a code fragment that will print a description of the invocation
fn desc(&self) -> impl ToTokens {
let argnames = &self.argnames;
let name = if let Some(s) = &self.struct_ {
format!("{}::{}", s, self.sig.ident)
} else {
format!("{}::{}", self.mod_ident, self.sig.ident)
};
let fields = vec!["{:?}"; argnames.len()].join(", ");
let fstr = format!("{}({})", name, fields);
quote!(std::format!(#fstr, #(::mockall::MaybeDebugger(&#argnames)),*))
}
/// Generate code for the expect_ method
///
/// # Arguments
///
/// * `modname`: Name of the parent struct's private module
/// * `self_args`: If supplied, these are the
/// AngleBracketedGenericArguments of the self type of the
/// trait impl. e.g. The `T` in `impl Foo for Bar<T>`.
// Supplying modname is an unfortunately hack. Ideally MockFunction
// wouldn't need to know that.
pub fn expect(&self, modname: &Ident, self_args: Option<&PathArguments>)
-> impl ToTokens
{
let attrs = AttrFormatter::new(&self.attrs)
.doc(false)
.format();
let name = self.name();
let expect_ident = format_ident!("expect_{}", &name);
let expectation_obj = self.expectation_obj(self_args);
let funcname = &self.sig.ident;
let (_, tg, _) = if self.is_method_generic() {
&self.egenerics
} else {
&self.call_generics
}.split_for_impl();
let (ig, _, wc) = self.call_generics.split_for_impl();
let mut wc = wc.cloned();
if self.is_method_generic() && (self.return_ref || self.return_refmut) {
// Add Senc + Sync, required for downcast, since Expectation
// stores an Option<#owned_output>
send_syncify(&mut wc, self.owned_output.clone());
}
let tbf = tg.as_turbofish();
let vis = &self.call_vis;
#[cfg(not(feature = "nightly_derive"))]
let must_use = quote!(#[must_use =
"Must set return value when not using the \"nightly\" feature"
]);
#[cfg(feature = "nightly_derive")]
let must_use = quote!();
let substruct_obj = if let Some(trait_) = &self.trait_ {
let ident = format_ident!("{}_expectations", trait_);
quote!(#ident.)
} else {
quote!()
};
let docstr = format!("Create an [`Expectation`]({}/{}/struct.Expectation.html) for mocking the `{}` method",
modname, self.inner_mod_ident(), funcname);
quote!(
#must_use
#[doc = #docstr]
#(#attrs)*
#vis fn #expect_ident #ig(&mut self)
-> &mut #modname::#expectation_obj
#wc
{
self.#substruct_obj #name.expect#tbf()
}
)
}
/// Return the name of this function's expecation object
fn expectation_obj(&self, self_args: Option<&PathArguments>)
-> impl ToTokens
{
let inner_mod_ident = self.inner_mod_ident();
if let Some(PathArguments::AngleBracketed(abga)) = self_args {
// staticize any lifetimes that might be present in the Expectation
// object but not in the self args. These come from the method's
// return type.
let mut abga2 = abga.clone();
for _ in self.egenerics.lifetimes() {
let lt = Lifetime::new("'static", Span::call_site());
let la = GenericArgument::Lifetime(lt);
abga2.args.insert(0, la);
}
assert!(!self.is_method_generic(),
"specific impls with generic methods are TODO");
quote!(#inner_mod_ident::Expectation #abga2)
} else {
// staticize any lifetimes. This is necessary for methods that
// return non-static types, because the Expectation itself must be
// 'static.
let segenerics = staticize(&self.egenerics);
let (_, tg, _) = segenerics.split_for_impl();
quote!(#inner_mod_ident::Expectation #tg)
}
}
/// Return the name of this function's expecations object
pub fn expectations_obj(&self) -> impl ToTokens {
let inner_mod_ident = self.inner_mod_ident();
if self.is_method_generic() {
quote!(#inner_mod_ident::GenericExpectations)
} else {
quote!(#inner_mod_ident::Expectations)
}
}
pub fn field_definition(&self, modname: Option<&Ident>) -> TokenStream {
let name = self.name();
let attrs = AttrFormatter::new(&self.attrs)
.doc(false)
.format();
let expectations_obj = &self.expectations_obj();
if self.is_method_generic() {
quote!(#(#attrs)* #name: #modname::#expectations_obj)
} else {
// staticize any lifetimes. This is necessary for methods that
// return non-static types, because the Expectation itself must be
// 'static.
let segenerics = staticize(&self.egenerics);
let (_, tg, _) = segenerics.split_for_impl();
quote!(#(#attrs)* #name: #modname::#expectations_obj #tg)
}
}
/// Human-readable name of the mock function
fn funcname(&self) -> String {
if let Some(si) = &self.struct_ {
format!("{}::{}", si, self.name())
} else {
format!("{}", self.name())
}
}
fn hrtb(&self) -> Option<BoundLifetimes> {
if self.alifetimes.is_empty() {
None
} else {
Some(BoundLifetimes {
lifetimes: self.alifetimes.clone(),
lt_token: <Token![<]>::default(),
gt_token: <Token![>]>::default(),
.. Default::default()
})
}
}
fn is_expectation_generic(&self) -> bool {
self.egenerics.params.iter().any(|p| {
matches!(p, GenericParam::Type(_))
}) || self.egenerics.where_clause.is_some()
}
/// Is the mock method generic (as opposed to a non-generic method of a
/// generic mock struct)?
pub fn is_method_generic(&self) -> bool {
self.call_generics.params.iter().any(|p| {
matches!(p, GenericParam::Type(_))
}) || self.call_generics.where_clause.is_some()
}
fn outer_mod_path(&self, modname: Option<&Ident>) -> Path {
let mut path = if let Some(m) = modname {
Path::from(PathSegment::from(m.clone()))
} else {
Path { leading_colon: None, segments: Punctuated::new() }
};
path.segments.push(PathSegment::from(self.inner_mod_ident()));
path
}
fn inner_mod_ident(&self) -> Ident {
format_ident!("__{}", &self.name())
}
pub fn is_static(&self) -> bool {
self.is_static
}
pub fn name(&self) -> &Ident {
&self.sig.ident
}
/// Generate code for this function's private module
pub fn priv_module(&self) -> impl ToTokens {
let attrs = AttrFormatter::new(&self.attrs)
.doc(false)
.format();
let common = &Common{f: self};
let context = &Context{f: self};
let expectation: Box<dyn ToTokens> = if self.return_ref {
Box::new(RefExpectation{f: self})
} else if self.return_refmut {
Box::new(RefMutExpectation{f: self})
} else {
Box::new(StaticExpectation{f: self})
};
let expectations: Box<dyn ToTokens> = if self.return_ref {
Box::new(RefExpectations{f: self})
} else if self.return_refmut {
Box::new(RefMutExpectations{f: self})
} else {
Box::new(StaticExpectations{f: self})
};
let generic_expectations = GenericExpectations{f: self};
let guard: Box<dyn ToTokens> = if self.is_expectation_generic() {
Box::new(GenericExpectationGuard{f: self})
} else {
Box::new(ConcreteExpectationGuard{f: self})
};
let matcher = &Matcher{f: self};
let std_mutexguard = if self.is_static {
quote!(use ::std::sync::MutexGuard;)
} else {
quote!()
};
let inner_mod_ident = self.inner_mod_ident();
let rfunc: Box<dyn ToTokens> = if self.return_ref {
Box::new(RefRfunc{f: self})
} else if self.return_refmut {
Box::new(RefMutRfunc{f: self})
} else {
Box::new(StaticRfunc{f: self})
};
quote!(
#(#attrs)*
#[allow(missing_docs)]
pub mod #inner_mod_ident {
use super::*;
use ::mockall::CaseTreeExt;
#std_mutexguard
use ::std::{
boxed::Box,
mem,
ops::{DerefMut, Range},
sync::Mutex,
vec::Vec,
};
#rfunc
#matcher
#common
#expectation
#expectations
#generic_expectations
#guard
#context
}
)
}
}
/// Holds parts of the expectation that are common for all output types
struct Common<'a> {
f: &'a MockFunction
}
impl<'a> ToTokens for Common<'a> {
fn to_tokens(&self, tokens: &mut TokenStream) {
let argnames = &self.f.argnames;
let predty = &self.f.predty;
let hrtb = self.f.hrtb();
let funcname = self.f.funcname();
let (ig, tg, wc) = self.f.cgenerics.split_for_impl();
let lg = lifetimes_to_generics(&self.f.alifetimes);
let refpredty = &self.f.refpredty;
let with_generics_idents = (0..self.f.predty.len())
.map(|i| format_ident!("MockallMatcher{}", i))
.collect::<Vec<_>>();
let with_generics = with_generics_idents.iter()
.zip(self.f.predty.iter())
.map(|(id, mt)|
quote!(#id: #hrtb ::mockall::Predicate<#mt> + Send + 'static, )
).collect::<TokenStream>();
let with_args = self.f.argnames.iter()
.zip(with_generics_idents.iter())
.map(|(argname, id)| quote!(#argname: #id, ))
.collect::<TokenStream>();
let boxed_withargs = argnames.iter()
.map(|aa| quote!(Box::new(#aa), ))
.collect::<TokenStream>();
quote!(
/// Holds the stuff that is independent of the output type
struct Common #ig #wc {
matcher: Mutex<Matcher #tg>,
seq_handle: Option<::mockall::SeqHandle>,
times: ::mockall::Times
}
impl #ig std::default::Default for Common #tg #wc
{
fn default() -> Self {
Common {
matcher: Mutex::new(Matcher::default()),
seq_handle: None,
times: ::mockall::Times::default()
}
}
}
impl #ig Common #tg #wc {
fn call(&self, desc: &str) {
self.times.call()
.unwrap_or_else(|m| {
let desc = std::format!(
"{}", self.matcher.lock().unwrap());
panic!("{}: Expectation({}) {}", #funcname, desc,
m);
});
self.verify_sequence(desc);
if self.times.is_satisfied() {
self.satisfy_sequence()
}
}
fn in_sequence(&mut self, __mockall_seq: &mut ::mockall::Sequence)
-> &mut Self
{
assert!(self.times.is_exact(),
"Only Expectations with an exact call count have sequences");
self.seq_handle = Some(__mockall_seq.next_handle());
self
}
fn is_done(&self) -> bool {
self.times.is_done()
}
#[allow(clippy::ptr_arg)]
fn matches #lg (&self, #( #argnames: &#predty, )*) -> bool {
self.matcher.lock().unwrap().matches(#(#argnames, )*)
}
/// Forbid this expectation from ever being called.
fn never(&mut self) {
self.times.never();
}
fn satisfy_sequence(&self) {
if let Some(__mockall_handle) = &self.seq_handle {
__mockall_handle.satisfy()
}
}
/// Expect this expectation to be called any number of times
/// contained with the given range.
fn times<MockallR>(&mut self, __mockall_r: MockallR)
where MockallR: Into<::mockall::TimesRange>
{
self.times.times(__mockall_r)
}
fn with<#with_generics>(&mut self, #with_args)
{
let mut __mockall_guard = self.matcher.lock().unwrap();
*__mockall_guard.deref_mut() =
Matcher::Pred(Box::new((#boxed_withargs)));
}
fn withf<MockallF>(&mut self, __mockall_f: MockallF)
where MockallF: #hrtb Fn(#( #refpredty, )*)
-> bool + Send + 'static
{
let mut __mockall_guard = self.matcher.lock().unwrap();
*__mockall_guard.deref_mut() =
Matcher::Func(Box::new(__mockall_f));
}
fn withf_st<MockallF>(&mut self, __mockall_f: MockallF)
where MockallF: #hrtb Fn(#( #refpredty, )*)
-> bool + 'static
{
let mut __mockall_guard = self.matcher.lock().unwrap();
*__mockall_guard.deref_mut() =
Matcher::FuncSt(
::mockall::Fragile::new(Box::new(__mockall_f))
);
}
fn verify_sequence(&self, desc: &str) {
if let Some(__mockall_handle) = &self.seq_handle {
__mockall_handle.verify(desc)
}
}
}
impl #ig Drop for Common #tg #wc {
fn drop(&mut self) {
if !::std::thread::panicking() && !self.times.is_satisfied()
{
let desc = std::format!(
"{}", self.matcher.lock().unwrap());
panic!("{}: Expectation({}) called {} time(s) which is fewer than expected {}",
#funcname,
desc,
self.times.count(),
self.times.minimum());
}
}
}
).to_tokens(tokens);
}
}
/// Generates methods that are common for all Expectation types
struct CommonExpectationMethods<'a> {
f: &'a MockFunction
}
impl<'a> ToTokens for CommonExpectationMethods<'a> {
fn to_tokens(&self, tokens: &mut TokenStream) {
let argnames = &self.f.argnames;
let hrtb = self.f.hrtb();
let lg = lifetimes_to_generics(&self.f.alifetimes);
let predty = &self.f.predty;
let with_generics_idents = (0..self.f.predty.len())
.map(|i| format_ident!("MockallMatcher{}", i))
.collect::<Vec<_>>();
let with_generics = with_generics_idents.iter()
.zip(self.f.predty.iter())
.map(|(id, mt)|
quote!(#id: #hrtb ::mockall::Predicate<#mt> + Send + 'static, )
).collect::<TokenStream>();
let with_args = self.f.argnames.iter()
.zip(with_generics_idents.iter())
.map(|(argname, id)| quote!(#argname: #id, ))
.collect::<TokenStream>();
let v = &self.f.privmod_vis;
quote!(
/// Add this expectation to a
/// [`Sequence`](../../../mockall/struct.Sequence.html).
#v fn in_sequence(&mut self, __mockall_seq: &mut ::mockall::Sequence)
-> &mut Self
{
self.common.in_sequence(__mockall_seq);
self
}
fn is_done(&self) -> bool {
self.common.is_done()
}
/// Validate this expectation's matcher.
#[allow(clippy::ptr_arg)]
fn matches #lg (&self, #(#argnames: &#predty, )*) -> bool {
self.common.matches(#(#argnames, )*)
}
/// Forbid this expectation from ever being called.
#v fn never(&mut self) -> &mut Self {
self.common.never();
self
}
/// Create a new, default, [`Expectation`](struct.Expectation.html)
#v fn new() -> Self {
Self::default()
}
/// Expect this expectation to be called exactly once. Shortcut for
/// [`times(1)`](#method.times).
#v fn once(&mut self) -> &mut Self {
self.times(1)
}
/// Restrict the number of times that that this method may be called.
///
/// The argument may be:
/// * A fixed number: `.times(4)`
/// * Various types of range:
/// - `.times(5..10)`
/// - `.times(..10)`
/// - `.times(5..)`
/// - `.times(5..=10)`
/// - `.times(..=10)`
/// * The wildcard: `.times(..)`
#v fn times<MockallR>(&mut self, __mockall_r: MockallR) -> &mut Self
where MockallR: Into<::mockall::TimesRange>
{
self.common.times(__mockall_r);
self
}
/// Set matching crieteria for this Expectation.
///
/// The matching predicate can be anything implemening the
/// [`Predicate`](../../../mockall/trait.Predicate.html) trait. Only
/// one matcher can be set per `Expectation` at a time.
#v fn with<#with_generics>(&mut self, #with_args) -> &mut Self
{
self.common.with(#(#argnames, )*);
self
}
/// Set a matching function for this Expectation.
///
/// This is equivalent to calling [`with`](#method.with) with a
/// function argument, like `with(predicate::function(f))`.
#v fn withf<MockallF>(&mut self, __mockall_f: MockallF) -> &mut Self
where MockallF: #hrtb Fn(#(&#predty, )*)
-> bool + Send + 'static
{
self.common.withf(__mockall_f);
self
}
/// Single-threaded version of [`withf`](#method.withf).
/// Can be used when the argument type isn't `Send`.
#v fn withf_st<MockallF>(&mut self, __mockall_f: MockallF) -> &mut Self
where MockallF: #hrtb Fn(#(&#predty, )*)
-> bool + 'static
{
self.common.withf_st(__mockall_f);
self
}
).to_tokens(tokens);
}
}
/// Holds the moethods of the Expectations object that are common for all
/// Expectation types
struct CommonExpectationsMethods<'a> {
f: &'a MockFunction
}
impl<'a> ToTokens for CommonExpectationsMethods<'a> {
fn to_tokens(&self, tokens: &mut TokenStream) {
let (ig, tg, wc) = self.f.egenerics.split_for_impl();
let v = &self.f.privmod_vis;
quote!(
/// A collection of [`Expectation`](struct.Expectations.html)
/// objects. Users will rarely if ever use this struct directly.
#[doc(hidden)]
#v struct Expectations #ig ( Vec<Expectation #tg>) #wc;
impl #ig Expectations #tg #wc {
/// Verify that all current expectations are satisfied and clear
/// them.
#v fn checkpoint(&mut self) -> std::vec::Drain<Expectation #tg>
{
self.0.drain(..)
}
/// Create a new expectation for this method.
#v fn expect(&mut self) -> &mut Expectation #tg
{
self.0.push(Expectation::default());
let __mockall_l = self.0.len();
&mut self.0[__mockall_l - 1]
}
#v fn new() -> Self {
Self::default()
}
}
impl #ig Default for Expectations #tg #wc
{
fn default() -> Self {
Expectations(Vec::new())
}
}
).to_tokens(tokens);
}
}
/// The ExpectationGuard structure for static methods with no generic types
struct ExpectationGuardCommonMethods<'a> {
f: &'a MockFunction
}
impl<'a> ToTokens for ExpectationGuardCommonMethods<'a> {
fn to_tokens(&self, tokens: &mut TokenStream) {
if !self.f.is_static {
return;
}
let argnames = &self.f.argnames;
let argty = &self.f.argty;
let (_, tg, _) = self.f.egenerics.split_for_impl();
let keyid = gen_keyid(&self.f.egenerics);
let expectations = if self.f.is_expectation_generic() {
quote!(self.guard
.store
.get_mut(&::mockall::Key::new::#keyid())
.unwrap()
.downcast_mut::<Expectations #tg>()
.unwrap())
} else {
quote!(self.guard)
};
let hrtb = self.f.hrtb();
let output = &self.f.output;
let predty = &self.f.predty;
let with_generics_idents = (0..self.f.predty.len())
.map(|i| format_ident!("MockallMatcher{}", i))
.collect::<Vec<_>>();
let with_generics = with_generics_idents.iter()
.zip(self.f.predty.iter())
.map(|(id, mt)|
quote!(#id: #hrtb ::mockall::Predicate<#mt> + Send + 'static, )
).collect::<TokenStream>();
let with_args = self.f.argnames.iter()
.zip(with_generics_idents.iter())
.map(|(argname, id)| quote!(#argname: #id, ))
.collect::<TokenStream>();
let v = &self.f.privmod_vis;
quote!(
/// Just like
/// [`Expectation::in_sequence`](struct.Expectation.html#method.in_sequence)
#v fn in_sequence(&mut self,
__mockall_seq: &mut ::mockall::Sequence)
-> &mut Expectation #tg
{
#expectations.0[self.i].in_sequence(__mockall_seq)
}
/// Just like
/// [`Expectation::never`](struct.Expectation.html#method.never)
#v fn never(&mut self) -> &mut Expectation #tg {
#expectations.0[self.i].never()
}
/// Just like
/// [`Expectation::once`](struct.Expectation.html#method.once)
#v fn once(&mut self) -> &mut Expectation #tg {
#expectations.0[self.i].once()
}
/// Just like
/// [`Expectation::return_const`](struct.Expectation.html#method.return_const)
#v fn return_const<MockallOutput>
(&mut self, __mockall_c: MockallOutput)
-> &mut Expectation #tg
where MockallOutput: Clone + Into<#output> + Send + 'static
{
#expectations.0[self.i].return_const(__mockall_c)
}
/// Just like
/// [`Expectation::return_const_st`](struct.Expectation.html#method.return_const_st)
#v fn return_const_st<MockallOutput>
(&mut self, __mockall_c: MockallOutput)
-> &mut Expectation #tg
where MockallOutput: Clone + Into<#output> + 'static
{
#expectations.0[self.i].return_const_st(__mockall_c)
}
/// Just like
/// [`Expectation::returning`](struct.Expectation.html#method.returning)
#v fn returning<MockallF>(&mut self, __mockall_f: MockallF)
-> &mut Expectation #tg
where MockallF: #hrtb FnMut(#(#argty, )*)
-> #output + Send + 'static
{
#expectations.0[self.i].returning(__mockall_f)
}
/// Just like
/// [`Expectation::return_once`](struct.Expectation.html#method.return_once)
#v fn return_once<MockallF>(&mut self, __mockall_f: MockallF)
-> &mut Expectation #tg
where MockallF: #hrtb FnOnce(#(#argty, )*)
-> #output + Send + 'static
{
#expectations.0[self.i].return_once(__mockall_f)
}
/// Just like
/// [`Expectation::return_once_st`](struct.Expectation.html#method.return_once_st)
#v fn return_once_st<MockallF>(&mut self, __mockall_f: MockallF)
-> &mut Expectation #tg
where MockallF: #hrtb FnOnce(#(#argty, )*)
-> #output + 'static
{
#expectations.0[self.i].return_once_st(__mockall_f)
}
/// Just like
/// [`Expectation::returning_st`](struct.Expectation.html#method.returning_st)
#v fn returning_st<MockallF>(&mut self, __mockall_f: MockallF)
-> &mut Expectation #tg
where MockallF: #hrtb FnMut(#(#argty, )*)
-> #output + 'static
{
#expectations.0[self.i].returning_st(__mockall_f)
}
/// Just like
/// [`Expectation::times`](struct.Expectation.html#method.times)
#v fn times<MockallR>(&mut self, __mockall_r: MockallR)
-> &mut Expectation #tg
where MockallR: Into<::mockall::TimesRange>
{
#expectations.0[self.i].times(__mockall_r)
}
/// Just like
/// [`Expectation::with`](struct.Expectation.html#method.with)
#v fn with<#with_generics> (&mut self, #with_args)
-> &mut Expectation #tg
{
#expectations.0[self.i].with(#(#argnames, )*)
}
/// Just like
/// [`Expectation::withf`](struct.Expectation.html#method.withf)
#v fn withf<MockallF>(&mut self, __mockall_f: MockallF)
-> &mut Expectation #tg
where MockallF: #hrtb Fn(#(&#predty, )*)
-> bool + Send + 'static
{
#expectations.0[self.i].withf(__mockall_f)
}
/// Just like
/// [`Expectation::withf_st`](struct.Expectation.html#method.withf_st)
#v fn withf_st<MockallF>(&mut self, __mockall_f: MockallF)
-> &mut Expectation #tg
where MockallF: #hrtb Fn(#(&#predty, )*)
-> bool + 'static
{
#expectations.0[self.i].withf_st(__mockall_f)
}
).to_tokens(tokens);
}
}
/// The ExpectationGuard structure for static methods with no generic types
struct ConcreteExpectationGuard<'a> {
f: &'a MockFunction
}
impl<'a> ToTokens for ConcreteExpectationGuard<'a> {
fn to_tokens(&self, tokens: &mut TokenStream) {
if !self.f.is_static {
return;
}
let common_methods = ExpectationGuardCommonMethods{f: self.f};
let (_, tg, _) = self.f.egenerics.split_for_impl();
let ltdef = LifetimeDef::new(
Lifetime::new("'__mockall_lt", Span::call_site())
);
let mut e_generics = self.f.egenerics.clone();
e_generics.lt_token.get_or_insert(<Token![<]>::default());
e_generics.params.push(GenericParam::Lifetime(ltdef));
e_generics.gt_token.get_or_insert(<Token![>]>::default());
let (e_ig, e_tg, e_wc) = e_generics.split_for_impl();
let (ei_ig, _, _) = e_generics.split_for_impl();
let v = &self.f.privmod_vis;
quote!(
::mockall::lazy_static! {
#[doc(hidden)]
#v static ref EXPECTATIONS:
::std::sync::Mutex<Expectations #tg> =
::std::sync::Mutex::new(Expectations::new());
}
/// Like an [`&Expectation`](struct.Expectation.html) but
/// protected by a Mutex guard. Useful for mocking static
/// methods. Forwards accesses to an `Expectation` object.
// We must return the MutexGuard to the caller so he can
// configure the expectation. But we can't bundle both the
// guard and the &Expectation into the same structure; the
// borrow checker won't let us. Instead we'll record the
// expectation's position within the Expectations vector so we
// can proxy its methods.
//
// ExpectationGuard is only defined for expectations that return
// 'static return types.
#v struct ExpectationGuard #e_ig #e_wc {
guard: MutexGuard<'__mockall_lt, Expectations #tg>,
i: usize
}
#[allow(clippy::unused_unit)]
impl #ei_ig ExpectationGuard #e_tg #e_wc
{
// Should only be called from the mockall_derive generated
// code
#[doc(hidden)]
#v fn new(mut __mockall_guard: MutexGuard<'__mockall_lt, Expectations #tg>)
-> Self
{
__mockall_guard.expect(); // Drop the &Expectation
let __mockall_i = __mockall_guard.0.len() - 1;
ExpectationGuard{guard: __mockall_guard, i: __mockall_i}
}
#common_methods
}
).to_tokens(tokens);
}
}
/// The ExpectationGuard structure for static methods with generic types
struct GenericExpectationGuard<'a> {
f: &'a MockFunction
}
impl<'a> ToTokens for GenericExpectationGuard<'a> {
fn to_tokens(&self, tokens: &mut TokenStream) {
if !self.f.is_static {
return;
}
let common_methods = ExpectationGuardCommonMethods{f: self.f};
let (_, tg, _) = self.f.egenerics.split_for_impl();
let keyid = gen_keyid(&self.f.egenerics);
let ltdef = LifetimeDef::new(
Lifetime::new("'__mockall_lt", Span::call_site())
);
let mut egenerics = self.f.egenerics.clone();
egenerics.lt_token.get_or_insert(<Token![<]>::default());
egenerics.params.push(GenericParam::Lifetime(ltdef));
egenerics.gt_token.get_or_insert(<Token![>]>::default());
let (e_ig, e_tg, e_wc) = egenerics.split_for_impl();
let fn_params = &self.f.fn_params;
let tbf = tg.as_turbofish();
let v = &self.f.privmod_vis;
quote!(
::mockall::lazy_static! {
#v static ref EXPECTATIONS:
::std::sync::Mutex<GenericExpectations> =
::std::sync::Mutex::new(GenericExpectations::new());
}
/// Like an [`&Expectation`](struct.Expectation.html) but
/// protected by a Mutex guard. Useful for mocking static
/// methods. Forwards accesses to an `Expectation` object.
#v struct ExpectationGuard #e_ig #e_wc{
guard: MutexGuard<'__mockall_lt, GenericExpectations>,
i: usize,
_phantom: ::std::marker::PhantomData<(#(#fn_params,)*)>,
}
#[allow(clippy::unused_unit)]
impl #e_ig ExpectationGuard #e_tg #e_wc
{
// Should only be called from the mockall_derive generated
// code
#[doc(hidden)]
#v fn new(mut __mockall_guard: MutexGuard<'__mockall_lt, GenericExpectations>)
-> Self
{
let __mockall_ee: &mut Expectations #tg =
__mockall_guard.store.entry(
::mockall::Key::new::#keyid()
).or_insert_with(||
Box::new(Expectations #tbf ::new()))
.downcast_mut()
.unwrap();
__mockall_ee.expect(); // Drop the &Expectation
let __mockall_i = __mockall_ee.0.len() - 1;
ExpectationGuard{guard: __mockall_guard, i: __mockall_i,
_phantom: ::std::marker::PhantomData}
}
#common_methods
}
).to_tokens(tokens);
}
}
/// Generates Context, which manages the context for expectations of static
/// methods.
struct Context<'a> {
f: &'a MockFunction
}
impl<'a> ToTokens for Context<'a> {
fn to_tokens(&self, tokens: &mut TokenStream) {
if !self.f.is_static {
return;
}
let ltdef = LifetimeDef::new(
Lifetime::new("'__mockall_lt", Span::call_site())
);
let mut egenerics = self.f.egenerics.clone();
egenerics.lt_token.get_or_insert(<Token![<]>::default());
egenerics.params.push(GenericParam::Lifetime(ltdef));
egenerics.gt_token.get_or_insert(<Token![>]>::default());
let (_, e_tg, _) = egenerics.split_for_impl();
let (ty_ig, ty_tg, ty_wc) = self.f.type_generics.split_for_impl();
let mut meth_generics = self.f.call_generics.clone();
let ltdef = LifetimeDef::new(
Lifetime::new("'__mockall_lt", Span::call_site())
);
meth_generics.params.push(GenericParam::Lifetime(ltdef));
let (meth_ig, _meth_tg, meth_wc) = meth_generics.split_for_impl();
let ctx_fn_params = self.f.struct_generics.type_params()
.map(|tp| tp.ident.clone())
.collect::<Punctuated::<Ident, Token![,]>>();
let v = &self.f.privmod_vis;
#[cfg(not(feature = "nightly_derive"))]
let must_use = quote!(#[must_use =
"Must set return value when not using the \"nightly\" feature"
]);
#[cfg(feature = "nightly_derive")]
let must_use = quote!();
quote!(
/// Manages the context for expectations of static methods.
///
/// Expectations on this method will be validated and cleared when
/// the `Context` object drops. The `Context` object does *not*
/// provide any form of synchronization, so multiple tests that set
/// expectations on the same static method must provide their own.
#[must_use = "Context only serves to create expectations" ]
#v struct Context #ty_ig #ty_wc {
// Prevent "unused type parameter" errors
// Surprisingly, PhantomData<Fn(generics)> is Send even if
// generics are not, unlike PhantomData<generics>
_phantom: ::std::marker::PhantomData<
Box<dyn Fn(#ctx_fn_params) + Send>
>
}
impl #ty_ig Context #ty_tg #ty_wc {
/// Verify that all current expectations for this method are
/// satisfied and clear them.
#v fn checkpoint(&self) {
Self::do_checkpoint()
}
#[doc(hidden)]
#v fn do_checkpoint() {
let __mockall_timeses = EXPECTATIONS
.lock()
.unwrap()
.checkpoint()
.collect::<Vec<_>>();
}
/// Create a new expectation for this method.
#must_use
#v fn expect #meth_ig ( &self,) -> ExpectationGuard #e_tg
#meth_wc
{
ExpectationGuard::new(EXPECTATIONS.lock().unwrap())
}
}
impl #ty_ig Default for Context #ty_tg #ty_wc {
fn default() -> Self {
Context {_phantom: std::marker::PhantomData}
}
}
impl #ty_ig Drop for Context #ty_tg #ty_wc {
fn drop(&mut self) {
if !std::thread::panicking() {
Self::do_checkpoint()
}
}
}
).to_tokens(tokens);
}
}
struct Matcher<'a> {
f: &'a MockFunction
}
impl<'a> ToTokens for Matcher<'a> {
fn to_tokens(&self, tokens: &mut TokenStream) {
let (ig, tg, wc) = self.f.cgenerics.split_for_impl();
let argnames = &self.f.argnames;
let braces = argnames.iter()
.fold(String::new(), |mut acc, _argname| {
if acc.is_empty() {
acc.push_str("{}");
} else {
acc.push_str(", {}");
}
acc
});
let fn_params = &self.f.fn_params;
let hrtb = self.f.hrtb();
let indices = (0..argnames.len())
.map(|i| {
syn::Index::from(i)
}).collect::<Vec<_>>();
let lg = lifetimes_to_generics(&self.f.alifetimes);
let pred_matches = argnames.iter().enumerate()
.map(|(i, argname)| {
let idx = syn::Index::from(i);
quote!(__mockall_pred.#idx.eval(#argname),)
}).collect::<TokenStream>();
let preds = self.f.predty.iter()
.map(|t| quote!(Box<dyn #hrtb ::mockall::Predicate<#t> + Send>,))
.collect::<TokenStream>();
let predty = &self.f.predty;
let refpredty = &self.f.refpredty;
quote!(
enum Matcher #ig #wc {
Always,
Func(Box<dyn #hrtb Fn(#( #refpredty, )*) -> bool + Send>),
// Version of Matcher::Func for closures that aren't Send
FuncSt(::mockall::Fragile<Box<dyn #hrtb Fn(#( #refpredty, )*) -> bool>>),
Pred(Box<(#preds)>),
// Prevent "unused type parameter" errors
// Surprisingly, PhantomData<Fn(generics)> is Send even if
// generics are not, unlike PhantomData<generics>
_Phantom(Box<dyn Fn(#(#fn_params,)*) + Send>)
}
impl #ig Matcher #tg #wc {
#[allow(clippy::ptr_arg)]
fn matches #lg (&self, #( #argnames: &#predty, )*) -> bool {
match self {
Matcher::Always => true,
Matcher::Func(__mockall_f) =>
__mockall_f(#(#argnames, )*),
Matcher::FuncSt(__mockall_f) =>
(__mockall_f.get())(#(#argnames, )*),
Matcher::Pred(__mockall_pred) =>
[#pred_matches]
.iter()
.all(|__mockall_x| *__mockall_x),
_ => unreachable!()
}
}
}
impl #ig Default for Matcher #tg #wc {
#[allow(unused_variables)]
fn default() -> Self {
Matcher::Always
}
}
impl #ig ::std::fmt::Display for Matcher #tg #wc {
fn fmt(&self, __mockall_fmt: &mut ::std::fmt::Formatter<'_>)
-> ::std::fmt::Result
{
match self {
Matcher::Always => write!(__mockall_fmt, "<anything>"),
Matcher::Func(_) => write!(__mockall_fmt, "<function>"),
Matcher::FuncSt(_) => write!(__mockall_fmt, "<single threaded function>"),
Matcher::Pred(__mockall_p) => {
write!(__mockall_fmt, #braces,
#(__mockall_p.#indices,)*)
}
_ => unreachable!(),
}
}
}
).to_tokens(tokens);
}
}
struct RefRfunc<'a> {
f: &'a MockFunction
}
impl<'a> ToTokens for RefRfunc<'a> {
fn to_tokens(&self, tokens: &mut TokenStream) {
let fn_params = &self.f.fn_params;
let (ig, tg, wc) = self.f.egenerics.split_for_impl();
let lg = lifetimes_to_generics(&self.f.alifetimes);
let owned_output = &self.f.owned_output;
#[cfg(not(feature = "nightly_derive"))]
let default_err_msg =
"Returning default values requires the \"nightly\" feature";
#[cfg(feature = "nightly_derive")]
let default_err_msg =
"Can only return default values for types that impl std::Default";
quote!(
enum Rfunc #ig #wc {
Default(Option<#owned_output>),
Const(#owned_output),
// Prevent "unused type parameter" errors Surprisingly,
// PhantomData<Fn(generics)> is Send even if generics are not,
// unlike PhantomData<generics>
_Phantom(Mutex<Box<dyn Fn(#(#fn_params,)*) + Send>>)
}
impl #ig Rfunc #tg #wc {
fn call #lg (&self)
-> std::result::Result<&#owned_output, &'static str>
{
match self {
Rfunc::Default(Some(ref __mockall_o)) => {
::std::result::Result::Ok(__mockall_o)
},
Rfunc::Default(None) => {
Err(#default_err_msg)
},
Rfunc::Const(ref __mockall_o) => {
::std::result::Result::Ok(__mockall_o)
},
Rfunc::_Phantom(_) => unreachable!()
}
}
}
impl #ig std::default::Default for Rfunc #tg #wc
{
fn default() -> Self {
use ::mockall::ReturnDefault;
Rfunc::Default(::mockall::DefaultReturner::<#owned_output>
::maybe_return_default())
}
}
).to_tokens(tokens);
}
}
struct RefMutRfunc<'a> {
f: &'a MockFunction
}
impl<'a> ToTokens for RefMutRfunc<'a> {
fn to_tokens(&self, tokens: &mut TokenStream) {
let argnames = &self.f.argnames;
let argty = &self.f.argty;
let fn_params = &self.f.fn_params;
let (ig, tg, wc) = self.f.egenerics.split_for_impl();
let lg = lifetimes_to_generics(&self.f.alifetimes);
let owned_output = &self.f.owned_output;
let output = &self.f.output;
#[cfg(not(feature = "nightly_derive"))]
let default_err_msg =
"Returning default values requires the \"nightly\" feature";
#[cfg(feature = "nightly_derive")]
let default_err_msg =
"Can only return default values for types that impl std::Default";
quote!(
#[allow(clippy::unused_unit)]
enum Rfunc #ig #wc {
Default(Option<#owned_output>),
Mut((Box<dyn FnMut(#(#argty, )*) -> #owned_output + Send + Sync>),
Option<#owned_output>),
// Version of Rfunc::Mut for closures that aren't Send
MutSt((::mockall::Fragile<
Box<dyn FnMut(#(#argty, )*) -> #owned_output >>
), Option<#owned_output>
),
Var(#owned_output),
// Prevent "unused type parameter" errors Surprisingly,
// PhantomData<Fn(generics)> is Send even if generics are not,
// unlike PhantomData<generics>
_Phantom(Mutex<Box<dyn Fn(#(#fn_params,)*) + Send>>)
}
impl #ig Rfunc #tg #wc {
fn call_mut #lg (&mut self, #(#argnames: #argty, )*)
-> std::result::Result<#output, &'static str>
{
match self {
Rfunc::Default(Some(ref mut __mockall_o)) => {
::std::result::Result::Ok(__mockall_o)
},
Rfunc::Default(None) => {
Err(#default_err_msg)
},
Rfunc::Mut(ref mut __mockall_f, ref mut __mockall_o) =>
{
*__mockall_o = Some(__mockall_f(#(#argnames, )*));
if let Some(ref mut __mockall_o2) = __mockall_o {
::std::result::Result::Ok(__mockall_o2)
} else {
unreachable!()
}
},
Rfunc::MutSt(ref mut __mockall_f, ref mut __mockall_o)=>
{
*__mockall_o = Some((__mockall_f.get_mut())(
#(#argnames, )*)
);
if let Some(ref mut __mockall_o2) = __mockall_o {
::std::result::Result::Ok(__mockall_o2)
} else {
unreachable!()
}
},
Rfunc::Var(ref mut __mockall_o) => {
::std::result::Result::Ok(__mockall_o)
},
Rfunc::_Phantom(_) => unreachable!()
}
}
}
impl #ig std::default::Default for Rfunc #tg #wc
{
fn default() -> Self {
use ::mockall::ReturnDefault;
Rfunc::Default(::mockall::DefaultReturner::<#owned_output>
::maybe_return_default())
}
}
).to_tokens(tokens);
}
}
struct StaticRfunc<'a> {
f: &'a MockFunction
}
impl<'a> ToTokens for StaticRfunc<'a> {
fn to_tokens(&self, tokens: &mut TokenStream) {
let argnames = &self.f.argnames;
let argty = &self.f.argty;
let fn_params = &self.f.fn_params;
let (ig, tg, wc) = self.f.egenerics.split_for_impl();
let hrtb = self.f.hrtb();
let lg = lifetimes_to_generics(&self.f.alifetimes);
let output = &self.f.output;
quote!(
#[allow(clippy::unused_unit)]
enum Rfunc #ig #wc {
Default,
// Indicates that a `return_once` expectation has already
// returned
Expired,
Mut(Box<dyn #hrtb FnMut(#(#argty, )*) -> #output + Send>),
// Version of Rfunc::Mut for closures that aren't Send
MutSt(::mockall::Fragile<
Box<dyn #hrtb FnMut(#(#argty, )*) -> #output >>
),
Once(Box<dyn #hrtb FnOnce(#(#argty, )*) -> #output + Send>),
// Version of Rfunc::Once for closure that aren't Send
OnceSt(::mockall::Fragile<
Box<dyn #hrtb FnOnce(#(#argty, )*) -> #output>>
),
// Prevent "unused type parameter" errors Surprisingly,
// PhantomData<Fn(generics)> is Send even if generics are not,
// unlike PhantomData<generics>
_Phantom(Box<dyn Fn(#(#fn_params,)*) + Send>)
}
impl #ig Rfunc #tg #wc {
fn call_mut #lg (&mut self, #( #argnames: #argty, )* )
-> std::result::Result<#output, &'static str>
{
match self {
Rfunc::Default => {
use ::mockall::ReturnDefault;
::mockall::DefaultReturner::<#output>
::return_default()
},
Rfunc::Expired => {
Err("called twice, but it returns by move")
},
Rfunc::Mut(__mockall_f) => {
::std::result::Result::Ok(__mockall_f( #(#argnames, )* ))
},
Rfunc::MutSt(__mockall_f) => {
::std::result::Result::Ok((__mockall_f.get_mut())(#(#argnames,)*))
},
Rfunc::Once(_) => {
if let Rfunc::Once(mut __mockall_f) =
mem::replace(self, Rfunc::Expired) {
::std::result::Result::Ok(__mockall_f( #(#argnames, )* ))
} else {
unreachable!()
}
},
Rfunc::OnceSt(_) => {
if let Rfunc::OnceSt(mut __mockall_f) =
mem::replace(self, Rfunc::Expired) {
::std::result::Result::Ok((__mockall_f.into_inner())(#(#argnames,)*))
} else {
unreachable!()
}
},
Rfunc::_Phantom(_) => unreachable!()
}
}
}
impl #ig std::default::Default for Rfunc #tg #wc
{
fn default() -> Self {
Rfunc::Default
}
}
).to_tokens(tokens);
}
}
/// An expectation type for functions that take a &self and return a reference
struct RefExpectation<'a> {
f: &'a MockFunction
}
impl<'a> ToTokens for RefExpectation<'a> {
fn to_tokens(&self, tokens: &mut TokenStream) {
let argnames = &self.f.argnames;
let argty = &self.f.argty;
let common_methods = CommonExpectationMethods{f: self.f};
let desc = self.f.desc();
let funcname = self.f.funcname();
let (ig, tg, wc) = self.f.egenerics.split_for_impl();
let (_, common_tg, _) = self.f.cgenerics.split_for_impl();
let lg = lifetimes_to_generics(&self.f.alifetimes);
let output = &self.f.output;
let owned_output = &self.f.owned_output;
let v = &self.f.privmod_vis;
quote!(
/// Expectation type for methods taking a `&self` argument and
/// returning immutable references. This is the type returned by
/// the `expect_*` methods.
#v struct Expectation #ig #wc {
common: Common #common_tg,
rfunc: Rfunc #tg,
}
#[allow(clippy::unused_unit)]
impl #ig Expectation #tg #wc {
/// Call this [`Expectation`] as if it were the real method.
#v fn call #lg (&self, #(#argnames: #argty, )*) -> #output
{
self.common.call(&#desc);
self.rfunc.call().unwrap_or_else(|m| {
let desc = std::format!(
"{}", self.common.matcher.lock().unwrap());
panic!("{}: Expectation({}) {}", #funcname, desc,
m);
})
}
/// Return a reference to a constant value from the `Expectation`
#v fn return_const(&mut self, __mockall_o: #owned_output)
-> &mut Self
{
self.rfunc = Rfunc::Const(__mockall_o);
self
}
#common_methods
}
impl #ig Default for Expectation #tg #wc
{
fn default() -> Self {
Expectation {
common: Common::default(),
rfunc: Rfunc::default()
}
}
}
).to_tokens(tokens);
}
}
/// For methods that take &mut self and return a reference
struct RefMutExpectation<'a> {
f: &'a MockFunction
}
impl<'a> ToTokens for RefMutExpectation<'a> {
fn to_tokens(&self, tokens: &mut TokenStream) {
let common_methods = CommonExpectationMethods{f: self.f};
let argnames = &self.f.argnames;
let argty = &self.f.argty;
let desc = self.f.desc();
let funcname = self.f.funcname();
let (ig, tg, wc) = self.f.egenerics.split_for_impl();
let (_, common_tg, _) = self.f.cgenerics.split_for_impl();
let lg = lifetimes_to_generics(&self.f.alifetimes);
let owned_output = &self.f.owned_output;
let v = &self.f.privmod_vis;
quote!(
/// Expectation type for methods taking a `&mut self` argument and
/// returning references. This is the type returned by the
/// `expect_*` methods.
#v struct Expectation #ig #wc {
common: Common #common_tg,
rfunc: Rfunc #tg
}
#[allow(clippy::unused_unit)]
impl #ig Expectation #tg #wc {
/// Simulating calling the real method for this expectation
#v fn call_mut #lg (&mut self, #(#argnames: #argty, )*)
-> &mut #owned_output
{
self.common.call(&#desc);
let desc = std::format!(
"{}", self.common.matcher.lock().unwrap());
self.rfunc.call_mut(#(#argnames, )*).unwrap_or_else(|m| {
panic!("{}: Expectation({}) {}", #funcname, desc,
m);
})
}
/// Convenience method that can be used to supply a return value
/// for a `Expectation`. The value will be returned by mutable
/// reference.
#v fn return_var(&mut self, __mockall_o: #owned_output) -> &mut Self
{
self.rfunc = Rfunc::Var(__mockall_o);
self
}
/// Supply a closure that the `Expectation` will use to create its
/// return value. The return value will be returned by mutable
/// reference.
#v fn returning<MockallF>(&mut self, __mockall_f: MockallF)
-> &mut Self
where MockallF: FnMut(#(#argty, )*) -> #owned_output + Send + Sync + 'static
{
self.rfunc = Rfunc::Mut(Box::new(__mockall_f), None);
self
}
/// Single-threaded version of [`returning`](#method.returning).
/// Can be used when the argument or return type isn't `Send`.
#v fn returning_st<MockallF>(&mut self, __mockall_f: MockallF)
-> &mut Self
where MockallF: FnMut(#(#argty, )*) -> #owned_output + 'static
{
self.rfunc = Rfunc::MutSt(
::mockall::Fragile::new(Box::new(__mockall_f)), None);
self
}
#common_methods
}
impl #ig Default for Expectation #tg #wc
{
fn default() -> Self {
Expectation {
common: Common::default(),
rfunc: Rfunc::default()
}
}
}
).to_tokens(tokens);
}
}
/// An expectation type for functions return a `'static` value
struct StaticExpectation<'a> {
f: &'a MockFunction
}
impl<'a> ToTokens for StaticExpectation<'a> {
fn to_tokens(&self, tokens: &mut TokenStream) {
let common_methods = CommonExpectationMethods{f: self.f};
let argnames = &self.f.argnames;
let argty = &self.f.argty;
let desc = self.f.desc();
let hrtb = self.f.hrtb();
let funcname = self.f.funcname();
let (ig, tg, wc) = self.f.egenerics.split_for_impl();
let (_, common_tg, _) = self.f.cgenerics.split_for_impl();
let lg = lifetimes_to_generics(&self.f.alifetimes);
let output = &self.f.output;
let v = &self.f.privmod_vis;
quote!(
/// Expectation type for methods that return a `'static` type.
/// This is the type returned by the `expect_*` methods.
#v struct Expectation #ig #wc {
common: Common #common_tg,
rfunc: Mutex<Rfunc #tg>,
}
#[allow(clippy::unused_unit)]
impl #ig Expectation #tg #wc {
/// Call this [`Expectation`] as if it were the real method.
#[doc(hidden)]
#v fn call #lg (&self, #(#argnames: #argty, )* ) -> #output
{
self.common.call(&#desc);
self.rfunc.lock().unwrap().call_mut(#(#argnames, )*)
.unwrap_or_else(|message| {
let desc = std::format!(
"{}", self.common.matcher.lock().unwrap());
panic!("{}: Expectation({}) {}", #funcname, desc,
message);
})
}
/// Return a constant value from the `Expectation`
///
/// The output type must be `Clone`. The compiler can't always
/// infer the proper type to use with this method; you will
/// usually need to specify it explicitly. i.e.
/// `return_const(42i32)` instead of `return_const(42)`.
// We must use Into<#output> instead of #output because where
// clauses don't accept equality constraints.
// https://github.com/rust-lang/rust/issues/20041
#[allow(unused_variables)]
#v fn return_const<MockallOutput>(&mut self,
__mockall_c: MockallOutput)
-> &mut Self
where MockallOutput: Clone + Into<#output> + Send + 'static
{
self.returning(move |#(#argnames, )*| __mockall_c.clone().into())
}
/// Single-threaded version of
/// [`return_const`](#method.return_const). This is useful for
/// return types that are not `Send`.
///
/// The output type must be `Clone`. The compiler can't always
/// infer the proper type to use with this method; you will
/// usually need to specify it explicitly. i.e.
/// `return_const(42i32)` instead of `return_const(42)`.
///
/// It is a runtime error to call the mock method from a
/// different thread than the one that originally called this
/// method.
// We must use Into<#output> instead of #output because where
// clauses don't accept equality constraints.
// https://github.com/rust-lang/rust/issues/20041
#[allow(unused_variables)]
#v fn return_const_st<MockallOutput>(&mut self,
__mockall_c: MockallOutput)
-> &mut Self
where MockallOutput: Clone + Into<#output> + 'static
{
self.returning_st(move |#(#argnames, )*| __mockall_c.clone().into())
}
/// Supply an `FnOnce` closure that will provide the return
/// value for this Expectation. This is useful for return types
/// that aren't `Clone`. It will be an error to call this
/// method multiple times.
#v fn return_once<MockallF>(&mut self, __mockall_f: MockallF)
-> &mut Self
where MockallF: #hrtb FnOnce(#(#argty, )*)
-> #output + Send + 'static
{
{
let mut __mockall_guard = self.rfunc.lock().unwrap();
*__mockall_guard.deref_mut() =
Rfunc::Once(Box::new(__mockall_f));
}
self
}
/// Single-threaded version of
/// [`return_once`](#method.return_once). This is useful for
/// return types that are neither `Send` nor `Clone`.
///
/// It is a runtime error to call the mock method from a
/// different thread than the one that originally called this
/// method. It is also a runtime error to call the method more
/// than once.
#v fn return_once_st<MockallF>(&mut self, __mockall_f:
MockallF) -> &mut Self
where MockallF: #hrtb FnOnce(#(#argty, )*)
-> #output + 'static
{
{
let mut __mockall_guard = self.rfunc.lock().unwrap();
*__mockall_guard.deref_mut() = Rfunc::OnceSt(
::mockall::Fragile::new(Box::new(__mockall_f)));
}
self
}
/// Supply a closure that will provide the return value for this
/// `Expectation`. The method's arguments are passed to the
/// closure by value.
#v fn returning<MockallF>(&mut self, __mockall_f: MockallF)
-> &mut Self
where MockallF: #hrtb FnMut(#(#argty, )*)
-> #output + Send + 'static
{
{
let mut __mockall_guard = self.rfunc.lock().unwrap();
*__mockall_guard.deref_mut() =
Rfunc::Mut(Box::new(__mockall_f));
}
self
}
/// Single-threaded version of [`returning`](#method.returning).
/// Can be used when the argument or return type isn't `Send`.
///
/// It is a runtime error to call the mock method from a
/// different thread than the one that originally called this
/// method.
#v fn returning_st<MockallF>(&mut self, __mockall_f: MockallF)
-> &mut Self
where MockallF: #hrtb FnMut(#(#argty, )*)
-> #output + 'static
{
{
let mut __mockall_guard = self.rfunc.lock().unwrap();
*__mockall_guard.deref_mut() = Rfunc::MutSt(
::mockall::Fragile::new(Box::new(__mockall_f)));
}
self
}
#common_methods
}
impl #ig Default for Expectation #tg #wc
{
fn default() -> Self {
Expectation {
common: Common::default(),
rfunc: Mutex::new(Rfunc::default())
}
}
}
).to_tokens(tokens);
}
}
/// An collection of RefExpectation's
struct RefExpectations<'a> {
f: &'a MockFunction
}
impl<'a> ToTokens for RefExpectations<'a> {
fn to_tokens(&self, tokens: &mut TokenStream) {
let common_methods = CommonExpectationsMethods{f: self.f};
let argnames = &self.f.argnames;
let argty = &self.f.argty;
let (ig, tg, wc) = self.f.egenerics.split_for_impl();
let lg = lifetimes_to_generics(&self.f.alifetimes);
let output = &self.f.output;
let predexprs = &self.f.predexprs;
let v = &self.f.privmod_vis;
quote!(
#common_methods
impl #ig Expectations #tg #wc {
/// Simulate calling the real method. Every current expectation
/// will be checked in FIFO order and the first one with
/// matching arguments will be used.
#v fn call #lg (&self, #(#argnames: #argty, )* )
-> Option<#output>
{
self.0.iter()
.find(|__mockall_e|
__mockall_e.matches(#(#predexprs, )*) &&
(!__mockall_e.is_done() || self.0.len() == 1))
.map(move |__mockall_e|
__mockall_e.call(#(#argnames),*)
)
}
}
).to_tokens(tokens);
}
}
/// An collection of RefMutExpectation's
struct RefMutExpectations<'a> {
f: &'a MockFunction
}
impl<'a> ToTokens for RefMutExpectations<'a> {
fn to_tokens(&self, tokens: &mut TokenStream) {
let common_methods = CommonExpectationsMethods{f: self.f};
let argnames = &self.f.argnames;
let argty = &self.f.argty;
let (ig, tg, wc) = self.f.egenerics.split_for_impl();
let lg = lifetimes_to_generics(&self.f.alifetimes);
let output = &self.f.output;
let predexprs = &self.f.predexprs;
let v = &self.f.privmod_vis;
quote!(
#common_methods
impl #ig Expectations #tg #wc {
/// Simulate calling the real method. Every current expectation
/// will be checked in FIFO order and the first one with
/// matching arguments will be used.
#v fn call_mut #lg (&mut self, #(#argnames: #argty, )* )
-> Option<#output>
{
let __mockall_n = self.0.len();
self.0.iter_mut()
.find(|__mockall_e|
__mockall_e.matches(#(#predexprs, )*) &&
(!__mockall_e.is_done() || __mockall_n == 1))
.map(move |__mockall_e|
__mockall_e.call_mut(#(#argnames, )*)
)
}
}
).to_tokens(tokens);
}
}
/// An collection of Expectation's for methods returning static values
struct StaticExpectations<'a> {
f: &'a MockFunction
}
impl<'a> ToTokens for StaticExpectations<'a> {
fn to_tokens(&self, tokens: &mut TokenStream) {
let common_methods = CommonExpectationsMethods{f: self.f};
let argnames = &self.f.argnames;
let argty = &self.f.argty;
let (ig, tg, wc) = self.f.egenerics.split_for_impl();
let lg = lifetimes_to_generics(&self.f.alifetimes);
let output = &self.f.output;
let predexprs = &self.f.predexprs;
let v = &self.f.privmod_vis;
quote!(
#common_methods
impl #ig Expectations #tg #wc {
/// Simulate calling the real method. Every current expectation
/// will be checked in FIFO order and the first one with
/// matching arguments will be used.
#v fn call #lg (&self, #(#argnames: #argty, )* )
-> Option<#output>
{
self.0.iter()
.find(|__mockall_e|
__mockall_e.matches(#(#predexprs, )*) &&
(!__mockall_e.is_done() || self.0.len() == 1))
.map(move |__mockall_e|
__mockall_e.call(#(#argnames, )*)
)
}
}
).to_tokens(tokens);
}
}
struct GenericExpectations<'a> {
f: &'a MockFunction
}
impl<'a> ToTokens for GenericExpectations<'a> {
fn to_tokens(&self, tokens: &mut TokenStream) {
if ! self.f.is_expectation_generic() {
return;
}
if ! self.f.is_static() && ! self.f.is_method_generic() {
return;
}
let ge = StaticGenericExpectations{f: self.f};
let v = &self.f.privmod_vis;
quote!(
/// A collection of [`Expectation`](struct.Expectations.html)
/// objects for a generic method. Users will rarely if ever use
/// this struct directly.
#[doc(hidden)]
#[derive(Default)]
#v struct GenericExpectations{
store: std::collections::hash_map::HashMap<::mockall::Key,
Box<dyn ::mockall::AnyExpectations>>
}
impl GenericExpectations {
/// Verify that all current expectations are satisfied and clear
/// them. This applies to all sets of generic parameters!
#v fn checkpoint(&mut self) ->
std::collections::hash_map::Drain<::mockall::Key,
Box<dyn ::mockall::AnyExpectations>>
{
self.store.drain()
}
#v fn new() -> Self {
Self::default()
}
}
#ge
).to_tokens(tokens);
}
}
/// Generates methods for GenericExpectations for methods returning static
/// values
struct StaticGenericExpectations<'a> {
f: &'a MockFunction
}
impl<'a> ToTokens for StaticGenericExpectations<'a> {
fn to_tokens(&self, tokens: &mut TokenStream) {
let argnames = &self.f.argnames;
let argty = &self.f.argty;
let (ig, tg, wc) = self.f.egenerics.split_for_impl();
let keyid = gen_keyid(&self.f.egenerics);
let mut any_wc = wc.cloned();
if self.f.return_ref || self.f.return_refmut {
// Add Senc + Sync, required for downcast, since Expectation
// stores an Option<#owned_output>
send_syncify(&mut any_wc, self.f.owned_output.clone());
}
let tbf = tg.as_turbofish();
let output = &self.f.output;
let v = &self.f.privmod_vis;
let (call, get, self_, downcast) = if self.f.return_refmut {
(format_ident!("call_mut"),
format_ident!("get_mut"),
quote!(&mut self),
format_ident!("downcast_mut"))
} else {
(format_ident!("call"),
format_ident!("get"),
quote!(&self),
format_ident!("downcast_ref"))
};
quote!(
impl #ig ::mockall::AnyExpectations for Expectations #tg #any_wc {}
impl GenericExpectations {
/// Simulating calling the real method.
#v fn #call #ig (#self_, #(#argnames: #argty, )* )
-> Option<#output> #wc
{
self.store.#get(&::mockall::Key::new::#keyid())
.map(|__mockall_e| {
__mockall_e.#downcast::<Expectations #tg>()
.unwrap()
.#call(#(#argnames, )*)
}).flatten()
}
/// Create a new Expectation.
#v fn expect #ig (&mut self) -> &mut Expectation #tg #any_wc
{
self.store.entry(::mockall::Key::new::#keyid())
.or_insert_with(|| Box::new(Expectations #tbf::new()))
.downcast_mut::<Expectations #tg>()
.unwrap()
.expect()
}
}
).to_tokens(tokens)
}
}