# This is the test suite for the Math API
#
# It is used with *all* the example shims in this directory.

# Shim Data Formatting
# --------------------
# 
# The 'send' directive should be formatted thus:
#
#  { "command":"OPERATION",
#    "operands":[ x, y, ..., z ] }
#
# Where OPERATION is one of the operations defined
# below, and 'x, y, ..., z' are the values to be acted
# upon..
#
# The 'expect' directive (and therefore the data coming back from the
# shim) should be formatted thus:
#
#  { "command":"OPERATION", "result":RESULT }
#
#  Where "OPERATION" is the operation specified in the 'sent'
#  directive, and "RESULT" is the result of evaluating the given
#  operation with the given operands.
#
#  In the case of an error, the value of "RESULT" must be the string
#  "ERROR", and a third key, "err_msg", must be added. It will hold
#  the human-readable message describing the problem:
#
#  { "command":"OPERATION", "result":"ERROR", "err_msg":"MESSAGE" }



# first we need to set a test plan. it should be set to the number of
# tests in this file

begin environment
  diag Running the standard Catechesis test suite!
  plan 8
end

# now we'll run some successful tests

begin test
  diag These tests should succeed if you have implemented the spec commands correctly
  desc Simple addition
  send { "command":"add", "operands":[2, 2] }
  expect { "command":"add", "result":4 }
end

begin test
  desc Subtraction
  send { "command":"subtract", "operands":[5, 3] }
  expect { "command":"subtract", "result":2 }
end

begin test
  desc Multiplication
  send { "command":"multiply", "operands":[5, 3] }
  expect { "command":"multiply", "result":15 }
end

begin test
  desc Division
  send { "command":"divide", "operands":[10, 2] }
  expect { "command":"divide", "result":5 }
end

# followed by API-specified exceptions

begin test
  diag These should pass if you have implemented spec error handling correctly
  desc Missing operands 1
  send { "command":"add" }
  expect {"command":"add", "result":"ERROR", \
          "err_msg":"Missing operand: add requires 2; I found 0"}
end
begin test
  desc Missing operands 2
  send { "command":"add", "operands":[1] }
  expect {"command":"add", "result":"ERROR", \
          "err_msg":"Missing operand: add requires 2; I found 1"}
end

begin test
  desc Non-integer operands
  send { "command":"add", "operands":[1,"q"] }
  expect {"command":"add", "result":"ERROR", \
          "err_msg":"Non-integer operand found: q"}
end

begin test
  desc Division by zero
  send { "command":"divide", "operands":[1,0] }
  expect {"command":"divide", "result":"ERROR", \
          "err_msg":"Division by zero is undefined"}
end
