inspect and inspect with label (tested)

This commit is contained in:
Umur Ozkul 2022-04-22 15:43:37 +02:00
parent 2c5c5095b8
commit 417f0060b6
2 changed files with 25 additions and 4 deletions

View File

@ -0,0 +1,12 @@
open Jest
open Reducer_TestHelpers
/*
You can wrap around any expression with inspect(expr) to log the value of that expression.
This is useful for debugging. inspect(expr) returns the value of expr, but also prints it out.
There is a second version of inspect that takes a label, which will print out the label and the value.
*/
describe("Debugging", () => {
testEvalToBe("inspect(1)", "Ok(1)")
testEvalToBe("inspect(1, \"one\")", "Ok(1)")
})

View File

@ -43,16 +43,25 @@ let callInternal = (call: functionCall): result<'b, errorValue> => {
| None => RERecordPropertyNotFound("Record property not found", sIndex)->Error
}
let inspect = (value: expressionValue) => {
Js.log(`${value->toString}`)
value->Ok
}
let inspectLabel = (value: expressionValue, label: string) => {
Js.log(`${label}: ${value->toString}`)
value->Ok
}
switch call {
// | ("$constructRecord", pairArray)
// | ("$atIndex", [EvArray(anArray), EvNumber(fIndex)]) => arrayAtIndex(anArray, fIndex)
// | ("$atIndex", [EvRecord(aRecord), EvString(sIndex)]) => recordAtIndex(aRecord, sIndex)
| ("$constructRecord", [EvArray(arrayOfPairs)]) => constructRecord(arrayOfPairs)
| ("$atIndex", [EvArray(aValueArray), EvArray([EvNumber(fIndex)])]) =>
arrayAtIndex(aValueArray, fIndex)
| ("$atIndex", [EvRecord(dict), EvArray([EvString(sIndex)])]) => recordAtIndex(dict, sIndex)
| ("$atIndex", [obj, index]) =>
(toStringWithType(obj) ++ "??~~~~" ++ toStringWithType(index))->EvString->Ok
| ("$constructRecord", [EvArray(arrayOfPairs)]) => constructRecord(arrayOfPairs)
| ("inspect", [value, EvString(label)]) => inspectLabel(value, label)
| ("inspect", [value]) => inspect(value)
| call => callMathJs(call)
}
}