Skip to content

Commit

Permalink
util: Move indent() function into an indenter object
Browse files Browse the repository at this point in the history
Some test depend on overriding the function, which will no longer
work when we move to ESM (where imports are always read-only).

Prepare for that by moving the function into an "indenter"
object, where it can still be modified after the ESM port.
  • Loading branch information
fmuellner authored and ptomato committed Feb 5, 2024
1 parent ba00f83 commit 161a31e
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 27 deletions.
1 change: 1 addition & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -232,3 +232,4 @@ globals:
printerr: readonly
parserOptions:
ecmaVersion: 2020
sourceType: module
4 changes: 2 additions & 2 deletions src/consoleReporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

const {Gio, GObject} = imports.gi;

const Utils = jasmineImporter.utils;
const {indenter} = jasmineImporter.utils;

const YELLOW = '\x1b[33m';
const GREEN = '\x1b[32m';
Expand Down Expand Up @@ -243,7 +243,7 @@ ${this._color(failedExpectation.message, RED)}
Stack:
${this.filterStack(failedExpectation.stack)}
`;
this._print(Utils.indent(report, 2));
this._print(indenter.indent(report, 2));
});
}

Expand Down
18 changes: 10 additions & 8 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
/* exported indent */
/* exported indenter */

function indent(str, spaces) {
return str.split('\n').map(line => {
if (line === '')
return line;
return ' '.repeat(spaces) + line;
}).join('\n');
}
var indenter = {
indent(str, spaces) {
return str.split('\n').map(line => {
if (line === '')
return line;
return ' '.repeat(spaces) + line;
}).join('\n');
},
};
12 changes: 6 additions & 6 deletions src/verboseReporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
const {GObject} = imports.gi;

const {ConsoleReporter} = jasmineImporter.consoleReporter;
const Utils = jasmineImporter.utils;
const {indenter} = jasmineImporter.utils;

const GRAY = '\x1b[38;5;246m';
const YELLOW = '\x1b[33m';
Expand Down Expand Up @@ -39,14 +39,14 @@ var VerboseReporter = GObject.registerClass(class VerboseReporter extends Consol

suiteStarted(result) {
super.suiteStarted(result);
this._print(Utils.indent(this._color(result.description, GRAY),
this._print(indenter.indent(this._color(result.description, GRAY),
this._suiteLevel * 2));
this._print('\n');
}

suiteDone(result) {
if (result.status === 'disabled') {
this._print(Utils.indent(`${this._color('(disabled)', YELLOW)}\n`,
this._print(indenter.indent(`${this._color('(disabled)', YELLOW)}\n`,
this._suiteLevel * 2 + 2));
}

Expand All @@ -71,7 +71,7 @@ var VerboseReporter = GObject.registerClass(class VerboseReporter extends Consol
failed: `${this._failureCount})`,
disabled: 'x',
};
this._print(Utils.indent(this._color(symbols[result.status],
this._print(indenter.indent(this._color(symbols[result.status],
colors[result.status]), this._suiteLevel * 2 + 2));
this._print(` ${result.description}`);
if (result.time > 75)
Expand All @@ -87,9 +87,9 @@ var VerboseReporter = GObject.registerClass(class VerboseReporter extends Consol
this._print(this._color(`${index + 1}) ${result.fullName}\n\n`, RED));

result.failedExpectations.forEach(failedExpectation => {
this._print(Utils.indent(this._color(failedExpectation.message, GRAY), 2));
this._print(indenter.indent(this._color(failedExpectation.message, GRAY), 2));
this._print('\n');
this._print(Utils.indent(this.filterStack(failedExpectation.stack), 4));
this._print(indenter.indent(this.filterStack(failedExpectation.stack), 4));
this._print('\n\n');
});
}
Expand Down
4 changes: 2 additions & 2 deletions src/xmlWriter.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

const {GLib} = imports.gi;

const Utils = jasmineImporter.utils;
const {indenter} = jasmineImporter.utils;

var Node = class Node {
constructor(name) {
Expand Down Expand Up @@ -36,6 +36,6 @@ function _prettyprint(node) {
if (text.length !== 0)
text += '\n';

return elementTop + Utils.indent(children, 2) + Utils.indent(text, 2) +
return elementTop + indenter.indent(children, 2) + indenter.indent(text, 2) +
elementBottom;
}
2 changes: 1 addition & 1 deletion test/importerSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const Utils = imports.utils;

describe('Jasmine importer', function () {
it('hides Jasmine modules from the test code', function () {
expect(Object.keys(Utils)).not.toContain('indent');
expect(Object.keys(Utils)).not.toContain('indenter');
});

it("lets test code import modules named the same as Jasmine's", function () {
Expand Down
12 changes: 6 additions & 6 deletions test/utilsSpec.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
/* global jasmineImporter */

const Utils = jasmineImporter.utils;
const {indenter} = jasmineImporter.utils;

describe('Indent', function () {
it('prepends spaces to a string', function () {
expect(Utils.indent('foo', 4)).toEqual(' foo');
expect(indenter.indent('foo', 4)).toEqual(' foo');
});

it('prepends spaces to each line in a string', function () {
expect(Utils.indent('a\nb\nc', 4)).toEqual(' a\n b\n c');
expect(indenter.indent('a\nb\nc', 4)).toEqual(' a\n b\n c');
});

it('does not indent an extra blank line at the end of the string', function () {
expect(Utils.indent('a\nb\n', 4)).toEqual(' a\n b\n');
expect(indenter.indent('a\nb\n', 4)).toEqual(' a\n b\n');
});

it('handles zero spaces', function () {
expect(Utils.indent('foo', 0)).toEqual('foo');
expect(Utils.indent('a\nb\nc', 0)).toEqual('a\nb\nc');
expect(indenter.indent('foo', 0)).toEqual('foo');
expect(indenter.indent('a\nb\nc', 0)).toEqual('a\nb\nc');
});
});
4 changes: 2 additions & 2 deletions test/verboseReporterSpec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* global jasmineImporter */

const VerboseReporter = jasmineImporter.verboseReporter;
const Utils = jasmineImporter.utils;
const {indenter} = jasmineImporter.utils;

describe('Verbose console reporter', function () {
let out, reporter, timerSpy, timerSpies;
Expand Down Expand Up @@ -35,7 +35,7 @@ describe('Verbose console reporter', function () {
});

// disable indentation for test purposes
spyOn(Utils, 'indent').and.callFake(str => str);
spyOn(indenter, 'indent').and.callFake(str => str);
});

it('reports that the suite has started to the console', function () {
Expand Down

0 comments on commit 161a31e

Please sign in to comment.