Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add tests for per-block generators in field-colour #2220

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@ PULL_REQUEST_TEMPLATE.md
plugins/dev-create/templates/sample-app/*
plugins/dev-create/templates/sample-app-ts/*

# Golden test files
plugins/**/golden/*
2 changes: 2 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ module.exports = [
// specific examples that are sometimes copied into plugins
'plugins/dev-create/templates/sample-app',
'plugins/dev-create/templates/sample-app-ts',
// Golden test files
'plugins/**/golden/*',
],
},
js.configs.recommended, // eslint-recommended
Expand Down
30 changes: 15 additions & 15 deletions plugins/field-colour/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion plugins/field-colour/src/blocks/colourBlend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

import {Block, common as BlocklyCommon} from 'blockly';
import {Block, common as BlocklyCommon} from 'blockly/core';
import {
JavascriptGenerator,
Order as JavascriptOrder,
Expand Down
2 changes: 1 addition & 1 deletion plugins/field-colour/src/blocks/colourPicker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

import {Block, common as BlocklyCommon} from 'blockly';
import {Block, common as BlocklyCommon} from 'blockly/core';
import {
JavascriptGenerator,
Order as JavascriptOrder,
Expand Down
2 changes: 1 addition & 1 deletion plugins/field-colour/src/blocks/colourRandom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

import {Block, common as BlocklyCommon} from 'blockly';
import {Block, common as BlocklyCommon} from 'blockly/core';
import {
JavascriptGenerator,
Order as JavascriptOrder,
Expand Down
2 changes: 1 addition & 1 deletion plugins/field-colour/src/blocks/colourRgb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

import {Block, common as BlocklyCommon} from 'blockly';
import {Block, common as BlocklyCommon} from 'blockly/core';
import {
JavascriptGenerator,
Order as JavascriptOrder,
Expand Down
254 changes: 254 additions & 0 deletions plugins/field-colour/test/blocks_test.mocha.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,254 @@
/**
* @license
* Copyright 2024 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/

import fs from 'fs';
import * as Blockly from 'blockly/core';
import 'blockly/blocks';

import {javascriptGenerator} from 'blockly/javascript';
import {dartGenerator} from 'blockly/dart';
import {phpGenerator} from 'blockly/php';
import {pythonGenerator} from 'blockly/python';
import {luaGenerator} from 'blockly/lua';
import {installAllBlocks} from '../src/index';
import {assert} from 'chai';

const blockJson = {
blocks: {
languageVersion: 0,
blocks: [
{
type: 'colour_picker',
x: 13,
rachel-fenichel marked this conversation as resolved.
Show resolved Hide resolved
y: 13,
fields: {
COLOUR: '#ff0000',
},
},
{
type: 'colour_random',
x: 13,
y: 113,
},
{
type: 'colour_rgb',
x: 13,
y: 263,
inputs: {
RED: {
shadow: {
type: 'math_number',
fields: {
NUM: 0,
},
},
},
GREEN: {
shadow: {
type: 'math_number',
fields: {
NUM: 1,
},
},
},
BLUE: {
shadow: {
type: 'math_number',
fields: {
NUM: 20,
},
},
},
},
},
{
type: 'colour_blend',
x: 13,
y: 363,
inputs: {
COLOUR1: {
shadow: {
type: 'colour_picker',
fields: {
COLOUR: '#ff0000',
},
},
},
COLOUR2: {
shadow: {
type: 'colour_picker',
fields: {
COLOUR: '#3333ff',
},
},
},
RATIO: {
shadow: {
type: 'math_number',
fields: {
NUM: 0.5,
},
},
},
},
},
{
type: 'colour_picker',
x: 13,
y: 63,
fields: {
COLOUR: '#3333ff',
},
},
{
type: 'colour_rgb',
x: 13,
y: 163,
inputs: {
RED: {
shadow: {
type: 'math_number',
fields: {
NUM: 100,
},
},
},
GREEN: {
shadow: {
type: 'math_number',
fields: {
NUM: 50,
},
},
},
BLUE: {
shadow: {
type: 'math_number',
fields: {
NUM: 0,
},
},
},
},
},
{
type: 'colour_blend',
x: 13,
y: 463,
inputs: {
COLOUR1: {
shadow: {
type: 'colour_picker',
fields: {
COLOUR: '#000000',
},
},
},
COLOUR2: {
shadow: {
type: 'colour_picker',
fields: {
COLOUR: '#ffffff',
},
},
},
RATIO: {
shadow: {
type: 'math_number',
fields: {
NUM: 0.3,
},
},
},
},
},
],
},
};

/**
* Uninstall old blocks and generators, since they come pre-installed
* with Blockly.
* TODO(#2194): Delete this function and calls to it.
*/
function uninstallBlocks() {
delete Blockly.Blocks['colour_blend'];
delete Blockly.Blocks['colour_rgb'];
delete Blockly.Blocks['colour_random'];
delete Blockly.Blocks['colour_picker'];

const blockNames = [
'colour_blend',
'colour_rgb',
'colour_random',
'colour_picker',
];
blockNames.forEach((name) => {
delete javascriptGenerator.forBlock[name];
delete dartGenerator.forBlock[name];
delete luaGenerator.forBlock[name];
delete pythonGenerator.forBlock[name];
delete phpGenerator.forBlock[name];
});
}

/**
* Assert that the generated code matches the golden code for the specified
* language.
* @param {string} suffix The suffix of the golden file.
* @param {string} generated The generated code to compare against the
* golden file.
*/
function checkResult(suffix, generated) {
const fileName = `test/golden/golden.${suffix}`;
const goldenContents = fs.readFileSync(fileName);
// Normalize the line feeds.
const normalized = goldenContents.toString().replace(/(?:\r\n|\r|\n)/g, '\n');
assert.equal(generated, normalized);
rachel-fenichel marked this conversation as resolved.
Show resolved Hide resolved
}

suite('Colour Block Generators', function () {
suiteSetup(function () {
uninstallBlocks();
installAllBlocks({
javascript: javascriptGenerator,
dart: dartGenerator,
lua: luaGenerator,
python: pythonGenerator,
php: phpGenerator,
});
});
setup(function () {
this.workspace = new Blockly.Workspace();
Blockly.serialization.workspaces.load(blockJson, this.workspace);
});
test('JavaScript', function () {
const generated = javascriptGenerator.workspaceToCode(this.workspace);
checkResult('js', generated);
});
test('Dart', function () {
const generated = dartGenerator.workspaceToCode(this.workspace);
checkResult('dart', generated);
});
test('Lua', function () {
const generated = luaGenerator.workspaceToCode(this.workspace);
checkResult('lua', generated);
});
test('Python', function () {
const generated = pythonGenerator.workspaceToCode(this.workspace);
checkResult('py', generated);
});
test('PHP', function () {
const generated = phpGenerator.workspaceToCode(this.workspace);
checkResult('php', generated);
});
teardown(function () {
this.workspace.dispose();
});
suiteTeardown(function () {
uninstallBlocks();
});
});
Loading
Loading