diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..d10f2b8 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,32 @@ +name: Elixir Test and format + +on: + push: + branches: + - main + pull_request: + branches: + - "*" + +jobs: + test: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Set up Elixir + uses: actions/setup-elixir@v1 + with: + otp-version: "26.x" + elixir-version: "1.16.x" + + - name: Install dependencies + run: mix deps.get + + - name: Run tests + run: mix test + + - name: Check code formatting + run: mix format --check-formatted diff --git a/test/elixir_basic_types_test.exs b/test/elixir_basic_types_test.exs index 8723f18..998603a 100644 --- a/test/elixir_basic_types_test.exs +++ b/test/elixir_basic_types_test.exs @@ -26,22 +26,25 @@ defmodule BasicTypesTest do assert is_float(1 + 2.0) assert is_float(1.0 + 2) end + test "Arithmetic subtraction operator" do assert is_integer(2 - 1) assert is_float(2.0 - 1.0) assert is_float(2 - 1.0) assert is_float(2.0 - 1) end + test "Arithmetic multiplication operator" do assert is_integer(1 * 2) assert is_float(1.0 * 2.0) assert is_float(1 * 2.0) assert is_float(1.0 * 2) end + test "Arithmetic division operator" do assert is_float(10 / 2) - assert is_float(10 / 2.0) - assert is_float(10.0 / 2) + assert is_float(10 / 2.0) + assert is_float(10.0 / 2) end end @@ -93,9 +96,9 @@ defmodule BasicTypesTest do test "atoms" do assert :apple == :apple refute :apple == :orange - assert true == :true + assert true == true assert is_atom(false) - assert is_boolean(:false) + assert is_boolean(false) end test "strings" do diff --git a/test/elixir_case_cond_if_test.exs b/test/elixir_case_cond_if_test.exs index 8e7eb96..19cb54e 100644 --- a/test/elixir_case_cond_if_test.exs +++ b/test/elixir_case_cond_if_test.exs @@ -3,14 +3,18 @@ defmodule CaseCondAndIfTest do describe "case" do test "matching tuple with case" do - result = case {1, 2, 3} do - {4, 5, 6} -> - "This clause won't match" - {1, _x, 3} -> - "This clause will match and bind _x to 2 in this clause" - _ -> - "This clause would match any value" - end + result = + case {1, 2, 3} do + {4, 5, 6} -> + "This clause won't match" + + {1, _x, 3} -> + "This clause will match and bind _x to 2 in this clause" + + _ -> + "This clause would match any value" + end + assert result == "This clause will match and bind _x to 2 in this clause" end @@ -24,20 +28,25 @@ defmodule CaseCondAndIfTest do # end test "case with guard clause" do - result = case {1, 2, 3} do - {1, x, 3} when x > 0 -> - "Will match" - _ -> - "Would match, if guard condition were not satisfied" - end + result = + case {1, 2, 3} do + {1, x, 3} when x > 0 -> + "Will match" + + _ -> + "Would match, if guard condition were not satisfied" + end + assert result == "Will match" end test "errors in guards do not leak" do - result = case 1 do - x when hd(x) -> "Won't match" - x -> "Got #{x}" - end + result = + case 1 do + x when hd(x) -> "Won't match" + x -> "Got #{x}" + end + assert result == "Got 1" end @@ -52,25 +61,31 @@ defmodule CaseCondAndIfTest do describe "if/unless" do test "if true condition" do - result = if true do - "This works!" - end + result = + if true do + "This works!" + end + assert result == "This works!" end test "unless true condition" do - result = unless true do - "This will never be seen" - end + result = + unless true do + "This will never be seen" + end + assert result == nil end test "if else condition" do - result = if nil do - "This won't be seen" - else - "This will" - end + result = + if nil do + "This won't be seen" + else + "This will" + end + assert result == "This will" end @@ -84,45 +99,58 @@ defmodule CaseCondAndIfTest do test "return value from if to change value" do x = 1 - x = if true do - x + 1 - else - x - end + + x = + if true do + x + 1 + else + x + end + assert x == 2 end end describe "cond" do test "matching multiple conditions with cond" do - result = cond do - 2 + 2 == 5 -> - "This will not be true" - 2 * 2 == 3 -> - "Nor this" - 1 + 1 == 2 -> - "But this will" - end + result = + cond do + 2 + 2 == 5 -> + "This will not be true" + + 2 * 2 == 3 -> + "Nor this" + + 1 + 1 == 2 -> + "But this will" + end + assert result == "But this will" end test "cond with final true condition" do - result = cond do - 2 + 2 == 5 -> - "This is never true" - 2 * 2 == 3 -> - "Nor this" - true -> - "This is always true (equivalent to else)" - end + result = + cond do + 2 + 2 == 5 -> + "This is never true" + + 2 * 2 == 3 -> + "Nor this" + + true -> + "This is always true (equivalent to else)" + end + assert result == "This is always true (equivalent to else)" end test "cond considers non-nil and non-false as true" do - result = cond do - hd([1, 2, 3]) -> - "1 is considered as true" - end + result = + cond do + hd([1, 2, 3]) -> + "1 is considered as true" + end + assert result == "1 is considered as true" end end diff --git a/test/elixir_lists_and_tuples_test.exs b/test/elixir_lists_and_tuples_test.exs index c0da4ae..b14fd4b 100644 --- a/test/elixir_lists_and_tuples_test.exs +++ b/test/elixir_lists_and_tuples_test.exs @@ -19,9 +19,9 @@ defmodule ListsAndTuplesTest do end test "list operators never modify the existing list" do - new_list = [1,2,3] + new_list = [1, 2, 3] _ = new_list -- [3] - assert new_list == [1,2,3] + assert new_list == [1, 2, 3] end test "retrieving head and tail of a list" do @@ -105,9 +105,9 @@ defmodule ListsAndTuplesTest do end describe "Tagged tuples" do - test "File.read/1 returns tagged tuples" do - assert File.read("test/example_to_read_file.txt") == {:ok, "example content"} - assert File.read("unknown_file") == {:error, :enoent} - end + test "File.read/1 returns tagged tuples" do + assert File.read("test/example_to_read_file.txt") == {:ok, "example content"} + assert File.read("unknown_file") == {:error, :enoent} + end end end