Magician-in-Training in Swift on Exercism exercise contains only a single test for the func insert(_ newCard: Int, atBottomOf stack: [Int]) -> [Int]
function. While mentoring, I encountered a person, who wrote this code:
func insert(_ newCard: Int, atBottomOf stack: [Int]) -> [Int] {
guard !stack.isEmpty else {
return stack
}
var array = stack
array.insert(newCard, at: 0)
return array
}
The guard statement at the top is not just redundant, it actually prevents the function from executing with an empty stack
, which is incorrect. But there’s no auto test to catch this, and so all tests pass.