Skip to content

Commit

Permalink
Apply JavaScript formatting
Browse files Browse the repository at this point in the history
This changelist applies automated formatting to the MaterialX JavaScript codebase, aligning it with the 4-space indentation and Allman braces used in MaterialX C++.
  • Loading branch information
jstone-lucasfilm committed Jan 24, 2024
1 parent 8dbabce commit 9e92e1e
Show file tree
Hide file tree
Showing 17 changed files with 858 additions and 614 deletions.
20 changes: 11 additions & 9 deletions javascript/MaterialXTest/browser/esslShaderGenerator.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// MaterialX is served through a script tag in the test setup.

function createStandardSurfaceMaterial(mx)
function createStandardSurfaceMaterial(mx)
{
const doc = mx.createDocument();
const ssName = 'SR_default';
Expand All @@ -15,19 +15,21 @@ function createStandardSurfaceMaterial(mx)
return doc;
}

describe('Generate ESSL Shaders', function ()
describe('Generate ESSL Shaders', function ()
{
let mx;
const canvas = document.createElement('canvas');
const gl = canvas.getContext('webgl2');

this.timeout(60000);

before(async function () {
before(async function ()
{
mx = await MaterialX();
});

it('Compile Shaders', () => {
it('Compile Shaders', () =>
{
const doc = createStandardSurfaceMaterial(mx);

const gen = new mx.EsslShaderGenerator();
Expand All @@ -37,7 +39,7 @@ describe('Generate ESSL Shaders', function ()
doc.importLibrary(stdlib);

const elem = mx.findRenderableElement(doc);
try
try
{
const mxShader = gen.generate(elem.getNamePath(), elem, genContext);

Expand All @@ -47,7 +49,7 @@ describe('Generate ESSL Shaders', function ()
const glVertexShader = gl.createShader(gl.VERTEX_SHADER);
gl.shaderSource(glVertexShader, vShader);
gl.compileShader(glVertexShader);
if (!gl.getShaderParameter(glVertexShader, gl.COMPILE_STATUS))
if (!gl.getShaderParameter(glVertexShader, gl.COMPILE_STATUS))
{
console.error("-------- VERTEX SHADER FAILED TO COMPILE: ----------------");
console.error("--- VERTEX SHADER LOG ---");
Expand All @@ -61,7 +63,7 @@ describe('Generate ESSL Shaders', function ()
const glPixelShader = gl.createShader(gl.FRAGMENT_SHADER);
gl.shaderSource(glPixelShader, fShader);
gl.compileShader(glPixelShader);
if (!gl.getShaderParameter(glPixelShader, gl.COMPILE_STATUS))
if (!gl.getShaderParameter(glPixelShader, gl.COMPILE_STATUS))
{
console.error("-------- PIXEL SHADER FAILED TO COMPILE: ----------------");
console.error("--- PIXEL SHADER LOG ---");
Expand All @@ -75,7 +77,7 @@ describe('Generate ESSL Shaders', function ()
catch (errPtr)
{
console.error("-------- Failed code generation: ----------------");
console.error(mx.getExceptionMessage(errPtr));
console.error(mx.getExceptionMessage(errPtr));
}
});
});
5 changes: 3 additions & 2 deletions javascript/MaterialXTest/browser/karma.conf.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
module.exports = function(config) {
module.exports = function (config)
{
config.set({
basePath: '../', // base is the javascript folder
files: [
{ pattern: '_build/JsMaterialXGenShader.js', watched: false, included: true, served: true },
{ pattern: '_build/JsMaterialXGenShader.wasm', watched: false, included: false, served: true },
{pattern: '_build/JsMaterialXGenShader.data', watched: false, included: false, served: true, nocache: true },
{ pattern: '_build/JsMaterialXGenShader.data', watched: false, included: false, served: true, nocache: true },
{ pattern: 'browser/*.spec.js', watched: true, included: true, served: true },
],
mime: {
Expand Down
21 changes: 14 additions & 7 deletions javascript/MaterialXTest/codeExamples.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import { expect } from 'chai';
import Module from './_build/JsMaterialXCore.js';
import { getMtlxStrings } from './testHelpers';

describe('Code Examples', () => {
it('Building a MaterialX Document', async () => {
describe('Code Examples', () =>
{
it('Building a MaterialX Document', async () =>
{
const mx = await Module();
// Create a document.
const doc = mx.createDocument();
Expand Down Expand Up @@ -76,7 +78,8 @@ describe('Code Examples', () => {
// expect(roughness.getBoundValue(material).getValueString()).to.equal('0.5');
});

it('Traversing a Document Tree', async () => {
it('Traversing a Document Tree', async () =>
{
const xmlStr = getMtlxStrings(
['standard_surface_greysphere_calibration.mtlx'],
'../../resources/Materials/Examples/StandardSurface'
Expand All @@ -92,13 +95,16 @@ describe('Code Examples', () => {
let elementCount = 0;
let nodeCount = 0;
let fileCount = 0;
for(let elem of elements) {
for (let elem of elements)
{
elementCount++;
// Display the filename of each image node.
if (elem.isANode('image')) {
if (elem.isANode('image'))
{
nodeCount++;
const input = elem.getInput('file');
if (input) {
if (input)
{
fileCount++;
const filename = input.getValueString();
expect(elem.getName()).to.equal('image1');
Expand All @@ -111,7 +117,8 @@ describe('Code Examples', () => {
expect(fileCount).to.equal(1);
});

it('Building a MaterialX Document', async () => {
it('Building a MaterialX Document', async () =>
{
const xmlStr = getMtlxStrings(['standard_surface_marble_solid.mtlx'], '../../resources/Materials/Examples/StandardSurface')[0];
const mx = await Module();

Expand Down
41 changes: 27 additions & 14 deletions javascript/MaterialXTest/customBindings.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@ import { expect } from 'chai';
import Module from './_build/JsMaterialXCore.js';
import { getMtlxStrings } from './testHelpers';

describe('Custom Bindings', () => {
describe('Custom Bindings', () =>
{
const examplesPath = '../../resources/Materials/Examples/StandardSurface';

let mx;
before(async () => {
let mx;
before(async () =>
{
mx = await Module();
});

it('Optional parameters work as expected', () => {
it('Optional parameters work as expected', () =>
{
const doc = mx.createDocument();
// Call a method without optional argument
const nodeGraph = doc.addNodeGraph();
Expand All @@ -29,7 +32,8 @@ describe('Custom Bindings', () => {
expect(() => { nodeGraph.addNode(); }).to.throw;
});

it('Vector <-> Array conversion', () => {
it('Vector <-> Array conversion', () =>
{
// Functions that return vectors in C++ should return an array in JS
const doc = mx.createDocument();
const nodeGraph = doc.addNodeGraph();
Expand Down Expand Up @@ -66,7 +70,8 @@ describe('Custom Bindings', () => {
expect(nodes[2].getName()).to.equal('anotherNode'); // Name set explicitly at creation time
});

it('C++ exception handling', () => {
it('C++ exception handling', () =>
{
// Exceptions that are thrown and caught in C++ shouldn't bubble up to JS
const doc = mx.createDocument();
const nodeGraph1 = doc.addNodeGraph();
Expand All @@ -79,15 +84,18 @@ describe('Custom Bindings', () => {
// Exceptions that are not caught in C++ should throw with an exception pointer
nodeGraph1.addNode('node', 'node1');
expect(() => { nodeGraph1.addNode('node', 'node1'); }).to.throw;
try {
try
{
nodeGraph1.addNode('node', 'node1');
} catch (errPtr) {
} catch (errPtr)
{
expect(errPtr).to.be.a('number');
expect(mx.getExceptionMessage(errPtr)).to.be.a('string');
}
});

it('getReferencedSourceUris', async () => {
it('getReferencedSourceUris', async () =>
{
const doc = mx.createDocument();
const filename = 'standard_surface_look_brass_tiled.mtlx';
await mx.readFromXmlFile(doc, filename, examplesPath);
Expand All @@ -98,7 +106,8 @@ describe('Custom Bindings', () => {
expect(sourceUris.includes('standard_surface_brass_tiled.mtlx')).to.be.true;
});

it('Should invoke correct instance of \'validate\'', () => {
it('Should invoke correct instance of \'validate\'', () =>
{
// We check whether the correct function is called by provoking an error message that is specific to the
// function that we expect to be called.
const message = {};
Expand Down Expand Up @@ -126,7 +135,8 @@ describe('Custom Bindings', () => {
expect(message.message).to.include('Unit type definition does not exist in document')
});

it('StringResolver name substitution getters', () => {
it('StringResolver name substitution getters', () =>
{
const fnTestData = {
fnKey: 'fnValue',
fnKey1: 'fnValue1'
Expand Down Expand Up @@ -156,7 +166,8 @@ describe('Custom Bindings', () => {
expect(gnSubs).to.deep.equal(gnTestData);
});

it('getShaderNodes', async () => {
it('getShaderNodes', async () =>
{
const doc = mx.createDocument();
const fileNames = ['standard_surface_marble_solid.mtlx'];
const mtlxStrs = getMtlxStrings(fileNames, examplesPath);
Expand All @@ -175,14 +186,16 @@ describe('Custom Bindings', () => {
expect(shaderNodes.length).to.equal(0);
});

it('createValidName', () => {
it('createValidName', () =>
{
const testString = '_Note_:Please,turn.this+-into*1#valid\nname for_me';
const replaceRegex = /[^a-zA-Z0-9_:]/g
expect(mx.createValidName(testString)).to.equal(testString.replace(replaceRegex, '_'));
expect(mx.createValidName(testString, '-')).to.equal(testString.replace(replaceRegex, '-'));
});

it('getVersionIntegers', () => {
it('getVersionIntegers', () =>
{
const versionStringArr = mx.getVersionString().split('.').map((value) => parseInt(value, 10));

// Global getVersionIntegers
Expand Down
21 changes: 14 additions & 7 deletions javascript/MaterialXTest/document.spec.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,37 @@
import { expect } from 'chai';
import Module from './_build/JsMaterialXCore.js';

describe('Document', () => {
describe('Document', () =>
{
let mx, doc;
before(async () => {
before(async () =>
{
mx = await Module();
// Create a document.
doc = mx.createDocument();
});

function expectError(type, cb) {
try {
function expectError(type, cb)
{
try
{
cb();
throw new Error('Expected function to throw!');
} catch (exceptionPtr) {
} catch (exceptionPtr)
{
const message = mx.getExceptionMessage(exceptionPtr);
expect(message.indexOf(type) !== -1).to.be.true;
}
}

let nodeGraph;
it('Build document', () => {
it('Build document', () =>
{
// Create a node graph with constant and image sources.
nodeGraph = doc.addNodeGraph();
expect(nodeGraph).to.exist;
expectError('Child name is not unique: nodegraph1', () => {
expectError('Child name is not unique: nodegraph1', () =>
{
doc.addNodeGraph(nodeGraph.getName());
});
const constant = nodeGraph.addNode('constant');
Expand Down
Loading

0 comments on commit 9e92e1e

Please sign in to comment.