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 
28 #include "py/mphal.h"
29 #include "py/gc.h"
30 #include "py/runtime.h"
31 #include "py/compile.h"
32 #include "upytesthelper.h"
33 
34 static const char *test_exp_output;
35 static int test_exp_output_len, test_rem_output_len;
36 static int test_failed;
37 static void *heap_start, *heap_end;
38 
upytest_set_heap(void * start,void * end)39 void upytest_set_heap(void *start, void *end) {
40     heap_start = start;
41     heap_end = end;
42 }
43 
upytest_set_expected_output(const char * output,unsigned len)44 void upytest_set_expected_output(const char *output, unsigned len) {
45     test_exp_output = output;
46     test_exp_output_len = test_rem_output_len = len;
47     test_failed = false;
48 }
49 
upytest_is_failed(void)50 bool upytest_is_failed(void) {
51     if (test_failed) {
52         return true;
53     }
54     #if 0
55     if (test_rem_output_len != 0) {
56         printf("remaining len: %d\n", test_rem_output_len);
57     }
58     #endif
59     return test_rem_output_len != 0;
60 }
61 
62 // MP_PLAT_PRINT_STRN() should be redirected to this function.
63 // It will pass-thru any content to mp_hal_stdout_tx_strn_cooked()
64 // (the dfault value of MP_PLAT_PRINT_STRN), but will also match
65 // it to the expected output as set by upytest_set_expected_output().
66 // If mismatch happens, upytest_is_failed() returns true.
upytest_output(const char * str,mp_uint_t len)67 void upytest_output(const char *str, mp_uint_t len) {
68     if (!test_failed) {
69         if (len > test_rem_output_len) {
70             test_failed = true;
71         } else {
72             test_failed = memcmp(test_exp_output, str, len);
73             #if 0
74             if (test_failed) {
75                 printf("failed after char %u, within %d chars, res: %d\n",
76                     test_exp_output_len - test_rem_output_len, (int)len, test_failed);
77                 for (int i = 0; i < len; i++) {
78                     if (str[i] != test_exp_output[i]) {
79                         printf("%d %02x %02x\n", i, str[i], test_exp_output[i]);
80                     }
81                 }
82             }
83             #endif
84             test_exp_output += len;
85             test_rem_output_len -= len;
86         }
87     }
88     mp_hal_stdout_tx_strn_cooked(str, len);
89 }
90 
upytest_execute_test(const char * src)91 void upytest_execute_test(const char *src) {
92     // To provide clean room for each test, interpreter and heap are
93     // reinitialized before running each.
94     gc_init(heap_start, heap_end);
95     mp_init();
96     mp_obj_list_init(mp_sys_path, 0);
97     mp_obj_list_init(mp_sys_argv, 0);
98 
99     nlr_buf_t nlr;
100     if (nlr_push(&nlr) == 0) {
101         mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, src, strlen(src), 0);
102         qstr source_name = lex->source_name;
103         mp_parse_tree_t parse_tree = mp_parse(lex, MP_PARSE_FILE_INPUT);
104         mp_obj_t module_fun = mp_compile(&parse_tree, source_name, false);
105         mp_call_function_0(module_fun);
106         nlr_pop();
107     } else {
108         mp_obj_t exc = (mp_obj_t)nlr.ret_val;
109         if (mp_obj_is_subclass_fast(mp_obj_get_type(exc), &mp_type_SystemExit)) {
110             // Assume that sys.exit() is called to skip the test.
111             // TODO: That can be always true, we should set up convention to
112             // use specific exit code as skip indicator.
113             tinytest_set_test_skipped_();
114             goto end;
115         }
116         mp_obj_print_exception(&mp_plat_print, exc);
117         tt_abort_msg("Uncaught exception\n");
118     }
119 
120     if (upytest_is_failed()) {
121         tinytest_set_test_failed_();
122     }
123 
124 end:
125     mp_deinit();
126 }
127