Skip to content

Commit

Permalink
Format is applied and GHA test is added
Browse files Browse the repository at this point in the history
  • Loading branch information
jbiset committed Jul 29, 2024
1 parent c5d1cce commit 6015c16
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 64 deletions.
32 changes: 32 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -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
11 changes: 7 additions & 4 deletions test/elixir_basic_types_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
136 changes: 82 additions & 54 deletions test/elixir_case_cond_if_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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

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

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

Expand All @@ -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
Expand Down
12 changes: 6 additions & 6 deletions test/elixir_lists_and_tuples_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

0 comments on commit 6015c16

Please sign in to comment.