package datastructures

import org.scalatest._
import testutil.PendingIfUnimplemented
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers

class ListSpec extends AnyFlatSpec with Matchers with AppendedClues with PendingIfUnimplemented {
  val numbers = List(1 to 5: _*)
  val single = List(1)
  val strings = List("a", "b", "c")

  "A list" should "have a tail function that removes the first element" in {
    numbers.tail shouldBe List(2, 3, 4, 5)
    single.tail shouldBe List.Nil
    a[RuntimeException] should be thrownBy List().tail
  }

  it should "have a init function that removes the last element" in {
    numbers.init shouldBe List(1, 2, 3, 4)
    single.init shouldBe List.Nil
    a[RuntimeException] should be thrownBy List().init
  }

  it should "have a setHead method, which replaces the first element" in {
    numbers.setHead(8) shouldBe List(8, 2, 3, 4, 5)
    a[RuntimeException] should be thrownBy List().tail
  }

  it should "have a left fold" in {
    strings.foldLeft("X")(_ + _) should not be "Xcba" withClue ", this looks like a right fold with flipped args"
    strings.foldLeft("X")(_ + _) shouldBe "Xabc"
  }
}