tstrformat.nim 13 KB

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