test removing dollar for objects out of system (#20242)

* test removing dollar for objects out of system

* test & fixes

* fix bootstrap

* use nimPreviewSlimSystem, test stdlib category

* fix test
This commit is contained in:
metagn 2022-08-24 10:44:16 +03:00 • committed by GitHub
commit b8dc58d884
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 84 additions and 40 deletions

11
lib/std/objectdollar.nim Normal file
View file

@ -0,0 +1,11 @@
import std/private/miscdollars
proc `$`*[T: object](x: T): string =
## Generic `$` operator for objects with similar output to
## `$` for named tuples.
runnableExamples:
type Foo = object
a, b: int
let x = Foo(a: 23, b: 45)
assert $x == "(a: 23, b: 45)"
tupleObjectDollar(result, x)

View file

@ -12,3 +12,42 @@ template toLocation*(result: var string, file: string | cstring, line: int, col:
result.add ", "
addInt(result, col)
result.add ")"
when defined(nimHasIsNamedTuple):
proc isNamedTuple(T: typedesc): bool {.magic: "TypeTrait".}
else:
# for bootstrap; remove after release 1.2
proc isNamedTuple(T: typedesc): bool =
# Taken from typetraits.
when T isnot tuple: result = false
else:
var t: T
for name, _ in t.fieldPairs:
when name == "Field0":
return compiles(t.Field0)
else:
return true
return false
template tupleObjectDollar*[T: tuple | object](result: var string, x: T) =
result = "("
const isNamed = T is object or isNamedTuple(typeof(T))
var count {.used.} = 0
for name, value in fieldPairs(x):
if count > 0: result.add(", ")
when isNamed:
result.add(name)
result.add(": ")
count.inc
when compiles($value):
when value isnot string and value isnot seq and compiles(value.isNil):
if value.isNil: result.add "nil"
else: result.addQuoted(value)
else:
result.addQuoted(value)
else:
result.add("...")
when not isNamed:
if count == 1:
result.add(",") # $(1,) should print as the semantically legal (1,)
result.add(")")