From c5d1cce01d7f5873b18344807fa41a453a698450 Mon Sep 17 00:00:00 2001 From: jbiset Date: Mon, 29 Jul 2024 14:50:48 -0300 Subject: [PATCH 1/3] Commented tests that show warnings that are not necessary for the case to be tested --- test/elixir_case_cond_if_test.exs | 129 ++++++++++++++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 test/elixir_case_cond_if_test.exs diff --git a/test/elixir_case_cond_if_test.exs b/test/elixir_case_cond_if_test.exs new file mode 100644 index 0000000..8e7eb96 --- /dev/null +++ b/test/elixir_case_cond_if_test.exs @@ -0,0 +1,129 @@ +defmodule CaseCondAndIfTest do + use ExUnit.Case + + 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 + assert result == "This clause will match and bind _x to 2 in this clause" + end + + # test "pattern match against an existing variable" do + # x = 1 + # result = case 10 do + # ^x -> "Won't match" + # _ -> "Will match" + # end + # assert result == "Will match" + # 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 + 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 + assert result == "Got 1" + end + + # test "no matching case clause raises an error" do + # assert_raise CaseClauseError, fn -> + # case :ok do + # :error -> "Won't match" + # end + # end + # end + end + + describe "if/unless" do + test "if true condition" do + 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 + assert result == nil + end + + test "if else condition" do + result = if nil do + "This won't be seen" + else + "This will" + end + assert result == "This will" + end + + # test "variable scoping in if" do + # x = 1 + # if true do + # x = x + 1 + # end + # assert x == 1 + # end + + test "return value from if to change value" do + x = 1 + 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 + 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 + 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 + assert result == "1 is considered as true" + end + end +end From 6015c160910ea2917f32bc70a6296eb5a13362da Mon Sep 17 00:00:00 2001 From: jbiset Date: Mon, 29 Jul 2024 16:50:13 -0300 Subject: [PATCH 2/3] Format is applied and GHA test is added --- .github/workflows/ci.yml | 32 ++++++ test/elixir_basic_types_test.exs | 11 ++- test/elixir_case_cond_if_test.exs | 136 ++++++++++++++++---------- test/elixir_lists_and_tuples_test.exs | 12 +-- 4 files changed, 127 insertions(+), 64 deletions(-) create mode 100644 .github/workflows/ci.yml 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 From c3a6a93cc2fedacba4e18b8392d0982d50033362 Mon Sep 17 00:00:00 2001 From: jbiset Date: Mon, 29 Jul 2024 16:58:16 -0300 Subject: [PATCH 3/3] Changed the actions used in GHA --- .github/workflows/ci.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d10f2b8..a8c2594 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -14,12 +14,12 @@ jobs: steps: - name: Checkout code - uses: actions/checkout@v2 + uses: actions/checkout@v3 - name: Set up Elixir - uses: actions/setup-elixir@v1 + uses: erlef/setup-beam@v1 with: - otp-version: "26.x" + otp-version: "25.x" elixir-version: "1.16.x" - name: Install dependencies