Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add all convenience functions combinations and property testing #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 17 additions & 16 deletions lib/case_style.ex
Original file line number Diff line number Diff line change
Expand Up @@ -91,22 +91,23 @@ defmodule CaseStyle do

### convenience functions

[
{:snake_to_camel, CaseStyle.SnakeCase, CaseStyle.CamelCase},
{:snake_to_graphql, CaseStyle.SnakeCase, CaseStyle.GraphQLCase},
{:snake_to_kebab, CaseStyle.SnakeCase, CaseStyle.KebabCase},
{:snake_to_pascal, CaseStyle.SnakeCase, CaseStyle.PascalCase},
{:camel_to_snake, CaseStyle.CamelCase, CaseStyle.SnakeCase},
{:camel_to_kebab, CaseStyle.CamelCase, CaseStyle.KebabCase},
{:camel_to_pascal, CaseStyle.CamelCase, CaseStyle.PascalCase},
{:graphql_to_snake, CaseStyle.GraphQLCase, CaseStyle.SnakeCase},
{:kebab_to_snake, CaseStyle.KebabCase, CaseStyle.SnakeCase},
{:kebab_to_camel, CaseStyle.KebabCase, CaseStyle.CamelCase},
{:kebab_to_pascal, CaseStyle.KebabCase, CaseStyle.PascalCase},
{:pascal_to_snake, CaseStyle.PascalCase, CaseStyle.SnakeCase},
{:pascal_to_camel, CaseStyle.PascalCase, CaseStyle.CamelCase},
{:pascal_to_kebab, CaseStyle.PascalCase, CaseStyle.KebabCase}
@available_casings [
snake: CaseStyle.SnakeCase,
camel: CaseStyle.CamelCase,
pascal: CaseStyle.PascalCase,
kebab: CaseStyle.KebabCase,
graphql: CaseStyle.GraphQLCase
]

Enum.flat_map(@available_casings, fn {casing_from, module_from} ->
Enum.flat_map(@available_casings, fn {casing_to, module_to} ->
if casing_from == casing_to do
[]
else
[{:"#{casing_from}_to_#{casing_to}", module_from, module_to}]
end
end)
end)
|> Enum.map(fn {func_name, from, to} ->
@spec unquote(func_name)(binary) :: {:ok, binary} | nimble_parsec_error
@doc "Converts from `#{from |> Module.split() |> Enum.join(".")}` to `#{to |> Module.split() |> Enum.join(".")}`"
Expand All @@ -128,7 +129,7 @@ defmodule CaseStyle do
to_string(casing, unquote(to))

_ ->
raise "Unable to convert #{input}"
raise "Unable to convert #{input} in #{unquote(func_name)}"
end
end
end)
Expand Down
2 changes: 1 addition & 1 deletion lib/case_style/camel_case.ex
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ defmodule CaseStyle.CamelCase do

@allowed_chars Enum.concat([?a..?z, ?A..?Z, ?0..?9])
@impl true
def matches?(<<first_char, _::binary>>) when first_char in ?A..?Z do
def matches?(<<first_char, _::binary>>) when first_char in ?A..?Z or first_char in ?0..?9 do
false
end

Expand Down
2 changes: 1 addition & 1 deletion lib/case_style/graphql_case.ex
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ defmodule CaseStyle.GraphQLCase do
@deprecated "use matches?/1 instead"
defdelegate might_be?(input), to: __MODULE__, as: :matches?

@allowed_chars Enum.concat([?a..?z, ?A..?Z, ?0..?9, '_'])
@allowed_chars Enum.concat([?a..?z, ?A..?Z, ?0..?9])
@impl true
def matches?(<<first_char, _::binary>>) when first_char in ?A..?Z or first_char in ?0..?9 do
false
Expand Down
2 changes: 1 addition & 1 deletion lib/case_style/pascal_case.ex
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ defmodule CaseStyle.PascalCase do

@allowed_chars Enum.concat([?a..?z, ?A..?Z, ?0..?9])
@impl true
def matches?(<<first_char, _::binary>>) when first_char in ?a..?z do
def matches?(<<first_char, _::binary>>) when first_char in ?a..?z or first_char in ?0..?9 do
false
end

Expand Down
5 changes: 3 additions & 2 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule CaseStyle.MixProject do
def project do
[
app: :case_style,
version: "0.5.0",
version: "0.6.0",
elixir: "~> 1.10",
start_permanent: Mix.env() == :prod,
deps: deps(),
Expand Down Expand Up @@ -37,7 +37,8 @@ defmodule CaseStyle.MixProject do
{:abnf_parsec, "~> 1.0", runtime: false},
{:dialyxir, "~> 1.1", only: [:dev], runtime: false},
{:credo, "~> 1.5", only: [:dev, :test], runtime: false},
{:ex_doc, "~> 0.24", only: [:dev], runtime: false}
{:ex_doc, "~> 0.24", only: [:dev], runtime: false},
{:stream_data, "~> 0.6", only: :test}
]
end
end
1 change: 1 addition & 0 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@
"makeup_elixir": {:hex, :makeup_elixir, "0.15.2", "dc72dfe17eb240552857465cc00cce390960d9a0c055c4ccd38b70629227e97c", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.1", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "fd23ae48d09b32eff49d4ced2b43c9f086d402ee4fd4fcb2d7fad97fa8823e75"},
"makeup_erlang": {:hex, :makeup_erlang, "0.1.1", "3fcb7f09eb9d98dc4d208f49cc955a34218fc41ff6b84df7c75b3e6e533cc65f", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "174d0809e98a4ef0b3309256cbf97101c6ec01c4ab0b23e926a9e17df2077cbb"},
"nimble_parsec": {:hex, :nimble_parsec, "1.1.0", "3a6fca1550363552e54c216debb6a9e95bd8d32348938e13de5eda962c0d7f89", [:mix], [], "hexpm", "08eb32d66b706e913ff748f11694b17981c0b04a33ef470e33e11b3d3ac8f54b"},
"stream_data": {:hex, :stream_data, "0.6.0", "e87a9a79d7ec23d10ff83eb025141ef4915eeb09d4491f79e52f2562b73e5f47", [:mix], [], "hexpm", "b92b5031b650ca480ced047578f1d57ea6dd563f5b57464ad274718c9c29501c"},
}
5 changes: 2 additions & 3 deletions test/case_style/camel_case_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ defmodule CaseStyle.CamelCaseTest do
@input input
test "#{@input}" do
{:ok, casing} = CaseStyle.from_string(@input, CaseStyle.CamelCase)
assert @input = output = CaseStyle.CamelCase.to_string(casing)
assert CaseStyle.CamelCase.matches?(output)
assert @input = CaseStyle.CamelCase.to_string(casing)
end
end
)
Expand All @@ -55,7 +54,7 @@ defmodule CaseStyle.CamelCaseTest do
end

