gis_lib_tokenize.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. """Test of gis library tokenizing of text
  2. @author Vaclav Petras
  3. """
  4. from grass.gunittest.case import TestCase
  5. from grass.gunittest.main import test
  6. import grass.lib.gis as libgis
  7. # TODO: add tests for content (now testing only number of items)
  8. class TokenizeTestCase(TestCase):
  9. """Test C function G_tokenize() from gis library"""
  10. def test_tokenize_comma(self):
  11. """Test G_tokenize with comma as delim"""
  12. tokens = libgis.G_tokenize("a,b,c,d", ",")
  13. num_of_tokens = libgis.G_number_of_tokens(tokens)
  14. self.assertEqual(num_of_tokens, 4, msg="Got wrong number of tokens")
  15. def test_tokenize_alternative_delim(self):
  16. """Test G_tokenize with semi colon as delim"""
  17. tokens = libgis.G_tokenize("a;b;c", ";")
  18. num_of_tokens = libgis.G_number_of_tokens(tokens)
  19. self.assertEqual(num_of_tokens, 3, msg="Got wrong number of tokens")
  20. def test_tokenize_with_text_delim(self):
  21. """Test G_tokenize with comma as delim and single quote text delim
  22. Expecting the 'wrong' number of tokens here.
  23. """
  24. tokens = libgis.G_tokenize("a,'b,c',d", ",")
  25. num_of_tokens = libgis.G_number_of_tokens(tokens)
  26. self.assertEqual(
  27. num_of_tokens,
  28. 4,
  29. msg="Got wrong number of tokens (expecting that the text"
  30. "delimiter is ignored)",
  31. )
  32. # alternatively this can be done using test with expected failure
  33. class Tokenize2TestCase(TestCase):
  34. """Test C function G_tokenize2() from gis library"""
  35. def test_tokenize2_comma(self):
  36. """Test G_tokenize2 without any text delim"""
  37. tokens = libgis.G_tokenize2("a,b,c,d", ",", "'")
  38. num_of_tokens = libgis.G_number_of_tokens(tokens)
  39. self.assertEqual(num_of_tokens, 4, msg="Got wrong number of tokens")
  40. def test_tokenize2_with_text_delim(self):
  41. """Test G_tokenize2 with , as delim and single quote text delim"""
  42. tokens = libgis.G_tokenize2("a,'b,c',d", ",", "'")
  43. num_of_tokens = libgis.G_number_of_tokens(tokens)
  44. self.assertEqual(num_of_tokens, 3, msg="Got wrong number of tokens")
  45. def test_tokenize2_with_alternative_text_delim(self):
  46. """Test G_tokenize2 with ; as delim and double quote text delim"""
  47. tokens = libgis.G_tokenize2('a;"b;c";d', ";", '"')
  48. num_of_tokens = libgis.G_number_of_tokens(tokens)
  49. self.assertEqual(num_of_tokens, 3, msg="Got wrong number of tokens")
  50. def test_tokenize2_with_text_delim_more_text_tokens(self):
  51. """Test G_tokenize2 with comma as delim and hash as text delim"""
  52. tokens = libgis.G_tokenize2("a,#b,c#,#5,d#,#7,2#", ",", "#")
  53. num_of_tokens = libgis.G_number_of_tokens(tokens)
  54. self.assertEqual(num_of_tokens, 4, msg="Got wrong number of tokens")
  55. def test_tokenize2_with_real_text(self):
  56. """Test G_tokenize2 with real world text"""
  57. tokens = libgis.G_tokenize2(
  58. "440,617722.81,3464034.494,951.987,"
  59. '"Low Erosion (1,5)","High Deposition (8,6)"',
  60. ",",
  61. '"',
  62. )
  63. num_of_tokens = libgis.G_number_of_tokens(tokens)
  64. self.assertEqual(num_of_tokens, 6, msg="Got wrong number of tokens")
  65. if __name__ == "__main__":
  66. test()