Skip to content

Commit f6bfe75

Browse files
committed
Make all parseutils examples auto-checking
- Also fix one example's output (ikString -> ikStr, ikVar instead of ikExpr)
1 parent aebcbfe commit f6bfe75

File tree

1 file changed

+25
-32
lines changed

1 file changed

+25
-32
lines changed

lib/pure/parseutils.nim

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -12,32 +12,29 @@
1212
##
1313
## To unpack raw bytes look at the `streams <streams.html>`_ module.
1414
##
15-
##
16-
## .. code-block::
17-
## import parseutils
15+
## .. code-block:: nim
16+
## :test:
1817
##
1918
## let logs = @["2019-01-10: OK_", "2019-01-11: FAIL_", "2019-01: aaaa"]
19+
## var outp: seq[string]
2020
##
2121
## for log in logs:
2222
## var res: string
2323
## if parseUntil(log, res, ':') == 10: # YYYY-MM-DD == 10
24-
## echo res & " - " & captureBetween(log, ' ', '_')
25-
## # => 2019-01-10 - OK
26-
##
24+
## outp.add(res & " - " & captureBetween(log, ' ', '_'))
25+
## doAssert outp == @["2019-01-10 - OK", "2019-01-11 - FAIL"]
2726
##
28-
## .. code-block::
29-
## import parseutils
27+
## .. code-block:: nim
28+
## :test:
3029
## from strutils import Digits, parseInt
3130
##
32-
## let input1 = "2019 school start"
33-
## let input2 = "3 years back"
34-
##
35-
## let startYear = input1[0..skipWhile(input1, Digits)-1] # 2019
36-
## let yearsBack = input2[0..skipWhile(input2, Digits)-1] # 3
37-
##
38-
## echo "Examination is in " & $(parseInt(startYear) + parseInt(yearsBack))
39-
## # => Examination is in 2022
40-
##
31+
## let
32+
## input1 = "2019 school start"
33+
## input2 = "3 years back"
34+
## startYear = input1[0 .. skipWhile(input1, Digits)-1] # 2019
35+
## yearsBack = input2[0 .. skipWhile(input2, Digits)-1] # 3
36+
## examYear = parseInt(startYear) + parseInt(yearsBack)
37+
## doAssert "Examination is in " & $examYear == "Examination is in 2022"
4138
##
4239
## **See also:**
4340
## * `strutils module<strutils.html>`_ for combined and identical parsing proc's
@@ -586,21 +583,17 @@ iterator interpolatedFragments*(s: string): tuple[kind: InterpolatedKind,
586583
value: string] =
587584
## Tokenizes the string `s` into substrings for interpolation purposes.
588585
##
589-
## Example:
590-
##
591-
## .. code-block:: nim
592-
## for k, v in interpolatedFragments(" $this is ${an example} $$"):
593-
## echo "(", k, ", \"", v, "\")"
594-
##
595-
## Results in:
596-
##
597-
## .. code-block:: nim
598-
## (ikString, " ")
599-
## (ikExpr, "this")
600-
## (ikString, " is ")
601-
## (ikExpr, "an example")
602-
## (ikString, " ")
603-
## (ikDollar, "$")
586+
runnableExamples:
587+
var outp: seq[tuple[kind: InterpolatedKind, value: string]]
588+
for k, v in interpolatedFragments(" $this is ${an example} $$"):
589+
outp.add (k, v)
590+
doAssert outp == @[(ikStr, " "),
591+
(ikVar, "this"),
592+
(ikStr, " is "),
593+
(ikExpr, "an example"),
594+
(ikStr, " "),
595+
(ikDollar, "$")]
596+
604597
var i = 0
605598
var kind: InterpolatedKind
606599
while true:

0 commit comments

Comments
 (0)