|
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 |
0 commit comments