Skip to content

Commit

Permalink
Changed: functions do not generate ' & " anymore in Faker::Intern…
Browse files Browse the repository at this point in the history
…et (#200)

* Add private method to remove special characters

* Update CHANGELOG.md

* Remove unnecessary special characters

* Add tests for special character methods - Faker.Internet

* Update CHANGELOG.md

* Fix regex expression

* Use refute instead of assert
  • Loading branch information
vbrazo authored and Igor Kapkov committed Sep 27, 2018
1 parent 1e03383 commit ef4b56f
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Change log itself follows [Keep a CHANGELOG](http://keepachangelog.com) format.

### Changed

* Remove `'` and `"` from generated names in `Faker::Internet` [[@vbrazo][]]
* Codebase been reformated with elixir formatter [[@vbrazo][]] and [[@igas][]]
* Documentation moved to hexdocs.pm [[@MarcusSky][]], [[@vbrazo][]], and [[@igas][]]
* Updated dependencies [[@igas][]]
Expand Down
27 changes: 19 additions & 8 deletions lib/faker/internet.ex
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,15 @@ defmodule Faker.Internet do
def user_name, do: user_name(Faker.random_between(0, 1))

defp user_name(0) do
"#{Name.first_name() |> String.replace(~s( ), ~s()) |> String.downcase()}#{
Faker.random_between(1900, 2100)
}"
"#{
remove_special_characters(Name.first_name())
|> String.replace(~s( ), ~s())
|> String.downcase()
}#{Faker.random_between(1900, 2100)}"
end

defp user_name(1) do
[Name.first_name(), Name.last_name()]
[remove_special_characters(Name.first_name()), remove_special_characters(Name.last_name())]
|> Enum.map_join(Util.pick(~w(. _)), &String.replace(&1, ~s( ), ~s()))
|> String.downcase()
end
Expand All @@ -91,7 +93,12 @@ defmodule Faker.Internet do
"""
@spec domain_word() :: String.t()
def domain_word do
"#{Name.last_name() |> String.split(["'"]) |> Enum.join() |> String.downcase()}"
"#{
remove_special_characters(Name.last_name())
|> String.split(["'"])
|> Enum.join()
|> String.downcase()
}"
end

@doc """
Expand All @@ -110,7 +117,7 @@ defmodule Faker.Internet do
"""
@spec email() :: String.t()
def email do
"#{user_name()}@#{domain_name()}"
"#{remove_special_characters(user_name())}@#{domain_name()}"
end

@doc """
Expand All @@ -129,7 +136,7 @@ defmodule Faker.Internet do
"""
@spec free_email() :: String.t()
def free_email do
"#{user_name()}@#{free_email_service()}"
"#{remove_special_characters(user_name())}@#{free_email_service()}"
end

@doc """
Expand All @@ -148,7 +155,7 @@ defmodule Faker.Internet do
"""
@spec safe_email() :: String.t()
def safe_email do
"#{user_name()}@example.#{Util.pick(~w(org com net))}"
"#{remove_special_characters(user_name())}@example.#{Util.pick(~w(org com net))}"
end

@doc """
Expand Down Expand Up @@ -328,4 +335,8 @@ defmodule Faker.Internet do
end

defp localised_module, do: Module.concat(__MODULE__, Faker.mlocale())

defp remove_special_characters(string) do
String.replace(string, ~s('"), "")
end
end
44 changes: 44 additions & 0 deletions test/faker/internet_test.exs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
defmodule Faker.InternetTest do
use ExUnit.Case, async: true

import Faker.Internet

doctest Faker.Internet
doctest Faker.Internet.En
doctest Faker.Internet.Es
doctest Faker.Internet.PtBr

@iterations 10_000

test "ip_v4_address/0" do
assert Regex.match?(~r/^(\d+\.){3}\d+$/, Faker.Internet.ip_v4_address())
end
Expand Down Expand Up @@ -37,4 +41,44 @@ defmodule Faker.InternetTest do

assert Regex.match?(~r/\A((hello|world)-)(hello|world)\z/, slug)
end

test "user_name/0" do
Stream.repeatedly(&user_name/0)
|> Enum.take(@iterations)
|> Enum.each(fn generated_value ->
refute String.contains?(generated_value, ~s('"))
end)
end

test "email/0" do
Stream.repeatedly(&email/0)
|> Enum.take(@iterations)
|> Enum.each(fn generated_value ->
refute String.contains?(generated_value, ~s('"))
end)
end

test "safe_email/0" do
Stream.repeatedly(&safe_email/0)
|> Enum.take(@iterations)
|> Enum.each(fn generated_value ->
refute String.contains?(generated_value, ~s('"))
end)
end

test "free_email/0" do
Stream.repeatedly(&free_email/0)
|> Enum.take(@iterations)
|> Enum.each(fn generated_value ->
refute String.contains?(generated_value, ~s('"))
end)
end

test "domain_word/0" do
Stream.repeatedly(&domain_word/0)
|> Enum.take(@iterations)
|> Enum.each(fn generated_value ->
refute String.contains?(generated_value, ~s('"))
end)
end
end

0 comments on commit ef4b56f

Please sign in to comment.