source: grass/trunk/raster/r.category/test_rcategory_doctest.txt

Last change on this file was 63652, checked in by neteler, 10 years ago

update g.list/g.remove calls to use new element names (#2409)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id
  • Property svn:mime-type set to text/plain
File size: 5.3 KB
Line 
1Run this test file from command line using
2
3python -m doctest -v doctest.txt
4
5
6>>> from grass.script.core import run_command, read_command, write_command
7
8Data preparation
9================
10
11>>> run_command('r.mapcalc', expression='test = if(col() < 3, col(), 2)')
120
13>>> print(read_command('r.info', map='test', flags='r'))
14min=1
15max=2
16<BLANKLINE>
17>>> run_command('r.mapcalc', expression='test_14 = if(col() < 5, col(), 4)')
180
19>>> print(read_command('r.info', map='test_14', flags='r'))
20min=1
21max=4
22<BLANKLINE>
23>>> run_command('r.mapcalc', expression='test_d = if(col() < 5, col() / 2., 4.5)')
240
25>>> print(read_command('r.info', map='test_d', flags='r'))
26min=0.5
27max=4.5
28<BLANKLINE>
29
30
31Basic input and output
32======================
33
34>>> write_command('r.category', map='test', rules='-', separator=':', stdin="""
35... 1:trees
36... 2:water
37... """)
380
39>>> read_command('r.category', map='test', separator=',')
40'1,trees\n2,water\n'
41
42
43Input and output with default separator
44=======================================
45
46>>> write_command('r.category', map='test', rules='-', stdin="""
47... 1\ttrees
48... 2\twater
49... """)
500
51>>> read_command('r.category', map='test', separator='tab')
52'1\ttrees\n2\twater\n'
53
54Tabs needs a special treatment.
55
56
57Category range
58==============
59
60>>> write_command('r.category', map='test_14', separator=':', rules='-', stdin="""
61... 1:trees
62... 2:4:buildings
63... """)
640
65>>> print(read_command('r.category', map='test_14', separator=' ')) # doctest: +NORMALIZE_WHITESPACE
661 trees
672 4:buildings
683
694
70<BLANKLINE>
71
72Output has spaces at the end of line.
73More importantly, the output of r.category is wrong but here we are expecting this wrong output.
74
75
76Floating point maps
77===================
78
79>>> write_command('r.category', map='test_d', separator=':', rules='-', stdin="""
80... 0:1.5:trees
81... 1.5:3:buildings
82... """)
830
84>>> print(read_command('r.category', map='test_d', separator=' ', vals=[1, 1.1, 2.1, 4])) # doctest: +NORMALIZE_WHITESPACE
851 trees
861.1 trees
872.1 buildings
884
89<BLANKLINE>
90
91Output has spaces at the end of line.
92More importantly, the output of r.category is wrong but here we are expecting this wrong output.
93
94
95Separators in output
96====================
97
98Test output first because now we perhaps have data correct.
99
100>>> print(read_command('r.category', map='test', separator='space'))
1011 trees
1022 water
103<BLANKLINE>
104>>> print(read_command('r.category', map='test', separator=','))
1051,trees
1062,water
107<BLANKLINE>
108>>> print(read_command('r.category', map='test', separator='XYZ'))
1091XYZtrees
1102XYZwater
111<BLANKLINE>
112>>> print(read_command('r.category', map='test', separator='newline'))
1131
114trees
1152
116water
117<BLANKLINE>
118>>> print(read_command('r.category', map='test', separator='\n&\n'))
1191
120&
121trees
1222
123&
124water
125<BLANKLINE>
126
127
128Separators in input
129===================
130
131>>> write_command('r.category', map='test', separator='comma', rules='-', stdin="""
132... 1,treesA
133... 2,waterA
134... """)
1350
136>>> print(read_command('r.category', map='test', separator='space'))
1371 treesA
1382 waterA
139<BLANKLINE>
140
141>>> write_command('r.category', map='test', separator=',', rules='-', stdin="""
142... 1,treesB
143... 2,waterB
144... """)
1450
146>>> print(read_command('r.category', map='test', separator='space'))
1471 treesB
1482 waterB
149<BLANKLINE>
150
151>>> write_command('r.category', map='test', separator='|', rules='-', stdin="""
152... 1|treesC
153... 2|waterC
154... """)
1550
156>>> print(read_command('r.category', map='test', separator=' '))
1571 treesC
1582 waterC
159<BLANKLINE>
160
161Multi words input
162=================
163
164>>> write_command('r.category', map='test', separator='|', rules='-', stdin="""
165... 1|small trees
166... 2|deep water
167... """)
1680
169>>> print(read_command('r.category', map='test', separator=' '))
1701 small trees
1712 deep water
172<BLANKLINE>
173
174>>> write_command('r.category', map='test', separator='tab', rules='-', stdin="""
175... 1\tvery small trees
176... 2\tvery deep water
177... """)
1780
179>>> print(read_command('r.category', map='test', separator=':'))
1801:very small trees
1812:very deep water
182<BLANKLINE>
183
184
185Extreme and incorrect inputs
186============================
187
188Some of these commands should not work and return 1.
189
190>>> write_command('r.category', map='test', separator='comma', rules='-', stdin="""
191... 1,trees, very green
192... 2,water, very deep
193... """)
1941
195
196>>> write_command('r.category', map='test', separator='|', rules='-', stdin="""
197... 1|trees, very green
198... 2|water, very deep
199... """)
2000
201>>> print(read_command('r.category', map='test', separator='space'))
2021 trees, very green
2032 water, very deep
204<BLANKLINE>
205
206>>> write_command('r.category', map='test', separator='tab', rules='-', stdin="""
207... 1\tvery green trees
208... 2\tvery deep\t water
209... """)
2101
211>>> print(read_command('r.category', map='test', separator='space'))
2121 trees, very green
2132 water, very deep
214<BLANKLINE>
215
216>>> write_command('r.category', map='test', separator=' ', rules='-', stdin="""
217... 1 very green
218... 2 very deep
219... """)
2201
221>>> print(read_command('r.category', map='test', separator='space'))
2221 trees, very green
2232 water, very deep
224<BLANKLINE>
225
226
227Clean the results
228=================
229
230This is useful when test is runned in location which is not deleted
231when test finishes. It could test if everything which was expected
232to be created was created if it would check all outputs properly.
233
234>>> run_command('g.remove', flags='f', type='raster', name='test')
2350
236>>> run_command('g.remove', flags='f', type='raster', name='test_14')
2370
238>>> run_command('g.remove', flags='f', type='raster', name='test_d')
2390
Note: See TracBrowser for help on using the repository browser.