more cleanups, some tests

This commit is contained in:
Vyacheslav Matyukhin 2022-10-05 16:22:29 +04:00
parent 6055320aa2
commit 0137b44689
No known key found for this signature in database
GPG Key ID: 3D2A774C5489F96C
3 changed files with 44 additions and 3 deletions

View File

@ -0,0 +1,41 @@
open Jest
open Expect
describe("SqError.Message", () => {
test("toString", () =>
expect(SqError.Message.REOther("test error")->SqError.Message.toString)->toBe(
"Error: test error",
)
)
})
describe("SqError", () => {
test("fromMessage", () =>
expect(SqError.Message.REOther("test error")->SqError.fromMessage->SqError.toString)->toBe(
"Error: test error",
)
)
test("toStringWithStackTrace with empty stacktrace", () =>
expect(
SqError.Message.REOther("test error")->SqError.fromMessage->SqError.toStringWithStackTrace,
)->toBe("Error: test error")
)
test("toStringWithStackTrace", () => {
let frameStack =
Reducer_FrameStack.make()
->Reducer_FrameStack.extend("frame1", None)
->Reducer_FrameStack.extend("frame2", None)
expect(
SqError.Message.REOther("test error")
->SqError.fromMessageWithFrameStack(frameStack)
->SqError.toStringWithStackTrace,
)->toBe(`Error: test error
Stack trace:
frame2
frame1
`)
})
})

View File

@ -43,8 +43,6 @@ export class SqFrame {
}
location() {
console.log(RSFrameStack);
console.log(RSFrameStack.Frame);
return RSFrameStack.Frame.getLocation(this._value);
}
}

View File

@ -8,7 +8,7 @@ module Frame = {
name ++
switch location {
| Some(location) =>
` at line ${location.start.line->Js.Int.toString}, column ${location.start.column->Js.Int.toString}`
` at line ${location.start.line->Js.Int.toString}, column ${location.start.column->Js.Int.toString}` // TODO - source id?
| None => ""
}
@ -27,9 +27,11 @@ let extend = (t: t, name: string, location: option<Reducer_Peggy_Parse.location>
location: location,
})
// this is useful for SyntaxErrors
let makeSingleFrameStack = (location: Reducer_Peggy_Parse.location): t =>
make()->extend(Reducer_T.topFrameName, Some(location))
// this includes the left offset because it's mostly used in SqError.toStringWithStackTrace
let toString = (t: t) =>
t
->Belt.List.map(s => " " ++ s->Frame.toString ++ "\n")