From cd05d1e4381df648859c79fe66fad2e1dab16946 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20Nyl=C3=A9n?= Date: Sun, 29 Nov 2020 04:26:23 +0100 Subject: [PATCH] Fix tests, update module doc --- lib/string_matcher.ex | 14 +++-- mix.exs | 2 +- test/string_matcher_test.exs | 100 ++++++++++++++++++++++++++++++++++- 3 files changed, 111 insertions(+), 5 deletions(-) diff --git a/lib/string_matcher.ex b/lib/string_matcher.ex index 71846e6..4ff1571 100644 --- a/lib/string_matcher.ex +++ b/lib/string_matcher.ex @@ -19,14 +19,22 @@ defmodule StringMatcher do ``` StringMatcher.new() - |> StringMatcher.add_regexp(~r/Del\s+(?[0-9]+?)\s+av\s+(?[0-9]+?)/i, %{}) - |> StringMatcher.add_regexp(~r/Originaltitel:\s+(?.*?)/i, %{}) - |> StringMatcher.add_regexp(~r/Produktion:\s+(?.*?) (?[0-9]+?)/i, %{}) + |> StringMatcher.add_regexp( + ~r/Del\s+(?[0-9]+?)\s+av\s+(?[0-9]+?)/i, + %{} + ) + |> StringMatcher.add_regexp(~r/Originaltitel: (?.*)\./i, %{}) + |> StringMatcher.add_regexp( + ~r/Produktion: (?.*?) (?[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 """ diff --git a/mix.exs b/mix.exs index 890bd07..3a2f9f4 100644 --- a/mix.exs +++ b/mix.exs @@ -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} ] diff --git a/test/string_matcher_test.exs b/test/string_matcher_test.exs index 4f5f56c..47f5ea5 100644 --- a/test/string_matcher_test.exs +++ b/test/string_matcher_test.exs @@ -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(?\d+)E(?\d+)/i, %{}) === [ + # {~r/S(?\d+)E(?\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+(?[0-9]+?)\s+av\s+(?[0-9]+?)/i, + %{} + ) + |> StringMatcher.add_regexp(~r/Originaltitel: (?.*)\./i, %{}) + |> StringMatcher.add_regexp( + ~r/Produktion: (?.*?) (?[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(?\d+)E(?\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(?\d+)E(?\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(?\d+)E(?\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(?\d+)E(?\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(?\d+)E(?\d+)/i, %{ + "name" => "Fargo" + }) + |> StringMatcher.match_captures("Prison Break S01E01") + + assert result === {:ok, %{"name" => "Fargo"}} + end end