Search code examples
unit-testingtestingkdb

How can I compare two values in kdb/Q and output a message based on pass/fail?


I'm trying to build my own unit testing framework in Q, and what I'm trying to do is something like this:

// Is the expected value (EQUAL_TO/GREATER_THAN/LESS_THAN/ETC) the actual value?
// Object Object Object -> String
// checkExpect[5; =; 2+3] -> "Pass"
// checkExpect[1; =; 2+3] -> "Fail"

The function takes in 3 arguments, the expected output, the operator ('=', '>', etc.), and the actual value, such as a function or expression.

I'm having trouble piecing those 3 together, and on top of that outputting either a "Pass" or "Fail" statement to see if the entire function passes.


Solution

  • Apply x and z as arguments to y

    The result boolean 0b and 1b can be used to index in to a list to choose either Pass or Fail

    q)checkExpect:{("Fail";"Pass") y[x;z]}
    q)checkExpect[5; =; 2+3]
    "Pass"
    q)checkExpect[1; =; 2+3]
    "Fail"
    

    https://code.kx.com/q/basics/function-notation/

    x y z are special arguments you can skip naming

    {[x;y;z] x+y+z} is the same as {x+y+z}

    You might define your function with chosen names for clarity instead:

    checkExpect:{[a;test;b] ("Fail";"Pass") test[a;b]}