upytesthelper.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2017 Linaro Limited
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include <string.h>
  27. #include "py/mphal.h"
  28. #include "py/gc.h"
  29. #include "py/runtime.h"
  30. #include "py/compile.h"
  31. #include "upytesthelper.h"
  32. static const char *test_exp_output;
  33. static int test_exp_output_len, test_rem_output_len;
  34. static int test_failed;
  35. static void *heap_start, *heap_end;
  36. void upytest_set_heap(void *start, void *end) {
  37. heap_start = start;
  38. heap_end = end;
  39. }
  40. void upytest_set_expected_output(const char *output, unsigned len) {
  41. test_exp_output = output;
  42. test_exp_output_len = test_rem_output_len = len;
  43. test_failed = false;
  44. }
  45. bool upytest_is_failed(void) {
  46. if (test_failed) {
  47. return true;
  48. }
  49. #if 0
  50. if (test_rem_output_len != 0) {
  51. printf("remaining len: %d\n", test_rem_output_len);
  52. }
  53. #endif
  54. return test_rem_output_len != 0;
  55. }
  56. // MP_PLAT_PRINT_STRN() should be redirected to this function.
  57. // It will pass-thru any content to mp_hal_stdout_tx_strn_cooked()
  58. // (the dfault value of MP_PLAT_PRINT_STRN), but will also match
  59. // it to the expected output as set by upytest_set_expected_output().
  60. // If mismatch happens, upytest_is_failed() returns true.
  61. void upytest_output(const char *str, mp_uint_t len) {
  62. if (!test_failed) {
  63. if (len > test_rem_output_len) {
  64. test_failed = true;
  65. } else {
  66. test_failed = memcmp(test_exp_output, str, len);
  67. #if 0
  68. if (test_failed) {
  69. printf("failed after char %u, within %d chars, res: %d\n",
  70. test_exp_output_len - test_rem_output_len, (int)len, test_failed);
  71. for (int i = 0; i < len; i++) {
  72. if (str[i] != test_exp_output[i]) {
  73. printf("%d %02x %02x\n", i, str[i], test_exp_output[i]);
  74. }
  75. }
  76. }
  77. #endif
  78. test_exp_output += len;
  79. test_rem_output_len -= len;
  80. }
  81. }
  82. mp_hal_stdout_tx_strn_cooked(str, len);
  83. }
  84. void upytest_execute_test(const char *src) {
  85. // To provide clean room for each test, interpreter and heap are
  86. // reinitialized before running each.
  87. gc_init(heap_start, heap_end);
  88. mp_init();
  89. mp_obj_list_init(mp_sys_path, 0);
  90. mp_obj_list_init(mp_sys_argv, 0);
  91. nlr_buf_t nlr;
  92. if (nlr_push(&nlr) == 0) {
  93. mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, src, strlen(src), 0);
  94. qstr source_name = lex->source_name;
  95. mp_parse_tree_t parse_tree = mp_parse(lex, MP_PARSE_FILE_INPUT);
  96. mp_obj_t module_fun = mp_compile(&parse_tree, source_name, MP_EMIT_OPT_NONE, false);
  97. mp_call_function_0(module_fun);
  98. nlr_pop();
  99. } else {
  100. mp_obj_t exc = (mp_obj_t)nlr.ret_val;
  101. if (mp_obj_is_subclass_fast(mp_obj_get_type(exc), &mp_type_SystemExit)) {
  102. // Assume that sys.exit() is called to skip the test.
  103. // TODO: That can be always true, we should set up convention to
  104. // use specific exit code as skip indicator.
  105. tinytest_set_test_skipped_();
  106. goto end;
  107. }
  108. mp_obj_print_exception(&mp_plat_print, exc);
  109. tt_abort_msg("Uncaught exception\n");
  110. }
  111. if (upytest_is_failed()) {
  112. tinytest_set_test_failed_();
  113. }
  114. end:
  115. mp_deinit();
  116. }