Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(325)

Unified Diff: third_party/WebKit/LayoutTests/bluetooth/generate.py

Issue 2423853002: bluetooth: Add script to generate tests based on templates (Closed)
Patch Set: Clean up Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/LayoutTests/bluetooth/generate.py
diff --git a/third_party/WebKit/LayoutTests/bluetooth/generate.py b/third_party/WebKit/LayoutTests/bluetooth/generate.py
new file mode 100644
index 0000000000000000000000000000000000000000..c29ecb1be9a1fc2902277546dd3ad4d0f790250e
--- /dev/null
+++ b/third_party/WebKit/LayoutTests/bluetooth/generate.py
@@ -0,0 +1,133 @@
+# Copyright 2016 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+"""Generator script for Web Bluetooth LayoutTests.
+
+For each script-tests/X.js creates the following test files depending on the
+contents of X.js
+- getPrimaryService/X.html
+- getPrimaryServices/X.html
+- getPrimaryServices/X-with-uuid.html
+
+script-tests/X.js files should contain "CALLS([variation1, variation2,...])"
+tokens that indicate what files to generate. Each variation in CALLS([...])
+should corresponds to a js function call and its arguments. Additionally a
+variation can end in [UUID] to indicate that the generated file's name should
+have the -with-uuid suffix.
+
+For example, for the following template file:
+
+// script-tests/example.js
+promise_test(() => {
+ assert_promise_rejects(navigator.bluetooth.requestDevice(...)
+ .then(device => device.gatt.CALLS(
Jeffrey Yasskin 2016/10/19 01:39:23 You're missing the '[' and ']' in this call, right
ortuno 2016/10/19 04:34:04 Done.
+ getPrimaryService('heart_rate'),
+ getPrimaryServices('heart_rate')[UUID])));
+}, 'example test');
+
+this script will generate:
+
+// getPrimaryService/example.html
+promise_test(() => {
+ assert_promise_rejects(navigator.bluetooth.requestDevice(...)
+ .then(device => device.gatt.getPrimaryService('heart_rate')));
+}, 'example test');
+
+// getPrimaryServices/example-with-uuid.html
+promise_test(() => {
+ assert_promise_rejects(navigator.bluetooth.requestDevice(...)
+ .then(device => device.gatt.getPrimaryServices('heart_rate')))
+}, 'example test');
+
+Run
+$ python //third_party/WebKit/LayoutTests/bluetooth/generate.py
+and commit the generated files.
+"""
+import glob
+import os
+import re
+import sys
+
+TEMPLATES_DIR = 'script-tests'
+
+
+class GeneratedTest:
+
+ def __init__(self, data, path, template):
+ self.data = data
+ self.path = path
+ self.template = template
+
+
+def GetGeneratedTests():
+ """Yields a GeneratedTest for each call in templates in script-tests."""
+ current_path = os.path.dirname(os.path.realpath(__file__))
+
+ # Read Base Test Template.
+ base_template_file_handle = open(
+ os.path.join(current_path, TEMPLATES_DIR, 'base_test_template.html'))
+ base_template_file_data = base_template_file_handle.read().decode('utf-8')
+ base_template_file_handle.close()
+
+ # Get Templates.
+ available_templates = glob.glob(
+ os.path.join(current_path, TEMPLATES_DIR, '*.js'))
+
+ # Generate Test Files
+ for template in available_templates:
+ # Read template
+ template_file_handle = open(template)
+ template_file_data = template_file_handle.read().decode('utf-8')
+ template_file_handle.close()
+
+ template_name = os.path.splitext(os.path.basename(template))[0]
+
+ result = re.search(r'CALLS\(\[(.*?)\]\)', template_file_data, re.MULTILINE
Jeffrey Yasskin 2016/10/19 01:39:23 If we have a python auto-formatter, then its outpu
ortuno 2016/10/19 04:34:04 Yeah the auto formatter changed it back to this.
+ | re.DOTALL)
+
+ if result is None:
+ raise Exception('Template must contain \'CALLS\' tokens')
+
+ new_test_file_data = base_template_file_data.replace('TEST',
+ template_file_data)
+ # Replace CALLS([...]) with CALLS so that we don't have to replace the
+ # CALLS([...]) for every new test file.
+ while result.group() in new_test_file_data:
Jeffrey Yasskin 2016/10/19 01:39:24 I'm confused why this is a loop. Do we expect the
ortuno 2016/10/19 04:34:04 Yup. Though I realized that it's pretty annoying t
+ new_test_file_data = new_test_file_data.replace(result.group(), 'CALLS')
+ calls = result.group(1)
+ calls = ''.join(calls.split())
Jeffrey Yasskin 2016/10/19 01:39:23 This removes all the whitespace, right? If so, com
ortuno 2016/10/19 04:34:04 Added a comment. Apparently this is faster than a
+ calls = calls.split(',')
+
+ for call in calls:
+ # Parse call
+ name, args, uuid_suffix = re.search(r'(.*?)\((.*?)\)(\[UUID\])?',
+ call).groups()
+
+ # Replace template tokens
+ call_test_file_data = new_test_file_data.replace(
+ 'CALLS', '{}({})'.format(name, args))
+ call_test_file_data = call_test_file_data.replace('FUNCTION_NAME', name)
Jeffrey Yasskin 2016/10/19 01:39:24 Please mention this token in the file comment.
ortuno 2016/10/19 04:34:04 Done.
+
+ # Get test file name
+ call_test_file_name = 'gen-{}{}.html'.format(template_name, '-with-uuid'
+ if uuid_suffix else '')
+ call_test_file_path = os.path.join(current_path, name,
+ call_test_file_name)
+
+ yield GeneratedTest(call_test_file_data, call_test_file_path, template)
+
+
+def main():
Jeffrey Yasskin 2016/10/19 01:39:23 Could you do a "for f in glob.iglob('gen-*'): os.r
ortuno 2016/10/19 04:34:04 I'm a bit scared about silently removing files. Do
Jeffrey Yasskin 2016/10/19 22:33:29 I *think* it's ok, especially since these files ar
ortuno 2016/10/20 00:27:03 Acknowledged.
+
+ for generated_test in GetGeneratedTests():
+ # Create or open test file
+ test_file_handle = open(generated_test.path, 'w+')
Jeffrey Yasskin 2016/10/19 01:39:24 Probably use 'wb' for the mode; you don't want \r\
ortuno 2016/10/19 04:34:04 Done. Thanks for the link.
+
+ # Write contents
+ test_file_handle.truncate()
+ test_file_handle.write(generated_test.data.encode('utf-8'))
+ test_file_handle.close()
+
+
+if __name__ == '__main__':
+ sys.exit(main())

Powered by Google App Engine
This is Rietveld 408576698