1 | var assert = require('assert');
|
2 | var should = require('should');
|
3 | var path = require('path');
|
4 |
|
5 | var Loader = require('..');
|
6 |
|
7 | describe('yaml-config-loader', function() {
|
8 | describe('translateKeyFormat', function() {
|
9 | it('should normalize camelCase, dashes, and underscores to a common output', function() {
|
10 | var object = {
|
11 | 'someKeyInCamelCase': 'Value 1',
|
12 | 'some_key_with_underscores': 'Value 2',
|
13 | 'some-key-with-dashes': 'Value 3',
|
14 | 'someOtherKeyWithCamelCase': 'Value 4',
|
15 | 'someCrazy-key-With-All_3': 'Value 5',
|
16 | };
|
17 | var loader = new Loader()
|
18 | var camelCase = loader.translateKeyFormat(object, 'camelCase');
|
19 | var capitalUnderscores = loader.translateKeyFormat(object, 'CAPITAL_UNDERSCORES');
|
20 | var lowerDashes = loader.translateKeyFormat(object, 'lower-dashes');
|
21 | var camelCaseExpected = {
|
22 | someKeyInCamelCase: 'Value 1',
|
23 | someKeyWithUnderscores: 'Value 2',
|
24 | someKeyWithDashes: 'Value 3',
|
25 | someOtherKeyWithCamelCase: 'Value 4',
|
26 | someCrazyKeyWithAll3: 'Value 5'
|
27 | };
|
28 | for (key in camelCaseExpected) {
|
29 | camelCase[key].should.equal(camelCaseExpected[key]);
|
30 | }
|
31 | var capitalUnderscoresExpected = {
|
32 | SOME_KEY_IN_CAMEL_CASE: 'Value 1',
|
33 | SOME_KEY_WITH_UNDERSCORES: 'Value 2',
|
34 | SOME_KEY_WITH_DASHES: 'Value 3',
|
35 | SOME_OTHER_KEY_WITH_CAMEL_CASE: 'Value 4',
|
36 | SOME_CRAZY_KEY_WITH_ALL_3: 'Value 5'
|
37 | };
|
38 | for (key in capitalUnderscoresExpected) {
|
39 | capitalUnderscores[key].should.equal(capitalUnderscoresExpected[key]);
|
40 | }
|
41 | var lowerDashesExpected = {
|
42 | 'some-key-in-camel-case': 'Value 1',
|
43 | 'some-key-with-underscores': 'Value 2',
|
44 | 'some-key-with-dashes': 'Value 3',
|
45 | 'some-other-key-with-camel-case': 'Value 4',
|
46 | 'some-crazy-key-with-all-3': 'Value 5'
|
47 | };
|
48 | for (key in lowerDashesExpected) {
|
49 | lowerDashes[key].should.equal(lowerDashesExpected[key]);
|
50 | }
|
51 | });
|
52 | });
|
53 | describe('addObject', function() {
|
54 | it('should load configuration onto the ojbect', function(done) {
|
55 | var loader = new Loader()
|
56 | .addObject({ 'MostBadass': 'Darth Vader', 'LeastBadass': 'Luke Skywalker' })
|
57 | .addObject({ 'MostBadass': 'Bobba Fett', 'Funniest': 'R2D2' })
|
58 | .load(function(error, config) {
|
59 | should.not.exist(error);
|
60 | config.MostBadass.should.equal('Bobba Fett');
|
61 | config.LeastBadass.should.equal('Luke Skywalker');
|
62 | config.Funniest.should.equal('R2D2');
|
63 | done();
|
64 | });
|
65 | });
|
66 | });
|
67 | describe('addAndNormalizeObject', function() {
|
68 | it('should translate and extract environment arguments', function(done) {
|
69 | var loader = new Loader()
|
70 | .addFile(path.join(__dirname, 'fixtures', 'fileLoader', 'config1.yaml'))
|
71 | .addAndNormalizeObject({ 'FIRST_MATE': 'Rain Wilson', 'NAGGIEST_DROID': 'R3D5', 'FUNNIEST_DROID': 'R2D2' })
|
72 | .addAndNormalizeObject({naggiestDroid: 'C3PO'})
|
73 | .load(function(error, config) {
|
74 | should.not.exist(error);
|
75 | config.captain.should.equal('The Skipper');
|
76 | config.firstMate.should.equal('Rain Wilson');
|
77 | config.funniestDroid.should.equal('R2D2');
|
78 | config.naggiestDroid.should.equal('C3PO');
|
79 | done();
|
80 | });
|
81 | });
|
82 | });
|
83 | });
|
84 |
|