tstrformat.nim 21 KB

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