Skip to content

Commit

Permalink
Switch to yarn package management, use mocha for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
steveukx committed Jun 17, 2019
1 parent 9aba726 commit 262021f
Show file tree
Hide file tree
Showing 16 changed files with 1,161 additions and 537 deletions.
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
*.iml
node_modules
.idea/

node_modules/
3 changes: 0 additions & 3 deletions .npmignore

This file was deleted.

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@
"mkdirp": "~0.5.1"
},
"devDependencies": {
"unit-test": "*"
"expect.js": "^0.3.1",
"mocha": "^6.1.4",
"rimraf": "^2.6.3",
"sinon": "^7.3.2"
},
"keywords": [
"properties",
Expand All @@ -36,7 +39,7 @@
"src/**/*.js"
],
"scripts": {
"test": "node test/runner.js"
"test": "mocha ./test/*.spec.js"
},
"license": "MIT"
}
54 changes: 54 additions & 0 deletions test/bind-to-server.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const expect = require('expect.js');
const {spy} = require('sinon');

describe('bind-to-server', () => {

let properties;

const tempFile = require('./utils/temporary-file');
const {givenFilePropertiesReader} = require('./utils/bdd');

function givenTheProperties (content) {
return properties = givenFilePropertiesReader(content);
}

beforeEach(() => {
});

afterEach(() => tempFile.tearDown());

it('test Creates directories when necessary - absolute paths', () => {
const dirPath = tempFile.pushDir('/tmp/' + Math.floor(Math.random() * 1e10).toString(16));
const app = {set: spy()};

givenTheProperties(`
some.property.dir = ${ dirPath }
foo.bar = A Value
`).bindToExpress(app, null, true);

expect(require('fs').statSync(dirPath).isDirectory()).to.be.ok();
});

it('test Creates directories when necessary - relative paths', () => {
const dirName = Math.floor(Math.random() * 1e10).toString(16);
const dirBase = process.cwd();
const dirPath = tempFile.pushDir(dirBase + '/' + dirName);
const app = {set: spy()};

givenTheProperties(`
some.property.dir = ${ dirName }
foo.bar = A Value
`).bindToExpress(app, dirBase, true);

expect(require('fs').statSync(dirPath).isDirectory()).to.be.ok();
});


});

69 changes: 0 additions & 69 deletions test/bindToServerTest.js

This file was deleted.

204 changes: 204 additions & 0 deletions test/reader.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
const expect = require('expect.js');
const {spy} = require('sinon');

