Skip to content

Commit cd05d1e

Browse files
committed
Fix tests, update module doc
1 parent f0d0f4d commit cd05d1e

File tree

3 files changed

+111
-5
lines changed

3 files changed

+111
-5
lines changed

lib/string_matcher.ex

+11-3
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,22 @@ defmodule StringMatcher do
1919
2020
```
2121
StringMatcher.new()
22-
|> StringMatcher.add_regexp(~r/Del\s+(?<episode_num>[0-9]+?)\s+av\s+(?<of_episodes>[0-9]+?)/i, %{})
23-
|> StringMatcher.add_regexp(~r/Originaltitel:\s+(?<original_title>.*?)/i, %{})
24-
|> StringMatcher.add_regexp(~r/Produktion:\s+(?<producer>.*?) (?<episode_num>[0-9]+?)/i, %{})
22+
|> StringMatcher.add_regexp(
23+
~r/Del\s+(?<episode_num>[0-9]+?)\s+av\s+(?<of_episodes>[0-9]+?)/i,
24+
%{}
25+
)
26+
|> StringMatcher.add_regexp(~r/Originaltitel: (?<original_title>.*)\./i, %{})
27+
|> StringMatcher.add_regexp(
28+
~r/Produktion: (?<production_company>.*?) (?<production_year>[0-9]+)\./i,
29+
%{}
30+
)
2531
|> StringMatcher.match_captures(string)
2632
```
2733
2834
This should return a tuple with a map. The map is returned value of the regular expressions.
2935
If no match is found you will receive `{:error, "no match"}`
36+
37+
Please take a look at our tests to see a working variant of parsing the text above.
3038
"""
3139

3240
@doc """

mix.exs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ defmodule StringMatcher.MixProject do
22
use Mix.Project
33

44
@name :string_matcher
5-
@version "0.1.2"
5+
@version "0.1.3"
66
@deps [
77
{:ex_doc, ">= 0.0.0", only: :dev, runtime: false}
88
]

test/string_matcher_test.exs

+99-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,102 @@
11
defmodule StringMatcherTest do
22
use ExUnit.Case
3-
doctest StringMatcher
3+
4+
test "returns an empty list" do
5+
assert StringMatcher.new() === []
6+
end
7+
8+
test "adding a regex returns a list of regex" do
9+
# assert StringMatcher.add_regexp([], ~r/S(?<season_num>\d+)E(?<episode_num>\d+)/i, %{}) === [
10+
# {~r/S(?<season_num>\d+)E(?<episode_num>\d+)/i, %{}}
11+
# ]
12+
end
13+
14+
test "can parse a long string" do
15+
string = """
16+
Del 5 av 6. Shakespeare är mycket nöjd med sin senaste pjäs, Så tuktas en argbigga. Men av någon anledning uppskattas inte berättelsen om hur en stark kvinna förnedras av en man av kvinnorna i Shakespeares närhet.\n
17+
\n
18+
Originaltitel: Upstart Crow.\n
19+
Produktion: BBC 2017.
20+
"""
21+
22+
## Lets run through the splitted string and capture the wanted details
23+
## and then merge them into a single map.
24+
result =
25+
String.split(string, "\n")
26+
|> Enum.map(fn string ->
27+
StringMatcher.new()
28+
|> StringMatcher.add_regexp(
29+
~r/Del\s+(?<episode_num>[0-9]+?)\s+av\s+(?<of_episodes>[0-9]+?)/i,
30+
%{}
31+
)
32+
|> StringMatcher.add_regexp(~r/Originaltitel: (?<original_title>.*)\./i, %{})
33+
|> StringMatcher.add_regexp(
34+
~r/Produktion: (?<production_company>.*?) (?<production_year>[0-9]+)\./i,
35+
%{}
36+
)
37+
|> StringMatcher.match_captures(string)
38+
|> case do
39+
{:ok, map} -> map
40+
_ -> %{}
41+
end
42+
end)
43+
|> Enum.reduce(%{}, &Map.merge/2)
44+
45+
assert result === %{
46+
"episode_num" => "5",
47+
"of_episodes" => "6",
48+
"original_title" => "Upstart Crow",
49+
"production_company" => "BBC",
50+
"production_year" => "2017"
51+
}
52+
end
53+
54+
test "can parse with a single regex" do
55+
result =
56+
StringMatcher.new()
57+
|> StringMatcher.add_regexp(~r/S(?<season_num>\d+)E(?<episode_num>\d+)/i, %{})
58+
|> StringMatcher.match("Prison Break E01")
59+
60+
assert result === {:error, "no match"}
61+
end
62+
63+
test "can match captures with a single regex" do
64+
result =
65+
StringMatcher.new()
66+
|> StringMatcher.add_regexp(~r/S(?<season_num>\d+)E(?<episode_num>\d+)/i, %{})
67+
|> StringMatcher.match_captures("Prison Break S01E01")
68+
69+
assert result === {:ok, %{"episode_num" => "01", "season_num" => "01"}}
70+
end
71+
72+
test "returns the custom specified value" do
73+
result =
74+
StringMatcher.new()
75+
|> StringMatcher.add_regexp(~r/S(?<season_num>\d+)E(?<episode_num>\d+)/i, %{
76+
"name" => "Fargo"
77+
})
78+
|> StringMatcher.match("Prison Break S01E01")
79+
80+
assert result === {:ok, %{"name" => "Fargo"}}
81+
end
82+
83+
test "can capture matchings" do
84+
result =
85+
StringMatcher.new()
86+
|> StringMatcher.add_regexp(~r/S(?<season_num>\d+)E(?<episode_num>\d+)/i, %{})
87+
|> StringMatcher.match_captures("Prison Break S01E01")
88+
89+
assert result === {:ok, %{"episode_num" => "01", "season_num" => "01"}}
90+
end
91+
92+
test "if passing a custom value to match captures we use the custom value" do
93+
result =
94+
StringMatcher.new()
95+
|> StringMatcher.add_regexp(~r/S(?<season_num>\d+)E(?<episode_num>\d+)/i, %{
96+
"name" => "Fargo"
97+
})
98+
|> StringMatcher.match_captures("Prison Break S01E01")
99+
100+
assert result === {:ok, %{"name" => "Fargo"}}
101+
end
4102
end

0 commit comments

Comments
 (0)