Skip to content

Commit

Permalink
Make ternaries and internal objects cloneable (#334)
Browse files Browse the repository at this point in the history
  • Loading branch information
martijnversluis authored Jun 7, 2021
1 parent 4c92b9d commit dd5cbcf
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 10 deletions.
7 changes: 7 additions & 0 deletions src/chord_sheet/chord_pro/composite.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ class Composite {
isRenderable() {
return true;
}

clone() {
return new Composite({
expressions: this.expressions.map((expression) => expression.clone()),
variable: this.variable,
});
}
}

export default Composite;
4 changes: 4 additions & 0 deletions src/chord_sheet/chord_pro/literal.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ class Literal {
isRenderable() {
return true;
}

clone() {
return new Literal(this.string);
}
}

export default Literal;
12 changes: 12 additions & 0 deletions src/chord_sheet/chord_pro/ternary.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,18 @@ class Ternary {
isRenderable() {
return true;
}

clone() {
return new Ternary({
variable: this.variable,
valueTest: this.valueTest,
trueExpression: this.trueExpression?.map((part) => part.clone()),
falseExpression: this.falseExpression?.map((part) => part.clone()),
line: this.line,
column: this.column,
offset: this.offset,
});
}
}

export default Ternary;
14 changes: 4 additions & 10 deletions test/chord_sheet/song.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import {
Song,
Metadata,
ChordSheetSerializer,
} from '../../src';

import LineStub from '../cloneable_stub';
import { createSong } from '../utilities';
import exampleSong from '../fixtures/song';
import serializedSong from '../fixtures/serialized_song.json';
Expand Down Expand Up @@ -106,14 +104,10 @@ describe('Song', () => {

describe('#clone', () => {
it('returns a clone of the song', () => {
const song = new Song();
song.lines = ['foo', 'bar'].map((value) => new LineStub(value));
song.metadata = new Metadata({ foo: 'bar' });
const clonedSong = song.clone();

const actualValues = clonedSong.lines.map((line) => line.value);
expect(actualValues).toEqual(['foo', 'bar']);
expect(clonedSong.metadata).toEqual({ foo: 'bar' });
const serializedExampleSong = new ChordSheetSerializer().serialize(exampleSong);
const clone = exampleSong.clone();
const serializedClone = new ChordSheetSerializer().serialize(clone);
expect(serializedClone).toEqual(serializedExampleSong);
});
});

Expand Down

0 comments on commit dd5cbcf

Please sign in to comment.