test_rcategory_doctest.txt 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. Run this test file from command line using
  2. python -m doctest -v doctest.txt
  3. >>> from grass.script.core import run_command, read_command, write_command
  4. Data preparation
  5. ================
  6. >>> run_command('r.mapcalc', expression='test = if(col() < 3, col(), 2)')
  7. 0
  8. >>> print(read_command('r.info', map='test', flags='r'))
  9. min=1
  10. max=2
  11. <BLANKLINE>
  12. >>> run_command('r.mapcalc', expression='test_14 = if(col() < 5, col(), 4)')
  13. 0
  14. >>> print(read_command('r.info', map='test_14', flags='r'))
  15. min=1
  16. max=4
  17. <BLANKLINE>
  18. >>> run_command('r.mapcalc', expression='test_d = if(col() < 5, col() / 2., 4.5)')
  19. 0
  20. >>> print(read_command('r.info', map='test_d', flags='r'))
  21. min=0.5
  22. max=4.5
  23. <BLANKLINE>
  24. Basic input and output
  25. ======================
  26. >>> write_command('r.category', map='test', rules='-', separator=':', stdin="""
  27. ... 1:trees
  28. ... 2:water
  29. ... """)
  30. 0
  31. >>> read_command('r.category', map='test', separator=',')
  32. '1,trees\n2,water\n'
  33. Input and output with default separator
  34. =======================================
  35. >>> write_command('r.category', map='test', rules='-', stdin="""
  36. ... 1\ttrees
  37. ... 2\twater
  38. ... """)
  39. 0
  40. >>> read_command('r.category', map='test', separator='tab')
  41. '1\ttrees\n2\twater\n'
  42. Tabs needs a special treatment.
  43. Category range
  44. ==============
  45. >>> write_command('r.category', map='test_14', separator=':', rules='-', stdin="""
  46. ... 1:trees
  47. ... 2:4:buildings
  48. ... """)
  49. 0
  50. >>> print(read_command('r.category', map='test_14', separator=' ')) # doctest: +NORMALIZE_WHITESPACE
  51. 1 trees
  52. 2 4:buildings
  53. 3
  54. 4
  55. <BLANKLINE>
  56. Output has spaces at the end of line.
  57. More importantly, the output of r.category is wrong but here we are expecting this wrong output.
  58. Floating point maps
  59. ===================
  60. >>> write_command('r.category', map='test_d', separator=':', rules='-', stdin="""
  61. ... 0:1.5:trees
  62. ... 1.5:3:buildings
  63. ... """)
  64. 0
  65. >>> print(read_command('r.category', map='test_d', separator=' ', vals=[1, 1.1, 2.1, 4])) # doctest: +NORMALIZE_WHITESPACE
  66. 1 trees
  67. 1.1 trees
  68. 2.1 buildings
  69. 4
  70. <BLANKLINE>
  71. Output has spaces at the end of line.
  72. More importantly, the output of r.category is wrong but here we are expecting this wrong output.
  73. Separators in output
  74. ====================
  75. Test output first because now we perhaps have data correct.
  76. >>> print(read_command('r.category', map='test', separator='space'))
  77. 1 trees
  78. 2 water
  79. <BLANKLINE>
  80. >>> print(read_command('r.category', map='test', separator=','))
  81. 1,trees
  82. 2,water
  83. <BLANKLINE>
  84. >>> print(read_command('r.category', map='test', separator='XYZ'))
  85. 1XYZtrees
  86. 2XYZwater
  87. <BLANKLINE>
  88. >>> print(read_command('r.category', map='test', separator='newline'))
  89. 1
  90. trees
  91. 2
  92. water
  93. <BLANKLINE>
  94. >>> print(read_command('r.category', map='test', separator='\n&\n'))
  95. 1
  96. &
  97. trees
  98. 2
  99. &
  100. water
  101. <BLANKLINE>
  102. Separators in input
  103. ===================
  104. >>> write_command('r.category', map='test', separator='comma', rules='-', stdin="""
  105. ... 1,treesA
  106. ... 2,waterA
  107. ... """)
  108. 0
  109. >>> print(read_command('r.category', map='test', separator='space'))
  110. 1 treesA
  111. 2 waterA
  112. <BLANKLINE>
  113. >>> write_command('r.category', map='test', separator=',', rules='-', stdin="""
  114. ... 1,treesB
  115. ... 2,waterB
  116. ... """)
  117. 0
  118. >>> print(read_command('r.category', map='test', separator='space'))
  119. 1 treesB
  120. 2 waterB
  121. <BLANKLINE>
  122. >>> write_command('r.category', map='test', separator='|', rules='-', stdin="""
  123. ... 1|treesC
  124. ... 2|waterC
  125. ... """)
  126. 0
  127. >>> print(read_command('r.category', map='test', separator=' '))
  128. 1 treesC
  129. 2 waterC
  130. <BLANKLINE>
  131. Multi words input
  132. =================
  133. >>> write_command('r.category', map='test', separator='|', rules='-', stdin="""
  134. ... 1|small trees
  135. ... 2|deep water
  136. ... """)
  137. 0
  138. >>> print(read_command('r.category', map='test', separator=' '))
  139. 1 small trees
  140. 2 deep water
  141. <BLANKLINE>
  142. >>> write_command('r.category', map='test', separator='tab', rules='-', stdin="""
  143. ... 1\tvery small trees
  144. ... 2\tvery deep water
  145. ... """)
  146. 0
  147. >>> print(read_command('r.category', map='test', separator=':'))
  148. 1:very small trees
  149. 2:very deep water
  150. <BLANKLINE>
  151. Extreme and incorrect inputs
  152. ============================
  153. Some of these commands should not work and return 1.
  154. >>> write_command('r.category', map='test', separator='comma', rules='-', stdin="""
  155. ... 1,trees, very green
  156. ... 2,water, very deep
  157. ... """)
  158. 1
  159. >>> write_command('r.category', map='test', separator='|', rules='-', stdin="""
  160. ... 1|trees, very green
  161. ... 2|water, very deep
  162. ... """)
  163. 0
  164. >>> print(read_command('r.category', map='test', separator='space'))
  165. 1 trees, very green
  166. 2 water, very deep
  167. <BLANKLINE>
  168. >>> write_command('r.category', map='test', separator='tab', rules='-', stdin="""
  169. ... 1\tvery green trees
  170. ... 2\tvery deep\t water
  171. ... """)
  172. 1
  173. >>> print(read_command('r.category', map='test', separator='space'))
  174. 1 trees, very green
  175. 2 water, very deep
  176. <BLANKLINE>
  177. >>> write_command('r.category', map='test', separator=' ', rules='-', stdin="""
  178. ... 1 very green
  179. ... 2 very deep
  180. ... """)
  181. 1
  182. >>> print(read_command('r.category', map='test', separator='space'))
  183. 1 trees, very green
  184. 2 water, very deep
  185. <BLANKLINE>
  186. Clean the results
  187. =================
  188. This is useful when test is runned in location which is not deleted
  189. when test finishes. It could test if everything which was expected
  190. to be created was created if it would check all outputs properly.
  191. >>> run_command('g.remove', flags='f', type='raster', name='test')
  192. 0
  193. >>> run_command('g.remove', flags='f', type='raster', name='test_14')
  194. 0
  195. >>> run_command('g.remove', flags='f', type='raster', name='test_d')
  196. 0