mirror of
https://github.com/nim-lang/Nim.git
synced 2026-02-12 06:18:51 +00:00
JS unittest stacktrace fix, cleanup js repr and inclrtl includes (#14168)
This commit is contained in:
@@ -166,8 +166,6 @@
|
||||
##
|
||||
## * The effect system (``raises: []``) does not work with async procedures.
|
||||
|
||||
include "system/inclrtl"
|
||||
|
||||
import os, tables, strutils, times, heapqueue, options, asyncstreams
|
||||
import options, math, std/monotimes
|
||||
import asyncfutures except callSoon
|
||||
|
||||
@@ -17,8 +17,6 @@
|
||||
import
|
||||
hashes, math, locks
|
||||
|
||||
include "system/inclrtl"
|
||||
|
||||
type
|
||||
KeyValuePair[A, B] = tuple[hcode: Hash, key: A, val: B]
|
||||
KeyValuePairSeq[A, B] = ptr UncheckedArray[KeyValuePair[A, B]]
|
||||
|
||||
@@ -101,7 +101,9 @@ include "system/inclrtl"
|
||||
when declared(stdout):
|
||||
import os
|
||||
|
||||
when not defined(ECMAScript):
|
||||
const useTerminal = not defined(js)
|
||||
|
||||
when useTerminal:
|
||||
import terminal
|
||||
|
||||
type
|
||||
@@ -224,7 +226,7 @@ proc defaultConsoleFormatter*(): <//>ConsoleOutputFormatter =
|
||||
|
||||
method suiteStarted*(formatter: ConsoleOutputFormatter, suiteName: string) =
|
||||
template rawPrint() = echo("\n[Suite] ", suiteName)
|
||||
when not defined(ECMAScript):
|
||||
when useTerminal:
|
||||
if formatter.colorOutput:
|
||||
styledEcho styleBright, fgBlue, "\n[Suite] ", resetStyle, suiteName
|
||||
else: rawPrint()
|
||||
@@ -250,7 +252,7 @@ method testEnded*(formatter: ConsoleOutputFormatter, testResult: TestResult) =
|
||||
let prefix = if testResult.suiteName.len > 0: " " else: ""
|
||||
template rawPrint() = echo(prefix, "[", $testResult.status, "] ",
|
||||
testResult.testName)
|
||||
when not defined(ECMAScript):
|
||||
when useTerminal:
|
||||
if formatter.colorOutput:
|
||||
var color = case testResult.status
|
||||
of TestStatus.OK: fgGreen
|
||||
@@ -515,11 +517,10 @@ template test*(name, body) {.dirty.} =
|
||||
body
|
||||
|
||||
except:
|
||||
when not defined(js):
|
||||
let e = getCurrentException()
|
||||
let eTypeDesc = "[" & exceptionTypeName(e) & "]"
|
||||
checkpoint("Unhandled exception: " & getCurrentExceptionMsg() & " " & eTypeDesc)
|
||||
var stackTrace {.inject.} = e.getStackTrace()
|
||||
let e = getCurrentException()
|
||||
let eTypeDesc = "[" & exceptionTypeName(e) & "]"
|
||||
checkpoint("Unhandled exception: " & getCurrentExceptionMsg() & " " & eTypeDesc)
|
||||
var stackTrace {.inject.} = e.getStackTrace()
|
||||
fail()
|
||||
|
||||
finally:
|
||||
|
||||
@@ -8,34 +8,25 @@
|
||||
#
|
||||
# The generic ``repr`` procedure for the javascript backend.
|
||||
|
||||
proc reprInt(x: int64): string {.compilerproc.} = return $x
|
||||
proc reprFloat(x: float): string {.compilerproc.} =
|
||||
# Js toString doesn't differentiate between 1.0 and 1,
|
||||
# but we do.
|
||||
if $x == $(x.int): $x & ".0"
|
||||
else: $x
|
||||
proc reprInt(x: int64): string {.compilerproc.} = $x
|
||||
proc reprFloat(x: float): string {.compilerproc.} = $x
|
||||
|
||||
proc reprPointer(p: pointer): string {.compilerproc.} =
|
||||
# Do we need to generate the full 8bytes ? In js a pointer is an int anyway
|
||||
var tmp: int
|
||||
{. emit: """
|
||||
if (`p`_Idx == null) {
|
||||
`tmp` = 0;
|
||||
} else {
|
||||
`tmp` = `p`_Idx;
|
||||
}
|
||||
""" .}
|
||||
{.emit: "`tmp` = `p`_Idx || 0;".}
|
||||
result = $tmp
|
||||
|
||||
proc reprBool(x: bool): string {.compilerRtl.} =
|
||||
if x: result = "true"
|
||||
else: result = "false"
|
||||
|
||||
proc isUndefined[T](x: T): bool {.inline.} = {.emit: "`result` = `x` === undefined;"}
|
||||
|
||||
proc reprEnum(e: int, typ: PNimType): string {.compilerRtl.} =
|
||||
if not typ.node.sons[e].isUndefined:
|
||||
result = makeNimstrLit(typ.node.sons[e].name)
|
||||
var tmp: bool
|
||||
let item = typ.node.sons[e]
|
||||
{.emit: "`tmp` = `item` !== undefined".}
|
||||
if tmp:
|
||||
result = makeNimstrLit(item.name)
|
||||
else:
|
||||
result = $e & " (invalid data!)"
|
||||
|
||||
@@ -48,7 +39,7 @@ proc reprChar(x: char): string {.compilerRtl.} =
|
||||
else: add(result, x)
|
||||
add(result, "\'")
|
||||
|
||||
proc reprStrAux(result: var string, s: cstring, len: int) =
|
||||
proc reprStrAux(result: var string, s: cstring | string, len: int) =
|
||||
add(result, "\"")
|
||||
for i in 0 .. len-1:
|
||||
let c = s[i]
|
||||
@@ -63,17 +54,7 @@ proc reprStrAux(result: var string, s: cstring, len: int) =
|
||||
add(result, "\"")
|
||||
|
||||
proc reprStr(s: string): string {.compilerRtl.} =
|
||||
result = ""
|
||||
var sIsNil = false
|
||||
asm """`sIsNil` = `s` === null"""
|
||||
if sIsNil: # cast[pointer](s).isNil:
|
||||
# Handle nil strings here because they don't have a length field in js
|
||||
# TODO: check for null/undefined before generating call to length in js?
|
||||
# Also: c backend repr of a nil string is <pointer>"", but repr of an
|
||||
# array of string that is not initialized is [nil, nil, ...] ??
|
||||
add(result, "nil")
|
||||
else:
|
||||
reprStrAux(result, s, s.len)
|
||||
reprStrAux(result, s, s.len)
|
||||
|
||||
proc addSetElem(result: var string, elem: int, typ: PNimType) =
|
||||
# Dispatch each set element to the correct repr<Type> proc
|
||||
@@ -114,7 +95,6 @@ proc reprSetAux(result: var string, s: int, typ: PNimType) =
|
||||
add(result, "}")
|
||||
|
||||
proc reprSet(e: int, typ: PNimType): string {.compilerRtl.} =
|
||||
result = ""
|
||||
reprSetAux(result, e, typ)
|
||||
|
||||
type
|
||||
@@ -149,7 +129,7 @@ proc reprArray(a: pointer, typ: PNimType,
|
||||
|
||||
add(result, "]")
|
||||
|
||||
proc isPointedToNil(p: pointer): bool {.inline.}=
|
||||
proc isPointedToNil(p: pointer): bool =
|
||||
{. emit: "if (`p` === null) {`result` = true};\n" .}
|
||||
|
||||
proc reprRef(result: var string, p: pointer, typ: PNimType,
|
||||
@@ -191,7 +171,6 @@ proc reprRecordAux(result: var string, o: pointer, typ: PNimType, cl: var ReprCl
|
||||
add(result, "]")
|
||||
|
||||
proc reprRecord(o: pointer, typ: PNimType, cl: var ReprClosure): string {.compilerRtl.} =
|
||||
result = ""
|
||||
reprRecordAux(result, o, typ, cl)
|
||||
|
||||
|
||||
@@ -257,6 +236,5 @@ proc reprAux(result: var string, p: pointer, typ: PNimType,
|
||||
proc reprAny(p: pointer, typ: PNimType): string {.compilerRtl.} =
|
||||
var cl: ReprClosure
|
||||
initReprClosure(cl)
|
||||
result = ""
|
||||
reprAux(result, p, typ, cl)
|
||||
add(result, "\n")
|
||||
add(result, "\n")
|
||||
|
||||
22
tests/js/tunittest_error.nim
Normal file
22
tests/js/tunittest_error.nim
Normal file
@@ -0,0 +1,22 @@
|
||||
discard """
|
||||
exitcode: 1
|
||||
outputsub: "[FAILED] with exception"
|
||||
"""
|
||||
|
||||
import unittest
|
||||
|
||||
proc ddd() =
|
||||
raise newException(IOError, "didn't do stuff")
|
||||
|
||||
proc ccc() =
|
||||
ddd()
|
||||
|
||||
proc bbb() =
|
||||
ccc()
|
||||
|
||||
proc aaa() =
|
||||
bbb()
|
||||
|
||||
test "with exception":
|
||||
check 3 == 3
|
||||
aaa()
|
||||
Reference in New Issue
Block a user