Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 # Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 # Use of this source code is governed by a BSD-style license that can be | |
| 3 # found in the LICENSE file. | |
| 4 """Generator script for Web Bluetooth LayoutTests. | |
| 5 | |
| 6 For each script-tests/X.js creates the following test files depending on the | |
| 7 contents of X.js | |
| 8 - getPrimaryService/X.html | |
| 9 - getPrimaryServices/X.html | |
| 10 - getPrimaryServices/X-with-uuid.html | |
| 11 | |
| 12 script-tests/X.js files should contain "CALLS([variation1 | variation2 | ...])" | |
| 13 tokens that indicate what files to generate. Each variation in CALLS([...]) | |
| 14 should corresponds to a js function call and its arguments. Additionally a | |
| 15 variation can end in [UUID] to indicate that the generated file's name should | |
| 16 have the -with-uuid suffix. | |
| 17 | |
| 18 The PREVIOUS_CALL token will be replaced with the function that replaced CALLS. | |
| 19 | |
| 20 The FUNCTION_NAME token will be replaced with the name of the function that | |
| 21 replaced CALLS. | |
| 22 | |
| 23 For example, for the following template file: | |
| 24 | |
| 25 // script-tests/example.js | |
| 26 promise_test(() => { | |
| 27 return navigator.bluetooth.requestDevice(...) | |
| 28 .then(device => device.gatt.CALLS([ | |
| 29 getPrimaryService('heart_rate')| | |
| 30 getPrimaryServices('heart_rate')[UUID]])) | |
| 31 .then(device => device.gatt.PREVIOUS_CALL); | |
| 32 }, 'example test for FUNCTION_NAME'); | |
| 33 | |
| 34 this script will generate: | |
| 35 | |
| 36 // getPrimaryService/example.html | |
| 37 promise_test(() => { | |
| 38 return navigator.bluetooth.requestDevice(...) | |
| 39 .then(device => device.gatt.getPrimaryService('heart_rate')) | |
| 40 .then(device => device.gatt.getPrimaryService('heart_rate')); | |
| 41 }, 'example test for getPrimaryService'); | |
| 42 | |
| 43 // getPrimaryServices/example-with-uuid.html | |
| 44 promise_test(() => { | |
| 45 return navigator.bluetooth.requestDevice(...) | |
| 46 .then(device => device.gatt.getPrimaryServices('heart_rate')) | |
| 47 .then(device => device.gatt.getPrimaryServices('heart_rate')); | |
| 48 }, 'example test for getPrimaryServices'); | |
| 49 | |
| 50 Run | |
| 51 $ python //third_party/WebKit/LayoutTests/bluetooth/generate.py | |
| 52 and commit the generated files. | |
| 53 """ | |
| 54 import glob | |
| 55 import os | |
| 56 import re | |
| 57 import sys | |
| 58 | |
| 59 TEMPLATES_DIR = 'script-tests' | |
| 60 | |
| 61 | |
| 62 class GeneratedTest: | |
| 63 | |
| 64 def __init__(self, data, path, template): | |
| 65 self.data = data | |
| 66 self.path = path | |
| 67 self.template = template | |
| 68 | |
| 69 | |
| 70 def GetGeneratedTests(): | |
| 71 """Yields a GeneratedTest for each call in templates in script-tests.""" | |
| 72 current_path = os.path.dirname(os.path.realpath(__file__)) | |
| 73 | |
| 74 # Read Base Test Template. | |
| 75 base_template_file_handle = open( | |
| 76 os.path.join(current_path, TEMPLATES_DIR, 'base_test_template.html')) | |
| 77 base_template_file_data = base_template_file_handle.read().decode('utf-8') | |
| 78 base_template_file_handle.close() | |
| 79 | |
| 80 # Get Templates. | |
| 81 available_templates = glob.glob( | |
| 82 os.path.join(current_path, TEMPLATES_DIR, '*.js')) | |
| 83 | |
| 84 # Generate Test Files | |
| 85 for template in available_templates: | |
| 86 # Read template | |
| 87 template_file_handle = open(template) | |
| 88 template_file_data = template_file_handle.read().decode('utf-8') | |
| 89 template_file_handle.close() | |
| 90 | |
| 91 template_name = os.path.splitext(os.path.basename(template))[0] | |
| 92 | |
| 93 result = re.search(r'CALLS\(\[(.*?)\]\)', template_file_data, re.MULTILINE | |
| 94 | re.DOTALL) | |
| 95 | |
| 96 if result is None: | |
| 97 raise Exception('Template must contain \'CALLS\' tokens') | |
| 98 | |
| 99 new_test_file_data = base_template_file_data.replace('TEST', | |
| 100 template_file_data) | |
| 101 # Replace CALLS([...]) with CALLS so that we don't have to replace the | |
| 102 # CALLS([...]) for every new test file. | |
| 103 new_test_file_data = new_test_file_data.replace(result.group(), 'CALLS') | |
| 104 | |
| 105 # Replace 'PREVIOUS_CALL' with 'CALLS' so that we can replace it while | |
| 106 # replacing CALLS. | |
| 107 while 'PREVIOUS_CALL' in new_test_file_data: | |
| 108 new_test_file_data = new_test_file_data.replace('PREVIOUS_CALL', 'CALLS') | |
|
Jeffrey Yasskin
2016/10/19 22:33:29
https://docs.python.org/2/library/stdtypes.html#st
ortuno
2016/10/20 00:27:03
Ah right. I got used to using the loop because in
| |
| 109 | |
| 110 calls = result.group(1) | |
| 111 calls = ''.join(calls.split()) # Removes whitespace. | |
| 112 calls = calls.split('|') | |
| 113 | |
| 114 for call in calls: | |
| 115 # Parse call | |
| 116 name, args, uuid_suffix = re.search(r'(.*?)\((.*?)\)(\[UUID\])?', | |
| 117 call).groups() | |
| 118 | |
| 119 # Replace template tokens | |
| 120 call_test_file_data = new_test_file_data | |
| 121 | |
| 122 while 'CALLS' in call_test_file_data: | |
| 123 call_test_file_data = call_test_file_data.replace( | |
| 124 'CALLS', '{}({})'.format(name, args)) | |
| 125 | |
| 126 while 'FUNCTION_NAME' in call_test_file_data: | |
| 127 call_test_file_data = call_test_file_data.replace('FUNCTION_NAME', name) | |
| 128 | |
| 129 # Get test file name | |
| 130 call_test_file_name = 'gen-{}{}.html'.format(template_name, '-with-uuid' | |
| 131 if uuid_suffix else '') | |
| 132 call_test_file_path = os.path.join(current_path, name, | |
| 133 call_test_file_name) | |
| 134 | |
| 135 yield GeneratedTest(call_test_file_data, call_test_file_path, template) | |
| 136 | |
| 137 | |
| 138 def main(): | |
| 139 | |
| 140 for generated_test in GetGeneratedTests(): | |
| 141 # Create or open test file | |
| 142 test_file_handle = open(generated_test.path, 'wb') | |
| 143 | |
| 144 # Write contents | |
| 145 test_file_handle.write(generated_test.data.encode('utf-8')) | |
| 146 test_file_handle.close() | |
| 147 | |
| 148 | |
| 149 if __name__ == '__main__': | |
| 150 sys.exit(main()) | |
| OLD | NEW |