1
Run this test file from command line using
3
python -m doctest -v doctest.txt
6
>>> from grass.script.core import run_command, read_command, write_command
11
>>> run_command('r.mapcalc', expression='test = if(col() < 3, col(), 2)')
13
>>> print(read_command('r.info', map='test', flags='r'))
17
>>> run_command('r.mapcalc', expression='test_14 = if(col() < 5, col(), 4)')
19
>>> print(read_command('r.info', map='test_14', flags='r'))
23
>>> run_command('r.mapcalc', expression='test_d = if(col() < 5, col() / 2., 4.5)')
25
>>> print(read_command('r.info', map='test_d', flags='r'))
31
Basic input and output
32
======================
34
>>> write_command('r.category', map='test', rules='-', separator=':', stdin="""
39
>>> read_command('r.category', map='test', separator=',')
43
Input and output with default separator
44
=======================================
46
>>> write_command('r.category', map='test', rules='-', stdin="""
51
>>> read_command('r.category', map='test', separator='tab')
52
'1\ttrees\n2\twater\n'
54
Tabs needs a special treatment.
60
>>> write_command('r.category', map='test_14', separator=':', rules='-', stdin="""
65
>>> print(read_command('r.category', map='test_14', separator=' ')) # doctest: +NORMALIZE_WHITESPACE
72
Output has spaces at the end of line.
73
More importantly, the output of r.category is wrong but here we are expecting this wrong output.
79
>>> write_command('r.category', map='test_d', separator=':', rules='-', stdin="""
84
>>> print(read_command('r.category', map='test_d', separator=' ', vals=[1, 1.1, 2.1, 4])) # doctest: +NORMALIZE_WHITESPACE
91
Output has spaces at the end of line.
92
More importantly, the output of r.category is wrong but here we are expecting this wrong output.
98
Test output first because now we perhaps have data correct.
100
>>> print(read_command('r.category', map='test', separator='space'))
104
>>> print(read_command('r.category', map='test', separator=','))
108
>>> print(read_command('r.category', map='test', separator='XYZ'))
112
>>> print(read_command('r.category', map='test', separator='newline'))
118
>>> print(read_command('r.category', map='test', separator='\n&\n'))
131
>>> write_command('r.category', map='test', separator='comma', rules='-', stdin="""
136
>>> print(read_command('r.category', map='test', separator='space'))
141
>>> write_command('r.category', map='test', separator=',', rules='-', stdin="""
146
>>> print(read_command('r.category', map='test', separator='space'))
151
>>> write_command('r.category', map='test', separator='|', rules='-', stdin="""
156
>>> print(read_command('r.category', map='test', separator=' '))
164
>>> write_command('r.category', map='test', separator='|', rules='-', stdin="""
169
>>> print(read_command('r.category', map='test', separator=' '))
174
>>> write_command('r.category', map='test', separator='tab', rules='-', stdin="""
175
... 1\tvery small trees
176
... 2\tvery deep water
179
>>> print(read_command('r.category', map='test', separator=':'))
185
Extreme and incorrect inputs
186
============================
188
Some of these commands should not work and return 1.
190
>>> write_command('r.category', map='test', separator='comma', rules='-', stdin="""
191
... 1,trees, very green
192
... 2,water, very deep
196
>>> write_command('r.category', map='test', separator='|', rules='-', stdin="""
197
... 1|trees, very green
198
... 2|water, very deep
201
>>> print(read_command('r.category', map='test', separator='space'))
206
>>> write_command('r.category', map='test', separator='tab', rules='-', stdin="""
207
... 1\tvery green trees
208
... 2\tvery deep\t water
211
>>> print(read_command('r.category', map='test', separator='space'))
216
>>> write_command('r.category', map='test', separator=' ', rules='-', stdin="""
221
>>> print(read_command('r.category', map='test', separator='space'))
230
This is useful when test is runned in location which is not deleted
231
when test finishes. It could test if everything which was expected
232
to be created was created if it would check all outputs properly.
234
>>> run_command('g.remove', flags='f', type='raster', name='test')
236
>>> run_command('g.remove', flags='f', type='raster', name='test_14')
238
>>> run_command('g.remove', flags='f', type='raster', name='test_d')