test "starting with number" do
assert CaseStyle.CamelCase.matches?("1Testing")
refute CaseStyle.CamelCase.matches?("1Testing")
end
end
end
9 changes: 3 additions & 6 deletions test/case_style/graphql_case_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ defmodule CaseStyle.GraphQLCaseTest do
test "okteSting12" do
input = "_ok_teSting_12"
{:ok, casing} = CaseStyle.from_string(input, CaseStyle.SnakeCase)
assert CaseStyle.GraphQLCase.matches?(input)
assert "_okTesting_12" = output = CaseStyle.GraphQLCase.to_string(casing)
assert CaseStyle.GraphQLCase.matches?(output)
assert "_okTesting_12" = CaseStyle.GraphQLCase.to_string(casing)
assert CaseStyle.GraphQLCase.matches?("okTesting12")
end

Expand All @@ -27,8 +25,7 @@ defmodule CaseStyle.GraphQLCaseTest do
@input input
test "#{@input}" do
{:ok, casing} = CaseStyle.from_string(@input, CaseStyle.GraphQLCase)
assert @input = output = CaseStyle.GraphQLCase.to_string(casing)
assert CaseStyle.GraphQLCase.matches?(output)
assert @input = CaseStyle.GraphQLCase.to_string(casing)
end
end
)
Expand All @@ -43,7 +40,7 @@ defmodule CaseStyle.GraphQLCaseTest do
end

test "snakecase" do
assert CaseStyle.GraphQLCase.matches?("test_property")
refute CaseStyle.GraphQLCase.matches?("test_property")
end

test "kebabcase" do
Expand Down
5 changes: 2 additions & 3 deletions test/case_style/pascal_case_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ defmodule CaseStyle.PascalCaseTest do
@input input
test "#{@input}" do
{:ok, casing} = CaseStyle.from_string(@input, CaseStyle.PascalCase)
assert @input = output = CaseStyle.PascalCase.to_string(casing)
assert CaseStyle.PascalCase.matches?(output)
assert @input = CaseStyle.PascalCase.to_string(casing)
end
end
)
Expand Down Expand Up @@ -63,7 +62,7 @@ defmodule CaseStyle.PascalCaseTest do
end

test "starting with number" do
assert CaseStyle.PascalCase.matches?("1Testing")
refute CaseStyle.PascalCase.matches?("1Testing")
end
end
end
52 changes: 52 additions & 0 deletions test/case_style/property_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
defmodule CaseStyle.PropertyTest do
use ExUnit.Case, async: true
use ExUnitProperties

@available_casings [
snake: CaseStyle.SnakeCase,
camel: CaseStyle.CamelCase,
pascal: CaseStyle.PascalCase,
kebab: CaseStyle.KebabCase,
graphql: CaseStyle.GraphQLCase
]

function_pairs =
Enum.flat_map(@available_casings, fn {casing_from, module_from} ->
Enum.flat_map(@available_casings, fn {casing_to, module_to} ->
if casing_from == casing_to do
[]
else
[
{:"#{casing_from}_to_#{casing_to}!", :"#{casing_to}_to_#{casing_from}!", module_from,
module_to}
]
end
end)
end)

def generator(module) do
bind_filter(
string(Enum.concat([?a..?z, ?A..?Z, ?0..?9, [?-, ?_]]), min_length: 1),
fn name ->
if module.matches?(name) do
{:cont, constant(name)}
else
:skip
end
end,
10_000
)
end

Enum.map(function_pairs, fn {convert_from, convert_back, module_from, module_to} ->
property "#{convert_from} <-> #{convert_back}" do
check all(name <- generator(unquote(module_from))) do
one_way = CaseStyle.unquote(convert_from)(name)

if unquote(module_to).matches?(one_way) do
assert name == CaseStyle.unquote(convert_back)(one_way)
end
end
end
end)
end
2 changes: 1 addition & 1 deletion test/case_style_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ defmodule CaseStyleTest do
# But we still want to test that they actually work

refute CaseStyle.CamelCase.might_be?("test_property")
assert CaseStyle.GraphQLCase.might_be?("test_property")
assert CaseStyle.GraphQLCase.might_be?("testProperty")
assert CaseStyle.SnakeCase.might_be?("test_property")
refute CaseStyle.PascalCase.might_be?("test_property")
refute CaseStyle.KebabCase.might_be?("test_property")
Expand Down