_Seyed
Hey folks,
I’ve written a regular expression for validating input strings. For testing purposes, I want to setup a scenario where a set of test vectors can be automatically injected as input into a new test and match against the regex. I’m currently testing each vector manually. I’ve written the code below. I was also thinking of using a file as opposed to hard coding the strings. Is there a nice way to this? Any feedback is much appreciated.
setup_all do
sample_strings = %{
sample_str1: "test_string",
sample_str2: "cz-dev",
sample_str3: "new___cczdj",
sample_str4: "czar,,,nic",
}
end
defp input_length_valid(inp) do
case String.length(inp) > 100 do
true -> false
false -> true
end
end
test "Regular expression for string - test #1", sample_strings
do
assert sample_strings.sample_str3
|> String.replace("_", "-")
|> String.match?( ~r/^(my-regex)*$/)
assert sample_strings.sample_str3 |> input_length_valid() == true
end
test "Regular expression for string - test #2", sample_strings
do
assert sample_strings.sample_str2
|> String.replace("_", "-")
|> String.match?( ~r/^(my-regex)*$/)
assert sample_strings.sample_str2 |> input_length_valid() == true
end
Trending in Questions
Hi all,
I’m working on a Phoenix project where I need to make external HTTP requests and I’m trying to choose between Req and HTTPoison....
New
I’m very new to Elixir, so sorry if this is a silly question:
I am wondering what happens to the mailbox of a process when the machine i...
New
How do I tell Hologram not to escape HTML when substituting into templates?
{Textile.render(@product.long_description)}
escapes the HTM...
New
Background
When trying to execute mix release on a Windows 11 machine for a Phoenix project I get the following error:
* assembling mark...
New
I’d like to create a file with 600 permissions (rw-------), but there’s a race condition if I create it and then change the permissions. ...
New
Is there a way to import record definitions in the iex shell for pretty printing (similar to the rr command in the erl shell)? This woul...
New
When I use Mix.install with a git dependency, using git@github.com:... how do I change the SSH identity used?
I’ve got two Github accoun...
New
Other Trending Topics
Hey there,
I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first.
...
New
Hi everyone :waving_hand:
I wanted to share something I’ve been building — NowBlind, a random chat app designed to help people discover ...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly)
AI dream has many faces, but general...
New
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #code-sync
- #discussion
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #umbrella
- #elixirconf-us
- #full-time-contract
- #podcasts-by-brainlid
- #blog-post
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #elixirconf-eu
- #processes
- #security
- #api
- #forms
- #performance











Showing Posts 1 to 6- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
al2o3cr
If your sample strings are constants, you could put them in a module attribute:
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 acasewith booleans on both sides of the->s, figure out which one of the four you have and remove thecase.In this case you have “invert” - so
input_length_validsimplifies tonot(String.length(inp) > 100)or even justString.length(inp) <= 100As to the test itself, unless there are a LOT of cases in
@sample_stringsyou may find a single test easier to write. For instance, this is a very simple approach: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:
The main thing I like to keep in mind when structuring asserts like these: what do I want to learn when one fails?
_Seyed
Hey Matt,
Thanks a lot for the feedback.
My goal is to provide a test bed for trying out regular expressions. I’m currently displaying the strings that fail either test with
IO.inspect.On another note, I was thinking of adding the length_check to the pipe in
regex_matches?()/1; since it returns a binary I don’t think it’s a good idea since I have to short circuit the pipeline. Would you recommend awithstatement?Thank you very much for your guidance.
al2o3cr
To be frank, I don’t understand the intent of either
input_length_validor theString.replacehere since the inputs are literals in the file - so it’s hard to make specific recommendations.You could use an
=clause for theString.replaceif you wanted to usewith._Seyed
In my particular use case, the input strings should have less than 100 characters. I think you are correct that the
replacestatement is a preprocessing step and should not be part of the test. Thank you for putting that into perspective.dimitarvp
As an alternative to @al2o3cr’s excellent answer, you can just code-generate test cases for each value so you have one failing test for each value that doesn’t match well:
Or you can draw out the heavy guns and use property-based testing (with some limitations of the input values so they are not infinite).
_Seyed
Thanks a lot @dimitarvp.