Skip to content

Commit

Permalink
Fix tests, update module doc
Browse files Browse the repository at this point in the history
  • Loading branch information
jnylen committed Nov 29, 2020
1 parent f0d0f4d commit cd05d1e
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 5 deletions.
14 changes: 11 additions & 3 deletions lib/string_matcher.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,22 @@ defmodule StringMatcher do
```
StringMatcher.new()
|> StringMatcher.add_regexp(~r/Del\s+(?<episode_num>[0-9]+?)\s+av\s+(?<of_episodes>[0-9]+?)/i, %{})
|> StringMatcher.add_regexp(~r/Originaltitel:\s+(?<original_title>.*?)/i, %{})
|> StringMatcher.add_regexp(~r/Produktion:\s+(?<producer>.*?) (?<episode_num>[0-9]+?)/i, %{})
|> StringMatcher.add_regexp(
~r/Del\s+(?<episode_num>[0-9]+?)\s+av\s+(?<of_episodes>[0-9]+?)/i,
%{}
)
|> StringMatcher.add_regexp(~r/Originaltitel: (?<original_title>.*)\./i, %{})
|> StringMatcher.add_regexp(
~r/Produktion: (?<production_company>.*?) (?<production_year>[0-9]+)\./i,
%{}
)
|> StringMatcher.match_captures(string)
```
This should return a tuple with a map. The map is returned value of the regular expressions.
If no match is found you will receive `{:error, "no match"}`
Please take a look at our tests to see a working variant of parsing the text above.
"""

@doc """
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule StringMatcher.MixProject do
use Mix.Project

@name :string_matcher
@version "0.1.2"
@version "0.1.3"
@deps [
{:ex_doc, ">= 0.0.0", only: :dev, runtime: false}
]
Expand Down
100 changes: 99 additions & 1 deletion test/string_matcher_test.exs
Original file line number Diff line number Diff line change
@@ -1,4 +1,102 @@
defmodule StringMatcherTest do
use ExUnit.Case
doctest StringMatcher

test "returns an empty list" do
assert StringMatcher.new() === []
end

test "adding a regex returns a list of regex" do
# assert StringMatcher.add_regexp([], ~r/S(?<season_num>\d+)E(?<episode_num>\d+)/i, %{}) === [
# {~r/S(?<season_num>\d+)E(?<episode_num>\d+)/i, %{}}
# ]
end

test "can parse a long string" do
string = """
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
\n
Originaltitel: Upstart Crow.\n
Produktion: BBC 2017.
"""

## Lets run through the splitted string and capture the wanted details
## and then merge them into a single map.
result =
String.split(string, "\n")
|> Enum.map(fn string ->
StringMatcher.new()
|> StringMatcher.add_regexp(
~r/Del\s+(?<episode_num>[0-9]+?)\s+av\s+(?<of_episodes>[0-9]+?)/i,
%{}
)
|> StringMatcher.add_regexp(~r/Originaltitel: (?<original_title>.*)\./i, %{})
|> StringMatcher.add_regexp(
~r/Produktion: (?<production_company>.*?) (?<production_year>[0-9]+)\./i,
%{}
)
|> StringMatcher.match_captures(string)
|> case do
{:ok, map} -> map
_ -> %{}
end
end)
|> Enum.reduce(%{}, &Map.merge/2)

assert result === %{
"episode_num" => "5",
"of_episodes" => "6",
"original_title" => "Upstart Crow",
"production_company" => "BBC",
"production_year" => "2017"
}
end

test "can parse with a single regex" do
result =
StringMatcher.new()
|> StringMatcher.add_regexp(~r/S(?<season_num>\d+)E(?<episode_num>\d+)/i, %{})
|> StringMatcher.match("Prison Break E01")

assert result === {:error, "no match"}
end

test "can match captures with a single regex" do
result =
StringMatcher.new()
|> StringMatcher.add_regexp(~r/S(?<season_num>\d+)E(?<episode_num>\d+)/i, %{})
|> StringMatcher.match_captures("Prison Break S01E01")

assert result === {:ok, %{"episode_num" => "01", "season_num" => "01"}}
end

test "returns the custom specified value" do
result =
StringMatcher.new()
|> StringMatcher.add_regexp(~r/S(?<season_num>\d+)E(?<episode_num>\d+)/i, %{
"name" => "Fargo"
})
|> StringMatcher.match("Prison Break S01E01")

assert result === {:ok, %{"name" => "Fargo"}}
end

test "can capture matchings" do
result =
StringMatcher.new()
|> StringMatcher.add_regexp(~r/S(?<season_num>\d+)E(?<episode_num>\d+)/i, %{})
|> StringMatcher.match_captures("Prison Break S01E01")

assert result === {:ok, %{"episode_num" => "01", "season_num" => "01"}}
end

test "if passing a custom value to match captures we use the custom value" do
result =
StringMatcher.new()
|> StringMatcher.add_regexp(~r/S(?<season_num>\d+)E(?<episode_num>\d+)/i, %{
"name" => "Fargo"
})
|> StringMatcher.match_captures("Prison Break S01E01")

assert result === {:ok, %{"name" => "Fargo"}}
end
end

0 comments on commit cd05d1e

Please sign in to comment.