String validation with Regex and ExUnit

If your sample strings are constants, you could put them in a module attribute:

@sample_strings %{
  sample_str1: "test_string",
  sample_str2: "cz-dev",
  sample_str3: "new___cczdj",
  sample_str4: "czar,,,nic",
  ...
}

General comment on input_length_valid: there are only four functions that take a single boolean value and return a single boolean value (constant true, constant false, identity, and invert) - whenever you find yourself writing a case with booleans on both sides of the ->s, figure out which one of the four you have and remove the case.

In this case you have “invert” - so input_length_valid simplifies to not(String.length(inp) > 100) or even just String.length(inp) <= 100


As to the test itself, unless there are a LOT of cases in @sample_strings you may find a single test easier to write. For instance, this is a very simple approach:

defp regex_matches?(sample) do
  sample
  |> String.replace("_", "-")
  |> String.match?(~r/^(my-regex)$/)
end

test "sample strings match" do
  Enum.each(@sample_strings, fn {_label, sample} ->
    assert regex_matches?(sample)
    assert input_length_valid(sample)
  end)
end

One downside of this approach is that it will fail the test on the first string that doesn’t match; if you want to see all the failures in a single run you could accumulate them explicitly:

test "sample strings match" do
  failed_matches =
    Enum.reject(@sample_strings, fn {_label, sample} ->
      regex_matches?(sample)
    end)

  assert [] == failed_matches

  # similar for input_length_valid
end

The main thing I like to keep in mind when structuring asserts like these: what do I want to learn when one fails?