practracker_tests.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #!/usr/bin/python
  2. """Some simple tests for practracker metrics"""
  3. import unittest
  4. try:
  5. # python 2 names the module this way...
  6. from StringIO import StringIO
  7. except ImportError:
  8. # python 3 names the module this way.
  9. from io import StringIO
  10. import metrics
  11. function_file = """static void
  12. fun(directory_request_t *req, const char *resource)
  13. {
  14. time_t if_modified_since = 0;
  15. uint8_t or_diff_from[DIGEST256_LEN];
  16. }
  17. static void
  18. fun(directory_request_t *req,
  19. const char *resource)
  20. {
  21. time_t if_modified_since = 0;
  22. uint8_t or_diff_from[DIGEST256_LEN];
  23. }
  24. MOCK_IMPL(void,
  25. fun,(
  26. uint8_t dir_purpose,
  27. uint8_t router_purpose,
  28. const char *resource,
  29. int pds_flags,
  30. download_want_authority_t want_authority))
  31. {
  32. const routerstatus_t *rs = NULL;
  33. const or_options_t *options = get_options();
  34. }
  35. """
  36. class TestFunctionLength(unittest.TestCase):
  37. def test_function_length(self):
  38. funcs = StringIO(function_file)
  39. # All functions should have length 2
  40. for name, lines in metrics.get_function_lines(funcs):
  41. self.assertEqual(name, "fun")
  42. funcs.seek(0)
  43. for name, lines in metrics.get_function_lines(funcs):
  44. self.assertEqual(lines, 4)
  45. class TestIncludeCount(unittest.TestCase):
  46. def test_include_count(self):
  47. f = StringIO("""
  48. # include <abc.h>
  49. # include "def.h"
  50. #include "ghi.h"
  51. \t#\t include "jkl.h"
  52. """)
  53. self.assertEqual(metrics.get_include_count(f),4)
  54. if __name__ == '__main__':
  55. unittest.main()