| 1 | Run this test file from command line using
|
|---|
| 2 |
|
|---|
| 3 | python -m doctest -v doctest.txt
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 | >>> from grass.script.core import run_command, read_command, write_command
|
|---|
| 7 |
|
|---|
| 8 | Data preparation
|
|---|
| 9 | ================
|
|---|
| 10 |
|
|---|
| 11 | >>> run_command('r.mapcalc', expression='test = if(col() < 3, col(), 2)')
|
|---|
| 12 | 0
|
|---|
| 13 | >>> print(read_command('r.info', map='test', flags='r'))
|
|---|
| 14 | min=1
|
|---|
| 15 | max=2
|
|---|
| 16 | <BLANKLINE>
|
|---|
| 17 | >>> run_command('r.mapcalc', expression='test_14 = if(col() < 5, col(), 4)')
|
|---|
| 18 | 0
|
|---|
| 19 | >>> print(read_command('r.info', map='test_14', flags='r'))
|
|---|
| 20 | min=1
|
|---|
| 21 | max=4
|
|---|
| 22 | <BLANKLINE>
|
|---|
| 23 | >>> run_command('r.mapcalc', expression='test_d = if(col() < 5, col() / 2., 4.5)')
|
|---|
| 24 | 0
|
|---|
| 25 | >>> print(read_command('r.info', map='test_d', flags='r'))
|
|---|
| 26 | min=0.5
|
|---|
| 27 | max=4.5
|
|---|
| 28 | <BLANKLINE>
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 | Basic input and output
|
|---|
| 32 | ======================
|
|---|
| 33 |
|
|---|
| 34 | >>> write_command('r.category', map='test', rules='-', separator=':', stdin="""
|
|---|
| 35 | ... 1:trees
|
|---|
| 36 | ... 2:water
|
|---|
| 37 | ... """)
|
|---|
| 38 | 0
|
|---|
| 39 | >>> read_command('r.category', map='test', separator=',')
|
|---|
| 40 | '1,trees\n2,water\n'
|
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 | Input and output with default separator
|
|---|
| 44 | =======================================
|
|---|
| 45 |
|
|---|
| 46 | >>> write_command('r.category', map='test', rules='-', stdin="""
|
|---|
| 47 | ... 1\ttrees
|
|---|
| 48 | ... 2\twater
|
|---|
| 49 | ... """)
|
|---|
| 50 | 0
|
|---|
| 51 | >>> read_command('r.category', map='test', separator='tab')
|
|---|
| 52 | '1\ttrees\n2\twater\n'
|
|---|
| 53 |
|
|---|
| 54 | Tabs needs a special treatment.
|
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 | Category range
|
|---|
| 58 | ==============
|
|---|
| 59 |
|
|---|
| 60 | >>> write_command('r.category', map='test_14', separator=':', rules='-', stdin="""
|
|---|
| 61 | ... 1:trees
|
|---|
| 62 | ... 2:4:buildings
|
|---|
| 63 | ... """)
|
|---|
| 64 | 0
|
|---|
| 65 | >>> print(read_command('r.category', map='test_14', separator=' ')) # doctest: +NORMALIZE_WHITESPACE
|
|---|
| 66 | 1 trees
|
|---|
| 67 | 2 4:buildings
|
|---|
| 68 | 3
|
|---|
| 69 | 4
|
|---|
| 70 | <BLANKLINE>
|
|---|
| 71 |
|
|---|
| 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.
|
|---|
| 74 |
|
|---|
| 75 |
|
|---|
| 76 | Floating 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 | ... """)
|
|---|
| 83 | 0
|
|---|
| 84 | >>> print(read_command('r.category', map='test_d', separator=' ', vals=[1, 1.1, 2.1, 4])) # doctest: +NORMALIZE_WHITESPACE
|
|---|
| 85 | 1 trees
|
|---|
| 86 | 1.1 trees
|
|---|
| 87 | 2.1 buildings
|
|---|
| 88 | 4
|
|---|
| 89 | <BLANKLINE>
|
|---|
| 90 |
|
|---|
| 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.
|
|---|
| 93 |
|
|---|
| 94 |
|
|---|
| 95 | Separators in output
|
|---|
| 96 | ====================
|
|---|
| 97 |
|
|---|
| 98 | Test output first because now we perhaps have data correct.
|
|---|
| 99 |
|
|---|
| 100 | >>> print(read_command('r.category', map='test', separator='space'))
|
|---|
| 101 | 1 trees
|
|---|
| 102 | 2 water
|
|---|
| 103 | <BLANKLINE>
|
|---|
| 104 | >>> print(read_command('r.category', map='test', separator=','))
|
|---|
| 105 | 1,trees
|
|---|
| 106 | 2,water
|
|---|
| 107 | <BLANKLINE>
|
|---|
| 108 | >>> print(read_command('r.category', map='test', separator='XYZ'))
|
|---|
| 109 | 1XYZtrees
|
|---|
| 110 | 2XYZwater
|
|---|
| 111 | <BLANKLINE>
|
|---|
| 112 | >>> print(read_command('r.category', map='test', separator='newline'))
|
|---|
| 113 | 1
|
|---|
| 114 | trees
|
|---|
| 115 | 2
|
|---|
| 116 | water
|
|---|
| 117 | <BLANKLINE>
|
|---|
| 118 | >>> print(read_command('r.category', map='test', separator='\n&\n'))
|
|---|
| 119 | 1
|
|---|
| 120 | &
|
|---|
| 121 | trees
|
|---|
| 122 | 2
|
|---|
| 123 | &
|
|---|
| 124 | water
|
|---|
| 125 | <BLANKLINE>
|
|---|
| 126 |
|
|---|
| 127 |
|
|---|
| 128 | Separators in input
|
|---|
| 129 | ===================
|
|---|
| 130 |
|
|---|
| 131 | >>> write_command('r.category', map='test', separator='comma', rules='-', stdin="""
|
|---|
| 132 | ... 1,treesA
|
|---|
| 133 | ... 2,waterA
|
|---|
| 134 | ... """)
|
|---|
| 135 | 0
|
|---|
| 136 | >>> print(read_command('r.category', map='test', separator='space'))
|
|---|
| 137 | 1 treesA
|
|---|
| 138 | 2 waterA
|
|---|
| 139 | <BLANKLINE>
|
|---|
| 140 |
|
|---|
| 141 | >>> write_command('r.category', map='test', separator=',', rules='-', stdin="""
|
|---|
| 142 | ... 1,treesB
|
|---|
| 143 | ... 2,waterB
|
|---|
| 144 | ... """)
|
|---|
| 145 | 0
|
|---|
| 146 | >>> print(read_command('r.category', map='test', separator='space'))
|
|---|
| 147 | 1 treesB
|
|---|
| 148 | 2 waterB
|
|---|
| 149 | <BLANKLINE>
|
|---|
| 150 |
|
|---|
| 151 | >>> write_command('r.category', map='test', separator='|', rules='-', stdin="""
|
|---|
| 152 | ... 1|treesC
|
|---|
| 153 | ... 2|waterC
|
|---|
| 154 | ... """)
|
|---|
| 155 | 0
|
|---|
| 156 | >>> print(read_command('r.category', map='test', separator=' '))
|
|---|
| 157 | 1 treesC
|
|---|
| 158 | 2 waterC
|
|---|
| 159 | <BLANKLINE>
|
|---|
| 160 |
|
|---|
| 161 | Multi words input
|
|---|
| 162 | =================
|
|---|
| 163 |
|
|---|
| 164 | >>> write_command('r.category', map='test', separator='|', rules='-', stdin="""
|
|---|
| 165 | ... 1|small trees
|
|---|
| 166 | ... 2|deep water
|
|---|
| 167 | ... """)
|
|---|
| 168 | 0
|
|---|
| 169 | >>> print(read_command('r.category', map='test', separator=' '))
|
|---|
| 170 | 1 small trees
|
|---|
| 171 | 2 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 | ... """)
|
|---|
| 178 | 0
|
|---|
| 179 | >>> print(read_command('r.category', map='test', separator=':'))
|
|---|
| 180 | 1:very small trees
|
|---|
| 181 | 2:very deep water
|
|---|
| 182 | <BLANKLINE>
|
|---|
| 183 |
|
|---|
| 184 |
|
|---|
| 185 | Extreme and incorrect inputs
|
|---|
| 186 | ============================
|
|---|
| 187 |
|
|---|
| 188 | Some 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 | ... """)
|
|---|
| 194 | 1
|
|---|
| 195 |
|
|---|
| 196 | >>> write_command('r.category', map='test', separator='|', rules='-', stdin="""
|
|---|
| 197 | ... 1|trees, very green
|
|---|
| 198 | ... 2|water, very deep
|
|---|
| 199 | ... """)
|
|---|
| 200 | 0
|
|---|
| 201 | >>> print(read_command('r.category', map='test', separator='space'))
|
|---|
| 202 | 1 trees, very green
|
|---|
| 203 | 2 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 | ... """)
|
|---|
| 210 | 1
|
|---|
| 211 | >>> print(read_command('r.category', map='test', separator='space'))
|
|---|
| 212 | 1 trees, very green
|
|---|
| 213 | 2 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 | ... """)
|
|---|
| 220 | 1
|
|---|
| 221 | >>> print(read_command('r.category', map='test', separator='space'))
|
|---|
| 222 | 1 trees, very green
|
|---|
| 223 | 2 water, very deep
|
|---|
| 224 | <BLANKLINE>
|
|---|
| 225 |
|
|---|
| 226 |
|
|---|
| 227 | Clean the results
|
|---|
| 228 | =================
|
|---|
| 229 |
|
|---|
| 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.
|
|---|
| 233 |
|
|---|
| 234 | >>> run_command('g.remove', flags='f', type='raster', name='test')
|
|---|
| 235 | 0
|
|---|
| 236 | >>> run_command('g.remove', flags='f', type='raster', name='test_14')
|
|---|
| 237 | 0
|
|---|
| 238 | >>> run_command('g.remove', flags='f', type='raster', name='test_d')
|
|---|
| 239 | 0
|
|---|