source: grass/trunk/lib/gis/testsuite/gis_lib_tokenize.py

Last change on this file was 66113, checked in by neteler, 9 years ago

svn propset

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