change some code-blocks to runnableExamples and correct some errors in docs (#15900)

* original docs are wrong
* fix pathnorm
* Update lib/pure/pathnorm.nim

Co-authored-by: Juan Carlos <juancarlospaco@gmail.com>
This commit is contained in:
flywind 2020-11-12 17:47:56 +08:00 • committed by GitHub
commit ef2677f5f3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 51 additions and 57 deletions

View file

@ -262,11 +262,8 @@ func contains*(methods: set[HttpMethod], x: string): bool =
func `$`*(code: HttpCode): string = func `$`*(code: HttpCode): string =
## Converts the specified ``HttpCode`` into a HTTP status. ## Converts the specified ``HttpCode`` into a HTTP status.
## runnableExamples:
## For example: doAssert($Http404 == "404 Not Found")
##
## .. code-block:: nim
## doAssert($Http404 == "404 Not Found")
case code.int case code.int
of 100: "100 Continue" of 100: "100 Continue"
of 101: "101 Switching Protocols" of 101: "101 Switching Protocols"

View file

@ -519,9 +519,10 @@ proc `{}`*(node: JsonNode, keys: varargs[string]): JsonNode =
## This proc can be used to create tree structures on the ## This proc can be used to create tree structures on the
## fly (sometimes called `autovivification`:idx:): ## fly (sometimes called `autovivification`:idx:):
## ##
## .. code-block:: nim runnableExamples:
## myjson{"parent", "child", "grandchild"} = newJInt(1) var myjson = %* {"parent": {"child": {"grandchild": 1}}}
## doAssert myjson{"parent", "child", "grandchild"} == newJInt(1)
result = node result = node
for key in keys: for key in keys:
if isNil(result) or result.kind != JObject: if isNil(result) or result.kind != JObject:
@ -1258,32 +1259,30 @@ when defined(nimFixedForwardGeneric):
## * Sets in object variants are not supported. ## * Sets in object variants are not supported.
## * Not nil annotations are not supported. ## * Not nil annotations are not supported.
## ##
## Example: runnableExamples:
## let jsonNode = parseJson("""
## .. code-block:: Nim {
## let jsonNode = parseJson(""" "person": {
## { "name": "Nimmer",
## "person": { "age": 21
## "name": "Nimmer", },
## "age": 21 "list": [1, 2, 3, 4]
## }, }
## "list": [1, 2, 3, 4] """)
## }
## """) type
## Person = object
## type name: string
## Person = object age: int
## name: string
## age: int Data = object
## person: Person
## Data = object list: seq[int]
## person: Person
## list: seq[int] var data = to(jsonNode, Data)
## doAssert data.person.name == "Nimmer"
## var data = to(jsonNode, Data) doAssert data.person.age == 21
## doAssert data.person.name == "Nimmer" doAssert data.list == @[1, 2, 3, 4]
## doAssert data.person.age == 21
## doAssert data.list == @[1, 2, 3, 4]
var jsonPath = "" var jsonPath = ""
initFromJson(result, node, jsonPath) initFromJson(result, node, jsonPath)

View file

@ -862,10 +862,10 @@ when not defined(js):
## float value) equals ``m * 2**n``. frexp stores n in `exponent` and returns ## float value) equals ``m * 2**n``. frexp stores n in `exponent` and returns
## m. ## m.
## ##
## .. code-block:: nim runnableExamples:
## var x: int var x: int
## echo frexp(5.0, x) # 0.625 doAssert frexp(5.0, x) == 0.625
## echo x # 3 doAssert x == 3
var exp: int32 var exp: int32
result = c_frexp(x, exp) result = c_frexp(x, exp)
exponent = exp exponent = exp
@ -929,9 +929,9 @@ proc splitDecimal*[T: float32|float64](x: T): tuple[intpart: T, floatpart: T] =
## Both parts have the same sign as ``x``. Analogous to the ``modf`` ## Both parts have the same sign as ``x``. Analogous to the ``modf``
## function in C. ## function in C.
## ##
## .. code-block:: nim runnableExamples:
## echo splitDecimal(5.25) # (intpart: 5.0, floatpart: 0.25) doAssert splitDecimal(5.25) == (intpart: 5.0, floatpart: 0.25)
## echo splitDecimal(-2.73) # (intpart: -2.0, floatpart: -0.73) doAssert splitDecimal(-2.73) == (intpart: -2.0, floatpart: -0.73)
var var
absolute: T absolute: T
absolute = abs(x) absolute = abs(x)
@ -949,8 +949,8 @@ proc degToRad*[T: float32|float64](d: T): T {.inline.} =
## See also: ## See also:
## * `radToDeg proc <#radToDeg,T>`_ ## * `radToDeg proc <#radToDeg,T>`_
## ##
## .. code-block:: nim runnableExamples:
## echo degToRad(180.0) # 3.141592653589793 doAssert degToRad(180.0) == 3.141592653589793
result = T(d) * RadPerDeg result = T(d) * RadPerDeg
proc radToDeg*[T: float32|float64](d: T): T {.inline.} = proc radToDeg*[T: float32|float64](d: T): T {.inline.} =
@ -959,8 +959,8 @@ proc radToDeg*[T: float32|float64](d: T): T {.inline.} =
## See also: ## See also:
## * `degToRad proc <#degToRad,T>`_ ## * `degToRad proc <#degToRad,T>`_
## ##
## .. code-block:: nim runnableExamples:
## echo degToRad(2 * PI) # 360.0 doAssert radToDeg(2 * PI) == 360.0
result = T(d) / RadPerDeg result = T(d) / RadPerDeg
proc sgn*[T: SomeNumber](x: T): int {.inline.} = proc sgn*[T: SomeNumber](x: T): int {.inline.} =
@ -971,10 +971,10 @@ proc sgn*[T: SomeNumber](x: T): int {.inline.} =
## * `1` for positive numbers and ``Inf``, ## * `1` for positive numbers and ``Inf``,
## * `0` for positive zero, negative zero and ``NaN`` ## * `0` for positive zero, negative zero and ``NaN``
## ##
## .. code-block:: nim runnableExamples:
## echo sgn(5) # 1 doAssert sgn(5) == 1
## echo sgn(0) # 0 doAssert sgn(0) == 0
## echo sgn(-4.1) # -1 doAssert sgn(-4.1) == -1
ord(T(0) < x) - ord(x < T(0)) ord(T(0) < x) - ord(x < T(0))
{.pop.} {.pop.}

View file

@ -100,15 +100,13 @@ proc addNormalizePath*(x: string; result: var string; state: var int;
if result == "" and x != "": result = "." if result == "" and x != "": result = "."
proc normalizePath*(path: string; dirSep = DirSep): string = proc normalizePath*(path: string; dirSep = DirSep): string =
## Example: runnableExamples:
## when defined(posix):
## .. code-block:: nim doAssert normalizePath("./foo//bar/../baz") == "foo/baz"
## assert normalizePath("./foo//bar/../baz") == "foo/baz"
##
##
## - Turns multiple slashes into single slashes. ## - Turns multiple slashes into single slashes.
## - Resolves '/foo/../bar' to '/bar'. ## - Resolves `'/foo/../bar'` to `'/bar'`.
## - Removes './' from the path (but "foo/.." becomes ".") ## - Removes `'./'` from the path, but `"foo/.."` becomes `"."`.
result = newStringOfCap(path.len) result = newStringOfCap(path.len)
var state = 0 var state = 0
addNormalizePath(path, result, state, dirSep) addNormalizePath(path, result, state, dirSep)