Merge pull request #162 from srufle/master

Demonstrate put_in to manipulate nested struct values
This commit is contained in:
Felipe Seré
2017-01-26 20:02:30 +00:00
committed by GitHub
3 changed files with 23 additions and 0 deletions

View File

@@ -12,6 +12,7 @@
* Ria Cataquian
* Simone D'Amico
* snikolau
* Stephen Rufle
* Trung Lê
* Uku Taht
* Zander Mackie

View File

@@ -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

View File

@@ -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)