takashi kadono
/
Nucleo_446
Color Oled(SSD1331) connect to STMicroelectronics Nucleo-F466
mbed-os/UNITTESTS/unit_test/new.py@3:f3764f852aa8, 2018-10-11 (annotated)
- Committer:
- kadonotakashi
- Date:
- Thu Oct 11 02:27:46 2018 +0000
- Revision:
- 3:f3764f852aa8
- Parent:
- 0:8fdf9a60065b
Nucreo 446 + SSD1331 test version;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kadonotakashi | 0:8fdf9a60065b | 1 | """ |
kadonotakashi | 0:8fdf9a60065b | 2 | Copyright (c) 2018, Arm Limited |
kadonotakashi | 0:8fdf9a60065b | 3 | SPDX-License-Identifier: Apache-2.0 |
kadonotakashi | 0:8fdf9a60065b | 4 | |
kadonotakashi | 0:8fdf9a60065b | 5 | Licensed under the Apache License, Version 2.0 (the "License"); |
kadonotakashi | 0:8fdf9a60065b | 6 | you may not use this file except in compliance with the License. |
kadonotakashi | 0:8fdf9a60065b | 7 | You may obtain a copy of the License at |
kadonotakashi | 0:8fdf9a60065b | 8 | |
kadonotakashi | 0:8fdf9a60065b | 9 | http://www.apache.org/licenses/LICENSE-2.0 |
kadonotakashi | 0:8fdf9a60065b | 10 | |
kadonotakashi | 0:8fdf9a60065b | 11 | Unless required by applicable law or agreed to in writing, software |
kadonotakashi | 0:8fdf9a60065b | 12 | distributed under the License is distributed on an "AS IS" BASIS, |
kadonotakashi | 0:8fdf9a60065b | 13 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
kadonotakashi | 0:8fdf9a60065b | 14 | See the License for the specific language governing permissions and |
kadonotakashi | 0:8fdf9a60065b | 15 | limitations under the License. |
kadonotakashi | 0:8fdf9a60065b | 16 | |
kadonotakashi | 0:8fdf9a60065b | 17 | |
kadonotakashi | 0:8fdf9a60065b | 18 | UNIT TEST GENERATE NEW UNIT TESTS |
kadonotakashi | 0:8fdf9a60065b | 19 | """ |
kadonotakashi | 0:8fdf9a60065b | 20 | |
kadonotakashi | 0:8fdf9a60065b | 21 | from __future__ import print_function |
kadonotakashi | 0:8fdf9a60065b | 22 | import os |
kadonotakashi | 0:8fdf9a60065b | 23 | import re |
kadonotakashi | 0:8fdf9a60065b | 24 | import logging |
kadonotakashi | 0:8fdf9a60065b | 25 | |
kadonotakashi | 0:8fdf9a60065b | 26 | class UnitTestGeneratorTool(object): |
kadonotakashi | 0:8fdf9a60065b | 27 | """ |
kadonotakashi | 0:8fdf9a60065b | 28 | Generator tool to create new unit tests from template |
kadonotakashi | 0:8fdf9a60065b | 29 | """ |
kadonotakashi | 0:8fdf9a60065b | 30 | |
kadonotakashi | 0:8fdf9a60065b | 31 | def _replace_content(self, template_content, dirname, classname, extension): |
kadonotakashi | 0:8fdf9a60065b | 32 | if extension == "h": |
kadonotakashi | 0:8fdf9a60065b | 33 | content = re.sub(r"cppfile", |
kadonotakashi | 0:8fdf9a60065b | 34 | "", |
kadonotakashi | 0:8fdf9a60065b | 35 | template_content) |
kadonotakashi | 0:8fdf9a60065b | 36 | elif extension == "cpp": |
kadonotakashi | 0:8fdf9a60065b | 37 | content = re.sub(r"cppfile", |
kadonotakashi | 0:8fdf9a60065b | 38 | "../dirname/template.cpp", |
kadonotakashi | 0:8fdf9a60065b | 39 | template_content) |
kadonotakashi | 0:8fdf9a60065b | 40 | |
kadonotakashi | 0:8fdf9a60065b | 41 | content = re.sub(r"headerfile", "../dirname/template.h", content) |
kadonotakashi | 0:8fdf9a60065b | 42 | content = re.sub(r"dirname", dirname, content) |
kadonotakashi | 0:8fdf9a60065b | 43 | content = re.sub(r"template", classname, content) |
kadonotakashi | 0:8fdf9a60065b | 44 | |
kadonotakashi | 0:8fdf9a60065b | 45 | return content |
kadonotakashi | 0:8fdf9a60065b | 46 | |
kadonotakashi | 0:8fdf9a60065b | 47 | def create(self, mbed_os_root=None, filepath=None): |
kadonotakashi | 0:8fdf9a60065b | 48 | """ |
kadonotakashi | 0:8fdf9a60065b | 49 | Generate a unit test for a file to be tested from templates |
kadonotakashi | 0:8fdf9a60065b | 50 | |
kadonotakashi | 0:8fdf9a60065b | 51 | Keyword arguments: |
kadonotakashi | 0:8fdf9a60065b | 52 | mbed_os_root - Mbed OS root directory |
kadonotakashi | 0:8fdf9a60065b | 53 | filepath - Path to the file to be tested |
kadonotakashi | 0:8fdf9a60065b | 54 | """ |
kadonotakashi | 0:8fdf9a60065b | 55 | |
kadonotakashi | 0:8fdf9a60065b | 56 | if filepath is None: |
kadonotakashi | 0:8fdf9a60065b | 57 | logging.error("No filepath given. Cannot create a new unit test.") |
kadonotakashi | 0:8fdf9a60065b | 58 | return |
kadonotakashi | 0:8fdf9a60065b | 59 | |
kadonotakashi | 0:8fdf9a60065b | 60 | if mbed_os_root is None: |
kadonotakashi | 0:8fdf9a60065b | 61 | mbed_os_root = \ |
kadonotakashi | 0:8fdf9a60065b | 62 | os.path.normpath(os.path.join(os.path.dirname(__file__), |
kadonotakashi | 0:8fdf9a60065b | 63 | "../..")) |
kadonotakashi | 0:8fdf9a60065b | 64 | if os.path.isabs(filepath): |
kadonotakashi | 0:8fdf9a60065b | 65 | filepath = os.path.join( |
kadonotakashi | 0:8fdf9a60065b | 66 | os.path.relpath(os.path.dirname(filepath), mbed_os_root), |
kadonotakashi | 0:8fdf9a60065b | 67 | os.path.basename(filepath)) |
kadonotakashi | 0:8fdf9a60065b | 68 | |
kadonotakashi | 0:8fdf9a60065b | 69 | if not re.match(r"((\w+\/)+)(\w+)\.(c|cpp|h|hpp)$", filepath): |
kadonotakashi | 0:8fdf9a60065b | 70 | logging.error( |
kadonotakashi | 0:8fdf9a60065b | 71 | "No proper path to source file given.") |
kadonotakashi | 0:8fdf9a60065b | 72 | return |
kadonotakashi | 0:8fdf9a60065b | 73 | |
kadonotakashi | 0:8fdf9a60065b | 74 | if not os.path.isfile(os.path.join(mbed_os_root, filepath)): |
kadonotakashi | 0:8fdf9a60065b | 75 | logging.warning("Cannot find source file: %s from Mbed OS.", |
kadonotakashi | 0:8fdf9a60065b | 76 | filepath) |
kadonotakashi | 0:8fdf9a60065b | 77 | |
kadonotakashi | 0:8fdf9a60065b | 78 | dir_name = re.sub(r"(\\|\/)(\w+)\.(cpp|h)$", "", filepath) |
kadonotakashi | 0:8fdf9a60065b | 79 | |
kadonotakashi | 0:8fdf9a60065b | 80 | match = re.search(r"(\\|\/)(\w+)\.(cpp|h)$", filepath) |
kadonotakashi | 0:8fdf9a60065b | 81 | |
kadonotakashi | 0:8fdf9a60065b | 82 | if match: |
kadonotakashi | 0:8fdf9a60065b | 83 | class_name = match.group(2) |
kadonotakashi | 0:8fdf9a60065b | 84 | file_extension = match.group(3) |
kadonotakashi | 0:8fdf9a60065b | 85 | |
kadonotakashi | 0:8fdf9a60065b | 86 | # Create directory tree if not exist. |
kadonotakashi | 0:8fdf9a60065b | 87 | test_dir = os.path.join(mbed_os_root, "UNITTESTS", dir_name, class_name) |
kadonotakashi | 0:8fdf9a60065b | 88 | |
kadonotakashi | 0:8fdf9a60065b | 89 | if not os.path.exists(test_dir): |
kadonotakashi | 0:8fdf9a60065b | 90 | os.makedirs(test_dir) |
kadonotakashi | 0:8fdf9a60065b | 91 | else: |
kadonotakashi | 0:8fdf9a60065b | 92 | logging.error("Unit tests exist already for this file.") |
kadonotakashi | 0:8fdf9a60065b | 93 | return |
kadonotakashi | 0:8fdf9a60065b | 94 | |
kadonotakashi | 0:8fdf9a60065b | 95 | try: |
kadonotakashi | 0:8fdf9a60065b | 96 | suite_name = ("%s/%s" % (dir_name, class_name)).replace("/", "_") |
kadonotakashi | 0:8fdf9a60065b | 97 | |
kadonotakashi | 0:8fdf9a60065b | 98 | # Test source file template |
kadonotakashi | 0:8fdf9a60065b | 99 | template_source_file = \ |
kadonotakashi | 0:8fdf9a60065b | 100 | os.path.join(mbed_os_root, |
kadonotakashi | 0:8fdf9a60065b | 101 | "UNITTESTS/template/test_template.cpp") |
kadonotakashi | 0:8fdf9a60065b | 102 | |
kadonotakashi | 0:8fdf9a60065b | 103 | with open(template_source_file, "r") as input_file: |
kadonotakashi | 0:8fdf9a60065b | 104 | template_content = input_file.read() |
kadonotakashi | 0:8fdf9a60065b | 105 | |
kadonotakashi | 0:8fdf9a60065b | 106 | output_source_file = os.path.join(test_dir, |
kadonotakashi | 0:8fdf9a60065b | 107 | "test_%s.cpp" % class_name) |
kadonotakashi | 0:8fdf9a60065b | 108 | |
kadonotakashi | 0:8fdf9a60065b | 109 | with open(output_source_file, "w") as output_file: |
kadonotakashi | 0:8fdf9a60065b | 110 | content = self._replace_content(template_content, |
kadonotakashi | 0:8fdf9a60065b | 111 | dir_name, |
kadonotakashi | 0:8fdf9a60065b | 112 | class_name, |
kadonotakashi | 0:8fdf9a60065b | 113 | file_extension) |
kadonotakashi | 0:8fdf9a60065b | 114 | |
kadonotakashi | 0:8fdf9a60065b | 115 | output_file.writelines(content) |
kadonotakashi | 0:8fdf9a60065b | 116 | |
kadonotakashi | 0:8fdf9a60065b | 117 | # Definition file template |
kadonotakashi | 0:8fdf9a60065b | 118 | template_source_file = \ |
kadonotakashi | 0:8fdf9a60065b | 119 | os.path.join(mbed_os_root, |
kadonotakashi | 0:8fdf9a60065b | 120 | "UNITTESTS/template/unittest.cmake.template") |
kadonotakashi | 0:8fdf9a60065b | 121 | |
kadonotakashi | 0:8fdf9a60065b | 122 | with open(template_source_file, "r") as input_file: |
kadonotakashi | 0:8fdf9a60065b | 123 | template_content = input_file.read() |
kadonotakashi | 0:8fdf9a60065b | 124 | |
kadonotakashi | 0:8fdf9a60065b | 125 | output_source_file = os.path.join(test_dir, "unittest.cmake") |
kadonotakashi | 0:8fdf9a60065b | 126 | |
kadonotakashi | 0:8fdf9a60065b | 127 | with open(output_source_file, "w") as output_file: |
kadonotakashi | 0:8fdf9a60065b | 128 | content = self._replace_content(template_content, |
kadonotakashi | 0:8fdf9a60065b | 129 | dir_name, |
kadonotakashi | 0:8fdf9a60065b | 130 | class_name, |
kadonotakashi | 0:8fdf9a60065b | 131 | file_extension) |
kadonotakashi | 0:8fdf9a60065b | 132 | |
kadonotakashi | 0:8fdf9a60065b | 133 | output_file.writelines(content) |
kadonotakashi | 0:8fdf9a60065b | 134 | |
kadonotakashi | 0:8fdf9a60065b | 135 | logging.info(""" |
kadonotakashi | 0:8fdf9a60065b | 136 | Unit test %s created. |
kadonotakashi | 0:8fdf9a60065b | 137 | |
kadonotakashi | 0:8fdf9a60065b | 138 | Generated files into %s directory. |
kadonotakashi | 0:8fdf9a60065b | 139 | """, suite_name, test_dir) |
kadonotakashi | 0:8fdf9a60065b | 140 | |
kadonotakashi | 0:8fdf9a60065b | 141 | except IOError: |
kadonotakashi | 0:8fdf9a60065b | 142 | logging.error("Cannot create files for a unit test.") |