From ef62336f77f88fe3394830092139f174021c88a7 Mon Sep 17 00:00:00 2001 From: simonszc Date: Thu, 12 May 2016 12:08:48 -0700 Subject: [PATCH] file_read works, made best attempt on testing --- zach/data/one.txt | 1 + zach/data/three.txt | 1 + zach/data/two.txt | 1 + zach/file_read.js | 31 +++++++++++++++++++++++++++++++ zach/package.json | 21 +++++++++++++++++++++ zach/test/test.js | 28 ++++++++++++++++++++++++++++ 6 files changed, 83 insertions(+) create mode 100644 zach/data/one.txt create mode 100644 zach/data/three.txt create mode 100644 zach/data/two.txt create mode 100644 zach/file_read.js create mode 100644 zach/package.json create mode 100644 zach/test/test.js diff --git a/zach/data/one.txt b/zach/data/one.txt new file mode 100644 index 0000000..0868430 --- /dev/null +++ b/zach/data/one.txt @@ -0,0 +1 @@ +ONE IS MY NUMBER diff --git a/zach/data/three.txt b/zach/data/three.txt new file mode 100644 index 0000000..c37c0cf --- /dev/null +++ b/zach/data/three.txt @@ -0,0 +1 @@ +THREE IS MY NUMBER diff --git a/zach/data/two.txt b/zach/data/two.txt new file mode 100644 index 0000000..6a40077 --- /dev/null +++ b/zach/data/two.txt @@ -0,0 +1 @@ +TWO IS MY NUMBER diff --git a/zach/file_read.js b/zach/file_read.js new file mode 100644 index 0000000..7078ef8 --- /dev/null +++ b/zach/file_read.js @@ -0,0 +1,31 @@ +const fs = require('fs'); +const EventEmitter = require('events').EventEmitter; +const ee = new EventEmitter(); +module.exports = exports = {}; +module.exports.results = []; + +var reader = module.exports.reader = function (file, callback) { + fs.readFile('./data/' + file + '.txt', (err, data) => { + if (err) return console.log(err); + var fileText = data.toString('hex', 0, 8); + console.log(fileText); + module.exports.results.push(fileText); + debugger; + ee.emit('file ' + file + ' read'); + if (callback == true && file == 'three') { + callback(); + } + }) +} + +ee.on('file one read', () => { + reader('two'); +}) + +ee.on('file two read', () => { + reader('three'); +}) + +reader('one'); + +debugger; diff --git a/zach/package.json b/zach/package.json new file mode 100644 index 0000000..df3a769 --- /dev/null +++ b/zach/package.json @@ -0,0 +1,21 @@ +{ + "name": "zach", + "version": "1.0.0", + "description": "should console.log the first 8 bytes of three files in order", + "main": "file_read.js", + "directories": { + "test": "test" + }, + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "zach simonson", + "license": "ISC", + "devDependencies": { + "chai": "^3.5.0", + "gulp": "^3.9.1", + "gulp-eslint": "^2.0.0", + "gulp-mocha": "^2.2.0", + "mocha": "^2.4.5" + } +} diff --git a/zach/test/test.js b/zach/test/test.js new file mode 100644 index 0000000..e5de972 --- /dev/null +++ b/zach/test/test.js @@ -0,0 +1,28 @@ +var mocha = require('mocha'); +var expect = require('chai').expect; +var readerFile = require('../file_read.js'); + +describe('Reader', function() { + before(function(done) { + readerFile.reader('one', done); + }); + it('it should read the first file and return the correct string of eight hexes', function() { + var results = readerFile.results[0]; + var expected = '4f4e45204953204d'; + + expect(results).to.deep.equal(expected); + }); + it('it should read the second file and return the correct string of eight hexes', function() { + var results = readerFile.results[1]; + var expected = '54574f204953204d'; + + expect(results).to.deep.equal(expected); + + }); + it('it should read the third file and return the correct string of eight hexes', function() { + var results = readerFile.results[2]; + var expected = '5448524545204953'; + + expect(results).to.deep.equal(expected); + }) +})