elixir-todo-list/test/todo/list_test.exs

90 lines
2.6 KiB
Elixir

defmodule Todo.TestList do
use ExUnit.Case
alias Todo.List
test "simple usage" do
list = List.new()
assert [] = List.entries(list, ~D[2024-08-04])
list = List.add(list, %{date: ~D[2024-08-04], title: "Write some Elixir"})
assert [%{date: ~D[2024-08-04], title: "Write some Elixir"}] =
List.entries(list, ~D[2024-08-04])
end
test "id starts at 1 and increases by 1" do
list = List.new()
list = List.add(list, %{date: ~D[2024-08-04], title: "Write some Elixir"})
assert [%{date: ~D[2024-08-04], title: "Write some Elixir", id: 1}] =
List.entries(list, ~D[2024-08-04])
list = List.add(list, %{date: ~D[2024-08-04], title: "Write more Elixir"})
assert [
%{date: ~D[2024-08-04], title: "Write some Elixir", id: 1},
%{date: ~D[2024-08-04], title: "Write more Elixir", id: 2}
] =
List.entries(list, ~D[2024-08-04])
end
test "new/1 has expected elements" do
list =
List.new([
%{date: ~D[2024-08-04], title: "Write some Elixir"},
%{date: ~D[2024-08-04], title: "Write more Elixir"}
])
assert [
%{date: ~D[2024-08-04], title: "Write some Elixir"},
%{date: ~D[2024-08-04], title: "Write more Elixir"}
] =
List.entries(list, ~D[2024-08-04])
end
test "entries returns correct dates" do
list =
List.new([
%{date: ~D[2024-08-03], title: "Prepare writing Elixir"},
%{date: ~D[2024-08-04], title: "Write some Elixir"},
%{date: ~D[2024-08-05], title: "Pause writing Elixir"},
%{date: ~D[2024-08-04], title: "Write more Elixir"}
])
assert [%{date: ~D[2024-08-04]}, %{date: ~D[2024-08-04]}] = List.entries(list, ~D[2024-08-04])
end
test "update/3 updates selected entry" do
list =
List.new([
%{date: ~D[2024-08-04], title: "Write some Elixir"},
%{date: ~D[2024-08-04], title: "Write more Elixir"}
])
updated =
List.update(list, 1, fn %{title: title} = entry ->
%{entry | title: String.upcase(title)}
end)
assert [
%{date: ~D[2024-08-04], title: "WRITE SOME ELIXIR"},
%{date: ~D[2024-08-04], title: "Write more Elixir"}
] =
List.entries(updated, ~D[2024-08-04])
end
test "delete/2" do
list =
List.new([
%{date: ~D[2024-08-04], title: "Write some Elixir"},
%{date: ~D[2024-08-04], title: "Write more Elixir"}
])
new_list = List.delete(list, 2)
assert [
%{date: ~D[2024-08-04], title: "Write some Elixir"}
] = List.entries(new_list, ~D[2024-08-04])
end
end