Skip to content

Commit

Permalink
Use line endings based on script language #88
Browse files Browse the repository at this point in the history
Use CRLF in batchfile and LF in shellscript.
  • Loading branch information
undergroundwires committed Sep 28, 2022
1 parent bbc6156 commit 6b3f465
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/application/Context/State/Code/Generation/CodeBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { ICodeBuilder } from './ICodeBuilder';

const NewLine = '\n';
const TotalFunctionSeparatorChars = 58;

export abstract class CodeBuilder implements ICodeBuilder {
Expand Down Expand Up @@ -59,10 +58,12 @@ export abstract class CodeBuilder implements ICodeBuilder {
}

public toString(): string {
return this.lines.join(NewLine);
return this.lines.join(this.getNewLineTerminator());
}

protected abstract getCommentDelimiter(): string;

protected abstract writeStandardOut(text: string): string;

protected abstract getNewLineTerminator(): string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ export class BatchBuilder extends CodeBuilder {
protected writeStandardOut(text: string): string {
return `echo ${escapeForEcho(text)}`;
}

protected getNewLineTerminator(): string {
return '\r\n';
}
}

function escapeForEcho(text: string) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ export class ShellBuilder extends CodeBuilder {
protected writeStandardOut(text: string): string {
return `echo '${escapeForEcho(text)}'`;
}

protected getNewLineTerminator(): string {
return '\n';
}
}

function escapeForEcho(text: string) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,29 @@ describe('CodeBuilder', () => {
class CodeBuilderConcrete extends CodeBuilder {
private commentDelimiter = '//';

private newLineTerminator = '\n';

public withCommentDelimiter(delimiter: string): CodeBuilderConcrete {
this.commentDelimiter = delimiter;
return this;
}

public withNewLineTerminator(terminator: string): CodeBuilderConcrete {
this.newLineTerminator = terminator;
return this;
}

protected getCommentDelimiter(): string {
return this.commentDelimiter;
}

protected writeStandardOut(text: string): string {
return text;
}

protected getNewLineTerminator(): string {
return this.newLineTerminator;
}
}
describe('appendLine', () => {
it('when empty appends empty line', () => {
Expand All @@ -40,6 +51,43 @@ describe('CodeBuilder', () => {
const lines = getLines(result);
expect(lines[1]).to.equal('str');
});
describe('append multi-line string as multiple lines', () => {
describe('recognizes different line terminators', () => {
const delimiters = ['\n', '\r\n', '\r'];
for (const delimiter of delimiters) {
it(`using "${JSON.stringify(delimiter)}"`, () => {
// arrange
const line1 = 'line1';
const line2 = 'line2';
const code = `${line1}${delimiter}${line2}`;
const sut = new CodeBuilderConcrete();
// act
sut.appendLine(code);
// assert
const result = sut.toString();
const lines = getLines(result);
expect(lines).to.have.lengthOf(2);
expect(lines[0]).to.equal(line1);
expect(lines[1]).to.equal(line2);
});
}
});
it('normalizes different line terminators', () => {
// arrange
const lineTerminator = '🐒';
const lines = ['line1', 'line2', 'line3', 'line4'];
const code = `${lines[0]}\n${lines[1]}\r\n${lines[2]}\r${lines[3]}`;
const expected = `${lines[0]}${lineTerminator}${lines[1]}${lineTerminator}${lines[2]}${lineTerminator}${lines[3]}`;
const sut = new CodeBuilderConcrete()
.withNewLineTerminator(lineTerminator);
// act
const actual = sut
.appendLine(code)
.toString();
// assert
expect(actual).to.equal(expected);
});
});
});
it('appendFunction', () => {
// arrange
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ describe('BatchBuilder', () => {
public writeStandardOut(text: string): string {
return super.writeStandardOut(text);
}

public getNewLineTerminator(): string {
return super.getNewLineTerminator();
}
}
describe('getCommentDelimiter', () => {
it('returns expected', () => {
Expand Down Expand Up @@ -57,4 +61,15 @@ describe('BatchBuilder', () => {
});
}
});
describe('getNewLineTerminator', () => {
it('returns expected', () => {
// arrange
const expected = '\r\n';
const sut = new BatchBuilderRevealer();
// act
const actual = sut.getNewLineTerminator();
// assert
expect(expected).to.equal(actual);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ describe('ShellBuilder', () => {
public writeStandardOut(text: string): string {
return super.writeStandardOut(text);
}

public getNewLineTerminator(): string {
return super.getNewLineTerminator();
}
}
describe('getCommentDelimiter', () => {
it('returns expected', () => {
Expand Down Expand Up @@ -52,4 +56,15 @@ describe('ShellBuilder', () => {
});
}
});
describe('getNewLineTerminator', () => {
it('returns expected', () => {
// arrange
const expected = '\n';
const sut = new ShellBuilderRevealer();
// act
const actual = sut.getNewLineTerminator();
// assert
expect(expected).to.equal(actual);
});
});
});

0 comments on commit 6b3f465

Please sign in to comment.