Skip to content

Commit

Permalink
Merge pull request #509 from pattern-lab/fix-hidden-pattern-references
Browse files Browse the repository at this point in the history
Fix hidden pattern references
  • Loading branch information
bmuenzenmeyer authored Oct 11, 2016
2 parents 5b7dcf6 + a7aa23b commit 777dd1c
Show file tree
Hide file tree
Showing 13 changed files with 125 additions and 13 deletions.
10 changes: 3 additions & 7 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,9 @@ module.exports = function (grunt) {
dest: './core/lib/patternlab.js'
}
},
nodeunit: {
all: ['test/*_tests.js']
},
tape: {
options: {
pretty: true,
pretty: false,
output: 'console'
},
files: ['test/*_tests.js']
Expand All @@ -37,13 +34,12 @@ module.exports = function (grunt) {
// load all grunt tasks
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-eslint');
grunt.loadNpmTasks('grunt-contrib-nodeunit');
grunt.loadNpmTasks('grunt-tape');

//travis CI task
grunt.registerTask('travis', ['nodeunit', 'tape', 'eslint']);
grunt.registerTask('travis', ['tape', 'eslint']);

//to be run prior to releasing a version
grunt.registerTask('build', ['nodeunit', 'tape', 'eslint', 'concat']);
grunt.registerTask('build', ['tape', 'eslint', 'concat']);

};
14 changes: 11 additions & 3 deletions core/lib/object_factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
var patternEngines = require('./pattern_engines');
var path = require('path');
var extend = require('util')._extend;
// patternPrefixMatcher is intended to match the leading maybe-underscore,
// zero or more digits, and maybe-dash at the beginning of a pattern file name we can hack them
// off and get at the good part.
var patternPrefixMatcher = /^_?(\d+-)?/;

// Pattern properties

Expand All @@ -22,21 +26,21 @@ var Pattern = function (relPath, data, patternlab) {
this.jsonFileData = data || {};

// strip leading "00-" from the file name and flip tildes to dashes
this.patternBaseName = this.fileName.replace(/^\d*\-/, '').replace('~', '-'); // 'colors'
this.patternBaseName = this.fileName.replace(patternPrefixMatcher, '').replace('~', '-'); // 'colors'

// Fancy name. No idea how this works. 'Colors'
this.patternName = this.patternBaseName.split('-').reduce(function (val, working) {
return val.charAt(0).toUpperCase() + val.slice(1) + ' ' + working.charAt(0).toUpperCase() + working.slice(1);
}, '').trim(); //this is the display name for the ui. strip numeric + hyphen prefixes

// the top-level pattern group this pattern belongs to. 'atoms'
this.patternGroup = this.subdir.split(path.sep)[0].replace(/^\d*-/, '');
this.patternGroup = this.subdir.split(path.sep)[0].replace(patternPrefixMatcher, '');

//00-atoms if needed
this.patternType = this.subdir.split(path.sep)[0];

// the sub-group this pattern belongs to.
this.patternSubGroup = path.basename(this.subdir).replace(/^\d*-/, ''); // 'global'
this.patternSubGroup = path.basename(this.subdir).replace(patternPrefixMatcher, ''); // 'global'

//00-colors if needed
this.patternSubType = path.basename(this.subdir);
Expand All @@ -52,6 +56,10 @@ var Pattern = function (relPath, data, patternlab) {
// name of the pattern. UPDATE: this.key is now known as this.patternPartial
this.patternPartial = this.patternGroup + '-' + this.patternBaseName;

// Let's calculate the verbose name ahead of time! We don't use path.sep here
// on purpose. This isn't a file name!
this.verbosePartial = this.subdir + '/' + this.fileName;

this.isPattern = true;
this.isFlatPattern = this.patternGroup === this.patternSubGroup;
this.patternState = '';
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
"devDependencies": {
"grunt": "~1.0.1",
"grunt-contrib-concat": "^1.0.1",
"grunt-contrib-nodeunit": "^1.0.0",
"grunt-eslint": "^18.0.0",
"grunt-tape": "^0.1.0",
"tap": "^7.1.2"
Expand Down
24 changes: 23 additions & 1 deletion test/engine_handlebars_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ if (!engineLoader.handlebars) {
test.end()
})
return
};
}

// fake pattern lab constructor:
// sets up a fake patternlab object, which is needed by the pattern processing
Expand Down Expand Up @@ -70,6 +70,7 @@ function testFindPartials(test, partialTests) {
test.end();
}


tap.test('hello world handlebars pattern renders', function (test) {
test.plan(1);

Expand Down Expand Up @@ -200,3 +201,24 @@ tap.test('find_pattern_partials finds handlebars block partials', function (test
'{{#> myPartial }}'
]);
});

tap.test('hidden handlebars patterns can be called by their nice names', function (test) {
const util = require('./util/test_utils.js');

//arrange
const testPatternsPath = path.resolve(__dirname, 'files', '_handlebars-test-patterns');
const pl = util.fakePatternLab(testPatternsPath);
var pattern_assembler = new pa();

var hiddenPatternPath = path.join('00-atoms', '00-global', '_00-hidden.hbs');
var hiddenPattern = pattern_assembler.process_pattern_iterative(hiddenPatternPath, pl);
pattern_assembler.process_pattern_recursive(hiddenPatternPath, pl);

var testPatternPath = path.join('00-molecules', '00-global', '00-hidden-pattern-tester.hbs');
var testPattern = pattern_assembler.process_pattern_iterative(testPatternPath, pl);
pattern_assembler.process_pattern_recursive(testPatternPath, pl);

//act
test.equals(util.sanitized(testPattern.render()), util.sanitized('Here\'s the hidden atom: [I\'m the hidden atom\n]\n'));
test.end();
});
20 changes: 20 additions & 0 deletions test/engine_underscore_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,23 @@ tap.test('findPartial return the ID of the partial, given a whole partial call',

});

