Skip to content

Commit 083498e

Browse files
authored
Add test coverage for ascii_string/utf8_string min/max (#153)
1 parent d4b9d46 commit 083498e

2 files changed

Lines changed: 53 additions & 0 deletions

File tree

test/nimble_generator_test.exs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,24 @@ defmodule NimbleGeneratorTest do
112112
assert byte_size(generate(parsec)) === 3
113113
end
114114

115+
test "ascii_string" do
116+
assert ascii_string([?a..?z], 3) |> generate() =~ ~r/\A[a-z]{3}\z/
117+
118+
parsec = ascii_string([?a..?z], min: 2)
119+
assert eventually?(fn -> generate(parsec) =~ ~r/\A[a-z]{2}\z/ end)
120+
assert eventually?(fn -> generate(parsec) =~ ~r/\A[a-z]{5}\z/ end)
121+
122+
parsec = ascii_string([?a..?z], min: 2, max: 3)
123+
assert eventually?(fn -> generate(parsec) =~ ~r/\A[a-z]{2}\z/ end)
124+
assert eventually?(fn -> generate(parsec) =~ ~r/\A[a-z]{3}\z/ end)
125+
end
126+
127+
test "utf8_string" do
128+
parsec = utf8_string([..], min: 1, max: 2)
129+
assert eventually?(fn -> generate(parsec) =~ ~r/\A[á-é]{1}\z/u end)
130+
assert eventually?(fn -> generate(parsec) =~ ~r/\A[á-é]{2}\z/u end)
131+
end
132+
115133
defparsec :string_foo, string("foo"), export_metadata: true
116134
defparsec :string_choice, choice([parsec(:string_foo), string("bar")]), export_metadata: true
117135

test/nimble_parsec_test.exs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,41 @@ defmodule NimbleParsecTest do
312312
end
313313
end
314314

315+
describe "utf8_string/2 combinator with min/max over a newline-free range" do
316+
defparsecp :min_sliced_utf8_string, utf8_string([?a..?z, ..], min: 2)
317+
defparsecp :max_sliced_utf8_string, utf8_string([?a..?z, ..], max: 3)
318+
defparsecp :min_max_sliced_utf8_string, utf8_string([?a..?z, ..], min: 2, max: 3)
319+
defparsecp :not_newline_utf8_string, utf8_string([not: ?\n], min: 1)
320+
321+
@error "expected utf8 codepoint in the range \"a\" to \"z\" or in the range \"á\" to \"é\", " <>
322+
"followed by utf8 codepoint in the range \"a\" to \"z\" or in the range \"á\" to \"é\""
323+
324+
test "returns ok/error with min" do
325+
assert min_sliced_utf8_string("áé") == {:ok, ["áé"], "", %{}, {1, 0}, 4}
326+
assert min_sliced_utf8_string("aébc") == {:ok, ["aébc"], "", %{}, {1, 0}, 5}
327+
assert min_sliced_utf8_string("áé1") == {:ok, ["áé"], "1", %{}, {1, 0}, 4}
328+
assert min_sliced_utf8_string("á") == {:error, @error, "á", %{}, {1, 0}, 0}
329+
assert min_sliced_utf8_string("1á") == {:error, @error, "1á", %{}, {1, 0}, 0}
330+
end
331+
332+
test "returns ok/error with max" do
333+
assert max_sliced_utf8_string("1") == {:ok, [""], "1", %{}, {1, 0}, 0}
334+
assert max_sliced_utf8_string("áé") == {:ok, ["áé"], "", %{}, {1, 0}, 4}
335+
assert max_sliced_utf8_string("áéâ1") == {:ok, ["áéâ"], "1", %{}, {1, 0}, 6}
336+
assert max_sliced_utf8_string("áéâa") == {:ok, ["áéâ"], "a", %{}, {1, 0}, 6}
337+
end
338+
339+
test "returns ok/error with min/max" do
340+
assert min_max_sliced_utf8_string("á") == {:error, @error, "á", %{}, {1, 0}, 0}
341+
assert min_max_sliced_utf8_string("áé") == {:ok, ["áé"], "", %{}, {1, 0}, 4}
342+
assert min_max_sliced_utf8_string("áéâa") == {:ok, ["áéâ"], "a", %{}, {1, 0}, 6}
343+
end
344+
345+
test "does not slice past a newline" do
346+
assert not_newline_utf8_string("aé\nb") == {:ok, ["aé"], "\nb", %{}, {1, 0}, 3}
347+
end
348+
end
349+
315350
describe "ignore/2 combinator at compile time" do
316351
defparsecp :compile_ignore, ignore(string("TO"))
317352
defparsecp :compile_ignore_with_newline, ignore(string("T\nO"))

0 commit comments

Comments
 (0)