From 0137b446891d40dd6963109625b0d07f1ac46164 Mon Sep 17 00:00:00 2001 From: Vyacheslav Matyukhin Date: Wed, 5 Oct 2022 16:22:29 +0400 Subject: [PATCH] more cleanups, some tests --- .../squiggle-lang/__tests__/SqError_test.res | 41 +++++++++++++++++++ packages/squiggle-lang/src/js/SqError.ts | 2 - .../rescript/Reducer/Reducer_FrameStack.res | 4 +- 3 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 packages/squiggle-lang/__tests__/SqError_test.res diff --git a/packages/squiggle-lang/__tests__/SqError_test.res b/packages/squiggle-lang/__tests__/SqError_test.res new file mode 100644 index 00000000..55109dde --- /dev/null +++ b/packages/squiggle-lang/__tests__/SqError_test.res @@ -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 +`) + }) +}) diff --git a/packages/squiggle-lang/src/js/SqError.ts b/packages/squiggle-lang/src/js/SqError.ts index a2bd1154..5c63d907 100644 --- a/packages/squiggle-lang/src/js/SqError.ts +++ b/packages/squiggle-lang/src/js/SqError.ts @@ -43,8 +43,6 @@ export class SqFrame { } location() { - console.log(RSFrameStack); - console.log(RSFrameStack.Frame); return RSFrameStack.Frame.getLocation(this._value); } } diff --git a/packages/squiggle-lang/src/rescript/Reducer/Reducer_FrameStack.res b/packages/squiggle-lang/src/rescript/Reducer/Reducer_FrameStack.res index a1dfd691..b7370e00 100644 --- a/packages/squiggle-lang/src/rescript/Reducer/Reducer_FrameStack.res +++ b/packages/squiggle-lang/src/rescript/Reducer/Reducer_FrameStack.res @@ -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 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")