tstrformat.nim 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. discard """
  2. action: "run"
  3. output: '''Received (name: "Foo", species: "Bar")'''
  4. """
  5. # issue #7632
  6. import genericstrformat
  7. import strutils
  8. doAssert works(5) == "formatted 5"
  9. doAssert fails0(6) == "formatted 6"
  10. doAssert fails(7) == "formatted 7"
  11. doAssert fails2[0](8) == "formatted 8"
  12. # other tests
  13. import strformat
  14. type Obj = object
  15. proc `$`(o: Obj): string = "foobar"
  16. # for custom types, formatValue needs to be overloaded.
  17. template formatValue(result: var string; value: Obj; specifier: string) =
  18. result.formatValue($value, specifier)
  19. var o: Obj
  20. doAssert fmt"{o}" == "foobar"
  21. doAssert fmt"{o:10}" == "foobar "
  22. # see issue #7933
  23. var str = "abc"
  24. doAssert fmt">7.1 :: {str:>7.1}" == ">7.1 :: a"
  25. doAssert fmt">7.2 :: {str:>7.2}" == ">7.2 :: ab"
  26. doAssert fmt">7.3 :: {str:>7.3}" == ">7.3 :: abc"
  27. doAssert fmt">7.9 :: {str:>7.9}" == ">7.9 :: abc"
  28. doAssert fmt">7.0 :: {str:>7.0}" == ">7.0 :: "
  29. doAssert fmt" 7.1 :: {str:7.1}" == " 7.1 :: a "
  30. doAssert fmt" 7.2 :: {str:7.2}" == " 7.2 :: ab "
  31. doAssert fmt" 7.3 :: {str:7.3}" == " 7.3 :: abc "
  32. doAssert fmt" 7.9 :: {str:7.9}" == " 7.9 :: abc "
  33. doAssert fmt" 7.0 :: {str:7.0}" == " 7.0 :: "
  34. doAssert fmt"^7.1 :: {str:^7.1}" == "^7.1 :: a "
  35. doAssert fmt"^7.2 :: {str:^7.2}" == "^7.2 :: ab "
  36. doAssert fmt"^7.3 :: {str:^7.3}" == "^7.3 :: abc "
  37. doAssert fmt"^7.9 :: {str:^7.9}" == "^7.9 :: abc "
  38. doAssert fmt"^7.0 :: {str:^7.0}" == "^7.0 :: "
  39. str = "äöüe\u0309\u0319o\u0307\u0359"
  40. doAssert fmt"^7.1 :: {str:^7.1}" == "^7.1 :: ä "
  41. doAssert fmt"^7.2 :: {str:^7.2}" == "^7.2 :: äö "
  42. doAssert fmt"^7.3 :: {str:^7.3}" == "^7.3 :: äöü "
  43. doAssert fmt"^7.0 :: {str:^7.0}" == "^7.0 :: "
  44. # this is actually wrong, but the unicode module has no support for graphemes
  45. doAssert fmt"^7.4 :: {str:^7.4}" == "^7.4 :: äöüe "
  46. doAssert fmt"^7.9 :: {str:^7.9}" == "^7.9 :: äöüe\u0309\u0319o\u0307\u0359"
  47. # see issue #7932
  48. doAssert fmt"{15:08}" == "00000015" # int, works
  49. doAssert fmt"{1.5:08}" == "000001.5" # float, works
  50. doAssert fmt"{1.5:0>8}" == "000001.5" # workaround using fill char works for positive floats
  51. doAssert fmt"{-1.5:0>8}" == "0000-1.5" # even that does not work for negative floats
  52. doAssert fmt"{-1.5:08}" == "-00001.5" # works
  53. doAssert fmt"{1.5:+08}" == "+00001.5" # works
  54. doAssert fmt"{1.5: 08}" == " 00001.5" # works
  55. # only add explicitly requested sign if value != -0.0 (neg zero)
  56. doAssert fmt"{-0.0:g}" == "-0"
  57. doAssert fmt"{-0.0:+g}" == "-0"
  58. doAssert fmt"{-0.0: g}" == "-0"
  59. doAssert fmt"{0.0:g}" == "0"
  60. doAssert fmt"{0.0:+g}" == "+0"
  61. doAssert fmt"{0.0: g}" == " 0"
  62. # seq format
  63. let data1 = [1'i64, 10000'i64, 10000000'i64]
  64. let data2 = [10000000'i64, 100'i64, 1'i64]
  65. proc formatValue(result: var string; value: (array|seq|openArray); specifier: string) =
  66. result.add "["
  67. for i, it in value:
  68. if i != 0:
  69. result.add ", "
  70. result.formatValue(it, specifier)
  71. result.add "]"
  72. doAssert fmt"data1: {data1:8} #" == "data1: [ 1, 10000, 10000000] #"
  73. doAssert fmt"data2: {data2:8} =" == "data2: [10000000, 100, 1] ="
  74. doAssert fmt"data1: {data1=:8} #" == "data1: data1=[ 1, 10000, 10000000] #"
  75. doAssert fmt"data2: {data2=:8} =" == "data2: data2=[10000000, 100, 1] ="
  76. # custom format Value
  77. type
  78. Vec2[T] = object
  79. x,y: T
  80. proc formatValue[T](result: var string; value: Vec2[T]; specifier: string) =
  81. result.add '['
  82. result.formatValue value.x, specifier
  83. result.add ", "
  84. result.formatValue value.y, specifier
  85. result.add "]"
  86. let v1 = Vec2[float32](x:1.0, y: 2.0)
  87. let v2 = Vec2[int32](x:1, y: 1337)
  88. doAssert fmt"v1: {v1:+08} v2: {v2:>4}" == "v1: [+0000001, +0000002] v2: [ 1, 1337]"
  89. doAssert fmt"v1: {v1=:+08} v2: {v2=:>4}" == "v1: v1=[+0000001, +0000002] v2: v2=[ 1, 1337]"
  90. # bug #11012
  91. type
  92. Animal = object
  93. name, species: string
  94. AnimalRef = ref Animal
  95. proc print_object(animalAddr: AnimalRef) =
  96. echo fmt"Received {animalAddr[]}"
  97. print_object(AnimalRef(name: "Foo", species: "Bar"))
  98. # bug #11723
  99. let pos: Positive = 64
  100. doAssert fmt"{pos:3}" == " 64"
  101. doAssert fmt"{pos:3b}" == "1000000"
  102. doAssert fmt"{pos:3d}" == " 64"
  103. doAssert fmt"{pos:3o}" == "100"
  104. doAssert fmt"{pos:3x}" == " 40"
  105. doAssert fmt"{pos:3X}" == " 40"
  106. doAssert fmt"{pos=:3}" == "pos= 64"
  107. doAssert fmt"{pos=:3b}" == "pos=1000000"
  108. doAssert fmt"{pos=:3d}" == "pos= 64"
  109. doAssert fmt"{pos=:3o}" == "pos=100"
  110. doAssert fmt"{pos=:3x}" == "pos= 40"
  111. doAssert fmt"{pos=:3X}" == "pos= 40"
  112. let nat: Natural = 64
  113. doAssert fmt"{nat:3}" == " 64"
  114. doAssert fmt"{nat:3b}" == "1000000"
  115. doAssert fmt"{nat:3d}" == " 64"
  116. doAssert fmt"{nat:3o}" == "100"
  117. doAssert fmt"{nat:3x}" == " 40"
  118. doAssert fmt"{nat:3X}" == " 40"
  119. doAssert fmt"{nat=:3}" == "nat= 64"
  120. doAssert fmt"{nat=:3b}" == "nat=1000000"
  121. doAssert fmt"{nat=:3d}" == "nat= 64"
  122. doAssert fmt"{nat=:3o}" == "nat=100"
  123. doAssert fmt"{nat=:3x}" == "nat= 40"
  124. doAssert fmt"{nat=:3X}" == "nat= 40"
  125. # bug #12612
  126. proc my_proc =
  127. const value = "value"
  128. const a = &"{value}"
  129. assert a == value
  130. my_proc()
  131. block:
  132. template fmt(pattern: string; openCloseChar: char): untyped =
  133. fmt(pattern, openCloseChar, openCloseChar)
  134. let
  135. testInt = 123
  136. testStr = "foobar"
  137. testFlt = 3.141592
  138. doAssert ">><<".fmt('<', '>') == "><"
  139. doAssert " >> << ".fmt('<', '>') == " > < "
  140. doAssert "<<>>".fmt('<', '>') == "<>"
  141. doAssert " << >> ".fmt('<', '>') == " < > "
  142. doAssert "''".fmt('\'') == "'"
  143. doAssert "''''".fmt('\'') == "''"
  144. doAssert "'' ''".fmt('\'') == "' '"
  145. doAssert "<testInt>".fmt('<', '>') == "123"
  146. doAssert "<testInt>".fmt('<', '>') == "123"
  147. doAssert "'testFlt:1.2f'".fmt('\'') == "3.14"
  148. doAssert "<testInt><testStr>".fmt('<', '>') == "123foobar"
  149. doAssert """ ""{"123+123"}"" """.fmt('"') == " \"{246}\" "
  150. doAssert "(((testFlt:1.2f)))((111))".fmt('(', ')') == "(3.14)(111)"
  151. doAssert """(()"foo" & "bar"())""".fmt(')', '(') == "(foobar)"
  152. doAssert "{}abc`testStr' `testFlt:1.2f' `1+1' ``".fmt('`', '\'') == "{}abcfoobar 3.14 2 `"
  153. doAssert """x = '"foo" & "bar"'
  154. y = '123 + 111'
  155. z = '3 in {2..7}'
  156. """.fmt('\'') ==
  157. """x = foobar
  158. y = 234
  159. z = true
  160. """
  161. # tests from the very own strformat documentation!
  162. let msg = "hello"
  163. doAssert fmt"{msg}\n" == "hello\\n"
  164. doAssert &"{msg}\n" == "hello\n"
  165. doAssert fmt"{msg}{'\n'}" == "hello\n"
  166. doAssert fmt("{msg}\n") == "hello\n"
  167. doAssert "{msg}\n".fmt == "hello\n"
  168. doAssert fmt"{msg=}\n" == "msg=hello\\n"
  169. doAssert &"{msg=}\n" == "msg=hello\n"
  170. doAssert fmt"{msg=}{'\n'}" == "msg=hello\n"
  171. doAssert fmt("{msg=}\n") == "msg=hello\n"
  172. doAssert "{msg=}\n".fmt == "msg=hello\n"
  173. doAssert &"""{"abc":>4}""" == " abc"
  174. doAssert &"""{"abc":<4}""" == "abc "
  175. doAssert fmt"{-12345:08}" == "-0012345"
  176. doAssert fmt"{-1:3}" == " -1"
  177. doAssert fmt"{-1:03}" == "-01"
  178. doAssert fmt"{16:#X}" == "0x10"
  179. doAssert fmt"{123.456}" == "123.456"
  180. doAssert fmt"{123.456:>9.3f}" == " 123.456"
  181. doAssert fmt"{123.456:9.3f}" == " 123.456"
  182. doAssert fmt"{123.456:9.4f}" == " 123.4560"
  183. doAssert fmt"{123.456:>9.0f}" == " 123."
  184. doAssert fmt"{123.456:<9.4f}" == "123.4560 "
  185. doAssert fmt"{123.456:e}" == "1.234560e+02"
  186. doAssert fmt"{123.456:>13e}" == " 1.234560e+02"
  187. doAssert fmt"{123.456:13e}" == " 1.234560e+02"
  188. doAssert &"""{"abc"=:>4}""" == "\"abc\"= abc"
  189. doAssert &"""{"abc"=:<4}""" == "\"abc\"=abc "
  190. doAssert fmt"{-12345=:08}" == "-12345=-0012345"
  191. doAssert fmt"{-1=:3}" == "-1= -1"
  192. doAssert fmt"{-1=:03}" == "-1=-01"
  193. doAssert fmt"{16=:#X}" == "16=0x10"
  194. doAssert fmt"{123.456=}" == "123.456=123.456"
  195. doAssert fmt"{123.456=:>9.3f}" == "123.456= 123.456"
  196. doAssert fmt"{123.456=:9.3f}" == "123.456= 123.456"
  197. doAssert fmt"{123.456=:9.4f}" == "123.456= 123.4560"
  198. doAssert fmt"{123.456=:>9.0f}" == "123.456= 123."
  199. doAssert fmt"{123.456=:<9.4f}" == "123.456=123.4560 "
  200. doAssert fmt"{123.456=:e}" == "123.456=1.234560e+02"
  201. doAssert fmt"{123.456=:>13e}" == "123.456= 1.234560e+02"
  202. doAssert fmt"{123.456=:13e}" == "123.456= 1.234560e+02"
  203. ## tests for debug format string
  204. block:
  205. var name = "hello"
  206. let age = 21
  207. const hobby = "swim"
  208. doAssert fmt"{age*9 + 16=}" == "age*9 + 16=205"
  209. doAssert &"name: {name =}\nage: { age =: >7}\nhobby: { hobby= : 8}" ==
  210. "name: name =hello\nage: age = 21\nhobby: hobby= swim "
  211. doAssert fmt"{age == 12}" == "false"
  212. doAssert fmt"{name.toUpperAscii( ) = }" == "name.toUpperAscii( ) = HELLO"
  213. doAssert fmt"{ toUpperAscii( s = name ) = }" == " toUpperAscii( s = name ) = HELLO"
  214. doAssert fmt"{ strutils.toUpperAscii( s = name ) = }" == " strutils.toUpperAscii( s = name ) = HELLO"
  215. doAssert fmt"{age==12}" == "false"
  216. doAssert fmt"{age!= 12}" == "true"
  217. doAssert fmt"{age <= 12}" == "false"
  218. for i in 1 .. 10:
  219. doAssert fmt"{age.float =: .2f}" == "age.float = 21.00"
  220. doAssert fmt"{age.float() =:.3f}" == "age.float() =21.000"
  221. doAssert fmt"{float age= :.3f}" == "float age= 21.000"
  222. doAssert fmt"{12 == int(`!=`(age, 12))}" == "false"
  223. doAssert fmt"{0==1}" == "false"
  224. block:
  225. let x = "hello"
  226. doAssert fmt"{x=}" == "x=" & $x
  227. doAssert fmt"{x =}" == "x =" & $x
  228. let y = 3.1415926
  229. doAssert fmt"{y=:.2f}" == fmt"y={y:.2f}"
  230. doAssert fmt"{y=}" == fmt"y={y}"
  231. doAssert fmt"{y =: <16}" == fmt"y ={y: <16}"
  232. proc hello(a: string, b: float): int = 12
  233. template foo(a: string, b: float): int = 18
  234. doAssert fmt"{hello(x, y)=}" == "hello(x, y)=12"
  235. doAssert fmt"{hello(x, y) =}" == "hello(x, y) =12"
  236. doAssert fmt"{hello(x, y)= }" == "hello(x, y)= 12"
  237. doAssert fmt"{hello(x, y) = }" == "hello(x, y) = 12"
  238. doAssert fmt"{x.hello(y)=}" == "x.hello(y)=12"
  239. doAssert fmt"{x.hello(y) =}" == "x.hello(y) =12"
  240. doAssert fmt"{x.hello(y)= }" == "x.hello(y)= 12"
  241. doAssert fmt"{x.hello(y) = }" == "x.hello(y) = 12"
  242. doAssert fmt"{foo(x, y)=}" == "foo(x, y)=18"
  243. doAssert fmt"{foo(x, y) =}" == "foo(x, y) =18"
  244. doAssert fmt"{foo(x, y)= }" == "foo(x, y)= 18"
  245. doAssert fmt"{foo(x, y) = }" == "foo(x, y) = 18"
  246. doAssert fmt"{x.foo(y)=}" == "x.foo(y)=18"
  247. doAssert fmt"{x.foo(y) =}" == "x.foo(y) =18"
  248. doAssert fmt"{x.foo(y)= }" == "x.foo(y)= 18"
  249. doAssert fmt"{x.foo(y) = }" == "x.foo(y) = 18"