describe('Reader', () => {

let properties;

const tempFile = require('./utils/temporary-file');
const {givenFilePropertiesReader} = require('./utils/bdd');

function givenTheProperties (content) {
return properties = givenFilePropertiesReader(content);
}

beforeEach(() => {
});

afterEach(() => tempFile.tearDown());

it('test Able to read from a file', () => {
givenTheProperties('some.property=Value');
expect(properties.get('some.property')).to.be('Value');
});

it('test Merges multiple files', () => {
givenTheProperties('some.property=Value');

tempFile('[section]\nsome.property=Another Value');
properties.append(tempFile.files[tempFile.files.length - 1]);

expect(properties.get('section.some.property')).to.be('Another Value');
expect(properties.get('some.property')).to.be('Value');
});

it('test Runs a function across all items in the reader', () => {
givenTheProperties(
'a = 123\n' +
'b = true\n'
);

assertionsFor(spy(), properties, (s, c) => properties.each(s));
assertionsFor(spy(), {a: 'bcd'}, (s, c) => properties.each(s, c));

function assertionsFor (theSpy, theContext, run) {
run(theSpy, theContext);

expect(theSpy.callCount).to.be(2);
expect(theSpy.calledWith('a', '123')).to.be.ok();
expect(theSpy.calledWith('b', 'true')).to.be.ok();
expect(theSpy.alwaysCalledOn(theContext)).to.be.ok();
}
});

it('test Attempts type coercion', () => {
givenTheProperties(
'a = 123\n' +
'b = true\n' +
'c = false\n' +
'd = 0.1');
expect(properties.get('b')).to.be(true);
expect(properties.get('c')).to.be(false);
expect(properties.get('a')).to.be(123);
expect(properties.get('d')).to.be(0.1);
});

it('test Correctly handles values that are nothing but whitespace', () => {
givenTheProperties('a = \n');
expect(properties.getRaw('a')).to.be('');
});

it('test Allows access to non-parsed values', () => {
givenTheProperties(`
a = 123
b = true
c = false
d = 0.1
`);
expect(properties.getRaw('b')).to.be('true');
expect(properties.getRaw('c')).to.be('false');
expect(properties.getRaw('a')).to.be('123');
expect(properties.getRaw('d')).to.be('0.1');
});

it('test Properties are trimmed when parsed', () => {
givenTheProperties(`
some.property =Value
foo.bar = A Value`);

expect(properties.get('some.property')).to.be('Value');
expect(properties.get('foo.bar')).to.be('A Value');
});

it('test Blank lines are ignored', () => {
givenTheProperties('\n\nsome.property=Value\n\nfoo.bar = A Value');

expect(properties.length).to.be(2);
});

it('test Properties can be read back via their dot notation names', () => {
givenTheProperties('\n\nsome.property=Value\n\nfoo.bar = A Value');

expect(properties.path().some.property).to.be('Value');
expect(properties.path().foo.bar).to.be('A Value');
});

it('test Sets properties into an app', () => {
const app = {set: spy()};
givenTheProperties(`
some.property=Value
foo.bar = A Value`).bindToExpress(app);

expect(app.set.withArgs('properties', properties).calledOnce).to.be.ok();
expect(app.set.withArgs('some.property', 'Value').calledOnce).to.be.ok();
expect(app.set.withArgs('foo.bar', 'A Value').calledOnce).to.be.ok();
});

it('test Permits escaped new line characters', () => {
givenTheProperties('\n\nsome.property= Multi\\n Line \\nString \nfoo.bar = A Value');

// parsed access modifies the new line characters
expect(properties.get('foo.bar')).to.be('A Value');
expect(properties.get('some.property')).to.be('Multi\n Line \nString');

// raw access does not modify the new line characters
expect(properties.getRaw('some.property')).to.be('Multi\\n Line \\nString');
expect(properties.path().some.property).to.be('Multi\\n Line \\nString');
});

it('test Returns null when getting a missing property', () => {
givenTheProperties('prop = value');

// parsed access modifies the new line characters
expect(properties.get('prop')).to.be('value');
expect(properties.get('missing')).to.be(null);
});

it('test getByRoot when getting a bunch of objects', () => {
givenTheProperties(`
root.sect.a = 1
root.sect.b = bar
root.path.b = true
root.path.c = false
root.path.d = 0.1
`);

expect(properties.getByRoot('root.path').b).to.be(true);
expect(properties.getByRoot('root.path').c).to.be(false);

expect(properties.getByRoot('root.sect')).to.eql(
{
a: 1,
b: 'bar'
}
);
});

it('test getByRoot when names are sub strings', () => {
givenTheProperties(`
root.sect.a = 1
root.section.b = bar
root.sect.c = false
root.section.d = 0.1
`);

expect(properties.getByRoot('root.sect')).to.eql({
a: 1,
c: false
});
});

it('test getAllProperties returns properties map', () => {
givenTheProperties(`
root.a.b = Hello
some.thing = Else
`);

expect(properties.getAllProperties()).to.eql({
'root.a.b': "Hello",
'some.thing': 'Else'
});
});

it('test getAllProperties is immutable', () => {
givenTheProperties(`
root.a.b = Hello
some.thing = Else
`);

const all = properties.getAllProperties();
all['root.a.b'] = 'New Value';

expect(properties.getAllProperties()).to.eql({
'root.a.b': "Hello",
'some.thing': 'Else'
});
});

});
Loading

0 comments on commit 262021f

Please sign in to comment.