test_mavexpression.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #!/usr/bin/env python
  2. """
  3. Unit tests for the mavexpression library
  4. """
  5. from __future__ import print_function
  6. import unittest
  7. import random
  8. from pymavlink import mavexpression
  9. class ExpressionTest(unittest.TestCase):
  10. """
  11. Class to test evaluate_expression
  12. """
  13. def __init__(self, *args, **kwargs):
  14. """Constructor, set up some data that is reused in many tests"""
  15. self.varsDict = {}
  16. self.varsDict['lat'] = 5.67
  17. self.varsDict['speed'] = 8
  18. super(ExpressionTest, self).__init__(*args, **kwargs)
  19. def test_novars(self):
  20. """Test the evaluate_expression functionality"""
  21. assert mavexpression.evaluate_expression('1+2', {}) == 3
  22. assert mavexpression.evaluate_expression('4/0', {}) is None
  23. assert mavexpression.evaluate_expression('A+4', {}) is None
  24. def test_vars(self):
  25. """Test the evaluate_expression functionality with local vars"""
  26. assert mavexpression.evaluate_expression('lat+10', self.varsDict) == 15.67
  27. assert mavexpression.evaluate_expression('4.0/speed', self.varsDict) == 0.5
  28. assert mavexpression.evaluate_expression('speed+lat+wrong', self.varsDict) is None
  29. def test_mavextra(self):
  30. """Test evaluate_expression using the functions in mavextra.py"""
  31. assert mavexpression.evaluate_expression('kmh(10)', {}) == 36
  32. assert mavexpression.evaluate_expression('angle_diff(170, -90)', {}) == -100
  33. if __name__ == '__main__':
  34. unittest.main()