Skip to content

Commit

Permalink
feat(*): Adds InMemoryWriter (#490)
Browse files Browse the repository at this point in the history
Signed-off-by: jonathan.casey <jonathan.casey@docusign.com>
  • Loading branch information
jonathan-casey authored Aug 16, 2022
1 parent a9b483e commit 853837a
Show file tree
Hide file tree
Showing 5 changed files with 225 additions and 2 deletions.
4 changes: 3 additions & 1 deletion packages/concerto-util/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const HTTPFileLoader = require('./lib/loaders/httpfileloader');
const Writer = require('./lib/writer');
const FileWriter = require('./lib/filewriter');
const ModelWriter = require('./lib/modelwriter');
const InMemoryWriter = require('./lib/inmemorywriter');

// Logger
const Logger = require('./lib/logger');
Expand All @@ -58,8 +59,9 @@ module.exports = {
HTTPFileLoader,
Writer,
FileWriter,
InMemoryWriter,
ModelWriter,
Logger,
TypedStack,
Label
};
};
67 changes: 67 additions & 0 deletions packages/concerto-util/lib/inmemorywriter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

const Writer = require('./writer');

/**
* InMemoryWriter stores string representation of files in a map structure.
* The Map key is the filename, and the value are its string contents.
*
* @private
* @extends Writer
* @see See {@link Writer}
* @class
* @memberof module:concerto-core
*/
class InMemoryWriter extends Writer {

/**
* Create a FileWriter.
*
*/
constructor() {
super();
this.fileName = '';
this.data = new Map();
}

/**
* Creates the filename which will be used for association with its string content.
*
* @param {string} fileName - the name of the file.
*/
openFile(fileName) {
this.fileName = fileName;
}

/**
* Writes the contents of the buffer to the Map store.
*/
closeFile() {
this.data.set(this.fileName, this.getBuffer());
this.clearBuffer();
}

/**
* Returns the content of the Map store.
*
* @return {Map} - a Map containing the string representation of files. (k,v) => (filename, file content).
*/
getFilesInMemory() {
return this.data;
}
}
module.exports = InMemoryWriter;
121 changes: 121 additions & 0 deletions packages/concerto-util/test/inmemorywriter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

const chai = require('chai');
const should = chai.should();
chai.use(require('chai-as-promised'));
chai.use(require('chai-things'));
const sinon = require('sinon');

const Writer = require('../lib/writer');
const InMemoryWriter = require('../lib/inmemorywriter');

describe('InMemoryWriter', function() {

let sandbox;

beforeEach(() => {
sandbox = sinon.createSandbox();
});

afterEach(() => {
sandbox.restore();
});

describe('#constructor', function() {
it('main code path', function() {
let imw = new InMemoryWriter();
should.exist(imw);
});
});

describe('#openFile', function() {
it('main code path', function() {
let imw = new InMemoryWriter();
should.exist(imw);
imw.openFile('filename');
imw.fileName.should.equal('filename');
});
});

describe('#writeLine', function() {
it('main code path', function() {
let stub = sandbox.stub(Writer.prototype, 'writeLine');
stub.returns();
let imw = new InMemoryWriter();
imw.fileName = 'filename';
should.exist(imw);

imw.writeLine('tabs', 'text');

sinon.assert.calledWith(stub, 'tabs', 'text');
});
});

describe('#writeBeforeLine', function() {
it('main code path', function() {
let stub = sandbox.stub(Writer.prototype, 'writeBeforeLine');
stub.returns();
let imw = new InMemoryWriter();
imw.fileName = 'filename';
should.exist(imw);

imw.writeBeforeLine('tabs', 'text');

sinon.assert.calledWith(stub, 'tabs', 'text');
});
});

describe('#closeFile', function() {
it('main code path', function() {
let superClearBuffer = sandbox.stub(Writer.prototype, 'clearBuffer');
superClearBuffer.returns();

let superGetBuffer = sandbox.stub(Writer.prototype, 'getBuffer');
superGetBuffer.returns([0, 1, 2, 3]);

let imw = new InMemoryWriter();
should.exist(imw);
imw.fileName = 'filename';

imw.closeFile();

sinon.assert.calledOnce(superClearBuffer);
});
});

describe('#getFilesInMemory', function() {
it('main code path', function() {
let superClearBuffer = sandbox.stub(Writer.prototype, 'clearBuffer');
superClearBuffer.returns();

let superGetBuffer = sandbox.stub(Writer.prototype, 'getBuffer');
superGetBuffer.returns('lorem ipsum');

let imw = new InMemoryWriter();
should.exist(imw);

imw.fileName = 'filename.txt';
imw.writeLine('', 'lorem ipsum');
imw.closeFile();

imw.data.size.should.equal(1);
imw.getFilesInMemory().get('filename.txt').should.equal('lorem ipsum');

sinon.assert.calledOnce(superClearBuffer);
});
});
});
3 changes: 2 additions & 1 deletion packages/concerto-util/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import GitHubFileLoader = require("./lib/loaders/githubfileloader");
import HTTPFileLoader = require("./lib/loaders/httpfileloader");
import Writer = require("./lib/writer");
import FileWriter = require("./lib/filewriter");
import InMemoryWriter = require("./lib/inmemorywriter");
import ModelWriter = require("./lib/modelwriter");
import Logger = require("./lib/logger");
import TypedStack = require("./lib/typedstack");
import Label = require("./lib/label");
export { BaseException, BaseFileException, FileDownloader, CompositeFileLoader, DefaultFileLoader, GitHubFileLoader, HTTPFileLoader, Writer, FileWriter, ModelWriter, Logger, TypedStack, Label };
export { BaseException, BaseFileException, FileDownloader, CompositeFileLoader, DefaultFileLoader, GitHubFileLoader, HTTPFileLoader, Writer, FileWriter, InMemoryWriter, ModelWriter, Logger, TypedStack, Label };
32 changes: 32 additions & 0 deletions packages/concerto-util/types/lib/inmemorywriter.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
export = InMemoryWriter;
/**
* InMemoryWriter stores string representation of files in a map structure.
* The Map key is the filename, and the value are its string contents.
*
* @private
* @extends Writer
* @see See {@link Writer}
* @class
* @memberof module:concerto-core
*/
declare class InMemoryWriter extends Writer {
fileName: string;
data: Map<any, any>;
/**
* Creates the filename which will be used for association with its string content.
*
* @param {string} fileName - the name of the file.
*/
openFile(fileName: string): void;
/**
* Writes the contents of the buffer to the Map store.
*/
closeFile(): void;
/**
* Returns the content of the Map store.
*
* @return {Map} - a Map containing the string representation of files. (k,v) => (filename, file content).
*/
getFilesInMemory(): Map<any, any>;
}
import Writer = require("./writer");

0 comments on commit 853837a

Please # to comment.