Tin

Examples · learn/14-tests · 202 of 339

beside-main.test

examples/learn/14-tests/beside-main.test.tintin run examples/learn/14-tests/beside-main.test.tin
type Cart = { items: string[], total: int }

fn add(cart: inout Cart, item: string, price: int) {
  cart.items.push(item)
  cart.total += price
}

test "a cart adds up" {
  var cart = Cart { items: [], total: 0 }
  add(&cart, "tea", 300)
  add(&cart, "cake", 450)
  assertEq(cart.total, 750)
  assertEq(cart.items, ["tea", "cake"])
}

// tin run ignores the test blocks, but still checks that they compile
fn main() {
  var cart = Cart { items: [], total: 0 }
  add(&cart, "tea", 300)
  print(cart)
}