Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Elixir case, cond and if test #4

Merged
merged 3 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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@v3

- name: Set up Elixir
uses: erlef/setup-beam@v1
with:
otp-version: "25.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
157 changes: 157 additions & 0 deletions test/elixir_case_cond_if_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
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
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
Loading