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

Side by Side 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 unified diff | Download patch
OLDNEW
(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 For example, for the following template file:
19
20 // script-tests/example.js
21 promise_test(() => {
22 assert_promise_rejects(navigator.bluetooth.requestDevice(...)
23 .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.
24 getPrimaryService('heart_rate'),
25 getPrimaryServices('heart_rate')[UUID])));
26 }, 'example test');
27
28 this script will generate:
29
30 // getPrimaryService/example.html
31 promise_test(() => {
32 assert_promise_rejects(navigator.bluetooth.requestDevice(...)
33 .then(device => device.gatt.getPrimaryService('heart_rate')));
34 }, 'example test');
35
36 // getPrimaryServices/example-with-uuid.html
37 promise_test(() => {
38 assert_promise_rejects(navigator.bluetooth.requestDevice(...)
39 .then(device => device.gatt.getPrimaryServices('heart_rate')))
40 }, 'example test');
41
42 Run
43 $ python //third_party/WebKit/LayoutTests/bluetooth/generate.py
44 and commit the generated files.
45 """
46 import glob
47 import os
48 import re
49 import sys
50
51 TEMPLATES_DIR = 'script-tests'
52
53
54 class GeneratedTest:
55
56 def __init__(self, data, path, template):
57 self.data = data
58 self.path = path
59 self.template = template
60
61
62 def GetGeneratedTests():
63 """Yields a GeneratedTest for each call in templates in script-tests."""
64 current_path = os.path.dirname(os.path.realpath(__file__))
65
66 # Read Base Test Template.
67 base_template_file_handle = open(
68 os.path.join(current_path, TEMPLATES_DIR, 'base_test_template.html'))
69 base_template_file_data = base_template_file_handle.read().decode('utf-8')
70 base_template_file_handle.close()
71
72 # Get Templates.
73 available_templates = glob.glob(
74 os.path.join(current_path, TEMPLATES_DIR, '*.js'))
75
76 # Generate Test Files
77 for template in available_templates:
78 # Read template
79 template_file_handle = open(template)
80 template_file_data = template_file_handle.read().decode('utf-8')
81 template_file_handle.close()
82
83 template_name = os.path.splitext(os.path.basename(template))[0]
84
85 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.
86 | re.DOTALL)
87
88 if result is None:
89 raise Exception('Template must contain \'CALLS\' tokens')
90
91 new_test_file_data = base_template_file_data.replace('TEST',
92 template_file_data)
93 # Replace CALLS([...]) with CALLS so that we don't have to replace the
94 # CALLS([...]) for every new test file.
95 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
96 new_test_file_data = new_test_file_data.replace(result.group(), 'CALLS')
97 calls = result.group(1)
98 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
99 calls = calls.split(',')
100
101 for call in calls:
102 # Parse call
103 name, args, uuid_suffix = re.search(r'(.*?)\((.*?)\)(\[UUID\])?',
104 call).groups()
105
106 # Replace template tokens
107 call_test_file_data = new_test_file_data.replace(
108 'CALLS', '{}({})'.format(name, args))
109 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.
110
111 # Get test file name
112 call_test_file_name = 'gen-{}{}.html'.format(template_name, '-with-uuid'
113 if uuid_suffix else '')
114 call_test_file_path = os.path.join(current_path, name,
115 call_test_file_name)
116
117 yield GeneratedTest(call_test_file_data, call_test_file_path, template)
118
119
120 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.
121
122 for generated_test in GetGeneratedTests():
123 # Create or open test file
124 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.
125
126 # Write contents
127 test_file_handle.truncate()
128 test_file_handle.write(generated_test.data.encode('utf-8'))
129 test_file_handle.close()
130
131
132 if __name__ == '__main__':
133 sys.exit(main())
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698