diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 2a77081..7444f62 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -12,6 +12,7 @@ * Ria Cataquian * Simone D'Amico * snikolau +* Stephen Rufle * Trung LĂȘ * Uku Taht * Zander Mackie diff --git a/lib/koans/11_structs.ex b/lib/koans/11_structs.ex index 1c33fd5..b54336f 100644 --- a/lib/koans/11_structs.ex +++ b/lib/koans/11_structs.ex @@ -45,4 +45,23 @@ defmodule Structs do assert Map.fetch(silvia, :age) == ___ end + + defmodule Airline do + defstruct plane: %Plane{}, name: "Southwest" + end + + koan "Use the put_in macro to replace a nested value" do + airline = %Airline{plane: %Plane{maker: :boeing}} + assert put_in(airline.plane.maker, :airbus) == ___ + end + + koan "Use the update_in macro to modify a nested value" do + airline = %Airline{plane: %Plane{maker: :boeing, passengers: 200}} + assert update_in(airline.plane.passengers, &(&1 + 2)) == ___ + end + + koan "Use the put_in macro with atoms to replace a nested value in a non-struct" do + airline = %{plane: %{maker: :boeing}, name: "Southwest"} + assert put_in(airline[:plane][:maker], :cessna) == ___ + end end diff --git a/test/koans/structs_koans_test.exs b/test/koans/structs_koans_test.exs index 92622b9..b381dc0 100644 --- a/test/koans/structs_koans_test.exs +++ b/test/koans/structs_koans_test.exs @@ -10,6 +10,9 @@ defmodule StructsTests do 33, {:multiple, [true, false]}, {:ok, 22}, + %Structs.Airline{plane: %Structs.Plane{maker: :airbus}, name: "Southwest"}, + %Structs.Airline{plane: %Structs.Plane{maker: :boeing, passengers: 202}, name: "Southwest"}, + %{plane: %{maker: :cessna}, name: "Southwest"}, ] test_all(Structs, answers)