|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * Copyright (c) Microsoft Corporation. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +const fs = require('fs'); |
| 19 | + |
| 20 | +(async () => { |
| 21 | + const content = fs.readFileSync(process.argv[2]).toString(); |
| 22 | + const lines = content.split('\n'); |
| 23 | + for (let line of lines) { |
| 24 | + if (line.trim().startsWith('describe')) { |
| 25 | + console.log('# DESCRIBE ' + line) |
| 26 | + continue; |
| 27 | + } |
| 28 | + if (line.trim() === '});') { |
| 29 | + console.log(''); |
| 30 | + continue; |
| 31 | + } |
| 32 | + |
| 33 | + line = line.replace(/isWindows/g, 'is_win'); |
| 34 | + line = line.replace(/isLinux/g, 'is_linux'); |
| 35 | + line = line.replace(/isMac/g, 'is_mac'); |
| 36 | + line = line.replace(/isWebKit/g, 'is_webkit'); |
| 37 | + line = line.replace(/isChromium/g, 'is_chromium'); |
| 38 | + line = line.replace(/isFirefox/g, 'is_firefox'); |
| 39 | + |
| 40 | + let match = line.match(/it.*\(\'([^']+)\'.*async(?: function)?\s*\(\s*{(.*)}\s*\).*/); |
| 41 | + if (match) { |
| 42 | + console.log(`async def test_${match[1].replace(/[- =]|\[|\]|\>|\</g, '_')}(${match[2].trim()}):`); |
| 43 | + continue; |
| 44 | + } |
| 45 | + |
| 46 | + line = line.replace(/;$/g, ''); |
| 47 | + line = line.replace(/ const /g, ' '); |
| 48 | + line = line.replace(/ let /g, ' '); |
| 49 | + line = line.replace(/\&\&/g, 'and'); |
| 50 | + line = line.replace(/\|\|/g, 'or'); |
| 51 | + line = line.replace(/'/g, '"'); |
| 52 | + line = line.replace(/ = null/g, ' = None'); |
| 53 | + line = line.replace(/===/g, '=='); |
| 54 | + line = line.replace('await Promise.all([', 'await asyncio.gather('); |
| 55 | + line = line.replace(/\.\$\(/, '.querySelector('); |
| 56 | + line = line.replace(/\.\$$\(/, '.querySelectorAll('); |
| 57 | + line = line.replace(/\.\$eval\(/, '.evalOnSelector('); |
| 58 | + line = line.replace(/\.\$$eval\(/, '.evalOnSelectorAll('); |
| 59 | + |
| 60 | + match = line.match(/(\s+)expect\((.*)\).toEqual\((.*)\)/) |
| 61 | + if (match) |
| 62 | + line = `${match[1]}assert ${match[2]} == ${match[3]}`; |
| 63 | + match = line.match(/(\s+)expect\((.*)\).toBe\((.*)\)/) |
| 64 | + if (match) |
| 65 | + line = `${match[1]}assert ${match[2]} == ${match[3]}`; |
| 66 | + match = line.match(/(\s+)expect\((.*)\).toBeTruthy\((.*)\)/) |
| 67 | + if (match) |
| 68 | + line = `${match[1]}assert ${match[2]}`; |
| 69 | + match = line.match(/(\s+)expect\((.*)\).toBeGreaterThan\((.*)\)/) |
| 70 | + if (match) |
| 71 | + line = `${match[1]}assert ${match[2]} > ${match[3]}`; |
| 72 | + |
| 73 | + match = line.match(/(\s+)expect\((.*)\).toBeLessThan\((.*)\)/) |
| 74 | + if (match) |
| 75 | + line = `${match[1]}assert ${match[2]} < ${match[3]}`; |
| 76 | + |
| 77 | + match = line.match(/(\s+)expect\((.*)\).toBeGreaterThanOrEqual\((.*)\)/) |
| 78 | + if (match) |
| 79 | + line = `${match[1]}assert ${match[2]} >= ${match[3]}`; |
| 80 | + |
| 81 | + match = line.match(/(\s+)expect\((.*)\).toBeLessThanOrEqual\((.*)\)/) |
| 82 | + if (match) |
| 83 | + line = `${match[1]}assert ${match[2]} <= ${match[3]}`; |
| 84 | + |
| 85 | + line = line.replace(/ false/g, ' False'); |
| 86 | + line = line.replace(/ true/g, ' True'); |
| 87 | + |
| 88 | + line = line.replace(/ == null/g, ' == None'); |
| 89 | + if (line.trim().startsWith('assert') && line.endsWith(' == True')) |
| 90 | + line = line.substring(0, line.length - ' == True'.length); |
| 91 | + |
| 92 | + // Quote evaluate |
| 93 | + let index = line.indexOf('.evaluate('); |
| 94 | + if (index !== -1) { |
| 95 | + const tokens = [line.substring(0, index) + '.evaluate(\'']; |
| 96 | + let depth = 0; |
| 97 | + for (let i = index + '.evaluate('.length; i < line.length; ++i) { |
| 98 | + if (line[i] == '(') |
| 99 | + ++depth; |
| 100 | + if (line[i] == ')') |
| 101 | + --depth; |
| 102 | + if (depth < 0) { |
| 103 | + tokens.push('\'' + line.substring(i)); |
| 104 | + break; |
| 105 | + } |
| 106 | + if (depth === 0 && line[i] === ',') { |
| 107 | + tokens.push('\"' + line.substring(i)); |
| 108 | + break; |
| 109 | + } |
| 110 | + tokens.push(line[i]); |
| 111 | + } |
| 112 | + console.log(tokens.join('')); |
| 113 | + continue; |
| 114 | + } |
| 115 | + |
| 116 | + // Name keys in the dict |
| 117 | + index = line.indexOf('{'); |
| 118 | + if (index !== -1) { |
| 119 | + let ok = false; |
| 120 | + for (let i = index + 1; i < line.length; ++i) { |
| 121 | + if (line[i] === '}') { |
| 122 | + try { |
| 123 | + console.log(line.substring(0, index) + JSON.stringify(eval('(' + line.substring(index, i + 1) + ')')).replace(/\"/g, '\'') + line.substring(i + 1)); |
| 124 | + ok = true; |
| 125 | + break; |
| 126 | + } catch (e) { |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + if (ok) continue; |
| 131 | + } |
| 132 | + |
| 133 | + // Single line template strings |
| 134 | + index = line.indexOf('`'); |
| 135 | + if (index !== -1) { |
| 136 | + const tokens = [line.substring(0, index) + '\'']; |
| 137 | + let ok = false; |
| 138 | + for (let i = index + 1; i < line.length; ++i) { |
| 139 | + if (line[i] === '`') { |
| 140 | + tokens.push('\'' + line.substring(i + 1)); |
| 141 | + console.log(tokens.join('')); |
| 142 | + ok = true; |
| 143 | + break; |
| 144 | + } |
| 145 | + if (line[i] === '\'') |
| 146 | + tokens.push('"'); |
| 147 | + else |
| 148 | + tokens.push(line[i]); |
| 149 | + } |
| 150 | + if (ok) continue; |
| 151 | + } |
| 152 | + |
| 153 | + line = line.replace(/(\s+)/, '$1$1'); |
| 154 | + if (line.endsWith('{')) |
| 155 | + line = line.substring(0, line.length - 1).trimEnd() + ':'; |
| 156 | + if (line.trim().startsWith('}')) |
| 157 | + line = line.substring(0, line.indexOf('}')) + line.substring(line.indexOf('}') + 1).trim(); |
| 158 | + if (line.trim().startsWith('//')) |
| 159 | + line = line.replace(/\/\//, '#'); |
| 160 | + if (!line.trim()) |
| 161 | + line = ''; |
| 162 | + console.log(line); |
| 163 | + } |
| 164 | +})(); |
0 commit comments