1 # SPDX-License-Identifier: GPL-2.0-only
 
   3 # This file is part of Nominatim. (https://nominatim.org)
 
   5 # Copyright (C) 2022 by the Nominatim developer community.
 
   6 # For a full list of authors see the git log.
 
   8 Tests for sanitizer helper functions.
 
  12 from nominatim.errors import UsageError
 
  13 import nominatim.tokenizer.sanitizers.helpers as helpers
 
  15 @pytest.mark.parametrize('inp', ('fg34', 'f\\f', 'morning [glory]', '56.78'))
 
  16 def test_create_split_regex_no_params_unsplit(inp):
 
  17     regex = helpers.create_split_regex({})
 
  19     assert list(regex.split(inp)) == [inp]
 
  22 @pytest.mark.parametrize('inp,outp', [('here,there', ['here', 'there']),
 
  23                                       ('ying;;yang', ['ying', 'yang']),
 
  24                                       (';a; ;c;d,', ['', 'a', '', 'c', 'd', '']),
 
  25                                       ('1,  3  ,5', ['1', '3', '5'])
 
  27 def test_create_split_regex_no_params_split(inp, outp):
 
  28     regex = helpers.create_split_regex({})
 
  30     assert list(regex.split(inp)) == outp
 
  33 @pytest.mark.parametrize('delimiter', ['.', '\\', '[]', '   ', '/.*+'])
 
  34 def test_create_split_regex_custom(delimiter):
 
  35     regex = helpers.create_split_regex({'delimiters': delimiter})
 
  37     assert list(regex.split(f'out{delimiter}house')) == ['out', 'house']
 
  38     assert list(regex.split('out,house')) == ['out,house']
 
  41 def test_create_split_regex_empty_delimiter():
 
  42     with pytest.raises(UsageError):
 
  43         regex = helpers.create_split_regex({'delimiters': ''})