|
| 1 | +module proverb; |
| 2 | + |
| 3 | +pure string recite(immutable string[] strings) |
| 4 | +{ |
| 5 | + // implement this function |
| 6 | +} |
| 7 | + |
| 8 | +unittest |
| 9 | +{ |
| 10 | + immutable int allTestsEnabled = 0; |
| 11 | + |
| 12 | + // Zero pieces |
| 13 | + { |
| 14 | + string expected = ""; |
| 15 | + assert(recite([]) == expected); |
| 16 | + } |
| 17 | + |
| 18 | + static if (allTestsEnabled) |
| 19 | + { |
| 20 | + // One piece |
| 21 | + { |
| 22 | + string expected = "And all for the want of a nail."; |
| 23 | + assert(recite(["nail"]) == expected); |
| 24 | + } |
| 25 | + |
| 26 | + // Two pieces |
| 27 | + { |
| 28 | + string expected = "For want of a nail the shoe was lost.\n" |
| 29 | + ~ "And all for the want of a nail."; |
| 30 | + assert(recite(["nail", "shoe"]) == expected); |
| 31 | + } |
| 32 | + |
| 33 | + // Three pieces |
| 34 | + { |
| 35 | + string expected = "For want of a nail the shoe was lost.\n" |
| 36 | + ~ "For want of a shoe the horse was lost.\n" |
| 37 | + ~ "And all for the want of a nail."; |
| 38 | + assert(recite(["nail", "shoe", "horse"]) == expected); |
| 39 | + } |
| 40 | + |
| 41 | + // Full proverb |
| 42 | + { |
| 43 | + string expected = "For want of a nail the shoe was lost.\n" |
| 44 | + ~ "For want of a shoe the horse was lost.\n" |
| 45 | + ~ "For want of a horse the rider was lost.\n" |
| 46 | + ~ "For want of a rider the message was lost.\n" |
| 47 | + ~ "For want of a message the battle was lost.\n" |
| 48 | + ~ "For want of a battle the kingdom was lost.\n" |
| 49 | + ~ "And all for the want of a nail."; |
| 50 | + immutable string[] strings = [ |
| 51 | + "nail", "shoe", "horse", "rider", |
| 52 | + "message", "battle", "kingdom" |
| 53 | + ]; |
| 54 | + assert(recite(strings) == expected); |
| 55 | + } |
| 56 | + |
| 57 | + // Four pieces modernized |
| 58 | + { |
| 59 | + string expected = "For want of a pin the gun was lost.\n" |
| 60 | + ~ "For want of a gun the soldier was lost.\n" |
| 61 | + ~ "For want of a soldier the battle was lost.\n" |
| 62 | + ~ "And all for the want of a pin."; |
| 63 | + assert(recite(["pin", "gun", "soldier", "battle"]) == expected); |
| 64 | + } |
| 65 | + } |
| 66 | +} |
0 commit comments