Skip to content
This repository was archived by the owner on Mar 1, 2026. It is now read-only.

Commit 8875fcb

Browse files
committed
test helper has been refactored to use a glob pattern instead of hardcoding each test.
1 parent 3f1a344 commit 8875fcb

3 files changed

Lines changed: 52 additions & 68 deletions

File tree

README.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,26 @@ None of the code in this repository has solutions written by AI. You'll just hav
4343

4444
My approach to using LLM's and AI is to treat them like a teaching assistant who knows a lot but isn't always right. They're a great tool for learning and helping you understand a thing, but they are not (_in my humble opinion_) suitable for production output in most contexts.
4545

46-
Also, this is about learning and using LLM's for solutions doesn't help me - at all. It only pushes the barrier to learning further away from me.
46+
Also, this is about learning and using LLM's for solutions doesn't help me - at all. It only pushes the barrier to learning further away from me.
47+
48+
49+
## Running Tests
50+
51+
### Elixir
52+
53+
```
54+
mix test w*
55+
```
56+
57+
### Go
58+
59+
```bash
60+
cd w1
61+
go test -v
62+
```
63+
64+
### TypeScript
65+
66+
```bash
67+
pnpm vitest run
68+
```

test/test_helper.exs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
1-
defmodule Hackerrank.Tests.Helper do
2-
3-
1+
defmodule Hackerrank.Test.Helper do
2+
@moduledoc """
3+
A set of utilities for testing the Elixir HackerRank challenges
4+
"""
45

56
@doc """
6-
Load the contents of a file in a list of lines suitable
7-
for parsing by specific functions in each challenges module
7+
Loads and parses all test cases matching the given glob pattern.
8+
9+
For each file, applies the provided argument and expected result functions,
10+
returning a list of {args, expected} tuples ready for testing.
811
"""
9-
def load_test_case(tc) do
10-
IO.inspect File.cwd!
11-
File.read!(tc)
12-
|> String.split("\n")
13-
|> Enum.split_while(&(&1 != ""))
12+
def test_all(pattern, arg_fn, exp_fn) do
13+
Path.wildcard(pattern)
14+
|> Enum.map( fn tc -> get_args_expected(tc, arg_fn, exp_fn) end)
1415
end
1516

1617
@doc """
1718
get_args_expected returnsUsing the module attribute @test_cases this function will
1819
return a list of test cases that are not in @skip_tests
1920
"""
20-
def get_args_expected(tc) do
21+
def get_args_expected(tc, arg_fn, exp_fn) do
2122
# get the args and the expected data - it's raw, first expected can be dropped
2223
# as it is a blank line that delineates args from expected data.
2324

@@ -29,14 +30,14 @@ defmodule Hackerrank.Tests.Helper do
2930
|> Enum.split_while(&(&1 != ""))
3031

3132
# parse the args
32-
# args = parse_args(args)
33+
args = arg_fn.(args)
3334

3435
# parse expedted
35-
# expected = parse_expected(expected)
36+
expected = exp_fn.(expected)
3637

3738
# and done
38-
[args, expected]
39+
# [args, expected]
40+
{tc, args, expected}
3941
end
4042

41-
4243
end

w1/plus_minus_test.exs

Lines changed: 13 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
1-
defmodule Hackerank.W1.PlusMinus do
1+
defmodule Hackerank.W1.PlusMinusTest do
22

33
use ExUnit.Case
4-
alias Hackerrank.Tests.Helper
4+
alias Hackerrank.Test.Helper
55
alias Hackerrank.W1.PlusMinus
66

7-
defp parse_args(raw) do
7+
@pattern "w1/tc/1_*.txt"
8+
9+
test "run the tests" do
10+
Helper.test_all(@pattern, &parse_args/1, &parse_expected/1 )
11+
|> Enum.each(fn {_label, args, expected} ->
12+
assert PlusMinus.challenge(args) == expected
13+
end)
14+
end
15+
16+
def parse_args(raw) do
817
# the first arg isn't passed into the function, drop that
918
[_, raw] = raw
1019

@@ -13,57 +22,9 @@ defmodule Hackerank.W1.PlusMinus do
1322
|> Enum.map(&String.to_integer/1)
1423
end
1524

16-
17-
defp parse_expected(raw) do
25+
def parse_expected(raw) do
1826
# in this challange the raw data is already correct - a list of floaty-strings
1927
raw
2028
end
2129

22-
test "tc/1_0.txt" do
23-
# get the raw data from the file
24-
[raw_args, raw_expected] = Helper.get_args_expected("w1/tc/1_0.txt")
25-
26-
# now parse them into the data we need for this challenge
27-
args = parse_args(raw_args)
28-
expected = parse_expected(raw_expected)
29-
30-
# run the challenge
31-
result = PlusMinus.challenge(args)
32-
33-
# did it match what it should?
34-
assert result == expected
35-
36-
end
37-
test "tc/1_1.txt" do
38-
# get the raw data from the file
39-
[raw_args, raw_expected] = Helper.get_args_expected("w1/tc/1_1.txt")
40-
41-
# now parse them into the data we need for this challenge
42-
args = parse_args(raw_args)
43-
expected = parse_expected(raw_expected)
44-
45-
# run the challenge
46-
result = PlusMinus.challenge(args)
47-
48-
# did it match what it should?
49-
assert result == expected
50-
51-
end
52-
53-
test "tc/1_2.txt" do
54-
# get the raw data from the file
55-
[raw_args, raw_expected] = Helper.get_args_expected("w1/tc/1_2.txt")
56-
57-
# now parse them into the data we need for this challenge
58-
args = parse_args(raw_args)
59-
expected = parse_expected(raw_expected)
60-
61-
# run the challenge
62-
result = PlusMinus.challenge(args)
63-
64-
# did it match what it should?
65-
assert result == expected
66-
67-
end
68-
6930
end

0 commit comments

Comments
 (0)