@@ -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é\n b" ) == { :ok , [ "aé" ] , "\n b" , % { } , { 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\n O" ) )
0 commit comments