From 7368b0b3115ab7f2c67e59ee419299fb4b41c993 Mon Sep 17 00:00:00 2001 From: Eugene Matvejevs Date: Sun, 19 Feb 2017 18:35:12 +0000 Subject: [PATCH] implement better wrapper for files --- src/file.js | 61 +++++++++++++++++++++++++++++++++++++++++++++- src/processor.js | 8 +++--- tests/file.test.js | 48 ++++++++++++++++++++++++++++++++++++ 3 files changed, 112 insertions(+), 5 deletions(-) create mode 100644 tests/file.test.js diff --git a/src/file.js b/src/file.js index d81a6a6..a439628 100644 --- a/src/file.js +++ b/src/file.js @@ -1,2 +1,61 @@ module.exports = class File { -} + constructor() { + this.sourcePath = ''; + this.outputPath = ''; + this.content = ''; + } + + /** + * @returns {string} + */ + getSourcePath() { + return this.sourcePath; + } + + /** + * @param {string} path + * + * @returns {File} + */ + setSourcePath(path) { + this.sourcePath = path; + + return this; + } + + /** + * @returns {string} + */ + getOutputPath() { + return this.outputPath; + } + + /** + * @param {string} path + * + * @returns {File} + */ + setOutputPath(path) { + this.outputPath = path; + + return this; + } + + /** + * @returns {string} + */ + getContent() { + return this.content; + } + + /** + * @param {string} content + * + * @returns {File} + */ + setContent(content) { + this.content = content; + + return this; + } +}; diff --git a/src/processor.js b/src/processor.js index 6cc2e58..bd7c3e4 100644 --- a/src/processor.js +++ b/src/processor.js @@ -19,7 +19,7 @@ module.exports = class Processor { } write() { - this.files.forEach(file => fs.writeFile(file.output, JSON.stringify(file.content, null, 2), 'UTF-8')); + this.files.forEach(file => fs.writeFile(file.getOutputPath(), JSON.stringify(file.getContent(), null, 2), 'UTF-8')); } /** @@ -41,9 +41,9 @@ module.exports = class Processor { solvedJson = this.resolveOverwritten(config.envMap), completedJson = this.constructor.getMergedData(packageJson, solvedJson); - file.source = pathSource; - file.output = pathOutput; - file.content = completedJson; + file.setSourcePath(pathSource) + .setOutputPath(pathOutput) + .setContent(completedJson); return file; } diff --git a/tests/file.test.js b/tests/file.test.js new file mode 100644 index 0000000..bc8fe51 --- /dev/null +++ b/tests/file.test.js @@ -0,0 +1,48 @@ +"use strict"; + +const File = require('../src/file'); + +describe('file', () => { + describe('::constructor', () => { + it('fields [content|sourcePath|outputPath] should be defined', () => { + const file = new File(); + + expect(file.content).toBeDefined(); + expect(file.sourcePath).toBeDefined(); + expect(file.outputPath).toBeDefined(); + }) + }); + + describe('::(get|set)Content', () => { + it('getter should return content, what been set though setter', () => { + const file = new File(); + const content = 'test content'; + + file.setContent(content); + + expect(file.getContent()).toMatch(content); + }) + }); + + describe('::(get|set)SourcePath', () => { + it('getter should return content, what been set though setter', () => { + const file = new File(); + const content = 'test content'; + + file.setSourcePath(content); + + expect(file.getSourcePath()).toMatch(content); + }) + }); + + describe('::(get|set)OutputPath', () => { + it('getter should return content, what been set though setter', () => { + const file = new File(); + const content = 'test content'; + + file.setOutputPath(content); + + expect(file.getOutputPath()).toMatch(content); + }) + }); +});