tstrformat.nim 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. discard """
  2. matrix: "--mm:refc; --mm:orc"
  3. """
  4. import genericstrformat
  5. import std/[strformat, strutils, times, tables, json]
  6. import std/[assertions, formatfloat]
  7. import std/objectdollar
  8. proc main() =
  9. block: # issue #7632
  10. doAssert works(5) == "formatted 5"
  11. doAssert fails0(6) == "formatted 6"
  12. doAssert fails(7) == "formatted 7"
  13. doAssert fails2[0](8) == "formatted 8"
  14. block: # other tests
  15. type Obj = object
  16. proc `$`(o: Obj): string = "foobar"
  17. # for custom types, formatValue needs to be overloaded.
  18. template formatValue(result: var string; value: Obj; specifier: string) =
  19. result.formatValue($value, specifier)
  20. var o: Obj
  21. doAssert fmt"{o}" == "foobar"
  22. doAssert fmt"{o:10}" == "foobar "
  23. doAssert fmt"{o=}" == "o=foobar"
  24. doAssert fmt"{o=:10}" == "o=foobar "
  25. block: # see issue #7933
  26. var str = "abc"
  27. doAssert fmt">7.1 :: {str:>7.1}" == ">7.1 :: a"
  28. doAssert fmt">7.2 :: {str:>7.2}" == ">7.2 :: ab"
  29. doAssert fmt">7.3 :: {str:>7.3}" == ">7.3 :: abc"
  30. doAssert fmt">7.9 :: {str:>7.9}" == ">7.9 :: abc"
  31. doAssert fmt">7.0 :: {str:>7.0}" == ">7.0 :: "
  32. doAssert fmt" 7.1 :: {str:7.1}" == " 7.1 :: a "
  33. doAssert fmt" 7.2 :: {str:7.2}" == " 7.2 :: ab "
  34. doAssert fmt" 7.3 :: {str:7.3}" == " 7.3 :: abc "
  35. doAssert fmt" 7.9 :: {str:7.9}" == " 7.9 :: abc "
  36. doAssert fmt" 7.0 :: {str:7.0}" == " 7.0 :: "
  37. doAssert fmt"^7.1 :: {str:^7.1}" == "^7.1 :: a "
  38. doAssert fmt"^7.2 :: {str:^7.2}" == "^7.2 :: ab "
  39. doAssert fmt"^7.3 :: {str:^7.3}" == "^7.3 :: abc "
  40. doAssert fmt"^7.9 :: {str:^7.9}" == "^7.9 :: abc "
  41. doAssert fmt"^7.0 :: {str:^7.0}" == "^7.0 :: "
  42. doAssert fmt">7.1 :: {str=:>7.1}" == ">7.1 :: str= a"
  43. doAssert fmt">7.2 :: {str=:>7.2}" == ">7.2 :: str= ab"
  44. doAssert fmt">7.3 :: {str=:>7.3}" == ">7.3 :: str= abc"
  45. doAssert fmt">7.9 :: {str=:>7.9}" == ">7.9 :: str= abc"
  46. doAssert fmt">7.0 :: {str=:>7.0}" == ">7.0 :: str= "
  47. doAssert fmt" 7.1 :: {str=:7.1}" == " 7.1 :: str=a "
  48. doAssert fmt" 7.2 :: {str=:7.2}" == " 7.2 :: str=ab "
  49. doAssert fmt" 7.3 :: {str=:7.3}" == " 7.3 :: str=abc "
  50. doAssert fmt" 7.9 :: {str=:7.9}" == " 7.9 :: str=abc "
  51. doAssert fmt" 7.0 :: {str=:7.0}" == " 7.0 :: str= "
  52. doAssert fmt"^7.1 :: {str=:^7.1}" == "^7.1 :: str= a "
  53. doAssert fmt"^7.2 :: {str=:^7.2}" == "^7.2 :: str= ab "
  54. doAssert fmt"^7.3 :: {str=:^7.3}" == "^7.3 :: str= abc "
  55. doAssert fmt"^7.9 :: {str=:^7.9}" == "^7.9 :: str= abc "
  56. doAssert fmt"^7.0 :: {str=:^7.0}" == "^7.0 :: str= "
  57. str = "äöüe\u0309\u0319o\u0307\u0359"
  58. doAssert fmt"^7.1 :: {str:^7.1}" == "^7.1 :: ä "
  59. doAssert fmt"^7.2 :: {str:^7.2}" == "^7.2 :: äö "
  60. doAssert fmt"^7.3 :: {str:^7.3}" == "^7.3 :: äöü "
  61. doAssert fmt"^7.0 :: {str:^7.0}" == "^7.0 :: "
  62. doAssert fmt"^7.1 :: {str=:^7.1}" == "^7.1 :: str= ä "
  63. doAssert fmt"^7.2 :: {str=:^7.2}" == "^7.2 :: str= äö "
  64. doAssert fmt"^7.3 :: {str=:^7.3}" == "^7.3 :: str= äöü "
  65. doAssert fmt"^7.0 :: {str=:^7.0}" == "^7.0 :: str= "
  66. # this is actually wrong, but the unicode module has no support for graphemes
  67. doAssert fmt"^7.4 :: {str:^7.4}" == "^7.4 :: äöüe "
  68. doAssert fmt"^7.9 :: {str:^7.9}" == "^7.9 :: äöüe\u0309\u0319o\u0307\u0359"
  69. doAssert fmt"^7.4 :: {str=:^7.4}" == "^7.4 :: str= äöüe "
  70. doAssert fmt"^7.9 :: {str=:^7.9}" == "^7.9 :: str=äöüe\u0309\u0319o\u0307\u0359"
  71. block: # see issue #7932
  72. doAssert fmt"{15:08}" == "00000015" # int, works
  73. doAssert fmt"{1.5:08}" == "000001.5" # float, works
  74. doAssert fmt"{1.5:0>8}" == "000001.5" # workaround using fill char works for positive floats
  75. doAssert fmt"{-1.5:0>8}" == "0000-1.5" # even that does not work for negative floats
  76. doAssert fmt"{-1.5:08}" == "-00001.5" # works
  77. doAssert fmt"{1.5:+08}" == "+00001.5" # works
  78. doAssert fmt"{1.5: 08}" == " 00001.5" # works
  79. doAssert fmt"{15=:08}" == "15=00000015" # int, works
  80. doAssert fmt"{1.5=:08}" == "1.5=000001.5" # float, works
  81. doAssert fmt"{1.5=:0>8}" == "1.5=000001.5" # workaround using fill char works for positive floats
  82. doAssert fmt"{-1.5=:0>8}" == "-1.5=0000-1.5" # even that does not work for negative floats
  83. doAssert fmt"{-1.5=:08}" == "-1.5=-00001.5" # works
  84. doAssert fmt"{1.5=:+08}" == "1.5=+00001.5" # works
  85. doAssert fmt"{1.5=: 08}" == "1.5= 00001.5" # works
  86. block: # only add explicitly requested sign if value != -0.0 (neg zero)
  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"
  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. doAssert fmt"{0.0=: g}" == "0.0= 0"
  99. block: # seq format
  100. let data1 = [1'i64, 10000'i64, 10000000'i64]
  101. let data2 = [10000000'i64, 100'i64, 1'i64]
  102. proc formatValue(result: var string; value: (array|seq|openArray); specifier: string) =
  103. result.add "["
  104. for i, it in value:
  105. if i != 0:
  106. result.add ", "
  107. result.formatValue(it, specifier)
  108. result.add "]"
  109. doAssert fmt"data1: {data1:8} #" == "data1: [ 1, 10000, 10000000] #"
  110. doAssert fmt"data2: {data2:8} =" == "data2: [10000000, 100, 1] ="
  111. doAssert fmt"data1: {data1=:8} #" == "data1: data1=[ 1, 10000, 10000000] #"
  112. doAssert fmt"data2: {data2=:8} =" == "data2: data2=[10000000, 100, 1] ="
  113. block: # custom format Value
  114. type
  115. Vec2[T] = object
  116. x,y: T
  117. proc formatValue[T](result: var string; value: Vec2[T]; specifier: string) =
  118. result.add '['
  119. result.formatValue value.x, specifier
  120. result.add ", "
  121. result.formatValue value.y, specifier
  122. result.add "]"
  123. let v1 = Vec2[float32](x:1.0, y: 2.0)
  124. let v2 = Vec2[int32](x:1, y: 1337)
  125. doAssert fmt"v1: {v1:+08} v2: {v2:>4}" == "v1: [+0000001, +0000002] v2: [ 1, 1337]"
  126. doAssert fmt"v1: {v1=:+08} v2: {v2=:>4}" == "v1: v1=[+0000001, +0000002] v2: v2=[ 1, 1337]"
  127. block: # bug #11012
  128. type
  129. Animal = object
  130. name, species: string
  131. AnimalRef = ref Animal
  132. proc print_object(animalAddr: AnimalRef): string =
  133. fmt"Received {animalAddr[]}"
  134. doAssert print_object(AnimalRef(name: "Foo", species: "Bar")) == """Received (name: "Foo", species: "Bar")"""
  135. block: # bug #11723
  136. let pos: Positive = 64
  137. doAssert fmt"{pos:3}" == " 64"
  138. doAssert fmt"{pos:3b}" == "1000000"
  139. doAssert fmt"{pos:3d}" == " 64"
  140. doAssert fmt"{pos:3o}" == "100"
  141. doAssert fmt"{pos:3x}" == " 40"
  142. doAssert fmt"{pos:3X}" == " 40"
  143. doAssert fmt"{pos=:3}" == "pos= 64"
  144. doAssert fmt"{pos=:3b}" == "pos=1000000"
  145. doAssert fmt"{pos=:3d}" == "pos= 64"
  146. doAssert fmt"{pos=:3o}" == "pos=100"
  147. doAssert fmt"{pos=:3x}" == "pos= 40"
  148. doAssert fmt"{pos=:3X}" == "pos= 40"
  149. let nat: Natural = 64
  150. doAssert fmt"{nat:3}" == " 64"
  151. doAssert fmt"{nat:3b}" == "1000000"
  152. doAssert fmt"{nat:3d}" == " 64"
  153. doAssert fmt"{nat:3o}" == "100"
  154. doAssert fmt"{nat:3x}" == " 40"
  155. doAssert fmt"{nat:3X}" == " 40"
  156. doAssert fmt"{nat=:3}" == "nat= 64"
  157. doAssert fmt"{nat=:3b}" == "nat=1000000"
  158. doAssert fmt"{nat=:3d}" == "nat= 64"
  159. doAssert fmt"{nat=:3o}" == "nat=100"
  160. doAssert fmt"{nat=:3x}" == "nat= 40"
  161. doAssert fmt"{nat=:3X}" == "nat= 40"
  162. block: # bug #12612
  163. proc my_proc() =
  164. const value = "value"
  165. const a = &"{value}"
  166. doAssert a == value
  167. const b = &"{value=}"
  168. doAssert b == "value=" & value
  169. my_proc()
  170. block:
  171. template fmt(pattern: string; openCloseChar: char): untyped =
  172. fmt(pattern, openCloseChar, openCloseChar)
  173. let
  174. testInt = 123
  175. testStr = "foobar"
  176. testFlt = 3.141592
  177. doAssert ">><<".fmt('<', '>') == "><"
  178. doAssert " >> << ".fmt('<', '>') == " > < "
  179. doAssert "<<>>".fmt('<', '>') == "<>"
  180. doAssert " << >> ".fmt('<', '>') == " < > "
  181. doAssert "''".fmt('\'') == "'"
  182. doAssert "''''".fmt('\'') == "''"
  183. doAssert "'' ''".fmt('\'') == "' '"
  184. doAssert "<testInt>".fmt('<', '>') == "123"
  185. doAssert "<testInt>".fmt('<', '>') == "123"
  186. doAssert "'testFlt:1.2f'".fmt('\'') == "3.14"
  187. doAssert "<testInt><testStr>".fmt('<', '>') == "123foobar"
  188. doAssert """ ""{"123+123"}"" """.fmt('"') == " \"{246}\" "
  189. doAssert "(((testFlt:1.2f)))((111))".fmt('(', ')') == "(3.14)(111)"
  190. doAssert """(()"foo" & "bar"())""".fmt(')', '(') == "(foobar)"
  191. doAssert "{}abc`testStr' `testFlt:1.2f' `1+1' ``".fmt('`', '\'') == "{}abcfoobar 3.14 2 `"
  192. doAssert """x = '"foo" & "bar"'
  193. y = '123 + 111'
  194. z = '3 in {2..7}'
  195. """.fmt('\'') ==
  196. """x = foobar
  197. y = 234
  198. z = true
  199. """
  200. block: # tests from the very own strformat documentation!
  201. let msg = "hello"
  202. doAssert fmt"{msg}\n" == "hello\\n"
  203. doAssert &"{msg}\n" == "hello\n"
  204. doAssert fmt"{msg}{'\n'}" == "hello\n"
  205. doAssert fmt("{msg}\n") == "hello\n"
  206. doAssert "{msg}\n".fmt == "hello\n"
  207. doAssert fmt"{msg=}\n" == "msg=hello\\n"
  208. doAssert &"{msg=}\n" == "msg=hello\n"
  209. doAssert fmt"{msg=}{'\n'}" == "msg=hello\n"
  210. doAssert fmt("{msg=}\n") == "msg=hello\n"
  211. doAssert "{msg=}\n".fmt == "msg=hello\n"
  212. doAssert &"""{"abc":>4}""" == " abc"
  213. doAssert &"""{"abc":<4}""" == "abc "
  214. doAssert fmt"{-12345:08}" == "-0012345"
  215. doAssert fmt"{-1:3}" == " -1"
  216. doAssert fmt"{-1:03}" == "-01"
  217. doAssert fmt"{16:#X}" == "0x10"
  218. doAssert fmt"{123.456}" == "123.456"
  219. doAssert fmt"{123.456:>9.3f}" == " 123.456"
  220. doAssert fmt"{123.456:9.3f}" == " 123.456"
  221. doAssert fmt"{123.456:9.4f}" == " 123.4560"
  222. doAssert fmt"{123.456:>9.0f}" == " 123."
  223. doAssert fmt"{123.456:<9.4f}" == "123.4560 "
  224. doAssert fmt"{123.456:e}" == "1.234560e+02"
  225. doAssert fmt"{123.456:>13e}" == " 1.234560e+02"
  226. doAssert fmt"{123.456:13e}" == " 1.234560e+02"
  227. doAssert &"""{"abc"=:>4}""" == "\"abc\"= abc"
  228. doAssert &"""{"abc"=:<4}""" == "\"abc\"=abc "
  229. doAssert fmt"{-12345=:08}" == "-12345=-0012345"
  230. doAssert fmt"{-1=:3}" == "-1= -1"
  231. doAssert fmt"{-1=:03}" == "-1=-01"
  232. doAssert fmt"{16=:#X}" == "16=0x10"
  233. doAssert fmt"{123.456=}" == "123.456=123.456"
  234. doAssert fmt"{123.456=:>9.3f}" == "123.456= 123.456"
  235. doAssert fmt"{123.456=:9.3f}" == "123.456= 123.456"
  236. doAssert fmt"{123.456=:9.4f}" == "123.456= 123.4560"
  237. doAssert fmt"{123.456=:>9.0f}" == "123.456= 123."
  238. doAssert fmt"{123.456=:<9.4f}" == "123.456=123.4560 "
  239. doAssert fmt"{123.456=:e}" == "123.456=1.234560e+02"
  240. doAssert fmt"{123.456=:>13e}" == "123.456= 1.234560e+02"
  241. doAssert fmt"{123.456=:13e}" == "123.456= 1.234560e+02"
  242. let x = 3.14
  243. doAssert fmt"{(if x!=0: 1.0/x else: 0):.5}" == "0.31847"
  244. doAssert fmt"""{(block:
  245. var res: string
  246. for i in 1..15:
  247. res.add (if i mod 15 == 0: "FizzBuzz"
  248. elif i mod 5 == 0: "Buzz"
  249. elif i mod 3 == 0: "Fizz"
  250. else: $i) & " "
  251. res)}""" == "1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz "
  252. doAssert fmt"""{ "\{(" & msg & ")\}" }""" == "{(hello)}"
  253. doAssert fmt"""{{({ msg })}}""" == "{(hello)}"
  254. doAssert fmt"""{ $(\{msg:1,"world":2\}) }""" == """[("hello", 1), ("world", 2)]"""
  255. block: # tests for debug format string
  256. var name = "hello"
  257. let age = 21
  258. const hobby = "swim"
  259. doAssert fmt"{age*9 + 16=}" == "age*9 + 16=205"
  260. doAssert &"name: {name =}\nage: { age =: >7}\nhobby: { hobby= : 8}" ==
  261. "name: name =hello\nage: age = 21\nhobby: hobby= swim "
  262. doAssert fmt"{age == 12}" == "false"
  263. doAssert fmt"{name.toUpperAscii() = }" == "name.toUpperAscii() = HELLO"
  264. doAssert fmt"{name.toUpperAscii( ) = }" == "name.toUpperAscii( ) = HELLO"
  265. doAssert fmt"{ toUpperAscii( s = name ) = }" == " toUpperAscii( s = name ) = HELLO"
  266. doAssert fmt"{ strutils.toUpperAscii( s = name ) = }" == " strutils.toUpperAscii( s = name ) = HELLO"
  267. doAssert fmt"{age==12}" == "false"
  268. doAssert fmt"{age!= 12}" == "true"
  269. doAssert fmt"{age <= 12}" == "false"
  270. for i in 1 .. 10:
  271. doAssert fmt"{age.float =: .2f}" == "age.float = 21.00"
  272. doAssert fmt"{age.float() =:.3f}" == "age.float() =21.000"
  273. doAssert fmt"{float age= :.3f}" == "float age= 21.000"
  274. doAssert fmt"{12 == int(`!=`(age, 12))}" == "false"
  275. doAssert fmt"{0==1}" == "false"
  276. block: # It is space sensitive.
  277. let x = "12"
  278. doAssert fmt"{x=:}" == "x=12"
  279. doAssert fmt"{x=}" == "x=12"
  280. doAssert fmt"{x =:}" == "x =12"
  281. doAssert fmt"{x =}" == "x =12"
  282. doAssert fmt"{x= :}" == "x= 12"
  283. doAssert fmt"{x= }" == "x= 12"
  284. doAssert fmt"{x = :}" == "x = 12"
  285. doAssert fmt"{x = }" == "x = 12"
  286. doAssert fmt"{x = :}" == "x = 12"
  287. doAssert fmt"{x = }" == "x = 12"
  288. block:
  289. let x = "hello"
  290. doAssert fmt"{x=}" == "x=hello"
  291. doAssert fmt"{x =}" == "x =hello"
  292. let y = 3.1415926
  293. doAssert fmt"{y=:.2f}" == fmt"y={y:.2f}"
  294. doAssert fmt"{y=}" == fmt"y={y}"
  295. doAssert fmt"{y = : <8}" == fmt"y = 3.14159 "
  296. proc hello(a: string, b: float): int = 12
  297. template foo(a: string, b: float): int = 18
  298. doAssert fmt"{hello(x, y)=}" == "hello(x, y)=12"
  299. doAssert fmt"{hello(x, y) =}" == "hello(x, y) =12"
  300. doAssert fmt"{hello(x, y)= }" == "hello(x, y)= 12"
  301. doAssert fmt"{hello(x, y) = }" == "hello(x, y) = 12"
  302. doAssert fmt"{hello x, y=}" == "hello x, y=12"
  303. doAssert fmt"{hello x, y =}" == "hello x, y =12"
  304. doAssert fmt"{hello x, y= }" == "hello x, y= 12"
  305. doAssert fmt"{hello x, y = }" == "hello x, y = 12"
  306. doAssert fmt"{x.hello(y)=}" == "x.hello(y)=12"
  307. doAssert fmt"{x.hello(y) =}" == "x.hello(y) =12"
  308. doAssert fmt"{x.hello(y)= }" == "x.hello(y)= 12"
  309. doAssert fmt"{x.hello(y) = }" == "x.hello(y) = 12"
  310. doAssert fmt"{foo(x, y)=}" == "foo(x, y)=18"
  311. doAssert fmt"{foo(x, y) =}" == "foo(x, y) =18"
  312. doAssert fmt"{foo(x, y)= }" == "foo(x, y)= 18"
  313. doAssert fmt"{foo(x, y) = }" == "foo(x, y) = 18"
  314. doAssert fmt"{x.foo(y)=}" == "x.foo(y)=18"
  315. doAssert fmt"{x.foo(y) =}" == "x.foo(y) =18"
  316. doAssert fmt"{x.foo(y)= }" == "x.foo(y)= 18"
  317. doAssert fmt"{x.foo(y) = }" == "x.foo(y) = 18"
  318. block:
  319. template check(actual, expected: string) =
  320. doAssert actual == expected
  321. # Basic tests
  322. let s = "string"
  323. check &"{0} {s}", "0 string"
  324. check &"{s[0..2].toUpperAscii}", "STR"
  325. check &"{-10:04}", "-010"
  326. check &"{-10:<04}", "-010"
  327. check &"{-10:>04}", "-010"
  328. check &"0x{10:02X}", "0x0A"
  329. check &"{10:#04X}", "0x0A"
  330. check &"""{"test":#>5}""", "#test"
  331. check &"""{"test":>5}""", " test"
  332. check &"""{"test":#^7}""", "#test##"
  333. check &"""{"test": <5}""", "test "
  334. check &"""{"test":<5}""", "test "
  335. check &"{1f:.3f}", "1.000"
  336. check &"Hello, {s}!", "Hello, string!"
  337. # Tests for identifiers without parenthesis
  338. check &"{s} works{s}", "string worksstring"
  339. check &"{s:>7}", " string"
  340. doAssert(not compiles(&"{s_works}")) # parsed as identifier `s_works`
  341. # Misc general tests
  342. check &"{{}}", "{}"
  343. check &"{0}%", "0%"
  344. check &"{0}%asdf", "0%asdf"
  345. check &("\n{\"\\n\"}\n"), "\n\n\n"
  346. check &"""{"abc"}s""", "abcs"
  347. # String tests
  348. check &"""{"abc"}""", "abc"
  349. check &"""{"abc":>4}""", " abc"
  350. check &"""{"abc":<4}""", "abc "
  351. check &"""{"":>4}""", " "
  352. check &"""{"":<4}""", " "
  353. # Int tests
  354. check &"{12345}", "12345"
  355. check &"{ - 12345}", "-12345"
  356. check &"{12345:6}", " 12345"
  357. check &"{12345:>6}", " 12345"
  358. check &"{12345:4}", "12345"
  359. check &"{12345:08}", "00012345"
  360. check &"{-12345:08}", "-0012345"
  361. check &"{0:0}", "0"
  362. check &"{0:02}", "00"
  363. check &"{-1:3}", " -1"
  364. check &"{-1:03}", "-01"
  365. check &"{10}", "10"
  366. check &"{16:#X}", "0x10"
  367. check &"{16:^#7X}", " 0x10 "
  368. check &"{16:^+#7X}", " +0x10 "
  369. # Hex tests
  370. check &"{0:x}", "0"
  371. check &"{-0:x}", "0"
  372. check &"{255:x}", "ff"
  373. check &"{255:X}", "FF"
  374. check &"{-255:x}", "-ff"
  375. check &"{-255:X}", "-FF"
  376. check &"{255:x} uNaffeCteD CaSe", "ff uNaffeCteD CaSe"
  377. check &"{255:X} uNaffeCteD CaSe", "FF uNaffeCteD CaSe"
  378. check &"{255:4x}", " ff"
  379. check &"{255:04x}", "00ff"
  380. check &"{-255:4x}", " -ff"
  381. check &"{-255:04x}", "-0ff"
  382. # Float tests
  383. check &"{123.456}", "123.456"
  384. check &"{-123.456}", "-123.456"
  385. check &"{123.456:.3f}", "123.456"
  386. check &"{123.456:+.3f}", "+123.456"
  387. check &"{-123.456:+.3f}", "-123.456"
  388. check &"{-123.456:.3f}", "-123.456"
  389. check &"{123.456:1g}", "123.456"
  390. check &"{123.456:.1f}", "123.5"
  391. check &"{123.456:.0f}", "123."
  392. check &"{123.456:>9.3f}", " 123.456"
  393. check &"{123.456:9.3f}", " 123.456"
  394. check &"{123.456:>9.4f}", " 123.4560"
  395. check &"{123.456:>9.0f}", " 123."
  396. check &"{123.456:<9.4f}", "123.4560 "
  397. # Float (scientific) tests
  398. check &"{123.456:e}", "1.234560e+02"
  399. check &"{123.456:>13e}", " 1.234560e+02"
  400. check &"{123.456:<13e}", "1.234560e+02 "
  401. check &"{123.456:.1e}", "1.2e+02"
  402. check &"{123.456:.2e}", "1.23e+02"
  403. check &"{123.456:.3e}", "1.235e+02"
  404. # Note: times.format adheres to the format protocol. Test that this
  405. # works:
  406. when nimvm:
  407. discard
  408. else:
  409. var dt = dateTime(2000, mJan, 01, 00, 00, 00)
  410. check &"{dt:yyyy-MM-dd}", "2000-01-01"
  411. var tm = fromUnix(0)
  412. discard &"{tm}"
  413. var noww = now()
  414. check &"{noww}", $noww
  415. # Unicode string tests
  416. check &"""{"αβγ"}""", "αβγ"
  417. check &"""{"αβγ":>5}""", " αβγ"
  418. check &"""{"αβγ":<5}""", "αβγ "
  419. check &"""a{"a"}α{"α"}€{"€"}𐍈{"𐍈"}""", "aaαα€€𐍈𐍈"
  420. check &"""a{"a":2}α{"α":2}€{"€":2}𐍈{"𐍈":2}""", "aa αα €€ 𐍈𐍈 "
  421. # Invalid unicode sequences should be handled as plain strings.
  422. # Invalid examples taken from: https://stackoverflow.com/a/3886015/1804173
  423. let invalidUtf8 = [
  424. "\xc3\x28", "\xa0\xa1",
  425. "\xe2\x28\xa1", "\xe2\x82\x28",
  426. "\xf0\x28\x8c\xbc", "\xf0\x90\x28\xbc", "\xf0\x28\x8c\x28"
  427. ]
  428. for s in invalidUtf8:
  429. check &"{s:>5}", repeat(" ", 5-s.len) & s
  430. # bug #11089
  431. let flfoo: float = 1.0
  432. check &"{flfoo}", "1.0"
  433. # bug #11092
  434. check &"{high(int64)}", "9223372036854775807"
  435. check &"{low(int64)}", "-9223372036854775808"
  436. doAssert fmt"{'a'} {'b'}" == "a b"
  437. block: # test low(int64)
  438. doAssert &"{low(int64):-}" == "-9223372036854775808"
  439. block: #expressions plus formatting
  440. doAssert fmt"{if true\: 123.456 else\: 0=:>9.3f}" == "if true: 123.456 else: 0= 123.456"
  441. doAssert fmt"{(if true: 123.456 else: 0)=}" == "(if true: 123.456 else: 0)=123.456"
  442. doAssert fmt"{if true\: 123.456 else\: 0=:9.3f}" == "if true: 123.456 else: 0= 123.456"
  443. doAssert fmt"{(if true: 123.456 else: 0)=:9.4f}" == "(if true: 123.456 else: 0)= 123.4560"
  444. doAssert fmt"{(if true: 123.456 else: 0)=:>9.0f}" == "(if true: 123.456 else: 0)= 123."
  445. doAssert fmt"{if true\: 123.456 else\: 0=:<9.4f}" == "if true: 123.456 else: 0=123.4560 "
  446. doAssert fmt"""{(case true
  447. of false: 0.0
  448. of true: 123.456)=:e}""" == """(case true
  449. of false: 0.0
  450. of true: 123.456)=1.234560e+02"""
  451. doAssert fmt"""{block\:
  452. var res = 0.000123456
  453. for _ in 0..5\:
  454. res *= 10
  455. res=:>13e}""" == """block:
  456. var res = 0.000123456
  457. for _ in 0..5:
  458. res *= 10
  459. res= 1.234560e+02"""
  460. #side effects
  461. var x = 5
  462. doAssert fmt"{(x=7;123.456)=:13e}" == "(x=7;123.456)= 1.234560e+02"
  463. doAssert x==7
  464. block: #curly bracket expressions and tuples
  465. proc formatValue(result: var string; value:Table|bool|JsonNode; specifier:string) = result.add $value
  466. doAssert fmt"""{\{"a"\:1,"b"\:2\}.toTable() = }""" == """{"a":1,"b":2}.toTable() = {"a": 1, "b": 2}"""
  467. doAssert fmt"""{(\{3: (1,"hi",0.9),4: (4,"lo",1.1)\}).toTable()}""" == """{3: (1, "hi", 0.9), 4: (4, "lo", 1.1)}"""
  468. doAssert fmt"""{ (%* \{"name": "Isaac", "books": ["Robot Dreams"]\}) }""" == """{"name":"Isaac","books":["Robot Dreams"]}"""
  469. doAssert """%( \%\* {"name": "Isaac"})*""".fmt('%','*') == """{"name":"Isaac"}"""
  470. block: #parens in quotes that fool my syntax highlighter
  471. doAssert fmt"{(if true: ')' else: '(')}" == ")"
  472. doAssert fmt"{(if true: ']' else: ')')}" == "]"
  473. doAssert fmt"""{(if true: "\")\"" else: "\"(")}""" == """")""""
  474. doAssert &"""{(if true: "\")" else: "")}""" == "\")"
  475. doAssert &"{(if true: \"\\\")\" else: \"\")}" == "\")"
  476. doAssert fmt"""{(if true: "')" else: "")}""" == "')"
  477. doAssert fmt"""{(if true: "'" & "'" & ')' else: "")}""" == "'')"
  478. doAssert &"""{(if true: "'" & "'" & ')' else: "")}""" == "'')"
  479. doAssert &"{(if true: \"\'\" & \"'\" & ')' else: \"\")}" == "'')"
  480. doAssert fmt"""{(if true: "'" & ')' else: "")}""" == "')"
  481. block: # issue #20381
  482. var ss: seq[string]
  483. template myTemplate(s: string) =
  484. ss.add s
  485. ss.add s
  486. proc foo() =
  487. myTemplate fmt"hello"
  488. foo()
  489. doAssert ss == @["hello", "hello"]
  490. static: main()
  491. main()