diff --git a/Gruntfile.js b/Gruntfile.js index 63561c9ca..be14760e5 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -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'] @@ -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']); }; diff --git a/core/lib/object_factory.js b/core/lib/object_factory.js index a03e2b400..ff68a9e5b 100644 --- a/core/lib/object_factory.js +++ b/core/lib/object_factory.js @@ -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 @@ -22,7 +26,7 @@ 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) { @@ -30,13 +34,13 @@ var Pattern = function (relPath, data, patternlab) { }, '').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); @@ -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 = ''; diff --git a/package.json b/package.json index f8c226666..de331975f 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/test/engine_handlebars_tests.js b/test/engine_handlebars_tests.js index 7ab183420..cf1fa533e 100644 --- a/test/engine_handlebars_tests.js +++ b/test/engine_handlebars_tests.js @@ -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 @@ -70,6 +70,7 @@ function testFindPartials(test, partialTests) { test.end(); } + tap.test('hello world handlebars pattern renders', function (test) { test.plan(1); @@ -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(); +}); diff --git a/test/engine_underscore_tests.js b/test/engine_underscore_tests.js index 178db6d0b..8c245bdf4 100644 --- a/test/engine_underscore_tests.js +++ b/test/engine_underscore_tests.js @@ -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(); + }); diff --git a/test/files/_handlebars-test-patterns/00-atoms/00-global/_00-hidden.hbs b/test/files/_handlebars-test-patterns/00-atoms/00-global/_00-hidden.hbs new file mode 100644 index 000000000..b2c93a13e --- /dev/null +++ b/test/files/_handlebars-test-patterns/00-atoms/00-global/_00-hidden.hbs @@ -0,0 +1 @@ +I'm the hidden atom diff --git a/test/files/_handlebars-test-patterns/00-molecules/00-global/00-hidden-pattern-tester.hbs b/test/files/_handlebars-test-patterns/00-molecules/00-global/00-hidden-pattern-tester.hbs new file mode 100644 index 000000000..ce4a0c864 --- /dev/null +++ b/test/files/_handlebars-test-patterns/00-molecules/00-global/00-hidden-pattern-tester.hbs @@ -0,0 +1 @@ +Here's the hidden atom: [{{> atoms-hidden}}] diff --git a/test/files/_patterns/00-test/15-hidden-pattern-tester.mustache b/test/files/_patterns/00-test/15-hidden-pattern-tester.mustache new file mode 100644 index 000000000..5598f783f --- /dev/null +++ b/test/files/_patterns/00-test/15-hidden-pattern-tester.mustache @@ -0,0 +1,2 @@ +Hello there! +Here's the hidden atom: [{{> test-hidden-pattern}}] diff --git a/test/files/_patterns/00-test/_00-hidden-pattern.mustache b/test/files/_patterns/00-test/_00-hidden-pattern.mustache new file mode 100644 index 000000000..0f5831922 --- /dev/null +++ b/test/files/_patterns/00-test/_00-hidden-pattern.mustache @@ -0,0 +1 @@ +This is the hidden atom \ No newline at end of file diff --git a/test/files/_underscore-test-patterns/00-atoms/00-global/_00-hidden.html b/test/files/_underscore-test-patterns/00-atoms/00-global/_00-hidden.html new file mode 100644 index 000000000..b2c93a13e --- /dev/null +++ b/test/files/_underscore-test-patterns/00-atoms/00-global/_00-hidden.html @@ -0,0 +1 @@ +I'm the hidden atom diff --git a/test/files/_underscore-test-patterns/00-molecules/00-global/00-hidden-pattern-tester.html b/test/files/_underscore-test-patterns/00-molecules/00-global/00-hidden-pattern-tester.html new file mode 100644 index 000000000..fe3beef68 --- /dev/null +++ b/test/files/_underscore-test-patterns/00-molecules/00-global/00-hidden-pattern-tester.html @@ -0,0 +1 @@ +Here's the hidden atom: [<%=_.renderNamedPartial('atoms-hidden', obj)%>] diff --git a/test/pattern_assembler_tests.js b/test/pattern_assembler_tests.js index f03aeb7ae..09b60532d 100644 --- a/test/pattern_assembler_tests.js +++ b/test/pattern_assembler_tests.js @@ -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 @@ -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(); +}); diff --git a/test/util/test_utils.js b/test/util/test_utils.js new file mode 100644 index 000000000..652c52d5e --- /dev/null +++ b/test/util/test_utils.js @@ -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(); + } +};