Skip to content

Commit bd7c86a

Browse files
authored
chore: Improve code health of generator tests. (#8703)
1 parent b8bb26f commit bd7c86a

2 files changed

Lines changed: 15 additions & 12 deletions

File tree

tests/generators/index.html

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
* If some tests are failing, load test suites individually to continue
3939
* debugging.
4040
*/
41-
function loadSelected() {
41+
async function loadSelected() {
4242
var output = document.getElementById('importExport');
4343
output.style.background = 'gray';
4444

@@ -53,9 +53,12 @@
5353
if (boxList[i].checked) {
5454
var testUrl = boxList[i].value;
5555
if (testUrl) {
56-
var xmlText = fetchFile(testUrl);
56+
var xmlText = await fetchFile(testUrl);
5757
if (xmlText !== null) {
5858
fromXml(testUrl, xmlText, /* opt_append */ true);
59+
// Clean up the workspace to normalize the position of blocks and
60+
// thus the order of functions in the generated code.
61+
Blockly.getMainWorkspace().cleanUp();
5962
}
6063
}
6164
}
@@ -67,23 +70,24 @@
6770
/**
6871
* Ask the user for a file name, then load that file's contents.
6972
*/
70-
function loadOther() {
73+
async function loadOther() {
7174
var url = window.prompt('Enter URL of test file.');
7275
if (!url) {
7376
return;
7477
}
75-
var xmlText = fetchFile(url);
78+
var xmlText = await fetchFile(url);
7679
if (xmlText !== null) {
7780
fromXml(url, xmlText);
7881
}
7982
}
8083

81-
function fetchFile(xmlUrl) {
84+
async function fetchFile(xmlUrl) {
8285
try {
83-
var xmlHttp = new XMLHttpRequest();
84-
xmlHttp.open('GET', xmlUrl, false);
85-
xmlHttp.setRequestHeader('Content-Type', 'text/xml');
86-
xmlHttp.send('');
86+
const response = await fetch(xmlUrl);
87+
if (!response.ok) {
88+
throw new Error(`Got a 404 when loading ${xmlUrl}`);
89+
}
90+
return response.text();
8791
} catch (e) {
8892
// Attempt to diagnose the problem.
8993
var msg = 'Error: Unable to load XML data.\n';
@@ -95,7 +99,6 @@
9599
alert(msg + '\n' + e);
96100
return null;
97101
}
98-
return xmlHttp.responseText;
99102
}
100103

101104
/**
@@ -188,7 +191,7 @@
188191
function changeIndex() {
189192
var oneBasedIndex = document.getElementById('indexing').checked;
190193
demoWorkspace.options.oneBasedIndex = oneBasedIndex;
191-
demoWorkspace.getToolbox().flyout_.workspace_.options.oneBasedIndex = oneBasedIndex;
194+
demoWorkspace.getToolbox().getFlyout().getWorkspace().options.oneBasedIndex = oneBasedIndex;
192195
}
193196
</script>
194197

tests/generators/webdriver.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ async function runGeneratorsInBrowser(outputDir) {
7878

7979
await browser.execute(function() {
8080
checkAll();
81-
loadSelected();
81+
return loadSelected();
8282
});
8383

8484
await runLangGeneratorInBrowser(browser, prefix + '.js',

0 commit comments

Comments
 (0)