practracker_tests.py 1.7 KB

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