test.lua 714 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. -- Some primitives for tests.
  2. function check(x, msg)
  3. if not x then
  4. error(msg)
  5. end
  6. end
  7. function check_nil(x, msg)
  8. if x ~= nil then
  9. error(msg..'; should be nil but got "'..x..'"')
  10. end
  11. end
  12. function check_eq(x, expected, msg)
  13. if not eq(x, expected) then
  14. error(msg..'; should be "'..expected..'" but got "'..x..'"')
  15. end
  16. end
  17. function eq(a, b)
  18. if type(a) ~= type(b) then return false end
  19. if type(a) == 'table' then
  20. if #a ~= #b then return false end
  21. for k, v in pairs(a) do
  22. if not eq(b[k], v) then
  23. return false
  24. end
  25. end
  26. for k, v in pairs(b) do
  27. if not eq(a[k], v) then
  28. return false
  29. end
  30. end
  31. return true
  32. end
  33. return a == b
  34. end