|
472 | 472 |
|
473 | 473 | ### Feature: Print a value from a file |
474 | 474 |
|
475 | | -### KISS - ~~Keep it simple~~, STUPID |
| 475 | +#### KISS - ~~Keep it simple~~, STUPID |
476 | 476 |
|
477 | 477 | ```cpp |
478 | 478 | int main() { |
|
486 | 486 | } |
487 | 487 | ``` |
488 | 488 |
|
489 | | -##### Unit-Testing - **Give me a break!** |
| 489 | +##### Unit-Testing? - Give me a break! |
490 | 490 |
|
491 | 491 | ---- |
492 | 492 |
|
493 | 493 | ### A few iterations later... |
494 | 494 |
|
495 | 495 | ---- |
496 | 496 |
|
497 | | -#### **Stupid** vs <strike>SOLID</strike> |
| 497 | +#### Stupid vs <strike>SOLID</strike> |
498 | 498 |
|
499 | 499 | ```cpp |
500 | 500 | class Manager { // Indescriptive Naming |
|
510 | 510 | ```cpp |
511 | 511 | class App { |
512 | 512 | public: |
513 | | - App() { manager = new Manager; } // Tight Coupling |
514 | | - ~App() { delete manager; } |
| 513 | + App() |
| 514 | + : manager(std::make_unique<Manager>()); // Tight Coupling |
| 515 | + { } |
515 | 516 |
|
516 | 517 | __attribute__((always_inline)) void run() { // Premature Optimization |
517 | 518 | Logger::instance() // Singleton |
|
522 | 523 | ) |
523 | 524 | } |
524 | 525 | private: |
525 | | - Manager* manager; |
| 526 | + std::unique_ptr<Manager> manager; |
526 | 527 | }; |
527 | 528 | ``` |
528 | 529 |
|
529 | 530 | ---- |
530 | 531 |
|
531 | | -#### **Stupid** vs <strike>SOLID</strike> |
| 532 | +#### Stupid vs <strike>SOLID</strike> |
532 | 533 |
|
533 | 534 | #### Unit-testing? |
534 | 535 |
|
|
566 | 567 |
|
567 | 568 | ---- |
568 | 569 |
|
569 | | -#### <strike>Stupid</strike> vs **SOLID** |
| 570 | +#### <strike>Stupid</strike> vs SOLID |
570 | 571 |
|
571 | 572 | ```cpp |
572 | 573 | /** |
|
608 | 609 |
|
609 | 610 | ---- |
610 | 611 |
|
611 | | -#### <strike>Stupid</strike> vs **SOLID** (Dependency Injection) |
| 612 | +#### <strike>Stupid</strike> vs SOLID |
612 | 613 |
|
613 | 614 | ```cpp |
614 | 615 | /** |
615 | | - * "Depend on abstractions, not on concretions" |
| 616 | + * Dependency Inversion |
| 617 | + * "Depend on abstractions, not on concretions" |
616 | 618 | */ |
617 | 619 | class IReader { class IPrinter { |
618 | 620 | public: public: |
|
626 | 628 | IReader& reader; |
627 | 629 | IPrinter& printer; |
628 | 630 | ILogger& logger; |
629 | | -
|
630 | 631 | public: |
631 | 632 | /** |
632 | | - * "Don't call us, we'll call you", Hollywood principle |
| 633 | + * Dependency Injection |
| 634 | + * "Don't call us, we'll call you", Hollywood principle |
633 | 635 | */ |
634 | 636 | App(IReader& reader, IPrinter& printer, ILogger& logger); |
635 | 637 |
|
|
997 | 999 |
|
998 | 1000 | ---- |
999 | 1001 |
|
| 1002 | +### Automatic Mocks Injection - MAKE API |
| 1003 | +
|
| 1004 | +```cpp |
| 1005 | +namespace testing { |
| 1006 | + /** |
| 1007 | + * [Proposal - generic factories] |
| 1008 | + * http://.../wg21/docs/papers/2016/p0338r0.pdf |
| 1009 | + */ |
| 1010 | + template <class T, class... TArgs> |
| 1011 | + auto make(TArgs&&... args); |
| 1012 | + |
| 1013 | + template <template<auto...> class T, ... TArgs> |
| 1014 | + auto make(TArgs&&... args); |
| 1015 | +} |
| 1016 | +``` |
| 1017 | +
|
| 1018 | +---- |
| 1019 | +
|
1000 | 1020 | ####Test - Manual mocks injection |
1001 | 1021 |
|
1002 | 1022 | ```cpp |
|
1183 | 1203 |
|
1184 | 1204 | ```cpp |
1185 | 1205 | "[scenario] Value from a file is displayed"_test { |
1186 | | - GIVEN("Hhe App is created") { |
1187 | | - auto [app, mocks] = testing::make<App>() |
1188 | | - } |
1189 | | - GIVEN(mocks<Readable>(), read().WillOnce(Return(42)); |
| 1206 | + auto [app, mocks] = testing::make<App>() |
1190 | 1207 |
|
| 1208 | + GIVEN(mocks<Readable>(), read().WillOnce(Return(42)); |
1191 | 1209 | WHEN(app.run()); |
1192 | | -
|
1193 | 1210 | THEN(mocks<Printable>(), print(42)); |
1194 | 1211 | } |
1195 | 1212 | ``` |
|
1237 | 1254 |
|
1238 | 1255 | When at least 1000 shares of GOOGL were bought at price $950 |
1239 | 1256 |
|
1240 | | - Then A buy order for 100 shares of GOOGL should be executed |
| 1257 | + Then A buy order for 100 shares of GOOGL should've be executed |
1241 | 1258 | And The TS should own 100 shares of GOOGL |
1242 | 1259 | And The TS should have $90'000 |
1243 | 1260 | ``` |
|
1249 | 1266 |
|
1250 | 1267 | When 50 shares of MSFT were sold |
1251 | 1268 |
|
1252 | | - Then A sell order for 50 shares of MSFT stock should have been executed |
| 1269 | + Then A sell order for 50 shares of MSFT stock should've been executed |
1253 | 1270 | And The TS should own 50 shares of APPL stock |
1254 | 1271 | ``` |
1255 | 1272 |
|
|
1310 | 1327 |
|
1311 | 1328 | ---- |
1312 | 1329 |
|
1313 | | -TDD |
1314 | | -eXtreme programming -> pairing, one test one implementation |
1315 | | - |
1316 | | -> Always start from the expections (express intentions!) |
1317 | | ----- |
1318 | | - |
1319 | | - |
1320 | | -#### 1. TDD/RED - Write a bit of test (start from expectations, intentions) |
| 1330 | +### 1. TDD/RED - Write a bit of test (start from expectations, intentions) |
1321 | 1331 |
|
1322 | 1332 | ```cpp |
1323 | 1333 | "should not print anything when the input is empty" = [] { |
|
1327 | 1337 | ``` |
1328 | 1338 |
|
1329 | 1339 | ---- |
| 1340 | + |
| 1341 | +## TDD and eXtreme programming / Pairing |
| 1342 | + |
| 1343 | +<img src="images/pair.png" width="85%" /> |
| 1344 | + |
| 1345 | +> 1. One dev is writing a test and the other is making it pass (the simplest way) |
| 1346 | +> 2. Switch the rules! |
| 1347 | + |
| 1348 | +---- |
| 1349 | + |
1330 | 1350 | #### 2. TDD/GREEN - Make it compile/pass (the simpler way) |
1331 | 1351 | #### 3. TDD/REFECTOR - Remove duplicates/extract |
1332 | 1352 |
|
|
1433 | 1453 |
|
1434 | 1454 | ---- |
1435 | 1455 |
|
1436 | | - Take the next story... |
1437 | | - * Pair with someone else of the team! |
| 1456 | +<img style="background:none; border:none; box-shadow:none;" src="images/done.png" /> |
| 1457 | + |
| 1458 | +* #### Take the next story... |
| 1459 | +* #### Pair with someone else of the team! (knowledge sharing) |
1438 | 1460 |
|
1439 | 1461 | ============================================================================== |
1440 | 1462 |
|
|
0 commit comments