Skip to content

Commit 6d1eb56

Browse files
committed
added erlang ast test in captains-log
1 parent 72ee0b7 commit 6d1eb56

11 files changed

Lines changed: 540 additions & 397 deletions

File tree

lib/elixir_analyzer/constants.ex

Lines changed: 233 additions & 233 deletions
Large diffs are not rendered by default.
Lines changed: 73 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,73 @@
1-
defmodule ElixirAnalyzer.TestSuite.CaptainsLog do
2-
@moduledoc """
3-
This is an exercise analyzer extension module for the concept exercise Captains Log
4-
"""
5-
use ElixirAnalyzer.ExerciseTest
6-
alias ElixirAnalyzer.Constants
7-
8-
assert_call "random_planet_class uses Enum.random" do
9-
type :essential
10-
calling_fn module: CaptainsLog, name: :random_planet_class
11-
called_fn module: Enum, name: :random
12-
comment Constants.captains_log_use_enum_random()
13-
end
14-
15-
assert_call "random_ship_registry_number uses Enum.random" do
16-
type :essential
17-
calling_fn module: CaptainsLog, name: :random_ship_registry_number
18-
called_fn module: Enum, name: :random
19-
comment Constants.captains_log_use_enum_random()
20-
end
21-
22-
assert_no_call "random_stardate does not use Enum.random" do
23-
type :essential
24-
calling_fn module: CaptainsLog, name: :random_stardate
25-
called_fn module: Enum, name: :random
26-
comment Constants.captains_log_do_not_use_enum_random()
27-
end
28-
29-
assert_no_call "random_stardate does not use :rand.uniform_real" do
30-
type :essential
31-
calling_fn module: CaptainsLog, name: :random_stardate
32-
called_fn module: :rand, name: :uniform_real
33-
comment Constants.captains_log_do_not_use_rand_uniform_real()
34-
end
35-
36-
assert_call "random_stardate uses :rand.uniform" do
37-
type :essential
38-
calling_fn module: CaptainsLog, name: :random_stardate
39-
called_fn module: :rand, name: :uniform
40-
comment Constants.captains_log_use_rand_uniform()
41-
suppress_if Constants.solution_deprecated_random_module(), :fail
42-
suppress_if "random_stardate does not use :rand.uniform_real", :fail
43-
suppress_if "random_stardate does not use Enum.random", :fail
44-
end
45-
46-
assert_call "format_stardate uses :io_lib" do
47-
type :essential
48-
calling_fn module: CaptainsLog, name: :format_stardate
49-
called_fn module: :io_lib, name: :_
50-
comment Constants.captains_log_use_io_lib()
51-
end
52-
end
1+
defmodule ElixirAnalyzer.TestSuite.CaptainsLog do
2+
@moduledoc """
3+
This is an exercise analyzer extension module for the concept exercise Captains Log
4+
"""
5+
use ElixirAnalyzer.ExerciseTest
6+
alias ElixirAnalyzer.Constants
7+
alias ElixirAnalyzer.Source
8+
9+
assert_call "random_planet_class uses Enum.random" do
10+
type :essential
11+
calling_fn module: CaptainsLog, name: :random_planet_class
12+
called_fn module: Enum, name: :random
13+
comment Constants.captains_log_use_enum_random()
14+
end
15+
16+
assert_call "random_ship_registry_number uses Enum.random" do
17+
type :essential
18+
calling_fn module: CaptainsLog, name: :random_ship_registry_number
19+
called_fn module: Enum, name: :random
20+
comment Constants.captains_log_use_enum_random()
21+
end
22+
23+
assert_no_call "random_stardate does not use Enum.random" do
24+
type :essential
25+
calling_fn module: CaptainsLog, name: :random_stardate
26+
called_fn module: Enum, name: :random
27+
comment Constants.captains_log_do_not_use_enum_random()
28+
end
29+
30+
assert_no_call "random_stardate does not use :rand.uniform_real" do
31+
type :essential
32+
calling_fn module: CaptainsLog, name: :random_stardate
33+
called_fn module: :rand, name: :uniform_real
34+
comment Constants.captains_log_do_not_use_rand_uniform_real()
35+
end
36+
37+
assert_call "random_stardate uses :rand.uniform" do
38+
type :essential
39+
calling_fn module: CaptainsLog, name: :random_stardate
40+
called_fn module: :rand, name: :uniform
41+
comment Constants.captains_log_use_rand_uniform()
42+
suppress_if Constants.solution_deprecated_random_module(), :fail
43+
suppress_if "random_stardate does not use :rand.uniform_real", :fail
44+
suppress_if "random_stardate does not use Enum.random", :fail
45+
end
46+
47+
48+
check_source "format_stardate uses erlang" do
49+
type :essential
50+
comment Constants.captains_log_use_erlang()
51+
52+
check(%Source{code_ast: code_ast}) do
53+
{_, erlang?} =
54+
Macro.prewalk(code_ast, false, fn node, acc ->
55+
case node do
56+
# usage :io_lib.format/2
57+
{{:., _, [:io_lib, :format]}, _, _} ->
58+
{node, true}
59+
60+
# usage :erlang.function_name/arity
61+
# matches any function call from :erlang module
62+
{{:., _, [:erlang, _]}, _, _} ->
63+
{node, true}
64+
65+
_ ->
66+
{node, acc}
67+
end
68+
end)
69+
70+
erlang?
71+
end
72+
end
73+
end
Lines changed: 112 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -1,112 +1,112 @@
1-
defmodule ElixirAnalyzer.ExerciseTest.CaptainsLogTest do
2-
use ElixirAnalyzer.ExerciseTestCase,
3-
exercise_test_module: ElixirAnalyzer.TestSuite.CaptainsLog
4-
5-
alias ElixirAnalyzer.Constants
6-
7-
test_exercise_analysis "example solution",
8-
comments: [Constants.solution_same_as_exemplar()] do
9-
defmodule CaptainsLog do
10-
@planetary_classes ["D", "H", "J", "K", "L", "M", "N", "R", "T", "Y"]
11-
12-
def random_planet_class() do
13-
Enum.random(@planetary_classes)
14-
end
15-
16-
def random_ship_registry_number() do
17-
number = Enum.random(1000..9999)
18-
"NCC-#{number}"
19-
end
20-
21-
def random_stardate() do
22-
:rand.uniform() * 1000 + 41_000
23-
end
24-
25-
def format_stardate(stardate) do
26-
to_string(:io_lib.format("~.1f", [stardate]))
27-
end
28-
end
29-
end
30-
31-
describe "Expects random_planet_class and random_ship_registry_number to use Enum.random" do
32-
test_exercise_analysis "random_planet_class does not use Enum.random",
33-
comments_include: [Constants.captains_log_use_enum_random()] do
34-
defmodule CaptainsLog do
35-
@planetary_classes ["D", "H", "J", "K", "L", "M", "N", "R", "T", "Y"]
36-
37-
def random_planet_class() do
38-
@planetary_classes
39-
|> Enum.shuffle()
40-
|> hd
41-
end
42-
end
43-
end
44-
45-
test_exercise_analysis "random_ship_registry_number does not use Enum.random",
46-
comments_include: [Constants.captains_log_use_enum_random()] do
47-
defmodule CaptainsLog do
48-
def random_ship_registry_number() do
49-
number =
50-
1000..9999
51-
|> Enum.shuffle()
52-
|> hd
53-
54-
"NCC-#{number}"
55-
end
56-
end
57-
end
58-
end
59-
60-
describe "random_stardate uses :rand.uniform" do
61-
test_exercise_analysis "when using Enum.random instead",
62-
comments_include: [
63-
Constants.captains_log_do_not_use_enum_random()
64-
],
65-
comments_exclude: [
66-
Constants.captains_log_use_rand_uniform()
67-
] do
68-
defmodule CaptainsLog do
69-
def random_stardate do
70-
Enum.random(4_100_000..4_200_000) |> Kernel./(100)
71-
end
72-
end
73-
end
74-
75-
test_exercise_analysis "when using :rand.uniform_real instead",
76-
comments_include: [
77-
Constants.captains_log_do_not_use_rand_uniform_real()
78-
],
79-
comments_exclude: [
80-
Constants.captains_log_use_rand_uniform()
81-
] do
82-
defmodule CaptainsLog do
83-
def random_stardate do
84-
:rand.uniform_real() * 1000 + 41_000
85-
end
86-
end
87-
end
88-
89-
test_exercise_analysis "when using deprecated :random module",
90-
comments_include: [Constants.solution_deprecated_random_module()],
91-
comments_exclude: [Constants.captains_log_use_rand_uniform()] do
92-
defmodule CaptainsLog do
93-
def random_stardate() do
94-
:random.uniform() * 1000 + 41_000
95-
end
96-
end
97-
end
98-
end
99-
100-
test_exercise_analysis "format_stardate uses Float.round",
101-
comments_include: [Constants.captains_log_use_io_lib()] do
102-
defmodule CaptainsLog do
103-
def format_stardate(stardate) do
104-
if is_float(stardate) do
105-
Float.round(stardate, 1) |> to_string()
106-
else
107-
raise ArgumentError
108-
end
109-
end
110-
end
111-
end
112-
end
1+
defmodule ElixirAnalyzer.ExerciseTest.CaptainsLogTest do
2+
use ElixirAnalyzer.ExerciseTestCase,
3+
exercise_test_module: ElixirAnalyzer.TestSuite.CaptainsLog
4+
5+
alias ElixirAnalyzer.Constants
6+
7+
test_exercise_analysis "example solution",
8+
comments: [Constants.solution_same_as_exemplar()] do
9+
defmodule CaptainsLog do
10+
@planetary_classes ["D", "H", "J", "K", "L", "M", "N", "R", "T", "Y"]
11+
12+
def random_planet_class() do
13+
Enum.random(@planetary_classes)
14+
end
15+
16+
def random_ship_registry_number() do
17+
number = Enum.random(1000..9999)
18+
"NCC-#{number}"
19+
end
20+
21+
def random_stardate() do
22+
:rand.uniform() * 1000 + 41_000
23+
end
24+
25+
def format_stardate(stardate) do
26+
to_string(:io_lib.format("~.1f", [stardate]))
27+
end
28+
end
29+
end
30+
31+
describe "Expects random_planet_class and random_ship_registry_number to use Enum.random" do
32+
test_exercise_analysis "random_planet_class does not use Enum.random",
33+
comments_include: [Constants.captains_log_use_enum_random()] do
34+
defmodule CaptainsLog do
35+
@planetary_classes ["D", "H", "J", "K", "L", "M", "N", "R", "T", "Y"]
36+
37+
def random_planet_class() do
38+
@planetary_classes
39+
|> Enum.shuffle()
40+
|> hd
41+
end
42+
end
43+
end
44+
45+
test_exercise_analysis "random_ship_registry_number does not use Enum.random",
46+
comments_include: [Constants.captains_log_use_enum_random()] do
47+
defmodule CaptainsLog do
48+
def random_ship_registry_number() do
49+
number =
50+
1000..9999
51+
|> Enum.shuffle()
52+
|> hd
53+
54+
"NCC-#{number}"
55+
end
56+
end
57+
end
58+
end
59+
60+
describe "random_stardate uses :rand.uniform" do
61+
test_exercise_analysis "when using Enum.random instead",
62+
comments_include: [
63+
Constants.captains_log_do_not_use_enum_random()
64+
],
65+
comments_exclude: [
66+
Constants.captains_log_use_rand_uniform()
67+
] do
68+
defmodule CaptainsLog do
69+
def random_stardate do
70+
Enum.random(4_100_000..4_200_000) |> Kernel./(100)
71+
end
72+
end
73+
end
74+
75+
test_exercise_analysis "when using :rand.uniform_real instead",
76+
comments_include: [
77+
Constants.captains_log_do_not_use_rand_uniform_real()
78+
],
79+
comments_exclude: [
80+
Constants.captains_log_use_rand_uniform()
81+
] do
82+
defmodule CaptainsLog do
83+
def random_stardate do
84+
:rand.uniform_real() * 1000 + 41_000
85+
end
86+
end
87+
end
88+
89+
test_exercise_analysis "when using deprecated :random module",
90+
comments_include: [Constants.solution_deprecated_random_module()],
91+
comments_exclude: [Constants.captains_log_use_rand_uniform()] do
92+
defmodule CaptainsLog do
93+
def random_stardate() do
94+
:random.uniform() * 1000 + 41_000
95+
end
96+
end
97+
end
98+
end
99+
100+
test_exercise_analysis "format_stardate uses Float.round",
101+
comments_include: [Constants.captains_log_use_erlang()] do
102+
defmodule CaptainsLog do
103+
def format_stardate(stardate) do
104+
if is_float(stardate) do
105+
Float.round(stardate, 1) |> to_string()
106+
else
107+
raise ArgumentError
108+
end
109+
end
110+
end
111+
end
112+
end
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"authors": [
3+
"angelikatyborska"
4+
],
5+
"contributors": [
6+
"neenjaw"
7+
],
8+
"files": {
9+
"solution": [
10+
"lib/captains_log.ex"
11+
],
12+
"test": [
13+
"test/captains_log_test.exs"
14+
],
15+
"exemplar": [
16+
".meta/exemplar.ex"
17+
]
18+
},
19+
"blurb": "Learn about randomness and using Erlang libraries from Elixir by helping Mary generate stardates and starship registry numbers for her Star Trek themed pen-and-paper role playing sessions."
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
defmodule CaptainsLog do
2+
@planetary_classes ["D", "H", "J", "K", "L", "M", "N", "R", "T", "Y"]
3+
4+
def random_planet_class() do
5+
Enum.random(@planetary_classes)
6+
end
7+
8+
def random_ship_registry_number() do
9+
number = Enum.random(1000..9999)
10+
"NCC-#{number}"
11+
end
12+
13+
def random_stardate() do
14+
:rand.uniform() * 1000 + 41_000
15+
end
16+
17+
def format_stardate(stardate) do
18+
to_string(:io_lib.format("~.1f", [stardate]))
19+
end
20+
end
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"summary":"Check the comments for things to fix. 🛠","comments":[{"type":"essential","comment":"elixir.captains-log.use_erlang"},{"type":"informative","params":{"mentoring_request_url":"https://exercism.org/tracks/elixir/exercises/captains-log/mentor_discussions"},"comment":"elixir.general.feedback_request"}]}

0 commit comments

Comments
 (0)