tap.test('hidden underscore patterns can be called by their nice names', function(test){
const util = require('./util/test_utils.js');

//arrange
const testPatternsPath = path.resolve(__dirname, 'files', '_underscore-test-patterns');
const pl = util.fakePatternLab(testPatternsPath);
var pattern_assembler = new pa();

var hiddenPatternPath = path.join('00-atoms', '00-global', '_00-hidden.html');
var hiddenPattern = pattern_assembler.process_pattern_iterative(hiddenPatternPath, pl);
pattern_assembler.process_pattern_recursive(hiddenPatternPath, pl);

var testPatternPath = path.join('00-molecules', '00-global', '00-hidden-pattern-tester.html');
var testPattern = pattern_assembler.process_pattern_iterative(testPatternPath, pl);
pattern_assembler.process_pattern_recursive(testPatternPath, pl);

//act
test.equals(util.sanitized(testPattern.render()), util.sanitized('Here\'s the hidden atom: [I\'m the hidden atom\n]\n'));
test.end();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
I'm the hidden atom
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Here's the hidden atom: [{{> atoms-hidden}}]
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Hello there!
Here's the hidden atom: [{{> test-hidden-pattern}}]
1 change: 1 addition & 0 deletions test/files/_patterns/00-test/_00-hidden-pattern.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is the hidden atom
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
I'm the hidden atom
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Here's the hidden atom: [<%=_.renderNamedPartial('atoms-hidden', obj)%>]
25 changes: 24 additions & 1 deletion test/pattern_assembler_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ var pa = require('../core/lib/pattern_assembler');
var Pattern = require('../core/lib/object_factory').Pattern;
var path = require('path');



tap.test('process_pattern_recursive recursively includes partials', function(test) {

//tests inclusion of partial that will be discovered by diveSync later in iteration than parent
Expand Down Expand Up @@ -649,6 +651,27 @@ tap.test('addPattern - adds pattern template to patternlab partial object if ext
test.equals(patternlab.patterns.length, 1);
test.equals(patternlab.partials['test-bar'] != undefined, true);
test.equals(patternlab.partials['test-bar'], 'bar');
test.done();
test.end();
});

tap.test('hidden patterns can be called by their nice names', function(test){
var util = require('./util/test_utils.js');

//arrange
var testPatternsPath = path.resolve(__dirname, 'files', '_patterns');
var pl = util.fakePatternLab(testPatternsPath);
var pattern_assembler = new pa();

//act
var hiddenPatternPath = path.join('00-test', '_00-hidden-pattern.mustache');
var hiddenPattern = pattern_assembler.process_pattern_iterative(hiddenPatternPath, pl);
pattern_assembler.process_pattern_recursive(hiddenPatternPath, pl);

var testPatternPath = path.join('00-test', '15-hidden-pattern-tester.mustache');
var testPattern = pattern_assembler.process_pattern_iterative(testPatternPath, pl);
pattern_assembler.process_pattern_recursive(testPatternPath, pl);

//assert
test.equals(util.sanitized(testPattern.render()), util.sanitized('Hello there! Here\'s the hidden atom: [This is the hidden atom]'), 'hidden pattern rendered output not as expected');
test.end();
});
37 changes: 37 additions & 0 deletions test/util/test_utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"use strict";

module.exports = {

// fake pattern lab constructor:
// sets up a fake patternlab object, which is needed by the pattern processing
// apparatus.
fakePatternLab: (testPatternsPath) => {
var fpl = {
partials: {},
patterns: [],
footer: '',
header: '',
listitems: {},
listItemArray: [],
data: {
link: {}
},
config: require('../../patternlab-config.json'),
package: {}
};

// patch the pattern source so the pattern assembler can correctly determine
// the "subdir"
fpl.config.paths.source.patterns = testPatternsPath;

return fpl;
},

/**
* Strip out control characters from output if needed so make comparisons easier
* @param output - the template to strip
*/
sanitized: (outputTemplate) => {
return outputTemplate.replace(/\n/g, ' ').replace(/\r/g, ' ').replace(/\s\s+/g, ' ').trim();
}
};

0 comments on commit 777dd1c

Please sign in to comment.