[backport] Make all parseutils examples auto-checking (#13238)

- Also fix one example's output (ikString -> ikStr, ikVar instead of ikExpr)
This commit is contained in:
Kaushal Modi 2020-01-23 11:14:22 -05:00 • committed by Miran
commit c4ff4e553e

View file

@ -12,32 +12,29 @@
## ##
## To unpack raw bytes look at the `streams <streams.html>`_ module. ## To unpack raw bytes look at the `streams <streams.html>`_ module.
## ##
## ## .. code-block:: nim
## .. code-block:: ## :test:
## import parseutils
## ##
## let logs = @["2019-01-10: OK_", "2019-01-11: FAIL_", "2019-01: aaaa"] ## let logs = @["2019-01-10: OK_", "2019-01-11: FAIL_", "2019-01: aaaa"]
## var outp: seq[string]
## ##
## for log in logs: ## for log in logs:
## var res: string ## var res: string
## if parseUntil(log, res, ':') == 10: # YYYY-MM-DD == 10 ## if parseUntil(log, res, ':') == 10: # YYYY-MM-DD == 10
## echo res & " - " & captureBetween(log, ' ', '_') ## outp.add(res & " - " & captureBetween(log, ' ', '_'))
## # => 2019-01-10 - OK ## doAssert outp == @["2019-01-10 - OK", "2019-01-11 - FAIL"]
## ##
## ## .. code-block:: nim
## .. code-block:: ## :test:
## import parseutils
## from strutils import Digits, parseInt ## from strutils import Digits, parseInt
## ##
## let input1 = "2019 school start" ## let
## let input2 = "3 years back" ## input1 = "2019 school start"
## ## input2 = "3 years back"
## let startYear = input1[0..skipWhile(input1, Digits)-1] # 2019 ## startYear = input1[0 .. skipWhile(input1, Digits)-1] # 2019
## let yearsBack = input2[0..skipWhile(input2, Digits)-1] # 3 ## yearsBack = input2[0 .. skipWhile(input2, Digits)-1] # 3
## ## examYear = parseInt(startYear) + parseInt(yearsBack)
## echo "Examination is in " & $(parseInt(startYear) + parseInt(yearsBack)) ## doAssert "Examination is in " & $examYear == "Examination is in 2022"
## # => Examination is in 2022
##
## ##
## **See also:** ## **See also:**
## * `strutils module<strutils.html>`_ for combined and identical parsing proc's ## * `strutils module<strutils.html>`_ for combined and identical parsing proc's
@ -586,21 +583,17 @@ iterator interpolatedFragments*(s: string): tuple[kind: InterpolatedKind,
value: string] = value: string] =
## Tokenizes the string `s` into substrings for interpolation purposes. ## Tokenizes the string `s` into substrings for interpolation purposes.
## ##
## Example: runnableExamples:
## var outp: seq[tuple[kind: InterpolatedKind, value: string]]
## .. code-block:: nim for k, v in interpolatedFragments(" $this is ${an example} $$"):
## for k, v in interpolatedFragments(" $this is ${an example} $$"): outp.add (k, v)
## echo "(", k, ", \"", v, "\")" doAssert outp == @[(ikStr, " "),
## (ikVar, "this"),
## Results in: (ikStr, " is "),
## (ikExpr, "an example"),
## .. code-block:: nim (ikStr, " "),
## (ikString, " ") (ikDollar, "$")]
## (ikExpr, "this")
## (ikString, " is ")
## (ikExpr, "an example")
## (ikString, " ")
## (ikDollar, "$")
var i = 0 var i = 0
var kind: InterpolatedKind var kind: InterpolatedKind
while true: while true: