Skip to content

Add analysis for Leap #160

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

Merged
merged 3 commits into from
Sep 23, 2021
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
3 changes: 3 additions & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ config :elixir_analyzer,
"accumulate" => %{
analyzer_module: ElixirAnalyzer.TestSuite.Accumulate
},
"leap" => %{
analyzer_module: ElixirAnalyzer.TestSuite.Leap
},
"list-ops" => %{
analyzer_module: ElixirAnalyzer.TestSuite.ListOps
},
Expand Down
3 changes: 3 additions & 0 deletions lib/elixir_analyzer/constants.ex
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ defmodule ElixirAnalyzer.Constants do
# Lasagna Comments
lasagna_function_reuse: "elixir.lasagna.function_reuse",

# Leap Comments
leap_erlang_calendar: "elixir.leap.erlang_calendar",

# Log Level Comments
log_level_use_cond: "elixir.log-level.use_cond",

Expand Down
13 changes: 13 additions & 0 deletions lib/elixir_analyzer/test_suite/leap.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
defmodule ElixirAnalyzer.TestSuite.Leap do
@moduledoc """
This is an exercise analyzer extension module for the concept exercise Leap
"""

use ElixirAnalyzer.ExerciseTest

assert_no_call "does not call any functions from :calendar Erlang module" do
type :essential
called_fn module: :calendar, name: :_
comment ElixirAnalyzer.Constants.leap_erlang_calendar()
end
end
43 changes: 43 additions & 0 deletions test/elixir_analyzer/test_suite/leap_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
defmodule ElixirAnalyzer.TestSuite.LeapTest do
use ElixirAnalyzer.ExerciseTestCase,
exercise_test_module: ElixirAnalyzer.TestSuite.Leap

test_exercise_analysis "example solution",
comments: [] do
defmodule Year do
def leap_year?(year) do
div4?(year) && div100?(year) == div400?(year)
end

defp divides?(dividend, divisor), do: rem(dividend, divisor) == 0
defp div4?(dividend), do: divides?(dividend, 4)
defp div100?(dividend), do: divides?(dividend, 100)
defp div400?(dividend), do: divides?(dividend, 400)
end
end

test_exercise_analysis "forbids usage of :calendar Erlang module",
comments_include: [Constants.leap_erlang_calendar()] do
[
defmodule Year do
def leap_year?(year) do
:calendar.is_leap_year(year)
end
end,
defmodule Year do
import :calendar

def leap_year?(year) do
is_leap_year(year)
end
end,
defmodule Year do
alias :calendar, as: C

def leap_year?(year) do
C.is_leap_year(year)
end
end
]
end
end