1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
|
# Copyright (C) 2010-2023 Apple Inc. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# Examples of how to run:
# python3 Source/WebKit/Scripts/webkit/messages_unittest.py [-r]
# cd Source/WebKit/Scripts && python3 -m webkit.messages_unittest
# cd Source/WebKit/Scripts && python3 -m unittest discover -p '*_unittest.py'
from __future__ import print_function
import os
import re
import sys
import unittest
module_directory = os.path.dirname(os.path.abspath(__file__))
sys.path.append(os.path.abspath(os.path.join(module_directory, os.path.pardir)))
from webkit import messages, model, parser # noqa: E402
tests_directory = os.path.join(module_directory, 'tests')
reset_results = False
_test_receiver_names = [
'TestWithSuperclass',
'TestWithLegacyReceiver',
'TestWithoutAttributes',
'TestWithIfMessage',
'TestWithSemaphore',
'TestWithImageData',
'TestWithStream',
'TestWithStreamBatched',
'TestWithStreamBuffer',
'TestWithCVPixelBuffer',
'TestWithStreamServerConnectionHandle',
]
def receiver_header_file_name(receiver_name):
return '%sMessages.h' % receiver_name
def receiver_implementation_file_name(receiver_name):
return '%sMessageReceiver.cpp' % receiver_name
def parse_receiver(receiver_name):
with open(os.path.join(tests_directory, '{}.messages.in'.format(receiver_name))) as in_file:
return parser.parse(in_file)
assert(False)
class GeneratedFileContentsTest(unittest.TestCase):
def setUp(self):
self.test_receivers = [parse_receiver(receiver_name) for receiver_name in _test_receiver_names]
self.receivers = model.generate_global_model(self.test_receivers)
def assertGeneratedFileContentsEqual(self, actual_file_contents, expected_file_name):
try:
if reset_results:
with open(os.path.join(tests_directory, expected_file_name), mode='w') as out_file:
out_file.write(actual_file_contents)
return
with open(os.path.join(tests_directory, expected_file_name), mode='r') as in_file:
expected_file_contents = in_file.read()
actual_line_list = actual_file_contents.splitlines(False)
expected_line_list = expected_file_contents.splitlines(False)
for index, actual_line in enumerate(actual_line_list):
self.assertEqual(actual_line, expected_line_list[index])
self.assertEqual(len(actual_line_list), len(expected_line_list))
except Exception:
sys.stderr.write('In expected file %s\n' % expected_file_name)
raise
def test_receiver(self):
for receiver_name, receiver in zip(_test_receiver_names, self.test_receivers):
header_contents = messages.generate_messages_header(receiver)
self.assertGeneratedFileContentsEqual(header_contents, os.path.join(tests_directory, '{}Messages.h'.format(receiver_name)))
implementation_contents = messages.generate_message_handler(receiver)
self.assertGeneratedFileContentsEqual(implementation_contents, os.path.join(tests_directory, '{}MessageReceiver.cpp'.format(receiver_name)))
def test_message_names(self):
header_contents = messages.generate_message_names_header(self.receivers)
self.assertGeneratedFileContentsEqual(header_contents, os.path.join(tests_directory, 'MessageNames.h'))
implementation_contents = messages.generate_message_names_implementation(self.receivers)
self.assertGeneratedFileContentsEqual(implementation_contents, os.path.join(tests_directory, 'MessageNames.cpp'))
def test_message_argument_description(self):
receiver_header_files = ['{}Messages.h'.format(receiver.name) for receiver in self.receivers]
implementation_contents = messages.generate_message_argument_description_implementation(self.receivers, receiver_header_files)
self.assertGeneratedFileContentsEqual(implementation_contents, os.path.join(tests_directory, 'MessageArgumentDescriptions.cpp'))
def parse_sys_argv():
global reset_results
for index, arg in enumerate(sys.argv[1:]):
if arg in ('-r', '--r', '--reset', '--reset-results') or '--reset-results'.startswith(arg):
reset_results = True
del sys.argv[index + 1]
break
for index, arg in enumerate(sys.argv[1:]):
if arg in ('-h', '--h', '--help') or '--help'.startswith(arg):
print('''
Custom Options:
-r, --reset-results Reset expected results for {script_name}
'''.format(script_name=os.path.basename(__file__)))
break
if __name__ == '__main__':
parse_sys_argv()
unittest.main()
|