package tutorial;

import static org.junit.jupiter.api.Assertions.*;

import java.util.*;
import org.junit.jupiter.api.Test;

class LessonTest {

  @Test
  void groupsAndTotals() {
    var rows = List.of(
      new Ledger.Entry("car", 100),
      new Ledger.Entry("car", 200),
      new Ledger.Entry("bike", 50)
    );
    var groups = Ledger.group(rows, Ledger.Entry::vehicle);
    assertEquals(300, Ledger.total(groups.get("car")));
    assertEquals(50, Ledger.total(groups.get("bike")));
    assertThrows(UnsupportedOperationException.class, () ->
      groups.get("car").clear()
    );
  }

  @Test
  void invalidAndOverflow() {
    assertThrows(IllegalArgumentException.class, () ->
      new Ledger.Entry("car", -1)
    );
    assertThrows(ArithmeticException.class, () ->
      Ledger.total(
        List.of(
          new Ledger.Entry("car", Long.MAX_VALUE),
          new Ledger.Entry("car", 1)
        )
      )
    );
  }
}
