diff --git a/.eslintrc b/.eslintrc index 1a6fc01c8..dbecc8098 100644 --- a/.eslintrc +++ b/.eslintrc @@ -3,6 +3,10 @@ "node": true, "builtin": true }, + "parserOptions": { + "ecmaVersion": 6, + "sourceType": "module" + }, "globals": {}, "rules": { "block-scoped-var": 0, diff --git a/.travis.yml b/.travis.yml index 844871fce..ad1107b81 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,7 +3,6 @@ language: node_js node_js: - node - 6 - - 5 - 4 before_install: @@ -19,7 +18,6 @@ branches: only: - master - dev - - issue/438-runAllTestsTravis notifications: webhooks: diff --git a/Gruntfile.js b/Gruntfile.js deleted file mode 100644 index 2d74d0d19..000000000 --- a/Gruntfile.js +++ /dev/null @@ -1,41 +0,0 @@ -module.exports = function (grunt) { - - /****************************** - * Project configuration. - * Should only be needed if you are developing against core, running tests, linting and want to run tests or increment package numbers - *****************************/ - grunt.initConfig({ - pkg: grunt.file.readJSON('package.json'), - concat: { - options: { - stripBanners: true, - banner: '/* \n * <%= pkg.name %> - v<%= pkg.version %> - <%= grunt.template.today("yyyy") %> \n * \n * <%= pkg.author.name %>, <%= pkg.contributors[0].name %>, and the web community.\n * Licensed under the <%= pkg.license %> license. \n * \n * Many thanks to Brad Frost and Dave Olsen for inspiration, encouragement, and advice. \n *\n */\n\n', - }, - patternlab: { - src: './core/lib/patternlab.js', - dest: './core/lib/patternlab.js' - } - }, - nodeunit: { - all: ['test/*_tests.js'] - }, - eslint: { - options: { - configFile: './.eslintrc' - }, - target: ['./core/lib/*'] - } - }); - - // load all grunt tasks - grunt.loadNpmTasks('grunt-contrib-concat'); - grunt.loadNpmTasks('grunt-eslint'); - grunt.loadNpmTasks('grunt-contrib-nodeunit'); - - //travis CI task - grunt.registerTask('travis', ['nodeunit', 'eslint']); - - //to be run prior to releasing a version - grunt.registerTask('build', ['nodeunit', 'eslint', 'concat']); - -}; diff --git a/core/lib/object_factory.js b/core/lib/object_factory.js index a03e2b400..29b637e4d 100644 --- a/core/lib/object_factory.js +++ b/core/lib/object_factory.js @@ -4,6 +4,11 @@ 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 var Pattern = function (relPath, data, patternlab) { @@ -22,7 +27,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 +35,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 +57,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/core/lib/pattern_assembler.js b/core/lib/pattern_assembler.js index 69308215e..bb9d9e9d8 100644 --- a/core/lib/pattern_assembler.js +++ b/core/lib/pattern_assembler.js @@ -45,9 +45,7 @@ var pattern_assembler = function () { return patternlab.patterns[i]; } } - if (patternlab.config.debug) { - console.error('Could not find pattern with partial ' + partialName); - } + plutils.logOrange('Could not find pattern referenced with partial syntax ' + partialName + '. This can occur when a pattern was renamed, moved, or no longer exists but it still called within a different template somewhere.'); return undefined; } @@ -442,8 +440,8 @@ var pattern_assembler = function () { function parseDataLinksHelper(patternlab, obj, key) { var linkRE, dataObjAsString, linkMatches; - //check for link.patternPartial - linkRE = /link\.[A-z0-9-_]+/g; + //check for 'link.patternPartial' + linkRE = /(?:'|")(link\.[A-z0-9-_]+)(?:'|")/g; //stringify the passed in object dataObjAsString = JSON5.stringify(obj); @@ -458,7 +456,7 @@ var pattern_assembler = function () { if (dataLink && dataLink.split('.').length >= 2) { //get the partial the link refers to - var linkPatternPartial = dataLink.split('.')[1]; + var linkPatternPartial = dataLink.split('.')[1].replace('"', '').replace("'", ""); var pattern = getPartial(linkPatternPartial, patternlab); if (pattern !== undefined) { @@ -472,7 +470,7 @@ var pattern_assembler = function () { //also make sure our global replace didn't mess up a protocol fullLink = fullLink.replace(/:\//g, '://'); - dataObjAsString = dataObjAsString.replace(dataLink, fullLink); + dataObjAsString = dataObjAsString.replace('link.' + linkPatternPartial, fullLink); } } else { if (patternlab.config.debug) { diff --git a/core/lib/pattern_exporter.js b/core/lib/pattern_exporter.js index c53768420..1b899a6a2 100644 --- a/core/lib/pattern_exporter.js +++ b/core/lib/pattern_exporter.js @@ -4,6 +4,13 @@ var fs = require('fs-extra'); var pattern_exporter = function () { + /** + * Exports all pattern's final HTML as defined in patternlab-config.json to desired location. + * Originally created to help facilitate easier consumption by jekyll. + * This method is off spec with PL PHP and will change or be augmented some day. + * + * @param patternlab {object} patternlab reference + */ function exportPatterns(patternlab) { //read the config export options var exportPartials = patternlab.config.patternExportPatternPartials; diff --git a/core/lib/patternlab.js b/core/lib/patternlab.js index 15da30ce8..0e53d594c 100644 --- a/core/lib/patternlab.js +++ b/core/lib/patternlab.js @@ -1,10 +1,10 @@ -/* - * patternlab-node - v2.6.0-alpha - 2016 - * +/* + * patternlab-node - v2.6.1 - 2016 + * * Brian Muenzenmeyer, Geoff Pursell, and the web community. - * Licensed under the MIT license. - * - * Many thanks to Brad Frost and Dave Olsen for inspiration, encouragement, and advice. + * Licensed under the MIT license. + * + * Many thanks to Brad Frost and Dave Olsen for inspiration, encouragement, and advice. * */ @@ -17,15 +17,16 @@ var diveSync = require('diveSync'), cleanHtml = require('js-beautify').html, inherits = require('util').inherits, pm = require('./plugin_manager'), + fs = require('fs-extra'), plutils = require('./utilities'); var EventEmitter = require('events').EventEmitter; -function buildPatternData(dataFilesPath, fs) { +function buildPatternData(dataFilesPath, fsDep) { var dataFiles = glob.sync(dataFilesPath + '*.json', {"ignore" : [dataFilesPath + 'listitems.json']}); var mergeObject = {}; dataFiles.forEach(function (filePath) { - var jsonData = fs.readJSONSync(path.resolve(filePath), 'utf8'); + var jsonData = fsDep.readJSONSync(path.resolve(filePath), 'utf8'); mergeObject = _.merge(mergeObject, jsonData); }); return mergeObject; @@ -83,18 +84,42 @@ function checkConfiguration(patternlab) { * @param patternlab - global data store */ function initializePlugins(patternlab) { + + if (!patternlab.config.plugins) { return; } + var plugin_manager = new pm(patternlab.config, path.resolve(__dirname, '../../patternlab-config.json')); var foundPlugins = plugin_manager.detect_plugins(); if (foundPlugins && foundPlugins.length > 0) { for (var i = 0; i < foundPlugins.length; i++) { - var plugin = plugin_manager.load_plugin(foundPlugins[i]); + + let pluginKey = foundPlugins[i]; + + if (patternlab.config.debug) { + console.log('Found plugin: ', pluginKey); + console.log('Attempting to load and initialize plugin.'); + } + + var plugin = plugin_manager.load_plugin(pluginKey); plugin(patternlab); } } } +/** + * Installs a given plugin. Assumes it has already been pulled down via npm + * @param pluginName - the name of the plugin + */ +function installPlugin(pluginName) { + //get the config + var configPath = path.resolve(process.cwd(), 'patternlab-config.json'); + var config = fs.readJSONSync(path.resolve(configPath), 'utf8'); + var plugin_manager = new pm(config, configPath); + + plugin_manager.install_plugin(pluginName); +} + function PatternLabEventEmitter() { EventEmitter.call(this); } @@ -104,7 +129,6 @@ var patternlab_engine = function (config) { 'use strict'; var JSON5 = require('json5'), - fs = require('fs-extra'), pa = require('./pattern_assembler'), pe = require('./pattern_exporter'), lh = require('./lineage_hunter'), @@ -123,7 +147,6 @@ var patternlab_engine = function (config) { checkConfiguration(patternlab); - //todo: determine if this is the best place to wire up plugins initializePlugins(patternlab); var paths = patternlab.config.paths; @@ -266,6 +289,37 @@ var patternlab_engine = function (config) { } } + function writePatternFiles(headHTML, pattern, footerHTML) { + const nullFormatter = str => str; + const defaultFormatter = codeString => cleanHtml(codeString, {indent_size: 2}); + const makePath = type => path.join(paths.public.patterns, pattern.getPatternLink(patternlab, type)); + const patternPage = headHTML + pattern.patternPartialCode + footerHTML; + const eng = pattern.engine; + + //beautify the output if configured to do so + const formatters = config.cleanOutputHtml ? { + rendered: eng.renderedCodeFormatter || defaultFormatter, + rawTemplate: eng.rawTemplateCodeFormatter || defaultFormatter, + markupOnly: eng.markupOnlyCodeFormatter || defaultFormatter + } : { + rendered: nullFormatter, + rawTemplate: nullFormatter, + markupOnly: nullFormatter + }; + + //prepare the path and contents of each output file + const outputFiles = [ + { path: makePath('rendered'), content: formatters.rendered(patternPage, pattern) }, + { path: makePath('rawTemplate'), content: formatters.rawTemplate(pattern.template, pattern) }, + { path: makePath('markupOnly'), content: formatters.markupOnly(pattern.patternPartialCode, pattern) } + ].concat( + eng.addOutputFiles ? eng.addOutputFiles(paths, patternlab) : [] + ); + + //write the compiled template to the public patterns directory + outputFiles.forEach(outFile => fs.outputFileSync(outFile.path, outFile.content)); + } + function buildPatterns(deletePatternDir) { patternlab.events.emit('patternlab-build-pattern-start', patternlab); @@ -357,6 +411,8 @@ var patternlab_engine = function (config) { pattern.patternLineageRExists = pattern.lineageR.length > 0; pattern.patternLineageEExists = pattern.patternLineageExists || pattern.patternLineageRExists; + patternlab.events.emit('patternlab-pattern-before-data-merge', patternlab, pattern); + //render the pattern, but first consolidate any data we may have var allData; try { @@ -425,21 +481,7 @@ var patternlab_engine = function (config) { patternlab.events.emit('patternlab-pattern-write-begin', patternlab, pattern); //write the compiled template to the public patterns directory - var patternPage = headHTML + pattern.patternPartialCode + footerHTML; - - //beautify the output if configured to do so - var cleanedPatternPage = config.cleanOutputHtml ? cleanHtml(patternPage, {indent_size: 2}) : patternPage; - var cleanedPatternPartialCode = config.cleanOutputHtml ? cleanHtml(pattern.patternPartialCode, {indent_size: 2}) : pattern.patternPartialCode; - var cleanedPatternTemplateCode = config.cleanOutputHtml ? cleanHtml(pattern.template, {indent_size: 2}) : pattern.template; - - //write the compiled template to the public patterns directory - fs.outputFileSync(paths.public.patterns + pattern.getPatternLink(patternlab, 'rendered'), cleanedPatternPage); - - //write the mustache file too - fs.outputFileSync(paths.public.patterns + pattern.getPatternLink(patternlab, 'rawTemplate'), cleanedPatternTemplateCode); - - //write the encoded version too - fs.outputFileSync(paths.public.patterns + pattern.getPatternLink(patternlab, 'markupOnly'), cleanedPatternPartialCode); + writePatternFiles(headHTML, pattern, footerHTML); patternlab.events.emit('patternlab-pattern-write-end', patternlab, pattern); @@ -485,6 +527,9 @@ var patternlab_engine = function (config) { }, loadstarterkit: function (starterkitName, clean) { loadStarterKit(starterkitName, clean); + }, + installplugin: function (pluginName) { + installPlugin(pluginName); } }; }; diff --git a/core/lib/plugin_manager.js b/core/lib/plugin_manager.js index fdb331a5b..2a0869036 100644 --- a/core/lib/plugin_manager.js +++ b/core/lib/plugin_manager.js @@ -5,10 +5,21 @@ var plugin_manager = function (config, configPath) { fs = require('fs-extra'), util = require('./utilities'); + /** + * Loads a plugin + * + * @param pluginName {string} the name of the plugin + * @return {object} the loaded plugin + */ function loadPlugin(pluginName) { return require(path.join(process.cwd(), 'node_modules', pluginName)); } + /** + * Installs a plugin + * + * @param pluginName {string} the name of the plugin + */ function installPlugin(pluginName) { try { var pluginPath = path.resolve( @@ -25,9 +36,18 @@ var plugin_manager = function (config, configPath) { var pluginPathDirExists = pluginDirStats.isDirectory(); if (pluginPathDirExists) { - //write config entry back var diskConfig = fs.readJSONSync(path.resolve(configPath), 'utf8'); - diskConfig[pluginName] = false; + + //add the plugin entry to patternlab-config.json + if (!diskConfig.plugins) { + diskConfig.plugins = {}; + } + diskConfig.plugins[pluginName] = { + enabled: true, + initialized: false + }; + + //write config entry back fs.outputFileSync(path.resolve(configPath), JSON.stringify(diskConfig, null, 2)); util.logGreen('Plugin ' + pluginName + ' installed.'); @@ -40,6 +60,11 @@ var plugin_manager = function (config, configPath) { } } + /** + * Detect installed plugins + * + * @return {array} list of installed plugins + */ function detectPlugins() { var node_modules_path = path.join(process.cwd(), 'node_modules'); return fs.readdirSync(node_modules_path).filter(function (dir) { @@ -48,10 +73,18 @@ var plugin_manager = function (config, configPath) { }); } + /** + * Disables an installed plugin + * Not implemented yet + */ function disablePlugin(pluginName) { console.log('disablePlugin not implemented yet. No change made to state of plugin', pluginName); } + /** + * Enables an installed plugin + * Not implemented yet + */ function enablePlugin(pluginName) { console.log('enablePlugin not implemented yet. No change made to state of plugin', pluginName); } diff --git a/core/lib/starterkit_manager.js b/core/lib/starterkit_manager.js index 89ef77ecf..3dec08115 100644 --- a/core/lib/starterkit_manager.js +++ b/core/lib/starterkit_manager.js @@ -7,6 +7,12 @@ var starterkit_manager = function (config) { util = require('./utilities'), paths = config.paths; + /** + * Loads npm module identified by the starterkitName parameter. + * + * @param starterkitName {string} Kit name + * @param clean {boolean} Indicates if the directory should be cleaned before loading + */ function loadStarterKit(starterkitName, clean) { try { var kitPath = path.resolve( @@ -43,9 +49,9 @@ var starterkit_manager = function (config) { } /** - * @func listStarterkits - * @desc Fetches starterkit repos from GH API that contain 'starterkit' in their name for the user 'pattern-lab' - * @returns {Promise} Returns an Array<{name,url}> for the starterkit repos + * Fetches starterkit repos from GH API that contain 'starterkit' in their name for the user 'pattern-lab' + * + * @return {Promise} Returns an Array<{name,url}> for the starterkit repos */ function listStarterkits() { return fetch('https://api.github.com/search/repositories?q=starterkit+in:name+user:pattern-lab&sort=stars&order=desc', { @@ -77,6 +83,11 @@ var starterkit_manager = function (config) { } + /** + * Detects installed starter kits + * + * @return {array} List of starter kits installed + */ function detectStarterKits() { var node_modules_path = path.join(process.cwd(), 'node_modules'); var npm_modules = fs.readdirSync(node_modules_path).filter(function (dir) { diff --git a/core/lib/style_modifier_hunter.js b/core/lib/style_modifier_hunter.js index 8c0b7413f..f6f9f0596 100644 --- a/core/lib/style_modifier_hunter.js +++ b/core/lib/style_modifier_hunter.js @@ -2,6 +2,13 @@ var style_modifier_hunter = function () { + /** + * Modifies a patterns partial with any styleModifiers found on the supplied partial + * + * @param pattern {object} the pattern to extend + * @param partial {string} partial containing styleModifiers + * @param patternlab {object} the patternlab instance + */ function consumestylemodifier(pattern, partial, patternlab) { //extract the classname from the stylemodifier which comes in the format of :className var styleModifier = partial.match(/:([\w\-_|])+/g) ? partial.match(/:([\w\-_|])+/g)[0].slice(1) : null; diff --git a/core/lib/utilities.js b/core/lib/utilities.js index d05eec876..8e1488814 100644 --- a/core/lib/utilities.js +++ b/core/lib/utilities.js @@ -4,21 +4,45 @@ var fs = require('fs-extra'), path = require('path'); var util = { - // http://stackoverflow.com/questions/6274339/how-can-i-shuffle-an-array-in-javascript + /** + * Shuffles an array in place. + * http://stackoverflow.com/questions/6274339/how-can-i-shuffle-an-array-in-javascript + * + * @param {Array} o + * @returns {Array} o + */ shuffle: function (o) { /*eslint-disable curly*/ for (var j, x, i = o.length; i; j = Math.floor(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x); return o; }, + /** + * Logs a message to the console with green text. + * + * @param {string} message + * @returns {string} message + */ logGreen: function (message) { console.log('\x1b[32m', message, '\x1b[0m'); }, + /** + * Logs a message to the console with orange text. + * + * @param {string} message + * @returns {string} message + */ logOrange: function (message) { console.log('\x1b[33m', message, '\x1b[0m'); }, + /** + * Logs a message to the console with red text. + * + * @param {string} message + * @returns {string} message + */ logRed: function (message) { console.log('\x1b[41m', message, '\x1b[0m'); }, @@ -60,6 +84,12 @@ var util = { return obj2; }, + /** + * Determines whether or not an object is empty. + * + * @param {Object} obj + * @returns {Boolean} + */ isObjectEmpty: function (obj) { for (var prop in obj) { if (obj.hasOwnProperty(prop)) { return false; } @@ -67,8 +97,14 @@ var util = { return true; }, - // recursively delete the contents of directory - // adapted from https://gist.github.com/tkihira/2367067 + /** + * Recursively delete the contents of directory. + * Adapted from https://gist.github.com/tkihira/2367067 + * + * @param {string} dir - directory to empty + * @param {string} cleanDir - already empty directory + * @returns {undefined} + */ emptyDirectory: function (dir, cleanDir) { var list = fs.readdirSync(dir); for (var i = 0; i < list.length; i++) { diff --git a/package.json b/package.json index ab66fa4a4..b7bfd5a5e 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "patternlab-node", "description": "Pattern Lab is a collection of tools to help you create atomic design systems. This is the node command line interface (CLI).", - "version": "2.6.0-alpha", + "version": "2.6.1", "main": "./core/lib/patternlab.js", "dependencies": { "diveSync": "^0.3.0", @@ -16,10 +16,8 @@ "patternengine-node-mustache": "^1.0.0" }, "devDependencies": { - "grunt": "~1.0.1", - "grunt-contrib-concat": "^1.0.1", - "grunt-contrib-nodeunit": "^1.0.0", - "grunt-eslint": "^18.0.0" + "eslint": "^3.5.0", + "tap": "^7.1.2" }, "keywords": [ "Pattern Lab", @@ -44,7 +42,8 @@ ], "license": "MIT", "scripts": { - "test": "grunt travis --verbose" + "test": "eslint core/**/*.js && tap test/*_tests.js --reporter spec", + "lint": "eslint core/**/*.js" }, "engines": { "node": ">=4.0" diff --git a/test/annotation_exporter_tests.js b/test/annotation_exporter_tests.js index 49ae049c8..89004f24a 100644 --- a/test/annotation_exporter_tests.js +++ b/test/annotation_exporter_tests.js @@ -1,7 +1,7 @@ "use strict"; -var eol = require('os').EOL; -var Pattern = require('../core/lib/object_factory').Pattern; +var tap = require('tap'); + var extend = require('util')._extend; var anPath = './test/files/'; @@ -22,59 +22,55 @@ function createFakePatternLab(anPath, customProps) { var patternlab = createFakePatternLab(anPath); var ae = require('../core/lib/annotation_exporter')(patternlab); -exports['annotaton_exporter'] = { - - 'converts old JS annotations into new format': function (test) { - //arrange - //act - var annotations = ae.gatherJS(); - - //assert - test.equals(annotations.length, 2); - test.equals(annotations[1].el, '.logo'); - test.equals(annotations[1].title, 'Logo'); - test.equals(annotations[1].comment, "The logo image is an SVG file, which ensures that the logo displays crisply even on high resolution displays. A PNG fallback is provided for browsers that don't support SVG images.

Further reading: Optimizing Web Experiences for High Resolution Screens

"); - - test.done(); - }, - - 'converts new markdown annotations into an array': function (test) { - //arrange - //act - var annotations = ae.gatherMD(); - - //assert - test.equals(annotations.length, 3); - test.equals(annotations[1].el, '.logo'); - test.equals(annotations[1].title, 'Logo'); - test.equals(annotations[1].comment.replace(/\r?\n|\r/gm, ""), '

The logo image is an SVG file.

'); - - test.done(); - }, - - 'merges both annotation methods into one array' : function (test) { - //arrange - - //act - var annotations = ae.gather(); - - //assert - test.equals(annotations.length, 3); - test.equals(annotations[2].el, '#nav'); - test.equals(annotations[2].title, 'Navigation'); - test.equals(annotations[2].comment.replace(/\r?\n|\r/gm, ""), '

Navigation for adaptive web experiences can be tricky. Refer to these repsonsive patterns when evaluating solutions.

'); - - test.done(); - - }, - - 'when there are 0 annotation files' : function (test) { - var emptyAnPath = './test/files/empty/'; - var patternlab2 = createFakePatternLab(emptyAnPath); - var ae2 = require('../core/lib/annotation_exporter')(patternlab2); - - var annotations = ae2.gather(); - test.equals(annotations.length, 0); - test.done(); - } -}; +tap.test('converts old JS annotations into new format', function (test) { + //arrange + //act + var annotations = ae.gatherJS(); + + //assert + test.equals(annotations.length, 2); + test.equals(annotations[1].el, '.logo'); + test.equals(annotations[1].title, 'Logo'); + test.equals(annotations[1].comment, "The logo image is an SVG file, which ensures that the logo displays crisply even on high resolution displays. A PNG fallback is provided for browsers that don't support SVG images.

Further reading: Optimizing Web Experiences for High Resolution Screens

"); + + test.end(); +}); + +tap.test('converts new markdown annotations into an array', function (test) { + //arrange + //act + var annotations = ae.gatherMD(); + + //assert + test.equals(annotations.length, 3); + test.equals(annotations[1].el, '.logo'); + test.equals(annotations[1].title, 'Logo'); + test.equals(annotations[1].comment.replace(/\r?\n|\r/gm, ""), '

The logo image is an SVG file.

'); + + test.end(); +}); + +tap.test('merges both annotation methods into one array', function (test) { + //arrange + + //act + var annotations = ae.gather(); + + //assert + test.equals(annotations.length, 3); + test.equals(annotations[2].el, '#nav'); + test.equals(annotations[2].title, 'Navigation'); + test.equals(annotations[2].comment.replace(/\r?\n|\r/gm, ""), '

Navigation for adaptive web experiences can be tricky. Refer to these repsonsive patterns when evaluating solutions.

'); + + test.end(); +}); + +tap.test('when there are 0 annotation files', function (test) { + var emptyAnPath = './test/files/empty/'; + var patternlab2 = createFakePatternLab(emptyAnPath); + var ae2 = require('../core/lib/annotation_exporter')(patternlab2); + + var annotations = ae2.gather(); + test.equals(annotations.length, 0); + test.end(); +}); diff --git a/test/engine_handlebars_tests.js b/test/engine_handlebars_tests.js index 3521ee362..cf1fa533e 100644 --- a/test/engine_handlebars_tests.js +++ b/test/engine_handlebars_tests.js @@ -1,11 +1,22 @@ "use strict"; +/*eslint-disable no-shadow*/ +var tap = require('tap'); var path = require('path'); var pa = require('../core/lib/pattern_assembler'); var Pattern = require('../core/lib/object_factory').Pattern; var testPatternsPath = path.resolve(__dirname, 'files', '_handlebars-test-patterns'); var eol = require('os').EOL; +// don't run these tests unless handlebars is installed +var engineLoader = require('../core/lib/pattern_engines'); +if (!engineLoader.handlebars) { + tap.test('Handlebars engine not installed, skipping tests.', function (test) { + test.end() + }) + return +} + // fake pattern lab constructor: // sets up a fake patternlab object, which is needed by the pattern processing // apparatus. @@ -34,7 +45,7 @@ function fakePatternLab() { // function for testing sets of partials function testFindPartials(test, partialTests) { - test.expect(partialTests.length + 1); + test.plan(partialTests.length + 1); // setup current pattern from what we would have during execution // docs on partial syntax are here: @@ -56,138 +67,158 @@ function testFindPartials(test, partialTests) { test.equals(results[index], testString); }); - test.done(); + test.end(); } -exports['engine_handlebars'] = { - 'hello world handlebars pattern renders': function (test) { - test.expect(1); - - var patternPath = path.join('00-atoms', '00-global', '00-helloworld.hbs'); - - // do all the normal processing of the pattern - var patternlab = new fakePatternLab(); - var assembler = new pa(); - var helloWorldPattern = assembler.process_pattern_iterative(patternPath, patternlab); - assembler.process_pattern_recursive(patternPath, patternlab); - - test.equals(helloWorldPattern.render(), 'Hello world!' + eol); - test.done(); - }, - 'hello worlds handlebars pattern can see the atoms-helloworld partial and renders it twice': function (test) { - test.expect(1); - - // pattern paths - var pattern1Path = path.join('00-atoms', '00-global', '00-helloworld.hbs'); - var pattern2Path = path.join('00-molecules', '00-global', '00-helloworlds.hbs'); - - // set up environment - var patternlab = new fakePatternLab(); // environment - var assembler = new pa(); - - // do all the normal processing of the pattern - assembler.process_pattern_iterative(pattern1Path, patternlab); - var helloWorldsPattern = assembler.process_pattern_iterative(pattern2Path, patternlab); - assembler.process_pattern_recursive(pattern1Path, patternlab); - assembler.process_pattern_recursive(pattern2Path, patternlab); - - // test - test.equals(helloWorldsPattern.render(), 'Hello world!' + eol + ' and Hello world!' + eol + eol); - test.done(); - }, - 'handlebars partials can render JSON values': function (test) { - test.expect(1); - - // pattern paths - var pattern1Path = path.join('00-atoms', '00-global', '00-helloworld-withdata.hbs'); - - // set up environment - var patternlab = new fakePatternLab(); // environment - var assembler = new pa(); - - // do all the normal processing of the pattern - var helloWorldWithData = assembler.process_pattern_iterative(pattern1Path, patternlab); - assembler.process_pattern_recursive(pattern1Path, patternlab); - - // test - test.equals(helloWorldWithData.render(), 'Hello world!' + eol + 'Yeah, we got the subtitle from the JSON.' + eol); - test.done(); - }, - 'handlebars partials use the JSON environment from the calling pattern and can accept passed parameters': function (test) { - test.expect(1); - - // pattern paths - var atomPath = path.join('00-atoms', '00-global', '00-helloworld-withdata.hbs'); - var molPath = path.join('00-molecules', '00-global', '00-call-atom-with-molecule-data.hbs'); - - // set up environment - var patternlab = new fakePatternLab(); // environment - var assembler = new pa(); - - // do all the normal processing of the pattern - assembler.process_pattern_iterative(atomPath, patternlab); - var mol = assembler.process_pattern_iterative(molPath, patternlab); - assembler.process_pattern_recursive(atomPath, patternlab); - assembler.process_pattern_recursive(molPath, patternlab); - - // test - test.equals(mol.render(), '

Call with default JSON environment:

' + eol + 'This is Hello world!' + eol + 'from the default JSON.' + eol + eol + eol +'

Call with passed parameter:

' + eol + 'However, this is Hello world!' + eol + 'from a totally different blob.' + eol + eol); - test.done(); - }, - 'find_pattern_partials finds partials': function (test) { - testFindPartials(test, [ - "{{> molecules-comment-header}}", - "{{> molecules-comment-header}}", - "{{> " + eol + " molecules-comment-header" + eol + "}}", - "{{> molecules-weird-spacing }}", - "{{> molecules-ba_d-cha*rs }}" - ]); - }, - 'find_pattern_partials finds verbose partials': function (test) { - testFindPartials(test, [ - '{{> 01-molecules/06-components/03-comment-header.hbs }}', - "{{> 01-molecules/06-components/02-single-comment.hbs(description: 'A life is like a garden. Perfect moments can be had, but not preserved, except in memory.') }}", - '{{> molecules-single-comment:foo }}', - "{{>atoms-error(message: 'That\'s no moon...')}}", - "{{> atoms-error(message: 'That\'s no moon...') }}", - '{{> 00-atoms/00-global/06-test }}' - ]); - }, - 'find_pattern_partials finds simple partials with parameters': function (test) { - testFindPartials(test, [ - "{{> molecules-single-comment(description: 'A life isn\'t like a garden. Perfect moments can be had, but not preserved, except in memory.') }}", - '{{> molecules-single-comment(description:"A life is like a \"garden\". Perfect moments can be had, but not preserved, except in memory.") }}' - ]); - }, - 'find_pattern_partials finds simple partials with style modifiers': function (test) { - testFindPartials(test, [ - '{{> molecules-single-comment:foo }}' - ]); - }, - 'find_pattern_partials finds partials with handlebars parameters': function (test) { - testFindPartials(test, [ - '{{> atoms-title title="bravo" headingLevel="2" headingSize="bravo" position="left"}}', - '{{> atoms-title title="bravo"' + eol + ' headingLevel="2"' + eol + ' headingSize="bravo"' + eol + ' position="left"}}', - '{{> atoms-title title="color  midnight blue" headingSize="charlie"}}', - '{{> atoms-input label="city" required=true}}', - '{{> organisms-product-filter filterData}}', - '{{> atoms-input email required=true}}', - '{{> molecules-storycard variants.flex }}', - '{{> myPartial name=../name }}' - ]); - }, - - 'find_pattern_partials finds handlebars block partials': function (test) { - testFindPartials(test, [ - '{{#> myPartial }}' - ]); - } -}; - -// don't run these tests unless handlebars is installed -var engineLoader = require('../core/lib/pattern_engines'); -if (!engineLoader.handlebars) { - console.log("Handlebars engine not installed, skipping tests."); - delete exports.engine_handlebars; -} +tap.test('hello world handlebars pattern renders', function (test) { + test.plan(1); + + var patternPath = path.join('00-atoms', '00-global', '00-helloworld.hbs'); + + // do all the normal processing of the pattern + var patternlab = new fakePatternLab(); + var assembler = new pa(); + var helloWorldPattern = assembler.process_pattern_iterative(patternPath, patternlab); + assembler.process_pattern_recursive(patternPath, patternlab); + + test.equals(helloWorldPattern.render(), 'Hello world!' + eol); + test.end(); +}); + +tap.test('hello worlds handlebars pattern can see the atoms-helloworld partial and renders it twice', function (test) { + test.plan(1); + + // pattern paths + var pattern1Path = path.join('00-atoms', '00-global', '00-helloworld.hbs'); + var pattern2Path = path.join('00-molecules', '00-global', '00-helloworlds.hbs'); + + // set up environment + var patternlab = new fakePatternLab(); // environment + var assembler = new pa(); + + // do all the normal processing of the pattern + assembler.process_pattern_iterative(pattern1Path, patternlab); + var helloWorldsPattern = assembler.process_pattern_iterative(pattern2Path, patternlab); + assembler.process_pattern_recursive(pattern1Path, patternlab); + assembler.process_pattern_recursive(pattern2Path, patternlab); + + // test + test.equals(helloWorldsPattern.render(), 'Hello world!' + eol + ' and Hello world!' + eol + eol); + test.end(); +}); + +tap.test('handlebars partials can render JSON values', function (test) { + test.plan(1); + + // pattern paths + var pattern1Path = path.join('00-atoms', '00-global', '00-helloworld-withdata.hbs'); + + // set up environment + var patternlab = new fakePatternLab(); // environment + var assembler = new pa(); + + // do all the normal processing of the pattern + var helloWorldWithData = assembler.process_pattern_iterative(pattern1Path, patternlab); + assembler.process_pattern_recursive(pattern1Path, patternlab); + + // test + test.equals(helloWorldWithData.render(), 'Hello world!' + eol + 'Yeah, we got the subtitle from the JSON.' + eol); + test.end(); +}); + +tap.test('handlebars partials use the JSON environment from the calling pattern and can accept passed parameters', function (test) { + test.plan(1); + + // pattern paths + var atomPath = path.join('00-atoms', '00-global', '00-helloworld-withdata.hbs'); + var molPath = path.join('00-molecules', '00-global', '00-call-atom-with-molecule-data.hbs'); + + // set up environment + var patternlab = new fakePatternLab(); // environment + var assembler = new pa(); + + // do all the normal processing of the pattern + assembler.process_pattern_iterative(atomPath, patternlab); + var mol = assembler.process_pattern_iterative(molPath, patternlab); + assembler.process_pattern_recursive(atomPath, patternlab); + assembler.process_pattern_recursive(molPath, patternlab); + + // test + test.equals(mol.render(), '

Call with default JSON environment:

' + eol + 'This is Hello world!' + eol + 'from the default JSON.' + eol + eol + eol +'

Call with passed parameter:

' + eol + 'However, this is Hello world!' + eol + 'from a totally different blob.' + eol + eol); + test.end(); +}); + +tap.test('find_pattern_partials finds partials', function (test) { + testFindPartials(test, [ + "{{> molecules-comment-header}}", + "{{> molecules-comment-header}}", + "{{> " + eol + " molecules-comment-header" + eol + "}}", + "{{> molecules-weird-spacing }}", + "{{> molecules-ba_d-cha*rs }}" + ]); +}); + +tap.test('find_pattern_partials finds verbose partials', function (test) { + testFindPartials(test, [ + '{{> 01-molecules/06-components/03-comment-header.hbs }}', + "{{> 01-molecules/06-components/02-single-comment.hbs(description: 'A life is like a garden. Perfect moments can be had, but not preserved, except in memory.') }}", + '{{> molecules-single-comment:foo }}', + "{{>atoms-error(message: 'That\'s no moon...')}}", + "{{> atoms-error(message: 'That\'s no moon...') }}", + '{{> 00-atoms/00-global/06-test }}' + ]); +}); + +tap.test('find_pattern_partials finds simple partials with parameters', function (test) { + testFindPartials(test, [ + "{{> molecules-single-comment(description: 'A life isn\'t like a garden. Perfect moments can be had, but not preserved, except in memory.') }}", + '{{> molecules-single-comment(description:"A life is like a \"garden\". Perfect moments can be had, but not preserved, except in memory.") }}' + ]); +}); + +tap.test('find_pattern_partials finds simple partials with style modifiers', function (test) { + testFindPartials(test, [ + '{{> molecules-single-comment:foo }}' + ]); +}); + +tap.test('find_pattern_partials finds partials with handlebars parameters', function (test) { + testFindPartials(test, [ + '{{> atoms-title title="bravo" headingLevel="2" headingSize="bravo" position="left"}}', + '{{> atoms-title title="bravo"' + eol + ' headingLevel="2"' + eol + ' headingSize="bravo"' + eol + ' position="left"}}', + '{{> atoms-title title="color  midnight blue" headingSize="charlie"}}', + '{{> atoms-input label="city" required=true}}', + '{{> organisms-product-filter filterData}}', + '{{> atoms-input email required=true}}', + '{{> molecules-storycard variants.flex }}', + '{{> myPartial name=../name }}' + ]); +}); + +tap.test('find_pattern_partials finds handlebars block partials', function (test) { + testFindPartials(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_mustache_tests.js b/test/engine_mustache_tests.js index da811be0e..ea93ca723 100644 --- a/test/engine_mustache_tests.js +++ b/test/engine_mustache_tests.js @@ -1,11 +1,22 @@ "use strict"; +/*eslint-disable no-shadow*/ +var tap = require('tap'); var path = require('path'); var pa = require('../core/lib/pattern_assembler'); var Pattern = require('../core/lib/object_factory').Pattern; var testPatternsPath = path.resolve(__dirname, 'files', '_patterns'); var eol = require('os').EOL; +// don't run these tests unless mustache is installed +var engineLoader = require('../core/lib/pattern_engines'); +if (!engineLoader.mustache) { + tap.test('Mustache engine not installed, skipping tests.', function (test) { + test.end(); + }); + return; +} + // fake pattern lab constructor: // sets up a fake patternlab object, which is needed by the pattern processing // apparatus. @@ -33,7 +44,7 @@ function fakePatternLab() { // function for testing sets of partials function testFindPartials(test, partialTests) { - test.expect(partialTests.length + 1); + test.plan(partialTests.length + 1); // setup current pattern from what we would have during execution // docs on partial syntax are here: @@ -55,11 +66,11 @@ function testFindPartials(test, partialTests) { test.equals(results[index], testString); }); - test.done(); + test.end(); } function testFindPartialsWithStyleModifiers(test, partialTests) { - test.expect(partialTests.length + 1); + test.plan(partialTests.length + 1); // setup current pattern from what we would have during execution // docs on partial syntax are here: @@ -81,11 +92,11 @@ function testFindPartialsWithStyleModifiers(test, partialTests) { test.equals(results[index], testString); }); - test.done(); + test.end(); } function testFindPartialsWithPatternParameters(test, partialTests) { - test.expect(partialTests.length + 1); + test.plan(partialTests.length + 1); // setup current pattern from what we would have during execution // docs on partial syntax are here: @@ -107,110 +118,106 @@ function testFindPartialsWithPatternParameters(test, partialTests) { test.equals(results[index], testString); }); - test.done(); + test.end(); } -exports['engine_mustache'] = { - 'find_pattern_partials finds one simple partial': function (test) { - testFindPartials(test, [ - "{{> molecules-comment-header}}" - ]); - }, - - 'find_pattern_partials finds simple partials under stressed circumstances': function (test) { - testFindPartials(test, [ - "{{>molecules-comment-header}}", - "{{> " + eol + " molecules-comment-header" + eol + "}}", - "{{> molecules-weird-spacing }}" - ]); - }, - - 'find_pattern_partials finds one simple verbose partial': function (test) { - testFindPartials(test, [ - '{{> 00-atoms/00-global/06-test }}' - ]); - }, - - 'find_pattern_partials finds partials with parameters': function (test) { - testFindPartials(test, [ - "{{> molecules-single-comment(description: true) }}", - "{{> molecules-single-comment(description: 42) }}", - "{{> molecules-single-comment(description: '42') }}", - "{{> molecules-single-comment(description: \"42\") }}", - "{{> molecules-single-comment(description: 'test', anotherThing: 'retest') }}", - "{{> molecules-single-comment(description: false, anotherThing: \"retest\") }}", - '{{> molecules-single-comment(description:"A life is like a \"garden\". Perfect moments can be had, but not preserved, except in memory.") }}' - ]); - }, - - 'find_pattern_partials finds simple partials with style modifiers': function (test) { - testFindPartials(test, [ - '{{> molecules-single-comment:foo }}', - '{{> molecules-single-comment:foo|bar }}' - ]); - }, - 'find_pattern_partials finds mixed partials': function (test) { - testFindPartials(test, [ - '{{> molecules-single-comment:foo(description: "test", anotherThing: true) }}', - '{{> molecules-single-comment:foo|bar(description: true) }}' - ]); - }, - - 'find_pattern_partials finds one simple partial with styleModifier': function (test) { - testFindPartialsWithStyleModifiers(test, [ - "{{> molecules-comment-header:test}}" - ]); - }, - 'find_pattern_partials finds partial with many styleModifiers': function (test) { - testFindPartialsWithStyleModifiers(test, [ - "{{> molecules-comment-header:test|test2|test3}}" - ]); - }, - 'find_pattern_partials finds partials with differing styleModifiers': function (test) { - testFindPartialsWithStyleModifiers(test, [ - "{{> molecules-comment-header:test|test2|test3}}", - "{{> molecules-comment-header:foo-1}}", - "{{> molecules-comment-header:bar_1}}" - ]); - }, - 'find_pattern_partials finds partials with styleModifiers when parameters present': function (test) { - testFindPartialsWithStyleModifiers(test, [ - "{{> molecules-comment-header:test|test2|test3(description: true)}}", - "{{> molecules-comment-header:foo-1(description: 'foo')}}", - "{{> molecules-comment-header:bar_1(descrition: 'bar', anotherThing: 10102010) }}" - ]); - }, - - 'find_pattern_partials_with_parameters finds one simple partial with parameters': function (test) { - testFindPartialsWithPatternParameters(test, [ - "{{> molecules-comment-header(description: 'test')}}" - ]); - }, - 'find_pattern_partials_with_parameters finds partials with parameters': function (test) { - testFindPartialsWithPatternParameters(test, [ - "{{> molecules-single-comment(description: true) }}", - "{{> molecules-single-comment(description: 42) }}", - "{{> molecules-single-comment(description: '42') }}", - "{{> molecules-single-comment(description: \"42\") }}", - "{{> molecules-single-comment(description: 'test', anotherThing: 'retest') }}", - "{{> molecules-single-comment(description: false, anotherThing: \"retest\") }}", - '{{> molecules-single-comment(description:"A life is like a \"garden\". Perfect moments can be had, but not preserved, except in memory.") }}' - ]); - }, - 'find_pattern_partials finds partials with parameters when styleModifiers present': function (test) { - testFindPartialsWithPatternParameters(test, [ - "{{> molecules-comment-header:test|test2|test3(description: true)}}", - "{{> molecules-comment-header:foo-1(description: 'foo')}}", - "{{> molecules-comment-header:bar_1(descrition: 'bar', anotherThing: 10102010) }}" - ]); - } - -}; - -// don't run these tests unless mustache is installed -var engineLoader = require('../core/lib/pattern_engines'); -if (!engineLoader.mustache) { - console.log("Mustache engine not installed, skipping tests."); - delete exports.engine_mustache; -} +tap.test('find_pattern_partials finds one simple partial', function (test) { + testFindPartials(test, [ + "{{> molecules-comment-header}}" + ]); +}); + +tap.test('find_pattern_partials finds simple partials under stressed circumstances', function (test) { + testFindPartials(test, [ + "{{>molecules-comment-header}}", + "{{> " + eol + " molecules-comment-header" + eol + "}}", + "{{> molecules-weird-spacing }}" + ]); +}); + +tap.test('find_pattern_partials finds one simple verbose partial', function (test) { + testFindPartials(test, [ + '{{> 00-atoms/00-global/06-test }}' + ]); +}); + +tap.test('find_pattern_partials finds partials with parameters', function (test) { + testFindPartials(test, [ + "{{> molecules-single-comment(description: true) }}", + "{{> molecules-single-comment(description: 42) }}", + "{{> molecules-single-comment(description: '42') }}", + "{{> molecules-single-comment(description: \"42\") }}", + "{{> molecules-single-comment(description: 'test', anotherThing: 'retest') }}", + "{{> molecules-single-comment(description: false, anotherThing: \"retest\") }}", + '{{> molecules-single-comment(description:"A life is like a \"garden\". Perfect moments can be had, but not preserved, except in memory.") }}' + ]); +}); + +tap.test('find_pattern_partials finds simple partials with style modifiers', function (test) { + testFindPartials(test, [ + '{{> molecules-single-comment:foo }}', + '{{> molecules-single-comment:foo|bar }}' + ]); +}); + +tap.test('find_pattern_partials finds mixed partials', function (test) { + testFindPartials(test, [ + '{{> molecules-single-comment:foo(description: "test", anotherThing: true) }}', + '{{> molecules-single-comment:foo|bar(description: true) }}' + ]); +}); + +tap.test('find_pattern_partials finds one simple partial with styleModifier', function (test) { + testFindPartialsWithStyleModifiers(test, [ + "{{> molecules-comment-header:test}}" + ]); +}); + +tap.test('find_pattern_partials finds partial with many styleModifiers', function (test) { + testFindPartialsWithStyleModifiers(test, [ + "{{> molecules-comment-header:test|test2|test3}}" + ]); +}); + +tap.test('find_pattern_partials finds partials with differing styleModifiers', function (test) { + testFindPartialsWithStyleModifiers(test, [ + "{{> molecules-comment-header:test|test2|test3}}", + "{{> molecules-comment-header:foo-1}}", + "{{> molecules-comment-header:bar_1}}" + ]); +}); + +tap.test('find_pattern_partials finds partials with styleModifiers when parameters present', function (test) { + testFindPartialsWithStyleModifiers(test, [ + "{{> molecules-comment-header:test|test2|test3(description: true)}}", + "{{> molecules-comment-header:foo-1(description: 'foo')}}", + "{{> molecules-comment-header:bar_1(descrition: 'bar', anotherThing: 10102010) }}" + ]); +}); + +tap.test('find_pattern_partials_with_parameters finds one simple partial with parameters', function (test) { + testFindPartialsWithPatternParameters(test, [ + "{{> molecules-comment-header(description: 'test')}}" + ]); +}); + +tap.test('find_pattern_partials_with_parameters finds partials with parameters', function (test) { + testFindPartialsWithPatternParameters(test, [ + "{{> molecules-single-comment(description: true) }}", + "{{> molecules-single-comment(description: 42) }}", + "{{> molecules-single-comment(description: '42') }}", + "{{> molecules-single-comment(description: \"42\") }}", + "{{> molecules-single-comment(description: 'test', anotherThing: 'retest') }}", + "{{> molecules-single-comment(description: false, anotherThing: \"retest\") }}", + '{{> molecules-single-comment(description:"A life is like a \"garden\". Perfect moments can be had, but not preserved, except in memory.") }}' + ]); +}); + +tap.test('find_pattern_partials finds partials with parameters when styleModifiers present', function (test) { + testFindPartialsWithPatternParameters(test, [ + "{{> molecules-comment-header:test|test2|test3(description: true)}}", + "{{> molecules-comment-header:foo-1(description: 'foo')}}", + "{{> molecules-comment-header:bar_1(descrition: 'bar', anotherThing: 10102010) }}" + ]); +}); diff --git a/test/engine_twig_tests.js b/test/engine_twig_tests.js index c5f9773b4..5a8351f7a 100644 --- a/test/engine_twig_tests.js +++ b/test/engine_twig_tests.js @@ -1,11 +1,22 @@ "use strict"; /*eslint-disable dot-notation*/ +/*eslint-disable no-shadow*/ +var tap = require('tap'); var path = require('path'); var pa = require('../core/lib/pattern_assembler'); var Pattern = require('../core/lib/object_factory').Pattern; var eol = require('os').EOL; +// don't run these tests unless twig is installed +var engineLoader = require('../core/lib/pattern_engines'); +if (!engineLoader.twig) { + tap.test('Twig engine not installed, skipping tests.', function (test) { + test.end(); + }); + return; +} + // fake pattern lab constructor: // sets up a fake patternlab object, which is needed by the pattern processing // apparatus. @@ -34,7 +45,7 @@ function fakePatternLab() { // function for testing sets of partials function testFindPartials(test, partialTests) { - test.expect(partialTests.length + 1); + test.plan(partialTests.length + 1); // setup current pattern from what we would have during execution // docs on partial syntax are here: @@ -56,136 +67,133 @@ function testFindPartials(test, partialTests) { test.equals(results[index], testString); }); - test.done(); + test.end(); } -exports['engine_twig'] = { - 'button twig pattern renders': function (test) { - test.expect(1); - - var patternPath = path.join('00-atoms', '00-general', '08-button.twig'); - var expectedValue = '' + eol + eol + 'Button' + eol; - - // do all the normal processing of the pattern - var patternlab = new fakePatternLab(); - var assembler = new pa(); - var helloWorldPattern = assembler.process_pattern_iterative(patternPath, patternlab); - assembler.process_pattern_recursive(patternPath, patternlab); - - test.equals(helloWorldPattern.render(), expectedValue); - test.done(); - }, - 'media object twig pattern can see the atoms-button and atoms-image partials and renders them': function (test) { - test.expect(1); - - // pattern paths - var buttonPatternPath = path.join('00-atoms', '00-general', '08-button.twig'); - var imagePatternPath = path.join('00-atoms', '00-general', '09-image.twig'); - var mediaObjectPatternPath = path.join('00-molecules', '00-general', '00-media-object.twig'); - - var expectedValue = '\n\n\n\n\n
\n \n\n \n\nButton\n\n\n
\n\n \n \n\n

Oh, hello world!

\n
\n
\n'; - - // set up environment - var patternlab = new fakePatternLab(); // environment - var assembler = new pa(); - - // do all the normal processing of the pattern - assembler.process_pattern_iterative(buttonPatternPath, patternlab); - assembler.process_pattern_iterative(imagePatternPath, patternlab); - var mediaObjectPattern = assembler.process_pattern_iterative(mediaObjectPatternPath, patternlab); - assembler.process_pattern_recursive(buttonPatternPath, patternlab); - assembler.process_pattern_recursive(imagePatternPath, patternlab); - assembler.process_pattern_recursive(mediaObjectPatternPath, patternlab); - - // test - // this pattern is too long - so just remove line endings on both sides and compare output - test.equals(mediaObjectPattern.render().replace(/\r?\n|\r/gm, ""), expectedValue.replace(/\r?\n|\r/gm, "")); - test.done(); - }, - // 'twig partials can render JSON values': function (test) { - // test.expect(1); - - // // pattern paths - // var pattern1Path = path.resolve( - // testPatternsPath, - // '00-atoms', - // '00-global', - // '00-helloworld-withdata.hbs' - // ); - - // // set up environment - // var patternlab = new fakePatternLab(); // environment - // var assembler = new pa(); - - // // do all the normal processing of the pattern - // var helloWorldWithData = assembler.process_pattern_iterative(pattern1Path, patternlab); - // assembler.process_pattern_recursive(pattern1Path, patternlab); - - // // test - // test.equals(helloWorldWithData.render(), 'Hello world!\nYeah, we got the subtitle from the JSON.\n'); - // test.done(); - // }, - // 'twig partials use the JSON environment from the calling pattern and can accept passed parameters': function (test) { - // test.expect(1); - - // // pattern paths - // var atomPath = path.resolve( - // testPatternsPath, - // '00-atoms', - // '00-global', - // '00-helloworld-withdata.hbs' - // ); - // var molPath = path.resolve( - // testPatternsPath, - // '00-molecules', - // '00-global', - // '00-call-atom-with-molecule-data.hbs' - // ); - - // // set up environment - // var patternlab = new fakePatternLab(); // environment - // var assembler = new pa(); - - // // do all the normal processing of the pattern - // var atom = assembler.process_pattern_iterative(atomPath, patternlab); - // var mol = assembler.process_pattern_iterative(molPath, patternlab); - // assembler.process_pattern_recursive(atomPath, patternlab); - // assembler.process_pattern_recursive(molPath, patternlab); - - // // test - // test.equals(mol.render(), '

Call with default JSON environment:

\nThis is Hello world!\nfrom the default JSON.\n\n\n

Call with passed parameter:

\nHowever, this is Hello world!\nfrom a totally different blob.\n\n'); - // test.done(); - // }, - 'find_pattern_partials finds partials': function (test) { - testFindPartials(test, [ - '{% include "atoms-image" %}', - "{% include 'atoms-image' %}", - "{%include 'atoms-image'%}", - "{% include 'molecules-template' only %}", - "{% include 'organisms-sidebar' ignore missing %}", - "{% include 'organisms-sidebar' ignore missing only %}" - ]); - }, - 'find_pattern_partials finds verbose partials': function (test) { - testFindPartials(test, [ - "{% include '01-molecules/06-components/03-comment-header.twig' %}", - "{% include '00-atoms/00-global/06-test' %}" - ]); - }, - 'find_pattern_partials finds partials with twig parameters': function (test) { - testFindPartials(test, [ - "{% include 'molecules-template' with {'foo': 'bar'} %}", - "{% include 'molecules-template' with vars %}", - "{% include 'molecules-template.twig' with {'foo': 'bar'} only %}", - "{% include 'organisms-sidebar' ignore missing with {'foo': 'bar'} %}" - ]); - } -}; +tap.test('button twig pattern renders', function (test) { + test.plan(1); + + var patternPath = path.join('00-atoms', '00-general', '08-button.twig'); + var expectedValue = '' + eol + eol + 'Button' + eol; + + // do all the normal processing of the pattern + var patternlab = new fakePatternLab(); + var assembler = new pa(); + var helloWorldPattern = assembler.process_pattern_iterative(patternPath, patternlab); + assembler.process_pattern_recursive(patternPath, patternlab); + + test.equals(helloWorldPattern.render(), expectedValue); + test.end(); +}); + +tap.test('media object twig pattern can see the atoms-button and atoms-image partials and renders them', function (test) { + test.plan(1); + + // pattern paths + var buttonPatternPath = path.join('00-atoms', '00-general', '08-button.twig'); + var imagePatternPath = path.join('00-atoms', '00-general', '09-image.twig'); + var mediaObjectPatternPath = path.join('00-molecules', '00-general', '00-media-object.twig'); + + var expectedValue = '\n\n\n\n\n
\n \n\n \n\nButton\n\n\n
\n\n \n \n\n

Oh, hello world!

\n
\n
\n'; + + // set up environment + var patternlab = new fakePatternLab(); // environment + var assembler = new pa(); + + // do all the normal processing of the pattern + assembler.process_pattern_iterative(buttonPatternPath, patternlab); + assembler.process_pattern_iterative(imagePatternPath, patternlab); + var mediaObjectPattern = assembler.process_pattern_iterative(mediaObjectPatternPath, patternlab); + assembler.process_pattern_recursive(buttonPatternPath, patternlab); + assembler.process_pattern_recursive(imagePatternPath, patternlab); + assembler.process_pattern_recursive(mediaObjectPatternPath, patternlab); + + // test + // this pattern is too long - so just remove line endings on both sides and compare output + test.equals(mediaObjectPattern.render().replace(/\r?\n|\r/gm, ""), expectedValue.replace(/\r?\n|\r/gm, "")); + test.end(); +}); + +tap.test('twig partials can render JSON values', {skip: true}, function (test) { + test.plan(1); + + // pattern paths + var pattern1Path = path.resolve( + testPatternsPath, + '00-atoms', + '00-global', + '00-helloworld-withdata.hbs' + ); + // set up environment + var patternlab = new fakePatternLab(); // environment + var assembler = new pa(); + + // do all the normal processing of the pattern + var helloWorldWithData = assembler.process_pattern_iterative(pattern1Path, patternlab); + assembler.process_pattern_recursive(pattern1Path, patternlab); + + // test + test.equals(helloWorldWithData.render(), 'Hello world!\nYeah, we got the subtitle from the JSON.\n'); + test.end(); +}); + +tap.test('twig partials use the JSON environment from the calling pattern and can accept passed parameters', {skip: true}, function (test) { + test.plan(1); + + // pattern paths + var atomPath = path.resolve( + testPatternsPath, + '00-atoms', + '00-global', + '00-helloworld-withdata.hbs' + ); + var molPath = path.resolve( + testPatternsPath, + '00-molecules', + '00-global', + '00-call-atom-with-molecule-data.hbs' + ); + + // set up environment + var patternlab = new fakePatternLab(); // environment + var assembler = new pa(); + + // do all the normal processing of the pattern + var atom = assembler.process_pattern_iterative(atomPath, patternlab); + var mol = assembler.process_pattern_iterative(molPath, patternlab); + assembler.process_pattern_recursive(atomPath, patternlab); + assembler.process_pattern_recursive(molPath, patternlab); + + // test + test.equals(mol.render(), '

Call with default JSON environment:

\nThis is Hello world!\nfrom the default JSON.\n\n\n

Call with passed parameter:

\nHowever, this is Hello world!\nfrom a totally different blob.\n\n'); + test.end(); +}); + +tap.test('find_pattern_partials finds partials', function (test) { + testFindPartials(test, [ + '{% include "atoms-image" %}', + "{% include 'atoms-image' %}", + "{%include 'atoms-image'%}", + "{% include 'molecules-template' only %}", + "{% include 'organisms-sidebar' ignore missing %}", + "{% include 'organisms-sidebar' ignore missing only %}" + ]); +}); + +tap.test('find_pattern_partials finds verbose partials', function (test) { + testFindPartials(test, [ + "{% include '01-molecules/06-components/03-comment-header.twig' %}", + "{% include '00-atoms/00-global/06-test' %}" + ]); +}); + +tap.test('find_pattern_partials finds partials with twig parameters', function (test) { + testFindPartials(test, [ + "{% include 'molecules-template' with {'foo': 'bar'} %}", + "{% include 'molecules-template' with vars %}", + "{% include 'molecules-template.twig' with {'foo': 'bar'} only %}", + "{% include 'organisms-sidebar' ignore missing with {'foo': 'bar'} %}" + ]); +}); -// don't run these tests unless twig is installed -var engineLoader = require('../core/lib/pattern_engines'); -if (!engineLoader.twig) { - console.log("Twig engine not installed, skipping tests."); - delete exports.engine_twig; -} diff --git a/test/engine_underscore_tests.js b/test/engine_underscore_tests.js index 06583de00..8c245bdf4 100644 --- a/test/engine_underscore_tests.js +++ b/test/engine_underscore_tests.js @@ -1,10 +1,20 @@ "use strict"; +var tap = require('tap'); var path = require('path'); var pa = require('../core/lib/pattern_assembler'); var testPatternsPath = path.resolve(__dirname, 'files', '_underscore-test-patterns'); var eol = require('os').EOL; +// don't run tests unless underscore is installed +var engineLoader = require('../core/lib/pattern_engines'); +if (!engineLoader.underscore) { + tap.test('Underscore engine not installed, skipping tests', function (test) { + test.end(); + }); + return; +} + // fake pattern lab constructor: // sets up a fake patternlab object, which is needed by the pattern processing // apparatus. @@ -30,67 +40,83 @@ function fakePatternLab() { return fpl; } -exports['engine_underscore'] = { - 'hello world underscore pattern renders': function (test) { - test.expect(1); - - var patternPath = path.resolve( - testPatternsPath, - '00-atoms', - '00-global', - '00-helloworld.html' - ); - - // do all the normal processing of the pattern - var patternlab = new fakePatternLab(); - var assembler = new pa(); - var helloWorldPattern = assembler.process_pattern_iterative(patternPath, patternlab); - assembler.process_pattern_recursive(patternPath, patternlab); - - test.equals(helloWorldPattern.render(), 'Hello world!' + eol); - test.done(); - }, - 'underscore partials can render JSON values': function (test) { - test.expect(1); - - // pattern paths - var pattern1Path = path.resolve( - testPatternsPath, - '00-atoms', - '00-global', - '00-helloworld-withdata.html' - ); - - // set up environment - var patternlab = new fakePatternLab(); // environment - var assembler = new pa(); - - // do all the normal processing of the pattern - var helloWorldWithData = assembler.process_pattern_iterative(pattern1Path, patternlab); - assembler.process_pattern_recursive(pattern1Path, patternlab); - - // test - test.equals(helloWorldWithData.render(), 'Hello world!' + eol + 'Yeah, we got the subtitle from the JSON.' + eol); - test.done(); - }, - 'findPartial return the ID of the partial, given a whole partial call': function (test) { - var engineLoader = require('../core/lib/pattern_engines'); - var underscoreEngine = engineLoader.underscore; - - test.expect(1); - - // do all the normal processing of the pattern - // test - test.equals(underscoreEngine.findPartial("<%= _.renderNamedPartial('molecules-details', obj) %>"), 'molecules-details'); - test.done(); - } -}; - - - -// don't run these tests unless underscore is installed -var engineLoader = require('../core/lib/pattern_engines'); -if (!engineLoader.underscore) { - console.log("Underscore engine not installed, skipping tests."); - delete exports.engine_underscore; -} +tap.test('hello world underscore pattern renders', function (test) { + test.plan(1); + + var patternPath = path.resolve( + testPatternsPath, + '00-atoms', + '00-global', + '00-helloworld.html' + ); + + // do all the normal processing of the pattern + var patternlab = new fakePatternLab(); + var assembler = new pa(); + var helloWorldPattern = assembler.process_pattern_iterative(patternPath, patternlab); + assembler.process_pattern_recursive(patternPath, patternlab); + + test.equals(helloWorldPattern.render(), 'Hello world!' + eol); + test.end(); + +}); + +tap.test('underscore partials can render JSON values', function (test) { + + test.plan(1); + + // pattern paths + var pattern1Path = path.resolve( + testPatternsPath, + '00-atoms', + '00-global', + '00-helloworld-withdata.html' + ); + + // set up environment + var patternlab = new fakePatternLab(); // environment + var assembler = new pa(); + + // do all the normal processing of the pattern + var helloWorldWithData = assembler.process_pattern_iterative(pattern1Path, patternlab); + assembler.process_pattern_recursive(pattern1Path, patternlab); + + // test + test.equals(helloWorldWithData.render(), 'Hello world!' + eol + 'Yeah, we got the subtitle from the JSON.' + eol); + test.end(); + +}); + +tap.test('findPartial return the ID of the partial, given a whole partial call', function (test) { + var engineLoader = require('../core/lib/pattern_engines'); + var underscoreEngine = engineLoader.underscore; + + test.plan(1); + + // do all the normal processing of the pattern + // test + test.equals(underscoreEngine.findPartial("<%= _.renderNamedPartial('molecules-details', obj) %>"), 'molecules-details'); + test.end(); + +}); + +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/lineage_hunter_tests.js b/test/lineage_hunter_tests.js index aa77de5f1..aea24758d 100644 --- a/test/lineage_hunter_tests.js +++ b/test/lineage_hunter_tests.js @@ -1,5 +1,7 @@ "use strict"; +var tap = require('tap'); + var lh = require('../core/lib/lineage_hunter'); var pa = require('../core/lib/pattern_assembler'); var of = require('../core/lib/object_factory'); @@ -45,530 +47,527 @@ function createBasePatternLabObject() { return pl; } -exports['lineage hunter '] = { - 'find_lineage - finds lineage': function (test) { - - //setup current pattern from what we would have during execution - var currentPattern = new Pattern( - '02-organisms/00-global/00-header.mustache', // relative path now - null // data - ); - extend(currentPattern, { - "template": "\r\n
\r\n\t{{> atoms-logo }}\r\n\tSearch\r\n\tMenu\r\n\t{{> molecules-primary-nav }}\r\n\t{{> molecules-search }}\r\n
\r\n\r\n", - "patternPartialCode": "\r\n
\r\n\"Logo\tSearch\r\n\tMenu\r\n\r\n
\r\n
\r\n\t Search\r\n\t \r\n\t \r\n\t \r\n
\r\n
\r\n\r\n" - }); - - var patternlab = { - patterns: [ - Pattern.createEmpty({ - "name": "00-atoms-03-images-00-logo", - "subdir": "00-atoms\\03-images", - "filename": "00-logo.mustache", - "data": null, - "template": "\"Logo", - "patternPartialCode": "\"Logo", - "patternBaseName": "logo", - "patternLink": "00-atoms-03-images-00-logo/00-atoms-03-images-00-logo.html", - "patternGroup": "atoms", - "patternSubGroup": "atoms\\03-images", - "flatPatternPath": "00-atoms\\03-images", - "patternPartial": "atoms-logo", - "patternState": "", - "lineage": [], - "lineageIndex": [], - "lineageR": [], - "lineageRIndex": [] - }), - Pattern.createEmpty({ - "name": "01-molecules-05-navigation-00-primary-nav", - "subdir": "01-molecules\\05-navigation", - "filename": "00-primary-nav.mustache", - "data": null, - "template": "\r\n", - "patternPartialCode": "\r\n", - "patternBaseName": "primary-nav", - "patternLink": "01-molecules-05-navigation-00-primary-nav/01-molecules-05-navigation-00-primary-nav.html", - "patternGroup": "molecules", - "patternSubGroup": "molecules\\05-navigation", - "flatPatternPath": "01-molecules\\05-navigation", - "patternPartial": "molecules-primary-nav", - "patternState": "", - "lineage": [], - "lineageIndex": [], - "lineageR": [], - "lineageRIndex": [] - }), - Pattern.createEmpty({ - "name": "01-molecules-04-forms-00-search", - "subdir": "01-molecules\\04-forms", - "filename": "00-search.mustache", - "data": null, - "template": "
\r\n
\r\n\t Search\r\n\t \r\n\t \r\n\t \r\n
\r\n
", - "patternPartialCode": "
\r\n
\r\n\t Search\r\n\t \r\n\t \r\n\t \r\n
\r\n
", - "patternBaseName": "search", - "patternLink": "01-molecules-04-forms-00-search/01-molecules-04-forms-00-search.html", - "patternGroup": "molecules", - "patternSubGroup": "molecules\\04-forms", - "flatPatternPath": "01-molecules\\04-forms", - "patternPartial": "molecules-search", - "patternState": "", - "lineage": [], - "lineageIndex": [], - "lineageR": [], - "lineageRIndex": [] - }) - ], - config: { - outputFileSuffixes: { - rendered: '.rendered', - rawTemplate: '', - markupOnly: '.markup-only' - } +tap.test('find_lineage - finds lineage', function (test) { + + //setup current pattern from what we would have during execution + var currentPattern = new Pattern( + '02-organisms/00-global/00-header.mustache', // relative path now + null // data + ); + extend(currentPattern, { + "template": "\r\n
\r\n\t{{> atoms-logo }}\r\n\tSearch\r\n\tMenu\r\n\t{{> molecules-primary-nav }}\r\n\t{{> molecules-search }}\r\n
\r\n\r\n", + "patternPartialCode": "\r\n
\r\n\"Logo\tSearch\r\n\tMenu\r\n\r\n
\r\n
\r\n\t Search\r\n\t \r\n\t \r\n\t \r\n
\r\n
\r\n\r\n" + }); + + var patternlab = { + patterns: [ + Pattern.createEmpty({ + "name": "00-atoms-03-images-00-logo", + "subdir": "00-atoms\\03-images", + "filename": "00-logo.mustache", + "data": null, + "template": "\"Logo", + "patternPartialCode": "\"Logo", + "patternBaseName": "logo", + "patternLink": "00-atoms-03-images-00-logo/00-atoms-03-images-00-logo.html", + "patternGroup": "atoms", + "patternSubGroup": "atoms\\03-images", + "flatPatternPath": "00-atoms\\03-images", + "patternPartial": "atoms-logo", + "patternState": "", + "lineage": [], + "lineageIndex": [], + "lineageR": [], + "lineageRIndex": [] + }), + Pattern.createEmpty({ + "name": "01-molecules-05-navigation-00-primary-nav", + "subdir": "01-molecules\\05-navigation", + "filename": "00-primary-nav.mustache", + "data": null, + "template": "\r\n", + "patternPartialCode": "\r\n", + "patternBaseName": "primary-nav", + "patternLink": "01-molecules-05-navigation-00-primary-nav/01-molecules-05-navigation-00-primary-nav.html", + "patternGroup": "molecules", + "patternSubGroup": "molecules\\05-navigation", + "flatPatternPath": "01-molecules\\05-navigation", + "patternPartial": "molecules-primary-nav", + "patternState": "", + "lineage": [], + "lineageIndex": [], + "lineageR": [], + "lineageRIndex": [] + }), + Pattern.createEmpty({ + "name": "01-molecules-04-forms-00-search", + "subdir": "01-molecules\\04-forms", + "filename": "00-search.mustache", + "data": null, + "template": "
\r\n
\r\n\t Search\r\n\t \r\n\t \r\n\t \r\n
\r\n
", + "patternPartialCode": "
\r\n
\r\n\t Search\r\n\t \r\n\t \r\n\t \r\n
\r\n
", + "patternBaseName": "search", + "patternLink": "01-molecules-04-forms-00-search/01-molecules-04-forms-00-search.html", + "patternGroup": "molecules", + "patternSubGroup": "molecules\\04-forms", + "flatPatternPath": "01-molecules\\04-forms", + "patternPartial": "molecules-search", + "patternState": "", + "lineage": [], + "lineageIndex": [], + "lineageR": [], + "lineageRIndex": [] + }) + ], + config: { + outputFileSuffixes: { + rendered: '.rendered', + rawTemplate: '', + markupOnly: '.markup-only' } - }; - - var lineage_hunter = new lh(); - lineage_hunter.find_lineage(currentPattern, patternlab); - - test.equals(currentPattern.lineageIndex.length, 3); - test.equals(currentPattern.lineageIndex[0], "atoms-logo"); - test.equals(currentPattern.lineageIndex[1], "molecules-primary-nav"); - test.equals(currentPattern.lineageIndex[2], "molecules-search"); - - test.done(); - }, - - 'find_lineage - finds lineage with spaced pattern parameters': function (test) { - //setup current pattern from what we would have during execution - var currentPattern = createFakeEmptyErrorPattern(); - extend(currentPattern, { - "template": "{{> atoms-error(message: 'That\\'s no moon...') }}", - "extendedTemplate": "{{> atoms-error(message: 'That\\'s no moon...') }}" - }); - - var patternlab = { - patterns: [ - Pattern.create( - '00-atoms/05-alerts/00-error.mustache', - null, - { - "template": "

{{message}}

", - "extendedTemplate": "

{{message}}

" - }) - ], - config: { - outputFileSuffixes: { - rendered: '.rendered', - rawTemplate: '', - markupOnly: '.markup-only' - } + } + }; + + var lineage_hunter = new lh(); + lineage_hunter.find_lineage(currentPattern, patternlab); + + test.equals(currentPattern.lineageIndex.length, 3); + test.equals(currentPattern.lineageIndex[0], "atoms-logo"); + test.equals(currentPattern.lineageIndex[1], "molecules-primary-nav"); + test.equals(currentPattern.lineageIndex[2], "molecules-search"); + + test.end(); +}); + +tap.test('find_lineage - finds lineage with spaced pattern parameters', function (test) { + //setup current pattern from what we would have during execution + var currentPattern = createFakeEmptyErrorPattern(); + extend(currentPattern, { + "template": "{{> atoms-error(message: 'That\\'s no moon...') }}", + "extendedTemplate": "{{> atoms-error(message: 'That\\'s no moon...') }}" + }); + + var patternlab = { + patterns: [ + Pattern.create( + '00-atoms/05-alerts/00-error.mustache', + null, + { + "template": "

{{message}}

", + "extendedTemplate": "

{{message}}

" + }) + ], + config: { + outputFileSuffixes: { + rendered: '.rendered', + rawTemplate: '', + markupOnly: '.markup-only' } - }; + } + }; - lineage_hunter.find_lineage(currentPattern, patternlab); + lineage_hunter.find_lineage(currentPattern, patternlab); - test.equals(currentPattern.lineageIndex.length, 1); - test.equals(currentPattern.lineageIndex[0], "atoms-error"); - test.equals(patternlab.patterns[0].lineageRIndex.length, 1); - test.equals(patternlab.patterns[0].lineageR[0].lineagePattern, 'molecules-error'); + test.equals(currentPattern.lineageIndex.length, 1); + test.equals(currentPattern.lineageIndex[0], "atoms-error"); + test.equals(patternlab.patterns[0].lineageRIndex.length, 1); + test.equals(patternlab.patterns[0].lineageR[0].lineagePattern, 'molecules-error'); - test.done(); - }, + test.end(); +}); - 'cascade_pattern_states promotes a lower pattern state up to the consumer': function (test) { - //arrange - var pl = createBasePatternLabObject(); +tap.test('cascade_pattern_states promotes a lower pattern state up to the consumer', function (test) { + //arrange + var pl = createBasePatternLabObject(); - var atomPattern = new of.Pattern('00-test/01-bar.mustache'); - atomPattern.template = fs.readFileSync(pl.config.paths.source.patterns + '00-test/01-bar.mustache', 'utf8'); - atomPattern.extendedTemplate = atomPattern.template; - atomPattern.patternState = "inreview"; + var atomPattern = new of.Pattern('00-test/01-bar.mustache'); + atomPattern.template = fs.readFileSync(pl.config.paths.source.patterns + '00-test/01-bar.mustache', 'utf8'); + atomPattern.extendedTemplate = atomPattern.template; + atomPattern.patternState = "inreview"; - pattern_assembler.addPattern(atomPattern, pl); + pattern_assembler.addPattern(atomPattern, pl); - var consumerPattern = new of.Pattern('00-test/00-foo.mustache'); - consumerPattern.template = fs.readFileSync(pl.config.paths.source.patterns + '00-test/00-foo.mustache', 'utf8'); - consumerPattern.extendedTemplate = consumerPattern.template; - consumerPattern.patternState = "complete"; - pattern_assembler.addPattern(consumerPattern, pl); + var consumerPattern = new of.Pattern('00-test/00-foo.mustache'); + consumerPattern.template = fs.readFileSync(pl.config.paths.source.patterns + '00-test/00-foo.mustache', 'utf8'); + consumerPattern.extendedTemplate = consumerPattern.template; + consumerPattern.patternState = "complete"; + pattern_assembler.addPattern(consumerPattern, pl); - lineage_hunter.find_lineage(consumerPattern, pl); + lineage_hunter.find_lineage(consumerPattern, pl); - //act - lineage_hunter.cascade_pattern_states(pl); + //act + lineage_hunter.cascade_pattern_states(pl); - //assert - var consumerPatternReturned = pattern_assembler.getPartial('test-foo', pl); - test.equals(consumerPatternReturned.patternState, 'inreview'); - test.done(); - }, - - 'cascade_pattern_states promotes a lower pattern state up to the consumers lineage': function (test) { - //arrange - var pl = createBasePatternLabObject(); - - var atomPattern = new of.Pattern('00-test/01-bar.mustache'); - atomPattern.template = fs.readFileSync(pl.config.paths.source.patterns + '00-test/01-bar.mustache', 'utf8'); - atomPattern.extendedTemplate = atomPattern.template; - atomPattern.patternState = "inreview"; - - pattern_assembler.addPattern(atomPattern, pl); - - var consumerPattern = new of.Pattern('00-test/00-foo.mustache'); - consumerPattern.template = fs.readFileSync(pl.config.paths.source.patterns + '00-test/00-foo.mustache', 'utf8'); - consumerPattern.extendedTemplate = consumerPattern.template; - consumerPattern.patternState = "complete"; - pattern_assembler.addPattern(consumerPattern, pl); - - lineage_hunter.find_lineage(consumerPattern, pl); - - //act - lineage_hunter.cascade_pattern_states(pl); - - //assert - var consumerPatternReturned = pattern_assembler.getPartial('test-foo', pl); - test.equals(consumerPatternReturned.lineage[0].lineageState, 'inreview'); - test.done(); - }, - - 'cascade_pattern_states sets the pattern state on any lineage patterns reverse lineage': function (test) { - //arrange - var pl = createBasePatternLabObject(); - - var atomPattern = new of.Pattern('00-test/01-bar.mustache'); - atomPattern.template = fs.readFileSync(pl.config.paths.source.patterns + '00-test/01-bar.mustache', 'utf8'); - atomPattern.extendedTemplate = atomPattern.template; - atomPattern.patternState = "inreview"; - pattern_assembler.addPattern(atomPattern, pl); - - var consumerPattern = new of.Pattern('00-test/00-foo.mustache'); - consumerPattern.template = fs.readFileSync(pl.config.paths.source.patterns + '00-test/00-foo.mustache', 'utf8'); - consumerPattern.extendedTemplate = consumerPattern.template; - consumerPattern.patternState = "complete"; - pattern_assembler.addPattern(consumerPattern, pl); - - lineage_hunter.find_lineage(consumerPattern, pl); - - //act - lineage_hunter.cascade_pattern_states(pl); - - //assert - var consumedPatternReturned = pattern_assembler.getPartial('test-bar', pl); - test.equals(consumedPatternReturned.lineageR[0].lineageState, 'inreview'); - - test.done(); - }, - - 'cascade_pattern_states promotes lower pattern state when consumer does not have its own state': function (test) { - //arrange - var pl = createBasePatternLabObject(); - - var atomPattern = new of.Pattern('00-test/01-bar.mustache'); - atomPattern.template = fs.readFileSync(path.resolve(pl.config.paths.source.patterns, '00-test/01-bar.mustache'), 'utf8'); - atomPattern.extendedTemplate = atomPattern.template; - atomPattern.patternState = "inreview"; - - pattern_assembler.addPattern(atomPattern, pl); - - var consumerPattern = new of.Pattern('00-test/00-foo.mustache'); - consumerPattern.template = fs.readFileSync(path.resolve(pl.config.paths.source.patterns, '00-test/00-foo.mustache'), 'utf8'); - consumerPattern.extendedTemplate = consumerPattern.template; - pattern_assembler.addPattern(consumerPattern, pl); - - lineage_hunter.find_lineage(consumerPattern, pl); - - //act - lineage_hunter.cascade_pattern_states(pl); - - //assert - var consumerPatternReturned = pattern_assembler.getPartial('test-foo', pl); - test.equals(consumerPatternReturned.lineage.length, 1); - test.equals(consumerPatternReturned.lineage[0].lineageState, 'inreview'); - test.equals(consumerPatternReturned.patternState, 'inreview'); - test.done(); - }, - - 'find_lineage - finds lineage with unspaced pattern parameters': function (test) { - //setup current pattern from what we would have during execution - var currentPattern = createFakeEmptyErrorPattern(); - extend(currentPattern, { - "template": "{{>atoms-error(message: 'That\\'s no moon...')}}", - "extendedTemplate": "{{>atoms-error(message: 'That\\'s no moon...')}}" - }); - - var patternlab = { - patterns: [ - Pattern.createEmpty({ - "name": "01-atoms-05-alerts-00-error", - "subdir": "01-atoms\\05-alerts", - "filename": "00-error.mustache", - "data": null, - "template": "

{{message}}

", - "extendedTemplate": "

{{message}}

", - "patternBaseName": "error", - "patternLink": "01-atoms-05-alerts-00-error/01-atoms-05-alerts-00-error.html", - "patternGroup": "atoms", - "patternSubGroup": "atoms\\05-alerts", - "flatPatternPath": "01-atoms\\05-alerts", - "patternPartial": "atoms-error", - "patternState": "", - "lineage": [], - "lineageIndex": [], - "lineageR": [], - "lineageRIndex": [] - }) - ], - config: { - outputFileSuffixes: { - rendered: '.rendered', - rawTemplate: '', - markupOnly: '.markup-only' - } - } - }; - - var lineage_hunter = new lh(); - lineage_hunter.find_lineage(currentPattern, patternlab); - - test.equals(currentPattern.lineageIndex.length, 1); - test.equals(currentPattern.lineageIndex[0], "atoms-error"); - test.equals(patternlab.patterns[0].lineageRIndex.length, 1); - test.equals(patternlab.patterns[0].lineageR[0].lineagePattern, 'molecules-error'); - - test.done(); - }, - - 'find_lineage - finds lineage with spaced styleModifier': function (test) { - //setup current pattern from what we would have during execution - var currentPattern = Pattern.createEmpty({ - "name": "01-molecules-01-toast-00-error", - "subdir": "01-molecules\\01-toast", - "filename": "00-error.mustache", - "data": null, - "template": "{{> atoms-error:foo }}", - "extendedTemplate": "{{> atoms-error:foo }}", - "patternBaseName": "error", - "patternLink": "01-molecules-01-toast-00-error/01-molecules-01-toast-00-error.html", - "patternGroup": "molecules", - "patternSubGroup": "molecules\\01-toast", - "flatPatternPath": "01-molecules\\01-toast", - "patternPartial": "molecules-error", - "patternState": "", - "lineage": [], - "lineageIndex": [], - "lineageR": [], - "lineageRIndex": [] - }); - var patternlab = { - patterns: [ - Pattern.createEmpty({ - "name": "01-atoms-05-alerts-00-error", - "subdir": "01-atoms\\05-alerts", - "filename": "00-error.mustache", - "data": null, - "template": "

{{message}}

", - "extendedTemplate": "

{{message}}

", - "patternBaseName": "error", - "patternLink": "01-atoms-05-alerts-00-error/01-atoms-05-alerts-00-error.html", - "patternGroup": "atoms", - "patternSubGroup": "atoms\\05-alerts", - "flatPatternPath": "01-atoms\\05-alerts", - "patternPartial": "atoms-error", - "patternState": "", - "lineage": [], - "lineageIndex": [], - "lineageR": [], - "lineageRIndex": [] - }) - ], - config: { - outputFileSuffixes: { - rendered: '.rendered', - rawTemplate: '', - markupOnly: '.markup-only' - } + //assert + var consumerPatternReturned = pattern_assembler.getPartial('test-foo', pl); + test.equals(consumerPatternReturned.patternState, 'inreview'); + test.end(); +}); + +tap.test('cascade_pattern_states promotes a lower pattern state up to the consumers lineage', function (test) { + //arrange + var pl = createBasePatternLabObject(); + + var atomPattern = new of.Pattern('00-test/01-bar.mustache'); + atomPattern.template = fs.readFileSync(pl.config.paths.source.patterns + '00-test/01-bar.mustache', 'utf8'); + atomPattern.extendedTemplate = atomPattern.template; + atomPattern.patternState = "inreview"; + + pattern_assembler.addPattern(atomPattern, pl); + + var consumerPattern = new of.Pattern('00-test/00-foo.mustache'); + consumerPattern.template = fs.readFileSync(pl.config.paths.source.patterns + '00-test/00-foo.mustache', 'utf8'); + consumerPattern.extendedTemplate = consumerPattern.template; + consumerPattern.patternState = "complete"; + pattern_assembler.addPattern(consumerPattern, pl); + + lineage_hunter.find_lineage(consumerPattern, pl); + + //act + lineage_hunter.cascade_pattern_states(pl); + + //assert + var consumerPatternReturned = pattern_assembler.getPartial('test-foo', pl); + test.equals(consumerPatternReturned.lineage[0].lineageState, 'inreview'); + test.end(); +}); + +tap.test('cascade_pattern_states sets the pattern state on any lineage patterns reverse lineage', function (test) { + //arrange + var pl = createBasePatternLabObject(); + + var atomPattern = new of.Pattern('00-test/01-bar.mustache'); + atomPattern.template = fs.readFileSync(pl.config.paths.source.patterns + '00-test/01-bar.mustache', 'utf8'); + atomPattern.extendedTemplate = atomPattern.template; + atomPattern.patternState = "inreview"; + pattern_assembler.addPattern(atomPattern, pl); + + var consumerPattern = new of.Pattern('00-test/00-foo.mustache'); + consumerPattern.template = fs.readFileSync(pl.config.paths.source.patterns + '00-test/00-foo.mustache', 'utf8'); + consumerPattern.extendedTemplate = consumerPattern.template; + consumerPattern.patternState = "complete"; + pattern_assembler.addPattern(consumerPattern, pl); + + lineage_hunter.find_lineage(consumerPattern, pl); + + //act + lineage_hunter.cascade_pattern_states(pl); + + //assert + var consumedPatternReturned = pattern_assembler.getPartial('test-bar', pl); + test.equals(consumedPatternReturned.lineageR[0].lineageState, 'inreview'); + + test.end(); +}); + +tap.test('cascade_pattern_states promotes lower pattern state when consumer does not have its own state', function (test) { + //arrange + var pl = createBasePatternLabObject(); + + var atomPattern = new of.Pattern('00-test/01-bar.mustache'); + atomPattern.template = fs.readFileSync(path.resolve(pl.config.paths.source.patterns, '00-test/01-bar.mustache'), 'utf8'); + atomPattern.extendedTemplate = atomPattern.template; + atomPattern.patternState = "inreview"; + + pattern_assembler.addPattern(atomPattern, pl); + + var consumerPattern = new of.Pattern('00-test/00-foo.mustache'); + consumerPattern.template = fs.readFileSync(path.resolve(pl.config.paths.source.patterns, '00-test/00-foo.mustache'), 'utf8'); + consumerPattern.extendedTemplate = consumerPattern.template; + pattern_assembler.addPattern(consumerPattern, pl); + + lineage_hunter.find_lineage(consumerPattern, pl); + + //act + lineage_hunter.cascade_pattern_states(pl); + + //assert + var consumerPatternReturned = pattern_assembler.getPartial('test-foo', pl); + test.equals(consumerPatternReturned.lineage.length, 1); + test.equals(consumerPatternReturned.lineage[0].lineageState, 'inreview'); + test.equals(consumerPatternReturned.patternState, 'inreview'); + test.end(); +}); + +tap.test('find_lineage - finds lineage with unspaced pattern parameters', function (test) { + //setup current pattern from what we would have during execution + var currentPattern = createFakeEmptyErrorPattern(); + extend(currentPattern, { + "template": "{{>atoms-error(message: 'That\\'s no moon...')}}", + "extendedTemplate": "{{>atoms-error(message: 'That\\'s no moon...')}}" + }); + + var patternlab = { + patterns: [ + Pattern.createEmpty({ + "name": "01-atoms-05-alerts-00-error", + "subdir": "01-atoms\\05-alerts", + "filename": "00-error.mustache", + "data": null, + "template": "

{{message}}

", + "extendedTemplate": "

{{message}}

", + "patternBaseName": "error", + "patternLink": "01-atoms-05-alerts-00-error/01-atoms-05-alerts-00-error.html", + "patternGroup": "atoms", + "patternSubGroup": "atoms\\05-alerts", + "flatPatternPath": "01-atoms\\05-alerts", + "patternPartial": "atoms-error", + "patternState": "", + "lineage": [], + "lineageIndex": [], + "lineageR": [], + "lineageRIndex": [] + }) + ], + config: { + outputFileSuffixes: { + rendered: '.rendered', + rawTemplate: '', + markupOnly: '.markup-only' } - }; - - var lineage_hunter = new lh(); - lineage_hunter.find_lineage(currentPattern, patternlab); - - test.equals(currentPattern.lineageIndex.length, 1); - test.equals(currentPattern.lineageIndex[0], "atoms-error"); - - test.done(); - }, - - 'find_lineage - finds lineage with unspaced styleModifier': function (test) { - //setup current pattern from what we would have during execution - var currentPattern = Pattern.createEmpty({ - "name": "01-molecules-01-toast-00-error", - "subdir": "01-molecules\\01-toast", - "filename": "00-error.mustache", - "data": null, - "template": "{{> atoms-error:foo }}", - "extendedTemplate": "{{>atoms-error:foo}}", - "patternBaseName": "error", - "patternLink": "01-molecules-01-toast-00-error/01-molecules-01-toast-00-error.html", - "patternGroup": "molecules", - "patternSubGroup": "molecules\\01-toast", - "flatPatternPath": "01-molecules\\01-toast", - "patternPartial": "molecules-error", - "patternState": "", - "lineage": [], - "lineageIndex": [], - "lineageR": [], - "lineageRIndex": [] - }); - var patternlab = { - patterns: [ - Pattern.createEmpty({ - "name": "01-atoms-05-alerts-00-error", - "subdir": "01-atoms\\05-alerts", - "filename": "00-error.mustache", - "data": null, - "template": "

{{message}}

", - "extendedTemlpate": "

{{message}}

", - "patternBaseName": "error", - "patternLink": "01-atoms-05-alerts-00-error/01-atoms-05-alerts-00-error.html", - "patternGroup": "atoms", - "patternSubGroup": "atoms\\05-alerts", - "flatPatternPath": "01-atoms\\05-alerts", - "patternPartial": "atoms-error", - "patternState": "", - "lineage": [], - "lineageIndex": [], - "lineageR": [], - "lineageRIndex": [] - }) - ], - config: { - outputFileSuffixes: { - rendered: '.rendered', - rawTemplate: '', - markupOnly: '.markup-only' - } + } + }; + + var lineage_hunter = new lh(); + lineage_hunter.find_lineage(currentPattern, patternlab); + + test.equals(currentPattern.lineageIndex.length, 1); + test.equals(currentPattern.lineageIndex[0], "atoms-error"); + test.equals(patternlab.patterns[0].lineageRIndex.length, 1); + test.equals(patternlab.patterns[0].lineageR[0].lineagePattern, 'molecules-error'); + + test.end(); +}); + +tap.test('find_lineage - finds lineage with spaced styleModifier', function (test) { + //setup current pattern from what we would have during execution + var currentPattern = Pattern.createEmpty({ + "name": "01-molecules-01-toast-00-error", + "subdir": "01-molecules\\01-toast", + "filename": "00-error.mustache", + "data": null, + "template": "{{> atoms-error:foo }}", + "extendedTemplate": "{{> atoms-error:foo }}", + "patternBaseName": "error", + "patternLink": "01-molecules-01-toast-00-error/01-molecules-01-toast-00-error.html", + "patternGroup": "molecules", + "patternSubGroup": "molecules\\01-toast", + "flatPatternPath": "01-molecules\\01-toast", + "patternPartial": "molecules-error", + "patternState": "", + "lineage": [], + "lineageIndex": [], + "lineageR": [], + "lineageRIndex": [] + }); + var patternlab = { + patterns: [ + Pattern.createEmpty({ + "name": "01-atoms-05-alerts-00-error", + "subdir": "01-atoms\\05-alerts", + "filename": "00-error.mustache", + "data": null, + "template": "

{{message}}

", + "extendedTemplate": "

{{message}}

", + "patternBaseName": "error", + "patternLink": "01-atoms-05-alerts-00-error/01-atoms-05-alerts-00-error.html", + "patternGroup": "atoms", + "patternSubGroup": "atoms\\05-alerts", + "flatPatternPath": "01-atoms\\05-alerts", + "patternPartial": "atoms-error", + "patternState": "", + "lineage": [], + "lineageIndex": [], + "lineageR": [], + "lineageRIndex": [] + }) + ], + config: { + outputFileSuffixes: { + rendered: '.rendered', + rawTemplate: '', + markupOnly: '.markup-only' } - }; - - var lineage_hunter = new lh(); - lineage_hunter.find_lineage(currentPattern, patternlab); - - test.equals(currentPattern.lineageIndex.length, 1); - test.equals(currentPattern.lineageIndex[0], "atoms-error"); - - test.done(); - }, - - 'find_lineage - finds lineage with fuzzy partial with styleModifier': function (test) { - //setup current pattern from what we would have during execution - var currentPattern = Pattern.createEmpty({ - "name": "01-molecules-01-toast-00-error", - "subdir": "01-molecules\\01-toast", - "filename": "00-error.mustache", - "data": null, - "template": "{{> atoms-e:foo }}", - "extendedTemplate": "{{>atoms-e:foo}}", - "patternBaseName": "error", - "patternLink": "01-molecules-01-toast-00-error/01-molecules-01-toast-00-error.html", - "patternGroup": "molecules", - "patternSubGroup": "molecules\\01-toast", - "flatPatternPath": "01-molecules\\01-toast", - "patternPartial": "molecules-error", - "patternState": "", - "lineage": [], - "lineageIndex": [], - "lineageR": [], - "lineageRIndex": [] - }); - var patternlab = { - patterns: [ - Pattern.createEmpty({ - "name": "01-atoms-05-alerts-00-error", - "subdir": "01-atoms\\05-alerts", - "filename": "00-error.mustache", - "data": null, - "template": "

{{message}}

", - "extendedTemplate": "

{{message}}

", - "patternBaseName": "error", - "patternLink": "01-atoms-05-alerts-00-error/01-atoms-05-alerts-00-error.html", - "patternGroup": "atoms", - "patternSubGroup": "atoms\\05-alerts", - "flatPatternPath": "01-atoms\\05-alerts", - "patternPartial": "atoms-error", - "patternState": "", - "lineage": [], - "lineageIndex": [], - "lineageR": [], - "lineageRIndex": [] - }) - ], - config: { - outputFileSuffixes: { - rendered: '.rendered', - rawTemplate: '', - markupOnly: '.markup-only' - } + } + }; + + var lineage_hunter = new lh(); + lineage_hunter.find_lineage(currentPattern, patternlab); + + test.equals(currentPattern.lineageIndex.length, 1); + test.equals(currentPattern.lineageIndex[0], "atoms-error"); + + test.end(); +}); + +tap.test('find_lineage - finds lineage with unspaced styleModifier', function (test) { + //setup current pattern from what we would have during execution + var currentPattern = Pattern.createEmpty({ + "name": "01-molecules-01-toast-00-error", + "subdir": "01-molecules\\01-toast", + "filename": "00-error.mustache", + "data": null, + "template": "{{> atoms-error:foo }}", + "extendedTemplate": "{{>atoms-error:foo}}", + "patternBaseName": "error", + "patternLink": "01-molecules-01-toast-00-error/01-molecules-01-toast-00-error.html", + "patternGroup": "molecules", + "patternSubGroup": "molecules\\01-toast", + "flatPatternPath": "01-molecules\\01-toast", + "patternPartial": "molecules-error", + "patternState": "", + "lineage": [], + "lineageIndex": [], + "lineageR": [], + "lineageRIndex": [] + }); + var patternlab = { + patterns: [ + Pattern.createEmpty({ + "name": "01-atoms-05-alerts-00-error", + "subdir": "01-atoms\\05-alerts", + "filename": "00-error.mustache", + "data": null, + "template": "

{{message}}

", + "extendedTemlpate": "

{{message}}

", + "patternBaseName": "error", + "patternLink": "01-atoms-05-alerts-00-error/01-atoms-05-alerts-00-error.html", + "patternGroup": "atoms", + "patternSubGroup": "atoms\\05-alerts", + "flatPatternPath": "01-atoms\\05-alerts", + "patternPartial": "atoms-error", + "patternState": "", + "lineage": [], + "lineageIndex": [], + "lineageR": [], + "lineageRIndex": [] + }) + ], + config: { + outputFileSuffixes: { + rendered: '.rendered', + rawTemplate: '', + markupOnly: '.markup-only' } - }; - - var lineage_hunter = new lh(); - lineage_hunter.find_lineage(currentPattern, patternlab); - - test.equals(currentPattern.lineageIndex.length, 1); - test.equals(currentPattern.lineageIndex[0], "atoms-error"); - - test.done(); - }, - - 'find_lineage - does not apply lineage twice': function (test) { - //setup current pattern from what we would have during execution - var currentPattern = createFakeEmptyErrorPattern(); - extend(currentPattern, { - "template": "{{>atoms-error(message: 'That\\'s no moon...')}}", - "extendedTemplate": "{{>atoms-error(message: 'That\\'s no moon...')}}" - }); - var patternlab = { - patterns: [ - Pattern.createEmpty({ - "name": "01-atoms-05-alerts-00-error", - "subdir": "01-atoms\\05-alerts", - "filename": "00-error.mustache", - "data": null, - "template": "

{{message}}

", - "extendedTemplate": "

{{message}}

", - "patternBaseName": "error", - "patternLink": "01-atoms-05-alerts-00-error/01-atoms-05-alerts-00-error.html", - "patternGroup": "atoms", - "patternSubGroup": "atoms\\05-alerts", - "flatPatternPath": "01-atoms\\05-alerts", - "patternPartial": "atoms-error", - "patternState": "", - "lineage": [], - "lineageIndex": [], - "lineageR": [], - "lineageRIndex": [] - }) - ], - config: { - outputFileSuffixes: { - rendered: '.rendered', - rawTemplate: '', - markupOnly: '.markup-only' - } + } + }; + + var lineage_hunter = new lh(); + lineage_hunter.find_lineage(currentPattern, patternlab); + + test.equals(currentPattern.lineageIndex.length, 1); + test.equals(currentPattern.lineageIndex[0], "atoms-error"); + + test.end(); +}); + +tap.test('find_lineage - finds lineage with fuzzy partial with styleModifier', function (test) { + //setup current pattern from what we would have during execution + var currentPattern = Pattern.createEmpty({ + "name": "01-molecules-01-toast-00-error", + "subdir": "01-molecules\\01-toast", + "filename": "00-error.mustache", + "data": null, + "template": "{{> atoms-e:foo }}", + "extendedTemplate": "{{>atoms-e:foo}}", + "patternBaseName": "error", + "patternLink": "01-molecules-01-toast-00-error/01-molecules-01-toast-00-error.html", + "patternGroup": "molecules", + "patternSubGroup": "molecules\\01-toast", + "flatPatternPath": "01-molecules\\01-toast", + "patternPartial": "molecules-error", + "patternState": "", + "lineage": [], + "lineageIndex": [], + "lineageR": [], + "lineageRIndex": [] + }); + var patternlab = { + patterns: [ + Pattern.createEmpty({ + "name": "01-atoms-05-alerts-00-error", + "subdir": "01-atoms\\05-alerts", + "filename": "00-error.mustache", + "data": null, + "template": "

{{message}}

", + "extendedTemplate": "

{{message}}

", + "patternBaseName": "error", + "patternLink": "01-atoms-05-alerts-00-error/01-atoms-05-alerts-00-error.html", + "patternGroup": "atoms", + "patternSubGroup": "atoms\\05-alerts", + "flatPatternPath": "01-atoms\\05-alerts", + "patternPartial": "atoms-error", + "patternState": "", + "lineage": [], + "lineageIndex": [], + "lineageR": [], + "lineageRIndex": [] + }) + ], + config: { + outputFileSuffixes: { + rendered: '.rendered', + rawTemplate: '', + markupOnly: '.markup-only' } - }; + } + }; - var lineage_hunter = new lh(); - lineage_hunter.find_lineage(currentPattern, patternlab); - lineage_hunter.find_lineage(currentPattern, patternlab); + var lineage_hunter = new lh(); + lineage_hunter.find_lineage(currentPattern, patternlab); + + test.equals(currentPattern.lineageIndex.length, 1); + test.equals(currentPattern.lineageIndex[0], "atoms-error"); + + test.end(); +}); + +tap.test('find_lineage - does not apply lineage twice', function (test) { + //setup current pattern from what we would have during execution + var currentPattern = createFakeEmptyErrorPattern(); + extend(currentPattern, { + "template": "{{>atoms-error(message: 'That\\'s no moon...')}}", + "extendedTemplate": "{{>atoms-error(message: 'That\\'s no moon...')}}" + }); + var patternlab = { + patterns: [ + Pattern.createEmpty({ + "name": "01-atoms-05-alerts-00-error", + "subdir": "01-atoms\\05-alerts", + "filename": "00-error.mustache", + "data": null, + "template": "

{{message}}

", + "extendedTemplate": "

{{message}}

", + "patternBaseName": "error", + "patternLink": "01-atoms-05-alerts-00-error/01-atoms-05-alerts-00-error.html", + "patternGroup": "atoms", + "patternSubGroup": "atoms\\05-alerts", + "flatPatternPath": "01-atoms\\05-alerts", + "patternPartial": "atoms-error", + "patternState": "", + "lineage": [], + "lineageIndex": [], + "lineageR": [], + "lineageRIndex": [] + }) + ], + config: { + outputFileSuffixes: { + rendered: '.rendered', + rawTemplate: '', + markupOnly: '.markup-only' + } + } + }; - test.equals(currentPattern.lineageIndex.length, 1); - test.equals(currentPattern.lineageIndex[0], "atoms-error"); - test.equals(patternlab.patterns[0].lineageRIndex.length, 1); - test.equals(patternlab.patterns[0].lineageR[0].lineagePattern, 'molecules-error'); + var lineage_hunter = new lh(); + lineage_hunter.find_lineage(currentPattern, patternlab); + lineage_hunter.find_lineage(currentPattern, patternlab); - test.done(); - } + test.equals(currentPattern.lineageIndex.length, 1); + test.equals(currentPattern.lineageIndex[0], "atoms-error"); + test.equals(patternlab.patterns[0].lineageRIndex.length, 1); + test.equals(patternlab.patterns[0].lineageR[0].lineagePattern, 'molecules-error'); -}; + test.end(); +}); diff --git a/test/list_item_hunter_tests.js b/test/list_item_hunter_tests.js index 4d0a1574f..1add24827 100644 --- a/test/list_item_hunter_tests.js +++ b/test/list_item_hunter_tests.js @@ -1,424 +1,423 @@ -(function () { - "use strict"; +"use strict"; - var lih = require('../core/lib/list_item_hunter'); - var Pattern = require('../core/lib/object_factory').Pattern; - var extend = require('util')._extend; - var pa = require('../core/lib/pattern_assembler'); - var pattern_assembler = new pa(); - - // fake pattern creators - function createFakeListPattern(customProps) { - var inputs = { - relPath: '01-molecules/01-lists/00-list.mustache', - data: {} - }; - var pattern = new Pattern(inputs.relPath); - - return extend(pattern, customProps); - } - - function createFakePatternLab(customProps) { - - //NOTE: These listitems are faked so that pattern_assembler.combine_listitems has already clobbered them. - - var pl = { - "listitems": { - "1": [ - { - "title": "Foo", - "message": "FooM" - } - ], - "2" : [ - { - "title": "Foo", - "message": "FooM" - }, - { - "title": "Bar", - "message": "BarM" - } - ], - "3": [ - { - "title": "Foo", - "message": "FooM" - }, - { - "title": "Bar", - "message": "BarM" - }, - { - "title": "Baz", - "message": "BazM" - }, - ] - }, - "data": { - "link": {}, - "partials": [] - }, - "config": { - "debug": false, - "paths": { - "source": { - "patterns": "./test/files/_patterns" - } - }, - "outputFileSuffixes": { - "rendered": '' - } - }, - "partials" : {}, - "patterns" : [] - }; - - return extend(pl, customProps); - } - - exports['list_item_hunter'] = { - 'process_list_item_partials finds and outputs basic repeating blocks': function(test){ - //arrange - //setup current pattern from what we would have during execution - var currentPattern = createFakeListPattern({ - "template": "{{#listItems.two}}{{ title }}{{/listItems.two}}", - "extendedTemplate": "{{#listItems.two}}{{ title }}{{/listItems.two}}", - "key": "test-patternName" - }); - var patternlab = createFakePatternLab(); - var list_item_hunter = new lih(); - - //act - list_item_hunter.process_list_item_partials(currentPattern, patternlab); - - //assert - test.equals(currentPattern.extendedTemplate, "FooBar" ); - - test.done(); - }, - - 'process_list_item_partials listitems with lowercase name' : function(test){ - //arrange - //setup current pattern from what we would have during execution - var currentPattern = createFakeListPattern({ - "template": "{{#listitems.two}}{{ title }}{{/listitems.two}}", - "extendedTemplate" : "{{#listitems.two}}{{ title }}{{/listitems.two}}", - "key": "test-patternName" - }); - var patternlab = createFakePatternLab(); - var list_item_hunter = new lih(); - - //act - list_item_hunter.process_list_item_partials(currentPattern, patternlab); +var tap = require('tap'); - //assert - test.equals(currentPattern.extendedTemplate, "FooBar" ); +var lih = require('../core/lib/list_item_hunter'); +var Pattern = require('../core/lib/object_factory').Pattern; +var extend = require('util')._extend; +var pa = require('../core/lib/pattern_assembler'); +var pattern_assembler = new pa(); - test.done(); - }, - - 'process_list_item_partials finds partials and outputs repeated renders': function(test){ - //arrange - //setup current pattern from what we would have during execution - var currentPattern = createFakeListPattern({ - "template": "{{#listItems.two}}{{ title }}{{/listItems.two}}", - "extendedTemplate": "{{#listItems.two}}{{> test-simple }}{{/listItems.two}}", - "key": "test-patternName" - }); - - var patternlab = createFakePatternLab({ - "patterns": [ - { - "template": "{{ title }}", - "extendedTemplate" : "{{ title }}", - "patternPartial": "test-simple", - "jsonFileData" : {} - } - - ] - }); - - var list_item_hunter = new lih(); - - //act - list_item_hunter.process_list_item_partials(currentPattern, patternlab); +// fake pattern creators +function createFakeListPattern(customProps) { + var inputs = { + relPath: '01-molecules/01-lists/00-list.mustache', + data: {} + }; + var pattern = new Pattern(inputs.relPath); - //assert - test.equals(currentPattern.extendedTemplate, "FooBar" ); + return extend(pattern, customProps); +} - test.done(); - }, +function createFakePatternLab(customProps) { - 'process_list_item_partials finds verbose partials and outputs repeated renders' : function(test){ - var pattern1 = createFakeListPattern({ - "template": "{{#listItems.one}}{{> 00-test/00-foo.mustache }}{{/listItems.one}}", - "extendedTemplate" : "{{#listItems.one}}{{> 00-test/00-foo.mustache }}{{/listItems.one}}", - "patternPartial": "test-patternName1", - "relPath": "00-test/02-patternName1.mustache" - }); - - var pattern2 = createFakeListPattern({ - "template": "{{#listItems.two}}{{> 00-test/00-bar.mustache }}{{/listItems.two}}", - "extendedTemplate" : "{{#listItems.two}}{{> 00-test/00-bar.mustache }}{{/listItems.two}}", - "patternPartial": "test-patternName2", - "relPath": "00-test/03-patternName2.mustache" - }); - - var patternlab = createFakePatternLab({ - "patterns": [ - Pattern.create('00-test/00-foo.mustache', null, { - "template": "{{ title }}", - "extendedTemplate": "{{ title }}", - "relPath": "00-test/00-foo.mustache" - }), - Pattern.create('00-test/00-bar.mustache', null, { - "template": "{{ title }}", - "extendedTemplate": "{{ title }}", - "relPath": "00-test/00-bar.mustache" - }) - ] - }); - - // { - // "template": "{{ title }}", - // "extendedTemplate" : "{{ title }}", - // "subdir": "00-test", - // "fileName": "00-foo", - // "jsonFileData" : {}, - // "patternPartial": "test-foo", - // }, - // { - // "template": "{{ title }}", - // "extendedTemplate" : "{{ title }}", - // "subdir": "00-test", - // "fileName": "01-bar", - // "jsonFileData" : {}, - // "patternPartial": "test-bar", - // } - - var list_item_hunter = new lih(); - - //act - list_item_hunter.process_list_item_partials(pattern1, patternlab); - list_item_hunter.process_list_item_partials(pattern2, patternlab); - - //assert - test.equals(pattern1.extendedTemplate, "Foo" ); - test.equals(pattern2.extendedTemplate, "FooBar" ); - - test.done(); - }, + //NOTE: These listitems are faked so that pattern_assembler.combine_listitems has already clobbered them. - 'process_list_item_partials overwrites listItem property if that property is in local .listitem.json': function(test) { - //arrange - //setup current pattern from what we would have during execution - var currentPattern = createFakeListPattern({ - "template": "{{#listItems.two}}{{ title }}{{/listItems.two}}", - "extendedTemplate": "{{#listItems.two}}{{> test-simple }}{{/listItems.two}}", - "key": "test-patternName", - "jsonFileData": {}, - "listitems": { - "2": [ - { "title": "One" }, - { "title": "Two" } - ] + var pl = { + "listitems": { + "1": [ + { + "title": "Foo", + "message": "FooM" } - }); - var patternlab = createFakePatternLab({ - "patterns": [ - createFakeListPattern({ - "template": "{{ title }}", - "extendedTemplate": "{{ title }}", - "patternPartial": "test-simple", - "jsonFileData": {} - }) - ] - }); - var list_item_hunter = new lih(); - - //act - list_item_hunter.process_list_item_partials(currentPattern, patternlab); - - //assert - test.equals(currentPattern.extendedTemplate, "OneTwo" ); - - test.done(); - }, - - 'process_list_item_partials keeps listItem property if that property is not in local .listitem.json' : function(test){ - //arrange - //setup current pattern from what we would have during execution - var currentPattern = createFakeListPattern({ - "template": "{{#listItems.one}}{{ title }}{{/listItems.one}}", - "extendedTemplate": "{{#listItems.one}}{{> test-simple }}{{/listItems.one}}", - "key": "test-patternName", - "jsonFileData": {}, - "listitems": { - "2": [ - { "title": "One" }, - { "title": "Two" } - ] + ], + "2" : [ + { + "title": "Foo", + "message": "FooM" + }, + { + "title": "Bar", + "message": "BarM" } - }); - var patternlab = createFakePatternLab({ - "patterns": [ - createFakeListPattern({ - "template": "{{ title }}", - "extendedTemplate": "{{ title }}", - "patternPartial": "test-simple", - "jsonFileData" : {} - }) - ] - }); - var list_item_hunter = new lih(); - - //act - list_item_hunter.process_list_item_partials(currentPattern, patternlab); - - //assert - test.equals(currentPattern.extendedTemplate, "Foo" ); - - test.done(); + ], + "3": [ + { + "title": "Foo", + "message": "FooM" + }, + { + "title": "Bar", + "message": "BarM" + }, + { + "title": "Baz", + "message": "BazM" + }, + ] }, - - 'process_list_item_partials uses local listItem property if that property is not set globally' : function(test){ - //arrange - //setup current pattern from what we would have during execution - var currentPattern = createFakeListPattern({ - "template": "{{#listItems.one}}{{ title }}{{/listItems.one}}", - "extendedTemplate": "{{#listItems.one}}{{> test-simple }}{{/listItems.one}}", - "key": "test-patternName", - "jsonFileData": {}, - "listitems": { - "1": [ - { "title": "One" } - ], - "2": [ - { "title": "One" }, - { "title": "Two" } - ] + "data": { + "link": {}, + "partials": [] + }, + "config": { + "debug": false, + "paths": { + "source": { + "patterns": "./test/files/_patterns" } - }); - - var patternlab = createFakePatternLab({ - "patterns": [ + }, + "outputFileSuffixes": { + "rendered": '' + } + }, + "partials" : {}, + "patterns" : [] + }; - createFakeListPattern({ - "template": "{{ title }}", - "extendedTemplate": "{{ title }}", - "patternPartial": "test-simple", - "jsonFileData": {} - }) - ] - }); - delete patternlab.listitems["1"]; // remove the "1" list + return extend(pl, customProps); +} + + +tap.test('process_list_item_partials finds and outputs basic repeating blocks', function(test){ + //arrange + //setup current pattern from what we would have during execution + var currentPattern = createFakeListPattern({ + "template": "{{#listItems.two}}{{ title }}{{/listItems.two}}", + "extendedTemplate": "{{#listItems.two}}{{ title }}{{/listItems.two}}", + "key": "test-patternName" + }); + var patternlab = createFakePatternLab(); + var list_item_hunter = new lih(); + + //act + list_item_hunter.process_list_item_partials(currentPattern, patternlab); + + //assert + test.equals(currentPattern.extendedTemplate, "FooBar" ); + + test.end(); +}); + +tap.test('process_list_item_partials listitems with lowercase name', function(test){ + //arrange + //setup current pattern from what we would have during execution + var currentPattern = createFakeListPattern({ + "template": "{{#listitems.two}}{{ title }}{{/listitems.two}}", + "extendedTemplate" : "{{#listitems.two}}{{ title }}{{/listitems.two}}", + "key": "test-patternName" + }); + var patternlab = createFakePatternLab(); + var list_item_hunter = new lih(); + + //act + list_item_hunter.process_list_item_partials(currentPattern, patternlab); + + //assert + test.equals(currentPattern.extendedTemplate, "FooBar" ); + + test.end(); +}); + +tap.test('process_list_item_partials finds partials and outputs repeated renders', function(test){ + //arrange + //setup current pattern from what we would have during execution + var currentPattern = createFakeListPattern({ + "template": "{{#listItems.two}}{{ title }}{{/listItems.two}}", + "extendedTemplate": "{{#listItems.two}}{{> test-simple }}{{/listItems.two}}", + "key": "test-patternName" + }); + + var patternlab = createFakePatternLab({ + "patterns": [ + { + "template": "{{ title }}", + "extendedTemplate" : "{{ title }}", + "patternPartial": "test-simple", + "jsonFileData" : {} + } + + ] + }); + + var list_item_hunter = new lih(); + + //act + list_item_hunter.process_list_item_partials(currentPattern, patternlab); + + //assert + test.equals(currentPattern.extendedTemplate, "FooBar" ); + + test.end(); +}); + +tap.test('process_list_item_partials finds verbose partials and outputs repeated renders', function(test){ + var pattern1 = createFakeListPattern({ + "template": "{{#listItems.one}}{{> 00-test/00-foo.mustache }}{{/listItems.one}}", + "extendedTemplate" : "{{#listItems.one}}{{> 00-test/00-foo.mustache }}{{/listItems.one}}", + "patternPartial": "test-patternName1", + "relPath": "00-test/02-patternName1.mustache" + }); + + var pattern2 = createFakeListPattern({ + "template": "{{#listItems.two}}{{> 00-test/00-bar.mustache }}{{/listItems.two}}", + "extendedTemplate" : "{{#listItems.two}}{{> 00-test/00-bar.mustache }}{{/listItems.two}}", + "patternPartial": "test-patternName2", + "relPath": "00-test/03-patternName2.mustache" + }); + + var patternlab = createFakePatternLab({ + "patterns": [ + Pattern.create('00-test/00-foo.mustache', null, { + "template": "{{ title }}", + "extendedTemplate": "{{ title }}", + "relPath": "00-test/00-foo.mustache" + }), + Pattern.create('00-test/00-bar.mustache', null, { + "template": "{{ title }}", + "extendedTemplate": "{{ title }}", + "relPath": "00-test/00-bar.mustache" + }) + ] + }); + + // { + // "template": "{{ title }}", + // "extendedTemplate" : "{{ title }}", + // "subdir": "00-test", + // "fileName": "00-foo", + // "jsonFileData" : {}, + // "patternPartial": "test-foo", + // }, + // { + // "template": "{{ title }}", + // "extendedTemplate" : "{{ title }}", + // "subdir": "00-test", + // "fileName": "01-bar", + // "jsonFileData" : {}, + // "patternPartial": "test-bar", + // } + + var list_item_hunter = new lih(); + + //act + list_item_hunter.process_list_item_partials(pattern1, patternlab); + list_item_hunter.process_list_item_partials(pattern2, patternlab); + + //assert + test.equals(pattern1.extendedTemplate, "Foo" ); + test.equals(pattern2.extendedTemplate, "FooBar" ); + + test.end(); +}); + +tap.test('process_list_item_partials overwrites listItem property if that property is in local .listitem.json', function(test) { + //arrange + //setup current pattern from what we would have during execution + var currentPattern = createFakeListPattern({ + "template": "{{#listItems.two}}{{ title }}{{/listItems.two}}", + "extendedTemplate": "{{#listItems.two}}{{> test-simple }}{{/listItems.two}}", + "key": "test-patternName", + "jsonFileData": {}, + "listitems": { + "2": [ + { "title": "One" }, + { "title": "Two" } + ] + } + }); + var patternlab = createFakePatternLab({ + "patterns": [ + createFakeListPattern({ + "template": "{{ title }}", + "extendedTemplate": "{{ title }}", + "patternPartial": "test-simple", + "jsonFileData": {} + }) + ] + }); + var list_item_hunter = new lih(); + + //act + list_item_hunter.process_list_item_partials(currentPattern, patternlab); + + //assert + test.equals(currentPattern.extendedTemplate, "OneTwo" ); + + test.end(); +}); + +tap.test('process_list_item_partials keeps listItem property if that property is not in local .listitem.json', function(test){ + //arrange + //setup current pattern from what we would have during execution + var currentPattern = createFakeListPattern({ + "template": "{{#listItems.one}}{{ title }}{{/listItems.one}}", + "extendedTemplate": "{{#listItems.one}}{{> test-simple }}{{/listItems.one}}", + "key": "test-patternName", + "jsonFileData": {}, + "listitems": { + "2": [ + { "title": "One" }, + { "title": "Two" } + ] + } + }); + var patternlab = createFakePatternLab({ + "patterns": [ + createFakeListPattern({ + "template": "{{ title }}", + "extendedTemplate": "{{ title }}", + "patternPartial": "test-simple", + "jsonFileData" : {} + }) + ] + }); + var list_item_hunter = new lih(); + + //act + list_item_hunter.process_list_item_partials(currentPattern, patternlab); + + //assert + test.equals(currentPattern.extendedTemplate, "Foo" ); + + test.end(); +}); + +tap.test('process_list_item_partials uses local listItem property if that property is not set globally', function(test){ + //arrange + //setup current pattern from what we would have during execution + var currentPattern = createFakeListPattern({ + "template": "{{#listItems.one}}{{ title }}{{/listItems.one}}", + "extendedTemplate": "{{#listItems.one}}{{> test-simple }}{{/listItems.one}}", + "key": "test-patternName", + "jsonFileData": {}, + "listitems": { + "1": [ + { "title": "One" } + ], + "2": [ + { "title": "One" }, + { "title": "Two" } + ] + } + }); + + var patternlab = createFakePatternLab({ + "patterns": [ + + createFakeListPattern({ + "template": "{{ title }}", + "extendedTemplate": "{{ title }}", + "patternPartial": "test-simple", + "jsonFileData": {} + }) + ] + }); + delete patternlab.listitems["1"]; // remove the "1" list + + var list_item_hunter = new lih(); + + //act + list_item_hunter.process_list_item_partials(currentPattern, patternlab); + + //assert + test.equals(typeof patternlab.listitems["1"], "undefined"); + test.equals(currentPattern.extendedTemplate, "One" ); + + test.end(); +}); + +tap.test('process_list_item_partials - correctly ignores bookended partials without a style modifier when the same partial has a style modifier between', function(test){ + //arrange + var fs = require('fs-extra'); + var pa = require('../core/lib/pattern_assembler'); + var pattern_assembler = new pa(); + var list_item_hunter = new lih(); + var patterns_dir = './test/files/_patterns'; + + var pl = {}; + pl.config = {}; + pl.data = {}; + pl.data.link = {}; + pl.config.debug = false; + pl.patterns = []; + pl.partials = {}; + pl.config.patterns = { source: patterns_dir }; + pl.listitems = { + "1": [ + { + "message": "Foo" + } + ], + "2": [ + { + "message": "Foo" + }, + { + "message": "Bar" + } + ] + }; - var list_item_hunter = new lih(); + var atomPattern = new Pattern('00-test/03-styled-atom.mustache'); + atomPattern.template = fs.readFileSync(patterns_dir + '/00-test/03-styled-atom.mustache', 'utf8'); + atomPattern.extendedTemplate = atomPattern.template; + atomPattern.stylePartials = pattern_assembler.find_pattern_partials_with_style_modifiers(atomPattern); - //act - list_item_hunter.process_list_item_partials(currentPattern, patternlab); + var bookendPattern = new Pattern('00-test/11-bookend-listitem.mustache'); + bookendPattern.template = fs.readFileSync(patterns_dir + '/00-test/11-bookend-listitem.mustache', 'utf8'); + bookendPattern.extendedTemplate = bookendPattern.template; + bookendPattern.stylePartials = pattern_assembler.find_pattern_partials_with_style_modifiers(bookendPattern); - //assert - test.equals(typeof patternlab.listitems["1"], "undefined"); - test.equals(currentPattern.extendedTemplate, "One" ); + pl.patterns.push(atomPattern); + pl.patterns.push(bookendPattern); - test.done(); - }, + //act + list_item_hunter.process_list_item_partials(bookendPattern, pl); - 'process_list_item_partials - correctly ignores bookended partials without a style modifier when the same partial has a style modifier between' : function(test){ - //arrange - var fs = require('fs-extra'); - var pa = require('../core/lib/pattern_assembler'); - var pattern_assembler = new pa(); - var list_item_hunter = new lih(); - var patterns_dir = './test/files/_patterns'; - - var pl = {}; - pl.config = {}; - pl.data = {}; - pl.data.link = {}; - pl.config.debug = false; - pl.patterns = []; - pl.partials = {}; - pl.config.patterns = { source: patterns_dir }; - pl.listitems = { - "1": [ - { - "message": "Foo" - } - ], - "2": [ - { - "message": "Foo" - }, - { - "message": "Bar" - } - ] - }; - - var atomPattern = new Pattern('00-test/03-styled-atom.mustache'); - atomPattern.template = fs.readFileSync(patterns_dir + '/00-test/03-styled-atom.mustache', 'utf8'); - atomPattern.extendedTemplate = atomPattern.template; - atomPattern.stylePartials = pattern_assembler.find_pattern_partials_with_style_modifiers(atomPattern); - - var bookendPattern = new Pattern('00-test/11-bookend-listitem.mustache'); - bookendPattern.template = fs.readFileSync(patterns_dir + '/00-test/11-bookend-listitem.mustache', 'utf8'); - bookendPattern.extendedTemplate = bookendPattern.template; - bookendPattern.stylePartials = pattern_assembler.find_pattern_partials_with_style_modifiers(bookendPattern); - - pl.patterns.push(atomPattern); - pl.patterns.push(bookendPattern); - - //act - list_item_hunter.process_list_item_partials(bookendPattern, pl); - - //assert. here we expect {{styleModifier}} to be replaced with an empty string or the styleModifier value from the found partial with the :styleModifier - var expectedValue = '
Foo Foo Foo Bar Bar Bar
'; - test.equals(bookendPattern.extendedTemplate.replace(/\s\s+/g, ' ').replace(/\n/g, ' ').trim(), expectedValue.trim()); - test.done(); - }, + //assert. here we expect {{styleModifier}} to be replaced with an empty string or the styleModifier value from the found partial with the :styleModifier + var expectedValue = '
Foo Foo Foo Bar Bar Bar
'; + test.equals(bookendPattern.extendedTemplate.replace(/\s\s+/g, ' ').replace(/\n/g, ' ').trim(), expectedValue.trim()); + test.end(); +}); - 'process_list_item_partials - correctly ignores already processed partial that had a style modifier when the same partial no longer has one' : function(test){ - //arrange - var fs = require('fs-extra'); - var list_item_hunter = new lih(); +tap.test('process_list_item_partials - correctly ignores already processed partial that had a style modifier when the same partial no longer has one', function(test){ + //arrange + var fs = require('fs-extra'); + var list_item_hunter = new lih(); - var pl = createFakePatternLab(); + var pl = createFakePatternLab(); - var atomPattern = new Pattern('00-test/03-styled-atom.mustache'); - atomPattern.template = fs.readFileSync(pl.config.paths.source.patterns + '/00-test/03-styled-atom.mustache', 'utf8'); - atomPattern.extendedTemplate = atomPattern.template; - atomPattern.stylePartials = pattern_assembler.find_pattern_partials_with_style_modifiers(atomPattern); + var atomPattern = new Pattern('00-test/03-styled-atom.mustache'); + atomPattern.template = fs.readFileSync(pl.config.paths.source.patterns + '/00-test/03-styled-atom.mustache', 'utf8'); + atomPattern.extendedTemplate = atomPattern.template; + atomPattern.stylePartials = pattern_assembler.find_pattern_partials_with_style_modifiers(atomPattern); - var anotherStyledAtomPattern = new Pattern('00-test/12-another-styled-atom.mustache'); - anotherStyledAtomPattern.template = fs.readFileSync(pl.config.paths.source.patterns + '/00-test/12-another-styled-atom.mustache', 'utf8'); - anotherStyledAtomPattern.extendedTemplate = anotherStyledAtomPattern.template; - anotherStyledAtomPattern.stylePartials = pattern_assembler.find_pattern_partials_with_style_modifiers(anotherStyledAtomPattern); + var anotherStyledAtomPattern = new Pattern('00-test/12-another-styled-atom.mustache'); + anotherStyledAtomPattern.template = fs.readFileSync(pl.config.paths.source.patterns + '/00-test/12-another-styled-atom.mustache', 'utf8'); + anotherStyledAtomPattern.extendedTemplate = anotherStyledAtomPattern.template; + anotherStyledAtomPattern.stylePartials = pattern_assembler.find_pattern_partials_with_style_modifiers(anotherStyledAtomPattern); - var listPattern = new Pattern('00-test/13-listitem.mustache'); - listPattern.template = fs.readFileSync(pl.config.paths.source.patterns + '/00-test/13-listitem.mustache', 'utf8'); - listPattern.extendedTemplate = listPattern.template; - listPattern.stylePartials = pattern_assembler.find_pattern_partials_with_style_modifiers(listPattern); + var listPattern = new Pattern('00-test/13-listitem.mustache'); + listPattern.template = fs.readFileSync(pl.config.paths.source.patterns + '/00-test/13-listitem.mustache', 'utf8'); + listPattern.extendedTemplate = listPattern.template; + listPattern.stylePartials = pattern_assembler.find_pattern_partials_with_style_modifiers(listPattern); - pl.patterns.push(atomPattern); - pl.patterns.push(anotherStyledAtomPattern); - pl.patterns.push(listPattern); + pl.patterns.push(atomPattern); + pl.patterns.push(anotherStyledAtomPattern); + pl.patterns.push(listPattern); - //act + //act - //might need to cal processPatternRecursive instead - pattern_assembler.process_pattern_recursive(atomPattern.relPath, pl); - pattern_assembler.process_pattern_recursive(anotherStyledAtomPattern.relPath, pl); - pattern_assembler.process_pattern_recursive(listPattern.relPath, pl); + //might need to cal processPatternRecursive instead + pattern_assembler.process_pattern_recursive(atomPattern.relPath, pl); + pattern_assembler.process_pattern_recursive(anotherStyledAtomPattern.relPath, pl); + pattern_assembler.process_pattern_recursive(listPattern.relPath, pl); - //assert. - var expectedValue = '
FooM
'; - test.equals(listPattern.extendedTemplate.replace(/\s\s+/g, ' ').replace(/\n/g, ' ').trim(), expectedValue.trim()); - test.done(); - }, + //assert. + var expectedValue = '
FooM
'; + test.equals(listPattern.extendedTemplate.replace(/\s\s+/g, ' ').replace(/\n/g, ' ').trim(), expectedValue.trim()); + test.end(); +}); - }; -})(); diff --git a/test/markdown_parser_tests.js b/test/markdown_parser_tests.js index 60ab16762..5931dd832 100644 --- a/test/markdown_parser_tests.js +++ b/test/markdown_parser_tests.js @@ -1,12 +1,14 @@ "use strict"; +var tap = require('tap'); + var path = require('path'); var fs = require('fs-extra'); var mp = require('../core/lib/markdown_parser'); var markdown_parser = new mp(); -exports['markdown_parser'] = { - 'parses pattern description block correctly when frontmatter not present' : function(test) { + +tap.test('parses pattern description block correctly when frontmatter not present', function(test) { //arrange var markdownFileName = path.resolve("./test/files/_patterns/00-test/00-foo.md"); var markdownFileContents = fs.readFileSync(markdownFileName, 'utf8'); @@ -16,9 +18,10 @@ exports['markdown_parser'] = { //assert test.equals(returnObject.markdown, '

A Simple Include

\n

This pattern contains an include of test-bar. It also has this markdown file, which does not have frontmatter.

\n'); - test.done(); - }, - 'parses pattern description block correctly when frontmatter present' : function (test) { + test.end(); +}); + +tap.test('parses pattern description block correctly when frontmatter present', function (test) { //arrange var markdownFileName = path.resolve("./test/files/_patterns/00-test/01-bar.md"); var markdownFileContents = fs.readFileSync(markdownFileName, 'utf8'); @@ -29,19 +32,19 @@ exports['markdown_parser'] = { //assert test.equals(returnObject.markdown, '

A Simple Bit of Markup

\n

Foo cannot get simpler than bar, amiright?

\n'); test.equals(returnObject.status, 'complete'); - test.done(); - }, - 'parses frontmatter only when no markdown present': function (test) { - //arrange - var markdownFileName = path.resolve("./test/files/_patterns/00-test/03-styled-atom.md"); - var markdownFileContents = fs.readFileSync(markdownFileName, 'utf8'); - - //act - var returnObject = markdown_parser.parse(markdownFileContents); - - //assert - test.equals(returnObject.markdown, ''); - test.equals(returnObject.status, 'inprogress'); - test.done(); - } -}; + test.end(); +}); + +tap.test('parses frontmatter only when no markdown present', function (test) { + //arrange + var markdownFileName = path.resolve("./test/files/_patterns/00-test/03-styled-atom.md"); + var markdownFileContents = fs.readFileSync(markdownFileName, 'utf8'); + + //act + var returnObject = markdown_parser.parse(markdownFileContents); + + //assert + test.equals(returnObject.markdown, ''); + test.equals(returnObject.status, 'inprogress'); + test.end(); +}); diff --git a/test/object_factory_tests.js b/test/object_factory_tests.js index 77f203bce..f20034f61 100644 --- a/test/object_factory_tests.js +++ b/test/object_factory_tests.js @@ -1,5 +1,7 @@ "use strict"; +var tap = require('tap'); + // fake pattern lab constructor: // sets up a fake patternlab object, which is needed by the pattern processing // apparatus. @@ -26,76 +28,78 @@ var Pattern = require('../core/lib/object_factory').Pattern; var path = require('path'); var pl = fakePatternLab(); -exports['Pattern initialization'] = { - 'test Pattern initializes correctly' : function (test) { - var p = new Pattern('00-atoms/00-global/00-colors.mustache', { d: 123}); - test.equals(p.relPath, '00-atoms' + path.sep + '00-global' + path.sep + '00-colors.mustache'); - test.equals(p.name, '00-atoms-00-global-00-colors'); - test.equals(p.subdir, '00-atoms' + path.sep + '00-global'); - test.equals(p.fileName, '00-colors'); - test.equals(p.fileExtension, '.mustache'); - test.equals(p.jsonFileData.d, 123); - test.equals(p.patternBaseName, 'colors'); - test.equals(p.patternName, 'Colors'); - test.equals(p.getPatternLink(pl), '00-atoms-00-global-00-colors' + path.sep + '00-atoms-00-global-00-colors.rendered.html'); - test.equals(p.patternGroup, 'atoms'); - test.equals(p.patternSubGroup, 'global'); - test.equals(p.flatPatternPath, '00-atoms-00-global'); - test.equals(p.patternPartial, 'atoms-colors'); - test.equals(p.template, ''); - test.equals(p.patternPartialCode, ''); - test.equals(p.lineage.length, 0); - test.equals(p.lineageIndex.length, 0); - test.equals(p.lineageR.length, 0); - test.equals(p.lineageRIndex.length, 0); - test.equals(p.patternState, ''); - test.done(); - }, - 'test Pattern with one-directory subdir works as expected' : function (test) { - var p = new Pattern('00-atoms/00-colors.mustache', { d: 123}); - test.equals(p.relPath, '00-atoms' + path.sep + '00-colors.mustache'); - test.equals(p.name, '00-atoms-00-colors'); - test.equals(p.subdir, '00-atoms'); - test.equals(p.fileName, '00-colors'); - test.equals(p.fileExtension, '.mustache'); - test.equals(p.jsonFileData.d, 123); - test.equals(p.patternBaseName, 'colors'); - test.equals(p.patternName, 'Colors'); - test.equals(p.getPatternLink(pl), '00-atoms-00-colors' + path.sep + '00-atoms-00-colors.rendered.html'); - test.equals(p.patternGroup, 'atoms'); - test.equals(p.flatPatternPath, '00-atoms'); - test.equals(p.patternPartial, 'atoms-colors'); - test.equals(p.template, ''); - test.equals(p.lineage.length, 0); - test.equals(p.lineageIndex.length, 0); - test.equals(p.lineageR.length, 0); - test.equals(p.lineageRIndex.length, 0); - test.done(); - }, - 'test Pattern with no numbers in pattern group works as expected' : function (test) { - var p = new Pattern('atoms/colors.mustache', { d: 123}); - test.equals(p.relPath, 'atoms' + path.sep + 'colors.mustache'); - test.equals(p.name, 'atoms-colors'); - test.equals(p.subdir, 'atoms'); - test.equals(p.fileName, 'colors'); - test.equals(p.getPatternLink(pl), 'atoms-colors' + path.sep + 'atoms-colors.rendered.html'); - test.equals(p.patternGroup, 'atoms'); - test.equals(p.flatPatternPath, 'atoms'); - test.equals(p.patternPartial, 'atoms-colors'); - test.done(); - }, - 'test Pattern capitalizes patternDisplayName correctly' : function (test) { - var p = new Pattern('00-atoms/00-global/00-colors-alt.mustache', { d: 123}); - test.equals(p.patternBaseName, 'colors-alt'); - test.equals(p.patternName, 'Colors Alt'); - test.done(); - }, - 'The forms of Pattern.getPatternLink() work as expected': function (test) { +tap.test('test Pattern initializes correctly', function (test) { + var p = new Pattern('00-atoms/00-global/00-colors.mustache', { d: 123}); + test.equals(p.relPath, '00-atoms' + path.sep + '00-global' + path.sep + '00-colors.mustache'); + test.equals(p.name, '00-atoms-00-global-00-colors'); + test.equals(p.subdir, '00-atoms' + path.sep + '00-global'); + test.equals(p.fileName, '00-colors'); + test.equals(p.fileExtension, '.mustache'); + test.equals(p.jsonFileData.d, 123); + test.equals(p.patternBaseName, 'colors'); + test.equals(p.patternName, 'Colors'); + test.equals(p.getPatternLink(pl), '00-atoms-00-global-00-colors' + path.sep + '00-atoms-00-global-00-colors.rendered.html'); + test.equals(p.patternGroup, 'atoms'); + test.equals(p.patternSubGroup, 'global'); + test.equals(p.flatPatternPath, '00-atoms-00-global'); + test.equals(p.patternPartial, 'atoms-colors'); + test.equals(p.template, ''); + test.equals(p.patternPartialCode, ''); + test.equals(p.lineage.length, 0); + test.equals(p.lineageIndex.length, 0); + test.equals(p.lineageR.length, 0); + test.equals(p.lineageRIndex.length, 0); + test.equals(p.patternState, ''); + test.end(); +}); + +tap.test('test Pattern with one-directory subdir works as expected', function (test) { + var p = new Pattern('00-atoms/00-colors.mustache', { d: 123}); + test.equals(p.relPath, '00-atoms' + path.sep + '00-colors.mustache'); + test.equals(p.name, '00-atoms-00-colors'); + test.equals(p.subdir, '00-atoms'); + test.equals(p.fileName, '00-colors'); + test.equals(p.fileExtension, '.mustache'); + test.equals(p.jsonFileData.d, 123); + test.equals(p.patternBaseName, 'colors'); + test.equals(p.patternName, 'Colors'); + test.equals(p.getPatternLink(pl), '00-atoms-00-colors' + path.sep + '00-atoms-00-colors.rendered.html'); + test.equals(p.patternGroup, 'atoms'); + test.equals(p.flatPatternPath, '00-atoms'); + test.equals(p.patternPartial, 'atoms-colors'); + test.equals(p.template, ''); + test.equals(p.lineage.length, 0); + test.equals(p.lineageIndex.length, 0); + test.equals(p.lineageR.length, 0); + test.equals(p.lineageRIndex.length, 0); + test.end(); +}); + +tap.test('test Pattern with no numbers in pattern group works as expected', function (test) { + var p = new Pattern('atoms/colors.mustache', { d: 123}); + test.equals(p.relPath, 'atoms' + path.sep + 'colors.mustache'); + test.equals(p.name, 'atoms-colors'); + test.equals(p.subdir, 'atoms'); + test.equals(p.fileName, 'colors'); + test.equals(p.getPatternLink(pl), 'atoms-colors' + path.sep + 'atoms-colors.rendered.html'); + test.equals(p.patternGroup, 'atoms'); + test.equals(p.flatPatternPath, 'atoms'); + test.equals(p.patternPartial, 'atoms-colors'); + test.end(); +}); + +tap.test('test Pattern capitalizes patternDisplayName correctly', function (test) { + var p = new Pattern('00-atoms/00-global/00-colors-alt.mustache', { d: 123}); + test.equals(p.patternBaseName, 'colors-alt'); + test.equals(p.patternName, 'Colors Alt'); + test.end(); +}); + +tap.test('The forms of Pattern.getPatternLink() work as expected', function (test) { var p = new Pattern('00-atoms/00-global/00-colors.hbs'); test.equals(p.getPatternLink(pl), '00-atoms-00-global-00-colors' + path.sep + '00-atoms-00-global-00-colors.rendered.html'); test.equals(p.getPatternLink(pl, 'rendered'), '00-atoms-00-global-00-colors' + path.sep + '00-atoms-00-global-00-colors.rendered.html'); test.equals(p.getPatternLink(pl, 'rawTemplate'), '00-atoms-00-global-00-colors' + path.sep + '00-atoms-00-global-00-colors.hbs'); test.equals(p.getPatternLink(pl, 'markupOnly'), '00-atoms-00-global-00-colors' + path.sep + '00-atoms-00-global-00-colors.markup-only.html'); - test.done(); - } -}; + test.end(); +}); diff --git a/test/parameter_hunter_tests.js b/test/parameter_hunter_tests.js index 7db9ec85d..092f44428 100644 --- a/test/parameter_hunter_tests.js +++ b/test/parameter_hunter_tests.js @@ -1,350 +1,345 @@ -(function () { - "use strict"; - - var ph = require('../core/lib/parameter_hunter'); - - //setup current pattern from what we would have during execution - function currentPatternClosure() { - return { - "relPath": "02-organisms/02-comments/01-sticky-comment.mustache", - "fileName": "01-sticky-comment", - "subdir": "02-organisms/02-comments", - "name": "02-organisms-02-comments-01-sticky-comment", - "patternBaseName": "sticky-comment", - "patternLink": "02-organisms-02-comments-01-sticky-comment/02-organisms-02-comments-01-sticky-comment.html", - "patternGroup": "organisms", - "patternSubGroup": "comments", - "flatPatternPath": "02-organisms-02-comments", - "patternPartial": "organisms-sticky-comment", - "template": "{{> molecules-single-comment(description: 'A life is like a garden. Perfect moments can be had, but not preserved, except in memory.') }}", - "extendedTemplate": "{{> molecules-single-comment(description: 'A life is like a garden. Perfect moments can be had, but not preserved, except in memory.') }}", - "parameteredPartials": [ - "{{> molecules-single-comment(description: 'We are all in the gutter, but some of us are looking at the stars.') }}", - "{{> molecules-single-comment(description: 'A life is like a garden. Perfect moments can be had, but not preserved, except in memory.') }}" - ] - }; - } - - function patternlabClosure() { - return { - patterns: [ - { - "relPath": "01-molecules/06-components/02-single-comment.mustache", - "fileName": "02-single-comment", - "subdir": "01-molecules/06-components", - "name": "01-molecules-06-components-02-single-comment", - "patternBaseName": "single-comment", - "patternLink": "01-molecules-06-components-02-single-comment/01-molecules-06-components-02-single-comment.html", - "patternGroup": "molecules", - "patternSubGroup": "components", - "flatPatternPath": "01-molecules-06-components", - "patternPartial": "molecules-single-comment", - "template": "

{{description}}

", - "extendedTemplate": "

{{description}}

" - } - ], - config: { - debug: false - }, - data: { - description: 'Not a quote from a smart man' - }, - partials: {} - } +"use strict"; + +var tap = require('tap'); + +var ph = require('../core/lib/parameter_hunter'); + +//setup current pattern from what we would have during execution +function currentPatternClosure() { + return { + "relPath": "02-organisms/02-comments/01-sticky-comment.mustache", + "fileName": "01-sticky-comment", + "subdir": "02-organisms/02-comments", + "name": "02-organisms-02-comments-01-sticky-comment", + "patternBaseName": "sticky-comment", + "patternLink": "02-organisms-02-comments-01-sticky-comment/02-organisms-02-comments-01-sticky-comment.html", + "patternGroup": "organisms", + "patternSubGroup": "comments", + "flatPatternPath": "02-organisms-02-comments", + "patternPartial": "organisms-sticky-comment", + "template": "{{> molecules-single-comment(description: 'A life is like a garden. Perfect moments can be had, but not preserved, except in memory.') }}", + "extendedTemplate": "{{> molecules-single-comment(description: 'A life is like a garden. Perfect moments can be had, but not preserved, except in memory.') }}", + "parameteredPartials": [ + "{{> molecules-single-comment(description: 'We are all in the gutter, but some of us are looking at the stars.') }}", + "{{> molecules-single-comment(description: 'A life is like a garden. Perfect moments can be had, but not preserved, except in memory.') }}" + ] }; - - exports['parameter_hunter'] = { - 'parameter hunter finds and extends templates' : function(test){ - var currentPattern = currentPatternClosure(); - var patternlab = patternlabClosure(); - var parameter_hunter = new ph(); - - parameter_hunter.find_parameters(currentPattern, patternlab); - test.equals(currentPattern.extendedTemplate, '

A life is like a garden. Perfect moments can be had, but not preserved, except in memory.

'); - - test.done(); +} + +function patternlabClosure() { + return { + patterns: [ + { + "relPath": "01-molecules/06-components/02-single-comment.mustache", + "fileName": "02-single-comment", + "subdir": "01-molecules/06-components", + "name": "01-molecules-06-components-02-single-comment", + "patternBaseName": "single-comment", + "patternLink": "01-molecules-06-components-02-single-comment/01-molecules-06-components-02-single-comment.html", + "patternGroup": "molecules", + "patternSubGroup": "components", + "flatPatternPath": "01-molecules-06-components", + "patternPartial": "molecules-single-comment", + "template": "

{{description}}

", + "extendedTemplate": "

{{description}}

" + } + ], + config: { + debug: false + }, + data: { + description: 'Not a quote from a smart man' }, + partials: {} + } +}; - 'parameter hunter finds and extends templates with mixed parameter and global data' : function(test){ - var currentPattern = currentPatternClosure(); - var patternlab = patternlabClosure(); - var parameter_hunter = new ph(); +tap.test('parameter hunter finds and extends templates', function(test) { + var currentPattern = currentPatternClosure(); + var patternlab = patternlabClosure(); + var parameter_hunter = new ph(); - patternlab.patterns[0].template = "

{{foo}}

{{description}}

"; - patternlab.patterns[0].extendedTemplate = patternlab.patterns[0].template; - patternlab.data.foo = 'Bar'; - patternlab.data.description = 'Baz'; + parameter_hunter.find_parameters(currentPattern, patternlab); + test.equals(currentPattern.extendedTemplate, '

A life is like a garden. Perfect moments can be had, but not preserved, except in memory.

'); - parameter_hunter.find_parameters(currentPattern, patternlab); - test.equals(currentPattern.extendedTemplate, '

Bar

A life is like a garden. Perfect moments can be had, but not preserved, except in memory.

'); + test.end(); +}); - test.done(); - }, +tap.test('parameter hunter finds and extends templates with mixed parameter and global data', function(test) { + var currentPattern = currentPatternClosure(); + var patternlab = patternlabClosure(); + var parameter_hunter = new ph(); - 'parameter hunter finds and extends templates with verbose partials' : function(test){ - var currentPattern = currentPatternClosure(); - var patternlab = patternlabClosure(); - var parameter_hunter = new ph(); + patternlab.patterns[0].template = "

{{foo}}

{{description}}

"; + patternlab.patterns[0].extendedTemplate = patternlab.patterns[0].template; + patternlab.data.foo = 'Bar'; + patternlab.data.description = 'Baz'; - currentPattern.template = "{{> 01-molecules/06-components/02-single-comment(description: 'A life is like a garden. Perfect moments can be had, but not preserved, except in memory.') }}"; - currentPattern.extendedTemplate = currentPattern.template; - currentPattern.parameteredPartials[0] = "{{> 01-molecules/06-components/02-single-comment(description: 'We are all in the gutter, but some of us are looking at the stars.') }}"; - currentPattern.parameteredPartials[1] = currentPattern.template; + parameter_hunter.find_parameters(currentPattern, patternlab); + test.equals(currentPattern.extendedTemplate, '

Bar

A life is like a garden. Perfect moments can be had, but not preserved, except in memory.

'); - parameter_hunter.find_parameters(currentPattern, patternlab); - test.equals(currentPattern.extendedTemplate, '

A life is like a garden. Perfect moments can be had, but not preserved, except in memory.

'); + test.end(); +}); - test.done(); - }, +tap.test('parameter hunter finds and extends templates with verbose partials', function(test) { + var currentPattern = currentPatternClosure(); + var patternlab = patternlabClosure(); + var parameter_hunter = new ph(); - 'parameter hunter finds and extends templates with fully-pathed partials' : function(test){ - var currentPattern = currentPatternClosure(); - var patternlab = patternlabClosure(); - var parameter_hunter = new ph(); + currentPattern.template = "{{> 01-molecules/06-components/02-single-comment(description: 'A life is like a garden. Perfect moments can be had, but not preserved, except in memory.') }}"; + currentPattern.extendedTemplate = currentPattern.template; + currentPattern.parameteredPartials[0] = "{{> 01-molecules/06-components/02-single-comment(description: 'We are all in the gutter, but some of us are looking at the stars.') }}"; + currentPattern.parameteredPartials[1] = currentPattern.template; - currentPattern.template = "{{> 01-molecules/06-components/02-single-comment.mustache(description: 'A life is like a garden. Perfect moments can be had, but not preserved, except in memory.') }}"; - currentPattern.extendedTemplate = currentPattern.template; - currentPattern.parameteredPartials[0] = "{{> 01-molecules/06-components/02-single-comment.mustache(description: 'We are all in the gutter, but some of us are looking at the stars.') }}"; - currentPattern.parameteredPartials[1] = currentPattern.template; + parameter_hunter.find_parameters(currentPattern, patternlab); + test.equals(currentPattern.extendedTemplate, '

A life is like a garden. Perfect moments can be had, but not preserved, except in memory.

'); - parameter_hunter.find_parameters(currentPattern, patternlab); - test.equals(currentPattern.extendedTemplate, '

A life is like a garden. Perfect moments can be had, but not preserved, except in memory.

'); + test.end(); +}); - test.done(); - }, +tap.test('parameter hunter finds and extends templates with fully-pathed partials', function(test) { + var currentPattern = currentPatternClosure(); + var patternlab = patternlabClosure(); + var parameter_hunter = new ph(); - //previous tests were for unquoted parameter keys and single-quoted values. - //test other quoting options. - 'parameter hunter parses parameters with unquoted keys and unquoted values' : function(test){ - var currentPattern = currentPatternClosure(); - var patternlab = patternlabClosure(); - var parameter_hunter = new ph(); + currentPattern.template = "{{> 01-molecules/06-components/02-single-comment.mustache(description: 'A life is like a garden. Perfect moments can be had, but not preserved, except in memory.') }}"; + currentPattern.extendedTemplate = currentPattern.template; + currentPattern.parameteredPartials[0] = "{{> 01-molecules/06-components/02-single-comment.mustache(description: 'We are all in the gutter, but some of us are looking at the stars.') }}"; + currentPattern.parameteredPartials[1] = currentPattern.template; - currentPattern.template = "{{> molecules-single-comment(description: true) }}"; - currentPattern.extendedTemplate = currentPattern.template; - currentPattern.parameteredPartials[0] = currentPattern.template; + parameter_hunter.find_parameters(currentPattern, patternlab); + test.equals(currentPattern.extendedTemplate, '

A life is like a garden. Perfect moments can be had, but not preserved, except in memory.

'); - parameter_hunter.find_parameters(currentPattern, patternlab); - test.equals(currentPattern.extendedTemplate, '

true

'); + test.end(); +}); - test.done(); - }, +//previous tests were for unquoted parameter keys and single-quoted values. +//test other quoting options. +tap.test('parameter hunter parses parameters with unquoted keys and unquoted values', function(test) { + var currentPattern = currentPatternClosure(); + var patternlab = patternlabClosure(); + var parameter_hunter = new ph(); - 'parameter hunter parses parameters with unquoted keys and double-quoted values' : function(test){ - var currentPattern = currentPatternClosure(); - var patternlab = patternlabClosure(); - var parameter_hunter = new ph(); + currentPattern.template = "{{> molecules-single-comment(description: true) }}"; + currentPattern.extendedTemplate = currentPattern.template; + currentPattern.parameteredPartials[0] = currentPattern.template; - currentPattern.template = "{{> molecules-single-comment(description: \"true\") }}"; - currentPattern.extendedTemplate = currentPattern.template; - currentPattern.parameteredPartials[0] = currentPattern.template; + parameter_hunter.find_parameters(currentPattern, patternlab); + test.equals(currentPattern.extendedTemplate, '

true

'); - parameter_hunter.find_parameters(currentPattern, patternlab); - test.equals(currentPattern.extendedTemplate, '

true

'); + test.end(); +}); - test.done(); - }, +tap.test('parameter hunter parses parameters with unquoted keys and double-quoted values', function(test) { + var currentPattern = currentPatternClosure(); + var patternlab = patternlabClosure(); + var parameter_hunter = new ph(); - 'parameter hunter parses parameters with single-quoted keys and unquoted values' : function(test){ - var currentPattern = currentPatternClosure(); - var patternlab = patternlabClosure(); - var parameter_hunter = new ph(); + currentPattern.template = "{{> molecules-single-comment(description: \"true\") }}"; + currentPattern.extendedTemplate = currentPattern.template; + currentPattern.parameteredPartials[0] = currentPattern.template; - currentPattern.template = "{{> molecules-single-comment('description': true) }}"; - currentPattern.extendedTemplate = currentPattern.template; - currentPattern.parameteredPartials[0] = currentPattern.template; + parameter_hunter.find_parameters(currentPattern, patternlab); + test.equals(currentPattern.extendedTemplate, '

true

'); - parameter_hunter.find_parameters(currentPattern, patternlab); - test.equals(currentPattern.extendedTemplate, '

true

'); + test.end(); +}); - test.done(); - }, +tap.test('parameter hunter parses parameters with single-quoted keys and unquoted values', function(test) { + var currentPattern = currentPatternClosure(); + var patternlab = patternlabClosure(); + var parameter_hunter = new ph(); - 'parameter hunter parses parameters with single-quoted keys and single-quoted values wrapping internal escaped single-quotes' : function(test){ - var currentPattern = currentPatternClosure(); - var patternlab = patternlabClosure(); - var parameter_hunter = new ph(); + currentPattern.template = "{{> molecules-single-comment('description': true) }}"; + currentPattern.extendedTemplate = currentPattern.template; + currentPattern.parameteredPartials[0] = currentPattern.template; - currentPattern.template = "{{> molecules-single-comment('description': 'true not,\\'true\\'') }}"; - currentPattern.extendedTemplate = currentPattern.template; - currentPattern.parameteredPartials[0] = currentPattern.template; + parameter_hunter.find_parameters(currentPattern, patternlab); + test.equals(currentPattern.extendedTemplate, '

true

'); - parameter_hunter.find_parameters(currentPattern, patternlab); - test.equals(currentPattern.extendedTemplate, '

true not,'true'

'); + test.end(); +}); - test.done(); - }, +tap.test('parameter hunter parses parameters with single-quoted keys and single-quoted values wrapping internal escaped single-quotes', function(test) { + var currentPattern = currentPatternClosure(); + var patternlab = patternlabClosure(); + var parameter_hunter = new ph(); - 'parameter hunter parses parameters with single-quoted keys and double-quoted values wrapping internal single-quotes' : function(test){ - var currentPattern = currentPatternClosure(); - var patternlab = patternlabClosure(); - var parameter_hunter = new ph(); + currentPattern.template = "{{> molecules-single-comment('description': 'true not,\\'true\\'') }}"; + currentPattern.extendedTemplate = currentPattern.template; + currentPattern.parameteredPartials[0] = currentPattern.template; - currentPattern.template = "{{> molecules-single-comment('description': \"true not:'true'\") }}"; - currentPattern.extendedTemplate = currentPattern.template; - currentPattern.parameteredPartials[0] = currentPattern.template; + parameter_hunter.find_parameters(currentPattern, patternlab); + test.equals(currentPattern.extendedTemplate, '

true not,'true'

'); - parameter_hunter.find_parameters(currentPattern, patternlab); - test.equals(currentPattern.extendedTemplate, '

true not:'true'

'); + test.end(); +}); - test.done(); - }, +tap.test('parameter hunter parses parameters with single-quoted keys and double-quoted values wrapping internal single-quotes', function(test) { + var currentPattern = currentPatternClosure(); + var patternlab = patternlabClosure(); + var parameter_hunter = new ph(); - 'parameter hunter parses parameters with double-unquoted keys and unquoted values' : function(test){ - var currentPattern = currentPatternClosure(); - var patternlab = patternlabClosure(); - var parameter_hunter = new ph(); + currentPattern.template = "{{> molecules-single-comment('description': \"true not:'true'\") }}"; + currentPattern.extendedTemplate = currentPattern.template; + currentPattern.parameteredPartials[0] = currentPattern.template; - currentPattern.template = "{{> molecules-single-comment(\"description\": true) }}"; - currentPattern.extendedTemplate = currentPattern.template; - currentPattern.parameteredPartials[0] = currentPattern.template; + parameter_hunter.find_parameters(currentPattern, patternlab); + test.equals(currentPattern.extendedTemplate, '

true not:'true'

'); - parameter_hunter.find_parameters(currentPattern, patternlab); - test.equals(currentPattern.extendedTemplate, '

true

'); + test.end(); +}); - test.done(); - }, +tap.test('parameter hunter parses parameters with double-unquoted keys and unquoted values', function(test) { + var currentPattern = currentPatternClosure(); + var patternlab = patternlabClosure(); + var parameter_hunter = new ph(); - 'parameter hunter parses parameters with double-quoted keys and single-quoted values wrapping internal double-quotes' : function(test){ - var currentPattern = currentPatternClosure(); - var patternlab = patternlabClosure(); - var parameter_hunter = new ph(); + currentPattern.template = "{{> molecules-single-comment(\"description\": true) }}"; + currentPattern.extendedTemplate = currentPattern.template; + currentPattern.parameteredPartials[0] = currentPattern.template; - currentPattern.template = "{{> molecules-single-comment(\"description\": 'true not{\"true\"') }}"; - currentPattern.extendedTemplate = currentPattern.template; - currentPattern.parameteredPartials[0] = currentPattern.template; + parameter_hunter.find_parameters(currentPattern, patternlab); + test.equals(currentPattern.extendedTemplate, '

true

'); - parameter_hunter.find_parameters(currentPattern, patternlab); - test.equals(currentPattern.extendedTemplate, '

true not{"true"

'); + test.end(); +}); - test.done(); - }, +tap.test('parameter hunter parses parameters with double-quoted keys and single-quoted values wrapping internal double-quotes', function(test) { + var currentPattern = currentPatternClosure(); + var patternlab = patternlabClosure(); + var parameter_hunter = new ph(); - 'parameter hunter parses parameters with double-quoted keys and double-quoted values wrapping internal escaped double-quotes' : function(test){ - var currentPattern = currentPatternClosure(); - var patternlab = patternlabClosure(); - var parameter_hunter = new ph(); + currentPattern.template = "{{> molecules-single-comment(\"description\": 'true not{\"true\"') }}"; + currentPattern.extendedTemplate = currentPattern.template; + currentPattern.parameteredPartials[0] = currentPattern.template; - currentPattern.template = "{{> molecules-single-comment(\"description\": \"true not}\\\"true\\\"\") }}"; - currentPattern.extendedTemplate = currentPattern.template; - currentPattern.parameteredPartials[0] = currentPattern.template; + parameter_hunter.find_parameters(currentPattern, patternlab); + test.equals(currentPattern.extendedTemplate, '

true not{"true"

'); - parameter_hunter.find_parameters(currentPattern, patternlab); - test.equals(currentPattern.extendedTemplate, '

true not}"true"

'); + test.end(); +}); - test.done(); - }, +tap.test('parameter hunter parses parameters with double-quoted keys and double-quoted values wrapping internal escaped double-quotes', function(test) { + var currentPattern = currentPatternClosure(); + var patternlab = patternlabClosure(); + var parameter_hunter = new ph(); - 'parameter hunter parses parameters with combination of quoting schemes for keys and values' : function(test){ - var currentPattern = currentPatternClosure(); - var patternlab = patternlabClosure(); - var parameter_hunter = new ph(); + currentPattern.template = "{{> molecules-single-comment(\"description\": \"true not}\\\"true\\\"\") }}"; + currentPattern.extendedTemplate = currentPattern.template; + currentPattern.parameteredPartials[0] = currentPattern.template; - currentPattern.template = "{{> molecules-single-comment(description: true, 'foo': false, \"bar\": false, 'single': true, 'singlesingle': 'true', 'singledouble': \"true\", \"double\": true, \"doublesingle\": 'true', \"doubledouble\": \"true\") }}"; - currentPattern.extendedTemplate = currentPattern.template; - currentPattern.parameteredPartials[0] = currentPattern.template; + parameter_hunter.find_parameters(currentPattern, patternlab); + test.equals(currentPattern.extendedTemplate, '

true not}"true"

'); - parameter_hunter.find_parameters(currentPattern, patternlab); - test.equals(currentPattern.extendedTemplate, '

true

'); + test.end(); +}); - test.done(); - }, +tap.test('parameter hunter parses parameters with combination of quoting schemes for keys and values', function(test) { + var currentPattern = currentPatternClosure(); + var patternlab = patternlabClosure(); + var parameter_hunter = new ph(); - 'parameter hunter parses parameters with values containing a closing parenthesis' : function(test){ - // From issue #291 https://github.com/pattern-lab/patternlab-node/issues/291 - var currentPattern = currentPatternClosure(); - var patternlab = patternlabClosure(); - var parameter_hunter = new ph(); + currentPattern.template = "{{> molecules-single-comment(description: true, 'foo': false, \"bar\": false, 'single': true, 'singlesingle': 'true', 'singledouble': \"true\", \"double\": true, \"doublesingle\": 'true', \"doubledouble\": \"true\") }}"; + currentPattern.extendedTemplate = currentPattern.template; + currentPattern.parameteredPartials[0] = currentPattern.template; - currentPattern.template = "{{> molecules-single-comment(description: 'Hello ) World') }}"; - currentPattern.extendedTemplate = currentPattern.template; - currentPattern.parameteredPartials[0] = currentPattern.template; + parameter_hunter.find_parameters(currentPattern, patternlab); + test.equals(currentPattern.extendedTemplate, '

true

'); - parameter_hunter.find_parameters(currentPattern, patternlab); - test.equals(currentPattern.extendedTemplate, '

Hello ) World

'); + test.end(); +}); - test.done(); - }, +tap.test('parameter hunter parses parameters with values containing a closing parenthesis', function(test) { + // From issue #291 https://github.com/pattern-lab/patternlab-node/issues/291 + var currentPattern = currentPatternClosure(); + var patternlab = patternlabClosure(); + var parameter_hunter = new ph(); - 'parameter hunter parses parameters that follow a non-quoted value' : function(test){ - // From issue #291 https://github.com/pattern-lab/patternlab-node/issues/291 - var currentPattern = currentPatternClosure(); - var patternlab = patternlabClosure(); - var parameter_hunter = new ph(); + currentPattern.template = "{{> molecules-single-comment(description: 'Hello ) World') }}"; + currentPattern.extendedTemplate = currentPattern.template; + currentPattern.parameteredPartials[0] = currentPattern.template; - patternlab.patterns[0].template = "

{{foo}}

{{bar}}

"; - patternlab.patterns[0].extendedTemplate = patternlab.patterns[0].template; + parameter_hunter.find_parameters(currentPattern, patternlab); + test.equals(currentPattern.extendedTemplate, '

Hello ) World

'); - currentPattern.template = "{{> molecules-single-comment(foo: true, bar: \"Hello World\") }}"; - currentPattern.extendedTemplate = currentPattern.template; - currentPattern.parameteredPartials[0] = currentPattern.template; + test.end(); +}); - parameter_hunter.find_parameters(currentPattern, patternlab); - test.equals(currentPattern.extendedTemplate, '

true

Hello World

'); +tap.test('parameter hunter parses parameters that follow a non-quoted value', function(test) { + // From issue #291 https://github.com/pattern-lab/patternlab-node/issues/291 + var currentPattern = currentPatternClosure(); + var patternlab = patternlabClosure(); + var parameter_hunter = new ph(); - test.done(); - }, + patternlab.patterns[0].template = "

{{foo}}

{{bar}}

"; + patternlab.patterns[0].extendedTemplate = patternlab.patterns[0].template; - 'parameter hunter parses parameters whose keys contain escaped quotes' : function(test){ - // From issue #291 https://github.com/pattern-lab/patternlab-node/issues/291 - var currentPattern = currentPatternClosure(); - var patternlab = patternlabClosure(); - var parameter_hunter = new ph(); + currentPattern.template = "{{> molecules-single-comment(foo: true, bar: \"Hello World\") }}"; + currentPattern.extendedTemplate = currentPattern.template; + currentPattern.parameteredPartials[0] = currentPattern.template; - patternlab.patterns[0].template = "

{{ silly'key }}

{{bar}}

{{ another\"silly-key }}

"; - patternlab.patterns[0].extendedTemplate = patternlab.patterns[0].template; + parameter_hunter.find_parameters(currentPattern, patternlab); + test.equals(currentPattern.extendedTemplate, '

true

Hello World

'); - currentPattern.template = "{{> molecules-single-comment('silly\\\'key': true, bar: \"Hello World\", \"another\\\"silly-key\": 42 ) }}"; - currentPattern.extendedTemplate = currentPattern.template; - currentPattern.parameteredPartials[0] = currentPattern.template; + test.end(); +}); - parameter_hunter.find_parameters(currentPattern, patternlab); - test.equals(currentPattern.extendedTemplate, '

true

Hello World

42

'); +tap.test('parameter hunter parses parameters whose keys contain escaped quotes', function(test) { + // From issue #291 https://github.com/pattern-lab/patternlab-node/issues/291 + var currentPattern = currentPatternClosure(); + var patternlab = patternlabClosure(); + var parameter_hunter = new ph(); - test.done(); - }, + patternlab.patterns[0].template = "

{{ silly'key }}

{{bar}}

{{ another\"silly-key }}

"; + patternlab.patterns[0].extendedTemplate = patternlab.patterns[0].template; - 'parameter hunter skips malformed parameters' : function(test){ - // From issue #291 https://github.com/pattern-lab/patternlab-node/issues/291 - var currentPattern = currentPatternClosure(); - var patternlab = patternlabClosure(); - var parameter_hunter = new ph(); + currentPattern.template = "{{> molecules-single-comment('silly\\\'key': true, bar: \"Hello World\", \"another\\\"silly-key\": 42 ) }}"; + currentPattern.extendedTemplate = currentPattern.template; + currentPattern.parameteredPartials[0] = currentPattern.template; - patternlab.patterns[0].template = "

{{foo}}

"; - patternlab.patterns[0].extendedTemplate = patternlab.patterns[0].template; + parameter_hunter.find_parameters(currentPattern, patternlab); + test.equals(currentPattern.extendedTemplate, '

true

Hello World

42

'); - currentPattern.abspath = __filename; - currentPattern.template = "{{> molecules-single-comment( missing-val: , : missing-key, : , , foo: \"Hello World\") }}"; - currentPattern.extendedTemplate = currentPattern.template; - currentPattern.parameteredPartials[0] = currentPattern.template; + test.end(); +}); - console.log('\nPattern Lab should catch JSON.parse() errors and output useful debugging information...'); - parameter_hunter.find_parameters(currentPattern, patternlab); - test.equals(currentPattern.extendedTemplate, '

'); +tap.test('parameter hunter skips malformed parameters', function(test) { + // From issue #291 https://github.com/pattern-lab/patternlab-node/issues/291 + var currentPattern = currentPatternClosure(); + var patternlab = patternlabClosure(); + var parameter_hunter = new ph(); - test.done(); - }, + patternlab.patterns[0].template = "

{{foo}}

"; + patternlab.patterns[0].extendedTemplate = patternlab.patterns[0].template; - 'parameter hunter parses parameters containing html tags' : function(test){ - // From issue #145 https://github.com/pattern-lab/patternlab-node/issues/145 - var currentPattern = currentPatternClosure(); - var patternlab = patternlabClosure(); - var parameter_hunter = new ph(); + currentPattern.abspath = __filename; + currentPattern.template = "{{> molecules-single-comment( missing-val: , : missing-key, : , , foo: \"Hello World\") }}"; + currentPattern.extendedTemplate = currentPattern.template; + currentPattern.parameteredPartials[0] = currentPattern.template; - patternlab.patterns[0].template = "

{{{ tag1 }}}

{{{ tag2 }}}

{{{ tag3 }}}

"; - patternlab.patterns[0].extendedTemplate = patternlab.patterns[0].template; + console.log('\nPattern Lab should catch JSON.parse() errors and output useful debugging information...'); + parameter_hunter.find_parameters(currentPattern, patternlab); + test.equals(currentPattern.extendedTemplate, '

'); - currentPattern.template = "{{> molecules-single-comment(tag1: 'Single-quoted', tag2: \"Double-quoted\", tag3: 'With attributes') }}"; - currentPattern.extendedTemplate = currentPattern.template; - currentPattern.parameteredPartials[0] = currentPattern.template; + test.end(); +}); - parameter_hunter.find_parameters(currentPattern, patternlab); - test.equals(currentPattern.extendedTemplate, '

Single-quoted

Double-quoted

With attributes

'); +tap.test('parameter hunter parses parameters containing html tags', function(test){ + // From issue #145 https://github.com/pattern-lab/patternlab-node/issues/145 + var currentPattern = currentPatternClosure(); + var patternlab = patternlabClosure(); + var parameter_hunter = new ph(); - test.done(); - } + patternlab.patterns[0].template = "

{{{ tag1 }}}

{{{ tag2 }}}

{{{ tag3 }}}

"; + patternlab.patterns[0].extendedTemplate = patternlab.patterns[0].template; + currentPattern.template = "{{> molecules-single-comment(tag1: 'Single-quoted', tag2: \"Double-quoted\", tag3: 'With attributes') }}"; + currentPattern.extendedTemplate = currentPattern.template; + currentPattern.parameteredPartials[0] = currentPattern.template; - }; + parameter_hunter.find_parameters(currentPattern, patternlab); + test.equals(currentPattern.extendedTemplate, '

Single-quoted

Double-quoted

With attributes

'); -}()); + test.end(); +}); diff --git a/test/pattern_assembler_tests.js b/test/pattern_assembler_tests.js index 33c3898d1..6f9bafa31 100644 --- a/test/pattern_assembler_tests.js +++ b/test/pattern_assembler_tests.js @@ -1,641 +1,677 @@ -(function () { - "use strict"; - - var pa = require('../core/lib/pattern_assembler'); - var Pattern = require('../core/lib/object_factory').Pattern; - var path = require('path'); - - exports['pattern_assembler'] = { - 'process_pattern_recursive recursively includes partials' : function(test){ - test.expect(3); - - //tests inclusion of partial that will be discovered by diveSync later in iteration than parent - //prepare to diveSync - var diveSync = require('diveSync'); - var fs = require('fs-extra'); - var pa = require('../core/lib/pattern_assembler'); - var plMain = require('../core/lib/patternlab'); - var pattern_assembler = new pa(); - var patterns_dir = './test/files/_patterns'; - var patternlab = {}; - patternlab.config = fs.readJSONSync('./patternlab-config.json'); - patternlab.config.paths.source.patterns = patterns_dir; - patternlab.config.outputFileSuffixes = {rendered : ''}; - - //patternlab.data = fs.readJSONSync(path.resolve(patternlab.config.paths.source.data, 'data.json')); - patternlab.data = {}; - //patternlab.listitems = fs.readJSONSync(path.resolve(patternlab.config.paths.source.data, 'listitems.json')); - patternlab.listitems = {}; - //patternlab.header = fs.readFileSync(path.resolve(patternlab.config.paths.source.patternlabFiles, 'templates/pattern-header-footer/header.html'), 'utf8'); - patternlab.header = ''; - //patternlab.footer = fs.readFileSync(path.resolve(patternlab.config.paths.source.patternlabFiles, 'templates/pattern-header-footer/footer.html'), 'utf8'); - patternlab.footer = ''; - patternlab.patterns = []; - patternlab.data.link = {}; - patternlab.partials = {}; - - //diveSync once to perform iterative populating of patternlab object - plMain.process_all_patterns_iterative(pattern_assembler, patterns_dir, patternlab); - - //diveSync again to recursively include partials, filling out the - //extendedTemplate property of the patternlab.patterns elements - plMain.process_all_patterns_recursive(pattern_assembler, patterns_dir, patternlab); - - //get test output for comparison - var foo = fs.readFileSync(patterns_dir + '/00-test/00-foo.mustache', 'utf8').trim(); - var bar = fs.readFileSync(patterns_dir + '/00-test/01-bar.mustache', 'utf8').trim(); - var fooExtended; - - //get extended pattern - for(var i = 0; i < patternlab.patterns.length; i++){ - if(patternlab.patterns[i].fileName === '00-foo'){ - fooExtended = patternlab.patterns[i].extendedTemplate.trim(); - break; - } - } - - //check initial values - test.equals(foo, '{{> test-bar }}'); - test.equals(bar, 'bar'); - //test that 00-foo.mustache included partial 01-bar.mustache - test.equals(fooExtended, 'bar'); - - test.done(); - }, - 'processPatternRecursive - correctly replaces all stylemodifiers when multiple duplicate patterns with different stylemodifiers found' : function(test){ - //arrange - var fs = require('fs-extra'); - var pattern_assembler = new pa(); - var patterns_dir = './test/files/_patterns'; - - var pl = {}; - pl.config = { - paths: { - source: { - patterns: patterns_dir - } - }, - outputFileSuffixes: { - rendered : '' - } - }; - pl.data = {}; - pl.data.link = {}; - pl.config.debug = false; - pl.patterns = []; - pl.partials = {}; - - var atomPattern = new Pattern('00-test/03-styled-atom.mustache'); - atomPattern.template = fs.readFileSync(patterns_dir + '/00-test/03-styled-atom.mustache', 'utf8'); - atomPattern.stylePartials = pattern_assembler.find_pattern_partials_with_style_modifiers(atomPattern); - - var groupPattern = new Pattern('00-test/04-group.mustache'); - groupPattern.template = fs.readFileSync(patterns_dir + '/00-test/04-group.mustache', 'utf8'); - groupPattern.stylePartials = pattern_assembler.find_pattern_partials_with_style_modifiers(groupPattern); - - pattern_assembler.addPattern(atomPattern, pl); - pattern_assembler.addPattern(groupPattern, pl); - - //act - - pattern_assembler.process_pattern_recursive('00-test' + path.sep + '04-group.mustache', pl, {}); - - //assert - var expectedValue = '
{{message}} {{message}} {{message}} {{message}}
'; - test.equals(groupPattern.extendedTemplate.replace(/\s\s+/g, ' ').replace(/\n/g, ' ').trim(), expectedValue.trim()); - test.done(); - }, - 'processPatternRecursive - correctly replaces multiple stylemodifier classes on same partial' : function(test){ - //arrange - var fs = require('fs-extra'); - var pattern_assembler = new pa(); - var patterns_dir = './test/files/_patterns'; - - var pl = {}; - pl.config = { - paths: { - source: { - patterns: patterns_dir - } - }, - outputFileSuffixes: { - rendered : '' - } - }; - pl.data = {}; - pl.data.link = {}; - pl.config.debug = false; - pl.patterns = []; - pl.partials = {}; - - var atomPattern = new Pattern('00-test/03-styled-atom.mustache'); - atomPattern.template = fs.readFileSync(patterns_dir + '/00-test/03-styled-atom.mustache', 'utf8'); - atomPattern.stylePartials = pattern_assembler.find_pattern_partials_with_style_modifiers(atomPattern); - atomPattern.parameteredPartials = pattern_assembler.find_pattern_partials_with_parameters(atomPattern); - - var groupPattern = new Pattern('00-test/10-multiple-classes-numeric.mustache'); - groupPattern.template = fs.readFileSync(patterns_dir + '/00-test/10-multiple-classes-numeric.mustache', 'utf8'); - groupPattern.stylePartials = pattern_assembler.find_pattern_partials_with_style_modifiers(groupPattern); - groupPattern.parameteredPartials = pattern_assembler.find_pattern_partials_with_parameters(groupPattern); - - pattern_assembler.addPattern(atomPattern, pl); - pattern_assembler.addPattern(groupPattern, pl); - - //act - pattern_assembler.process_pattern_recursive('00-test' + path.sep + '10-multiple-classes-numeric.mustache', pl, {}); - - //assert - var expectedValue = '
{{message}} {{message}} bar
'; - test.equals(groupPattern.extendedTemplate.replace(/\s\s+/g, ' ').replace(/\n/g, ' ').trim(), expectedValue.trim()); - test.done(); - }, - 'processPatternRecursive - correctly ignores a partial without a style modifier when the same partial later has a style modifier' : function(test){ - //arrange - var fs = require('fs-extra'); - var pattern_assembler = new pa(); - var patterns_dir = './test/files/_patterns'; - - var pl = {}; - pl.config = { - paths: { - source: { - patterns: patterns_dir - } - }, - outputFileSuffixes: { - rendered : '' - } - }; - pl.data = {}; - pl.data.link = {}; - pl.config.debug = false; - pl.patterns = []; - pl.partials = {}; - - var atomPattern = new Pattern('00-test/03-styled-atom.mustache'); - atomPattern.template = fs.readFileSync(patterns_dir + '/00-test/03-styled-atom.mustache', 'utf8'); - atomPattern.stylePartials = pattern_assembler.find_pattern_partials_with_style_modifiers(atomPattern); - - var mixedPattern = new Pattern('00-test/06-mixed.mustache'); - mixedPattern.template = fs.readFileSync(patterns_dir + '/00-test/06-mixed.mustache', 'utf8'); - mixedPattern.stylePartials = pattern_assembler.find_pattern_partials_with_style_modifiers(mixedPattern); - - pattern_assembler.addPattern(atomPattern, pl); - pattern_assembler.addPattern(mixedPattern, pl); - - //act - pattern_assembler.process_pattern_recursive('00-test' + path.sep + '06-mixed.mustache', pl, {}); - - //assert. here we expect {{styleModifier}} to be in the first group, since it was not replaced by anything. rendering with data will then remove this (correctly) - var expectedValue = '
{{message}} {{message}} {{message}} {{message}}
'; - test.equals(mixedPattern.extendedTemplate.replace(/\s\s+/g, ' ').replace(/\n/g, ' ').trim(), expectedValue.trim()); - test.done(); - }, - 'processPatternRecursive - correctly ignores bookended partials without a style modifier when the same partial has a style modifier between' : function(test){ - //arrange - var fs = require('fs-extra'); - var pattern_assembler = new pa(); - var patterns_dir = './test/files/_patterns'; - - var pl = {}; - pl.config = { - paths: { - source: { - patterns: patterns_dir - } - }, - outputFileSuffixes: { - rendered : '' - } - }; - pl.data = {}; - pl.data.link = {}; - pl.config.debug = false; - pl.patterns = []; - pl.partials = {}; - - var atomPattern = new Pattern('00-test/03-styled-atom.mustache'); - atomPattern.template = fs.readFileSync(patterns_dir + '/00-test/03-styled-atom.mustache', 'utf8'); - atomPattern.stylePartials = pattern_assembler.find_pattern_partials_with_style_modifiers(atomPattern); - - var bookendPattern = new Pattern('00-test/09-bookend.mustache'); - bookendPattern.template = fs.readFileSync(patterns_dir + '/00-test/09-bookend.mustache', 'utf8'); - bookendPattern.stylePartials = pattern_assembler.find_pattern_partials_with_style_modifiers(bookendPattern); - - pattern_assembler.addPattern(atomPattern, pl); - pattern_assembler.addPattern(bookendPattern, pl); - - //act - pattern_assembler.process_pattern_recursive('00-test' + path.sep + '09-bookend.mustache', pl, {}); - - //assert. here we expect {{styleModifier}} to be in the first and last group, since it was not replaced by anything. rendering with data will then remove this (correctly) - var expectedValue = '
{{message}} {{message}} {{message}} {{message}}
'; - var actualValue = bookendPattern.extendedTemplate.replace(/\s\s+/g, ' ').replace(/\n/g, ' '); - test.equals(actualValue.trim(), expectedValue.trim(), 'actual value:\n' + actualValue + '\nexpected value:\n' + expectedValue); - test.done(); - }, - 'processPatternRecursive - correctly ignores a partial without a style modifier when the same partial later has a style modifier and pattern parameters' : function(test){ - //arrange - var fs = require('fs-extra'); - var pattern_assembler = new pa(); - var patterns_dir = './test/files/_patterns'; - - var pl = {}; - pl.config = { - paths: { - source: { - patterns: patterns_dir - } - }, - outputFileSuffixes: { - rendered : '' - } - }; - pl.data = {}; - pl.data.link = {}; - pl.config.debug = false; - pl.patterns = []; - pl.partials = {}; - - var atomPattern = new Pattern('00-test/03-styled-atom.mustache'); - atomPattern.template = fs.readFileSync(patterns_dir + '/00-test/03-styled-atom.mustache', 'utf8'); - atomPattern.stylePartials = pattern_assembler.find_pattern_partials_with_style_modifiers(atomPattern); - atomPattern.parameteredPartials = pattern_assembler.find_pattern_partials_with_parameters(atomPattern); - - var mixedPattern = new Pattern('00-test/07-mixed-params.mustache'); - mixedPattern.template = fs.readFileSync(patterns_dir + '/00-test/07-mixed-params.mustache', 'utf8'); - mixedPattern.stylePartials = pattern_assembler.find_pattern_partials_with_style_modifiers(mixedPattern); - mixedPattern.parameteredPartials = pattern_assembler.find_pattern_partials_with_parameters(mixedPattern); - - pattern_assembler.addPattern(atomPattern, pl); - pattern_assembler.addPattern(mixedPattern, pl); - - //act - pattern_assembler.process_pattern_recursive('00-test' + path.sep + '07-mixed-params.mustache', pl, {}); - - //assert. here we expect {{styleModifier}} to be in the first span, since it was not replaced by anything. rendering with data will then remove this (correctly) - var expectedValue = '
{{message}} 2 3 4
'; - test.equals(mixedPattern.extendedTemplate.replace(/\s\s+/g, ' ').replace(/\n/g, ' ').trim(), expectedValue.trim()); - test.done(); - }, - 'processPatternRecursive - correctly ignores bookended partials without a style modifier when the same partial has a style modifier and pattern parameters between' : function(test){ - //arrange - var fs = require('fs-extra'); - var pattern_assembler = new pa(); - var patterns_dir = './test/files/_patterns'; - - var pl = {}; - pl.config = { - paths: { - source: { - patterns: patterns_dir - } - }, - outputFileSuffixes: { - rendered : '' - } - }; - pl.data = {}; - pl.data.link = {}; - pl.config.debug = false; - pl.patterns = []; - pl.partials = {}; - - var atomPattern = new Pattern('00-test/03-styled-atom.mustache'); - atomPattern.template = fs.readFileSync(patterns_dir + '/00-test/03-styled-atom.mustache', 'utf8'); - atomPattern.stylePartials = pattern_assembler.find_pattern_partials_with_style_modifiers(atomPattern); - atomPattern.parameteredPartials = pattern_assembler.find_pattern_partials_with_parameters(atomPattern); - - var bookendPattern = new Pattern('00-test/08-bookend-params.mustache'); - bookendPattern.template = fs.readFileSync(patterns_dir + '/00-test/08-bookend-params.mustache', 'utf8'); - bookendPattern.stylePartials = pattern_assembler.find_pattern_partials_with_style_modifiers(bookendPattern); - bookendPattern.parameteredPartials = pattern_assembler.find_pattern_partials_with_parameters(bookendPattern); - - pattern_assembler.addPattern(atomPattern, pl); - pattern_assembler.addPattern(bookendPattern, pl); - - //act - pattern_assembler.process_pattern_recursive('00-test' + path.sep + '08-bookend-params.mustache', pl, {}); - - //assert. here we expect {{styleModifier}} to be in the first and last span, since it was not replaced by anything. rendering with data will then remove this (correctly) - var expectedValue = '
{{message}} 2 3 {{message}}
'; - test.equals(bookendPattern.extendedTemplate.replace(/\s\s+/g, ' ').replace(/\n/g, ' ').trim(), expectedValue.trim()); - test.done(); - }, - 'processPatternRecursive - does not pollute previous patterns when a later one is found with a styleModifier' : function(test){ - //arrange - var fs = require('fs-extra'); - var pattern_assembler = new pa(); - var patterns_dir = './test/files/_patterns'; - - var pl = {}; - pl.config = { - paths: { - source: { - patterns: patterns_dir - } - }, - outputFileSuffixes: { - rendered : '' - } - }; - pl.data = {}; - pl.data.link = {}; - pl.config.debug = false; - pl.patterns = []; - pl.partials = {}; - - var atomPattern = new Pattern('00-test/03-styled-atom.mustache'); - atomPattern.template = fs.readFileSync(patterns_dir + '/00-test/03-styled-atom.mustache', 'utf8'); - atomPattern.stylePartials = pattern_assembler.find_pattern_partials_with_style_modifiers(atomPattern); - atomPattern.parameteredPartials = pattern_assembler.find_pattern_partials_with_parameters(atomPattern); - - var anotherPattern = new Pattern('00-test/12-another-styled-atom.mustache'); - anotherPattern.template = fs.readFileSync(patterns_dir + '/00-test/12-another-styled-atom.mustache', 'utf8'); - anotherPattern.stylePartials = pattern_assembler.find_pattern_partials_with_style_modifiers(anotherPattern); - anotherPattern.parameteredPartials = pattern_assembler.find_pattern_partials_with_parameters(anotherPattern); - - pattern_assembler.addPattern(atomPattern, pl); - pattern_assembler.addPattern(anotherPattern, pl); - - //act - pattern_assembler.process_pattern_recursive('00-test' + path.sep + '12-another-styled-atom.mustache', pl, {}); - - //assert - var expectedCleanValue = ' {{message}} '; - var expectedSetValue = ' {{message}} '; - - //this is the "atom" - it should remain unchanged - test.equals(atomPattern.template.replace(/\s\s+/g, ' ').replace(/\n/g, ' ').trim(), expectedCleanValue.trim()); - test.equals(atomPattern.extendedTemplate.replace(/\s\s+/g, ' ').replace(/\n/g, ' ').trim(), expectedCleanValue.trim()); - - // this is the style modifier pattern, which should resolve correctly - test.equals(anotherPattern.template.replace(/\s\s+/g, ' ').replace(/\n/g, ' ').trim(), '{{> test-styled-atom:test_1 }}'); - test.equals(anotherPattern.extendedTemplate.replace(/\s\s+/g, ' ').replace(/\n/g, ' ').trim(), expectedSetValue.trim()); - - test.done(); +"use strict"; + +var tap = require('tap'); + +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 + //prepare to diveSync + var diveSync = require('diveSync'); + var fs = require('fs-extra'); + var pa = require('../core/lib/pattern_assembler'); + var plMain = require('../core/lib/patternlab'); + var pattern_assembler = new pa(); + var patterns_dir = './test/files/_patterns'; + var patternlab = {}; + patternlab.config = fs.readJSONSync('./patternlab-config.json'); + patternlab.config.paths.source.patterns = patterns_dir; + patternlab.config.outputFileSuffixes = {rendered: ''}; + + //patternlab.data = fs.readJSONSync(path.resolve(patternlab.config.paths.source.data, 'data.json')); + patternlab.data = {}; + //patternlab.listitems = fs.readJSONSync(path.resolve(patternlab.config.paths.source.data, 'listitems.json')); + patternlab.listitems = {}; + //patternlab.header = fs.readFileSync(path.resolve(patternlab.config.paths.source.patternlabFiles, 'templates/pattern-header-footer/header.html'), 'utf8'); + patternlab.header = ''; + //patternlab.footer = fs.readFileSync(path.resolve(patternlab.config.paths.source.patternlabFiles, 'templates/pattern-header-footer/footer.html'), 'utf8'); + patternlab.footer = ''; + patternlab.patterns = []; + patternlab.data.link = {}; + patternlab.partials = {}; + + //diveSync once to perform iterative populating of patternlab object + plMain.process_all_patterns_iterative(pattern_assembler, patterns_dir, patternlab); + + //diveSync again to recursively include partials, filling out the + //extendedTemplate property of the patternlab.patterns elements + plMain.process_all_patterns_recursive(pattern_assembler, patterns_dir, patternlab); + + //get test output for comparison + var foo = fs.readFileSync(patterns_dir + '/00-test/00-foo.mustache', 'utf8').trim(); + var bar = fs.readFileSync(patterns_dir + '/00-test/01-bar.mustache', 'utf8').trim(); + var fooExtended; + + //get extended pattern + for (var i = 0; i < patternlab.patterns.length; i++) { + if (patternlab.patterns[i].fileName === '00-foo') { + fooExtended = patternlab.patterns[i].extendedTemplate.trim(); + break; + } + } + + //check initial values + test.equals(foo, '{{> test-bar }}', 'foo template not as expected'); + test.equals(bar, 'bar', 'bar template not as expected'); + //test that 00-foo.mustache included partial 01-bar.mustache + test.equals(fooExtended, 'bar', 'foo includes bar'); + + test.end(); +}); + +tap.test('processPatternRecursive - correctly replaces all stylemodifiers when multiple duplicate patterns with different stylemodifiers found', function(test) { + //arrange + var fs = require('fs-extra'); + var pattern_assembler = new pa(); + var patterns_dir = './test/files/_patterns'; + + var pl = {}; + pl.config = { + paths: { + source: { + patterns: patterns_dir + } }, - 'processPatternRecursive - ensure deep-nesting works' : function(test){ - //arrange - var fs = require('fs-extra'); - var pattern_assembler = new pa(); - var patterns_dir = './test/files/_patterns'; - - var pl = {}; - pl.config = { - paths: { - source: { - patterns: patterns_dir - } - }, - outputFileSuffixes: { - rendered : '' - } - }; - pl.data = {}; - pl.data.link = {}; - pl.config.debug = false; - pl.patterns = []; - pl.partials = {}; - - var atomPattern = new Pattern('00-test/01-bar.mustache'); - atomPattern.template = fs.readFileSync(patterns_dir + '/00-test/01-bar.mustache', 'utf8'); - atomPattern.stylePartials = pattern_assembler.find_pattern_partials_with_style_modifiers(atomPattern); - atomPattern.parameteredPartials = pattern_assembler.find_pattern_partials_with_parameters(atomPattern); - - var templatePattern = new Pattern('00-test/00-foo.mustache'); - templatePattern.template = fs.readFileSync(patterns_dir + '/00-test/00-foo.mustache', 'utf8'); - templatePattern.stylePartials = pattern_assembler.find_pattern_partials_with_style_modifiers(templatePattern); - templatePattern.parameteredPartials = pattern_assembler.find_pattern_partials_with_parameters(templatePattern); - - var pagesPattern = new Pattern('00-test/14-inception.mustache'); - pagesPattern.template = fs.readFileSync(patterns_dir + '/00-test/14-inception.mustache', 'utf8'); - pagesPattern.stylePartials = pattern_assembler.find_pattern_partials_with_style_modifiers(pagesPattern); - pagesPattern.parameteredPartials = pattern_assembler.find_pattern_partials_with_parameters(pagesPattern); - - pattern_assembler.addPattern(atomPattern, pl); - pattern_assembler.addPattern(templatePattern, pl); - pattern_assembler.addPattern(pagesPattern, pl); - - //act - pattern_assembler.process_pattern_recursive('00-test' + path.sep + '14-inception.mustache', pl, {}); - - //assert - var expectedCleanValue = 'bar'; - var expectedSetValue = 'bar'; - - //this is the "atom" - it should remain unchanged - test.equals(atomPattern.template.replace(/\s\s+/g, ' ').replace(/\n/g, ' ').trim(), expectedCleanValue); - test.equals(atomPattern.extendedTemplate.replace(/\s\s+/g, ' ').replace(/\n/g, ' ').trim(), expectedCleanValue); - - //this is the "template pattern" - it should have an updated extendedTemplate but an unchanged template - test.equals(templatePattern.template.replace(/\s\s+/g, ' ').replace(/\n/g, ' ').trim(), '{{> test-bar }}'); - test.equals(templatePattern.extendedTemplate.replace(/\s\s+/g, ' ').replace(/\n/g, ' ').trim(), expectedSetValue.trim()); - - //this is the "pages pattern" - it should have an updated extendedTemplate equal to the template pattern but an unchanged template - test.equals(pagesPattern.template.replace(/\s\s+/g, ' ').replace(/\n/g, ' ').trim(), '{{> test-foo }}'); - test.equals(pagesPattern.extendedTemplate.replace(/\s\s+/g, ' ').replace(/\n/g, ' ').trim(), expectedSetValue.trim()); - test.done(); + outputFileSuffixes: { + rendered: '' + } + }; + pl.data = {}; + pl.data.link = {}; + pl.config.debug = false; + pl.patterns = []; + pl.partials = {}; + + var atomPattern = new Pattern('00-test/03-styled-atom.mustache'); + atomPattern.template = fs.readFileSync(patterns_dir + '/00-test/03-styled-atom.mustache', 'utf8'); + atomPattern.stylePartials = pattern_assembler.find_pattern_partials_with_style_modifiers(atomPattern); + + var groupPattern = new Pattern('00-test/04-group.mustache'); + groupPattern.template = fs.readFileSync(patterns_dir + '/00-test/04-group.mustache', 'utf8'); + groupPattern.stylePartials = pattern_assembler.find_pattern_partials_with_style_modifiers(groupPattern); + + pattern_assembler.addPattern(atomPattern, pl); + pattern_assembler.addPattern(groupPattern, pl); + + //act + + pattern_assembler.process_pattern_recursive('00-test' + path.sep + '04-group.mustache', pl, {}); + + //assert + var expectedValue = '
{{message}} {{message}} {{message}} {{message}}
'; + test.equals(groupPattern.extendedTemplate.replace(/\s\s+/g, ' ').replace(/\n/g, ' ').trim(), expectedValue.trim()); + test.end(); +}); + +tap.test('processPatternRecursive - correctly replaces multiple stylemodifier classes on same partial', function(test) { + //arrange + var fs = require('fs-extra'); + var pattern_assembler = new pa(); + var patterns_dir = './test/files/_patterns'; + + var pl = {}; + pl.config = { + paths: { + source: { + patterns: patterns_dir + } }, - 'setState - applies any patternState matching the pattern' : function(test){ - //arrange - var pa = require('../core/lib/pattern_assembler'); - var pattern_assembler = new pa(); - var patternlab = {}; - patternlab.config = {}; - patternlab.config.patternStates = {}; - patternlab.config.patternStates["pages-homepage-emergency"] = "inprogress"; - - var pattern = { - patternPartial: "pages-homepage-emergency" - }; - - //act - pattern_assembler.setPatternState(pattern, patternlab); - - //assert - test.equals(pattern.patternState, "inprogress"); - test.done(); - }, - 'setState - does not apply any patternState if nothing matches the pattern' : function(test){ - //arrange - var pa = require('../core/lib/pattern_assembler'); - var pattern_assembler = new pa(); - var patternlab = {}; - patternlab.config = {}; - patternlab.config.patternStates = {}; - patternlab.config.patternStates["pages-homepage-emergency"] = "inprogress"; - - var pattern = { - key: "pages-homepage", - patternState: "" - }; - - //act - pattern_assembler.setPatternState(pattern, patternlab); - - //assert - test.equals(pattern.patternState, ""); - test.done(); - }, - 'parseDataLinks - replaces found link.* data for their expanded links' : function(test){ - //arrange - var diveSync = require('diveSync'); - var fs = require('fs-extra'); - var pa = require('../core/lib/pattern_assembler'); - var plMain = require('../core/lib/patternlab'); - var pattern_assembler = new pa(); - var patterns_dir = './test/files/_patterns/'; - var patternlab = {}; - //THIS IS BAD - patternlab.config = fs.readJSONSync('./patternlab-config.json'); - patternlab.config.paths.source.patterns = patterns_dir; - patternlab.config.outputFileSuffixes = { rendered : '' }; - patternlab.data = {}; - patternlab.listitems = {}; - patternlab.header = {}; - patternlab.footer = {}; - //patternlab.data = fs.readJSONSync(path.resolve(patternlab.config.paths.source.data, 'data.json')); - //patternlab.listitems = fs.readJSONSync(path.resolve(patternlab.config.paths.source.data, 'listitems.json')); - //patternlab.header = fs.readFileSync(path.resolve(patternlab.config.paths.source.patternlabFiles, 'templates/pattern-header-footer/header.html'), 'utf8'); - //patternlab.footer = fs.readFileSync(path.resolve(patternlab.config.paths.source.patternlabFiles, 'templates/pattern-header-footer/footer.html'), 'utf8'); - patternlab.patterns = [ - { - patternPartial: 'twitter-brad' - }, - { - patternPartial: 'twitter-dave' - }, - { - patternPartial: 'twitter-brian' - } - ]; - patternlab.data.link = {}; - patternlab.partials = {}; - - //diveSync once to perform iterative populating of patternlab object - plMain.process_all_patterns_iterative(pattern_assembler, patterns_dir, patternlab); - - //for the sake of the test, also imagining I have the following pages... - patternlab.data.link['twitter-brad'] = 'https://twitter.com/brad_frost'; - patternlab.data.link['twitter-dave'] = 'https://twitter.com/dmolsen'; - patternlab.data.link['twitter-brian'] = 'https://twitter.com/bmuenzenmeyer'; - - patternlab.data.brad = { url: "link.twitter-brad" }; - patternlab.data.dave = { url: "link.twitter-dave" }; - patternlab.data.brian = { url: "link.twitter-brian" }; - - - var pattern; - for(var i = 0; i < patternlab.patterns.length; i++){ - if(patternlab.patterns[i].patternPartial === 'test-nav'){ - pattern = patternlab.patterns[i]; - } - } - - //assert before - test.equals(pattern.jsonFileData.brad.url, "link.twitter-brad"); - test.equals(pattern.jsonFileData.dave.url, "link.twitter-dave"); - test.equals(pattern.jsonFileData.brian.url, "link.twitter-brian"); - - //act - pattern_assembler.parse_data_links(patternlab); - - //assert after - test.equals(pattern.jsonFileData.brad.url, "https://twitter.com/brad_frost"); - test.equals(pattern.jsonFileData.dave.url, "https://twitter.com/dmolsen"); - test.equals(pattern.jsonFileData.brian.url, "https://twitter.com/bmuenzenmeyer"); - - test.equals(patternlab.data.brad.url, "https://twitter.com/brad_frost"); - test.equals(patternlab.data.dave.url, "https://twitter.com/dmolsen"); - test.equals(patternlab.data.brian.url, "https://twitter.com/bmuenzenmeyer"); - test.done(); - }, - 'get_pattern_by_key - returns the fuzzy result when no others found' : function(test){ - //arrange - var pattern_assembler = new pa(); - var patternlab = {}; - patternlab.patterns = []; - - patternlab.patterns.push({ - patternPartial: 'character-han-solo', - subdir: 'character', - fileName: 'han-solo' - }); - - //act - var result = pattern_assembler.getPartial('character-han', patternlab); - //assert - test.equals(result, patternlab.patterns[0]); - test.done(); - }, - 'get_pattern_by_key - returns the exact key if found' : function(test){ - //arrange - var pattern_assembler = new pa(); - var patternlab = {}; - patternlab.patterns = []; - - patternlab.patterns.push({ - patternPartial: 'molecules-primary-nav-jagged', - subdir: 'molecules', - fileName: 'primary-nav-jagged' - }, { - patternPartial: 'molecules-primary-nav', - subdir: 'molecules', - fileName: 'molecules-primary-nav' - }); - - //act - var result = pattern_assembler.getPartial('molecules-primary-nav', patternlab); - //assert - test.equals(result, patternlab.patterns[1]); - test.done(); - }, - 'addPattern - adds pattern extended template to patternlab partial object' : function(test){ - //arrange - var pattern_assembler = new pa(); - var patternlab = {}; - patternlab.patterns = []; - patternlab.partials = {}; - patternlab.data = {link: {}}; - patternlab.config = { debug: false }; - patternlab.config.outputFileSuffixes = {rendered : ''}; - - var pattern = new Pattern('00-test/01-bar.mustache'); - pattern.extendedTemplate = 'barExtended'; - pattern.template = 'bar'; - - //act - pattern_assembler.addPattern(pattern, patternlab); - - //assert - test.equals(patternlab.patterns.length, 1); - test.equals(patternlab.partials['test-bar'] != undefined, true); - test.equals(patternlab.partials['test-bar'], 'barExtended'); - test.done(); - }, - 'addPattern - adds pattern template to patternlab partial object if extendedtemplate does not exist yet' : function(test){ - //arrange - var pattern_assembler = new pa(); - var patternlab = {}; - patternlab.patterns = []; - patternlab.partials = {}; - patternlab.data = {link: {}}; - patternlab.config = { debug: false }; - patternlab.config.outputFileSuffixes = {rendered : ''}; - - var pattern = new Pattern('00-test/01-bar.mustache'); - pattern.extendedTemplate = undefined; - pattern.template = 'bar'; - - //act - pattern_assembler.addPattern(pattern, patternlab); - - //assert - test.equals(patternlab.patterns.length, 1); - test.equals(patternlab.partials['test-bar'] != undefined, true); - test.equals(patternlab.partials['test-bar'], 'bar'); - test.done(); - } - }; -})(); + outputFileSuffixes: { + rendered: '' + } + }; + pl.data = {}; + pl.data.link = {}; + pl.config.debug = false; + pl.patterns = []; + pl.partials = {}; + + var atomPattern = new Pattern('00-test/03-styled-atom.mustache'); + atomPattern.template = fs.readFileSync(patterns_dir + '/00-test/03-styled-atom.mustache', 'utf8'); + atomPattern.stylePartials = pattern_assembler.find_pattern_partials_with_style_modifiers(atomPattern); + atomPattern.parameteredPartials = pattern_assembler.find_pattern_partials_with_parameters(atomPattern); + + var groupPattern = new Pattern('00-test/10-multiple-classes-numeric.mustache'); + groupPattern.template = fs.readFileSync(patterns_dir + '/00-test/10-multiple-classes-numeric.mustache', 'utf8'); + groupPattern.stylePartials = pattern_assembler.find_pattern_partials_with_style_modifiers(groupPattern); + groupPattern.parameteredPartials = pattern_assembler.find_pattern_partials_with_parameters(groupPattern); + + pattern_assembler.addPattern(atomPattern, pl); + pattern_assembler.addPattern(groupPattern, pl); + + //act + pattern_assembler.process_pattern_recursive('00-test' + path.sep + '10-multiple-classes-numeric.mustache', pl, {}); + + //assert + var expectedValue = '
{{message}} {{message}} bar
'; + test.equals(groupPattern.extendedTemplate.replace(/\s\s+/g, ' ').replace(/\n/g, ' ').trim(), expectedValue.trim()); + test.end(); +}); + +tap.test('processPatternRecursive - correctly ignores a partial without a style modifier when the same partial later has a style modifier', function(test) { + //arrange + var fs = require('fs-extra'); + var pattern_assembler = new pa(); + var patterns_dir = './test/files/_patterns'; + + var pl = {}; + pl.config = { + paths: { + source: { + patterns: patterns_dir + } + }, + outputFileSuffixes: { + rendered: '' + } + }; + pl.data = {}; + pl.data.link = {}; + pl.config.debug = false; + pl.patterns = []; + pl.partials = {}; + + var atomPattern = new Pattern('00-test/03-styled-atom.mustache'); + atomPattern.template = fs.readFileSync(patterns_dir + '/00-test/03-styled-atom.mustache', 'utf8'); + atomPattern.stylePartials = pattern_assembler.find_pattern_partials_with_style_modifiers(atomPattern); + + var mixedPattern = new Pattern('00-test/06-mixed.mustache'); + mixedPattern.template = fs.readFileSync(patterns_dir + '/00-test/06-mixed.mustache', 'utf8'); + mixedPattern.stylePartials = pattern_assembler.find_pattern_partials_with_style_modifiers(mixedPattern); + + pattern_assembler.addPattern(atomPattern, pl); + pattern_assembler.addPattern(mixedPattern, pl); + + //act + pattern_assembler.process_pattern_recursive('00-test' + path.sep + '06-mixed.mustache', pl, {}); + + //assert. here we expect {{styleModifier}} to be in the first group, since it was not replaced by anything. rendering with data will then remove this (correctly) + var expectedValue = '
{{message}} {{message}} {{message}} {{message}}
'; + test.equals(mixedPattern.extendedTemplate.replace(/\s\s+/g, ' ').replace(/\n/g, ' ').trim(), expectedValue.trim()); + test.end(); +}); + +tap.test('processPatternRecursive - correctly ignores bookended partials without a style modifier when the same partial has a style modifier between', function(test) { + //arrange + var fs = require('fs-extra'); + var pattern_assembler = new pa(); + var patterns_dir = './test/files/_patterns'; + + var pl = {}; + pl.config = { + paths: { + source: { + patterns: patterns_dir + } + }, + outputFileSuffixes: { + rendered: '' + } + }; + pl.data = {}; + pl.data.link = {}; + pl.config.debug = false; + pl.patterns = []; + pl.partials = {}; + + var atomPattern = new Pattern('00-test/03-styled-atom.mustache'); + atomPattern.template = fs.readFileSync(patterns_dir + '/00-test/03-styled-atom.mustache', 'utf8'); + atomPattern.stylePartials = pattern_assembler.find_pattern_partials_with_style_modifiers(atomPattern); + + var bookendPattern = new Pattern('00-test/09-bookend.mustache'); + bookendPattern.template = fs.readFileSync(patterns_dir + '/00-test/09-bookend.mustache', 'utf8'); + bookendPattern.stylePartials = pattern_assembler.find_pattern_partials_with_style_modifiers(bookendPattern); + + pattern_assembler.addPattern(atomPattern, pl); + pattern_assembler.addPattern(bookendPattern, pl); + + //act + pattern_assembler.process_pattern_recursive('00-test' + path.sep + '09-bookend.mustache', pl, {}); + + //assert. here we expect {{styleModifier}} to be in the first and last group, since it was not replaced by anything. rendering with data will then remove this (correctly) + var expectedValue = '
{{message}} {{message}} {{message}} {{message}}
'; + var actualValue = bookendPattern.extendedTemplate.replace(/\s\s+/g, ' ').replace(/\n/g, ' '); + test.equals(actualValue.trim(), expectedValue.trim(), 'actual value:\n' + actualValue + '\nexpected value:\n' + expectedValue); + test.end(); +}); + +tap.test('processPatternRecursive - correctly ignores a partial without a style modifier when the same partial later has a style modifier and pattern parameters', function(test) { + //arrange + var fs = require('fs-extra'); + var pattern_assembler = new pa(); + var patterns_dir = './test/files/_patterns'; + + var pl = {}; + pl.config = { + paths: { + source: { + patterns: patterns_dir + } + }, + outputFileSuffixes: { + rendered: '' + } + }; + pl.data = {}; + pl.data.link = {}; + pl.config.debug = false; + pl.patterns = []; + pl.partials = {}; + + var atomPattern = new Pattern('00-test/03-styled-atom.mustache'); + atomPattern.template = fs.readFileSync(patterns_dir + '/00-test/03-styled-atom.mustache', 'utf8'); + atomPattern.stylePartials = pattern_assembler.find_pattern_partials_with_style_modifiers(atomPattern); + atomPattern.parameteredPartials = pattern_assembler.find_pattern_partials_with_parameters(atomPattern); + + var mixedPattern = new Pattern('00-test/07-mixed-params.mustache'); + mixedPattern.template = fs.readFileSync(patterns_dir + '/00-test/07-mixed-params.mustache', 'utf8'); + mixedPattern.stylePartials = pattern_assembler.find_pattern_partials_with_style_modifiers(mixedPattern); + mixedPattern.parameteredPartials = pattern_assembler.find_pattern_partials_with_parameters(mixedPattern); + + pattern_assembler.addPattern(atomPattern, pl); + pattern_assembler.addPattern(mixedPattern, pl); + + //act + pattern_assembler.process_pattern_recursive('00-test' + path.sep + '07-mixed-params.mustache', pl, {}); + + //assert. here we expect {{styleModifier}} to be in the first span, since it was not replaced by anything. rendering with data will then remove this (correctly) + var expectedValue = '
{{message}} 2 3 4
'; + test.equals(mixedPattern.extendedTemplate.replace(/\s\s+/g, ' ').replace(/\n/g, ' ').trim(), expectedValue.trim()); + test.end(); +}); + +tap.test('processPatternRecursive - correctly ignores bookended partials without a style modifier when the same partial has a style modifier and pattern parameters between', function(test) { + //arrange + var fs = require('fs-extra'); + var pattern_assembler = new pa(); + var patterns_dir = './test/files/_patterns'; + + var pl = {}; + pl.config = { + paths: { + source: { + patterns: patterns_dir + } + }, + outputFileSuffixes: { + rendered: '' + } + }; + pl.data = {}; + pl.data.link = {}; + pl.config.debug = false; + pl.patterns = []; + pl.partials = {}; + + var atomPattern = new Pattern('00-test/03-styled-atom.mustache'); + atomPattern.template = fs.readFileSync(patterns_dir + '/00-test/03-styled-atom.mustache', 'utf8'); + atomPattern.stylePartials = pattern_assembler.find_pattern_partials_with_style_modifiers(atomPattern); + atomPattern.parameteredPartials = pattern_assembler.find_pattern_partials_with_parameters(atomPattern); + + var bookendPattern = new Pattern('00-test/08-bookend-params.mustache'); + bookendPattern.template = fs.readFileSync(patterns_dir + '/00-test/08-bookend-params.mustache', 'utf8'); + bookendPattern.stylePartials = pattern_assembler.find_pattern_partials_with_style_modifiers(bookendPattern); + bookendPattern.parameteredPartials = pattern_assembler.find_pattern_partials_with_parameters(bookendPattern); + + pattern_assembler.addPattern(atomPattern, pl); + pattern_assembler.addPattern(bookendPattern, pl); + + //act + pattern_assembler.process_pattern_recursive('00-test' + path.sep + '08-bookend-params.mustache', pl, {}); + + //assert. here we expect {{styleModifier}} to be in the first and last span, since it was not replaced by anything. rendering with data will then remove this (correctly) + var expectedValue = '
{{message}} 2 3 {{message}}
'; + test.equals(bookendPattern.extendedTemplate.replace(/\s\s+/g, ' ').replace(/\n/g, ' ').trim(), expectedValue.trim()); + test.end(); +}); + +tap.test('processPatternRecursive - does not pollute previous patterns when a later one is found with a styleModifier', function(test) { + //arrange + var fs = require('fs-extra'); + var pattern_assembler = new pa(); + var patterns_dir = './test/files/_patterns'; + + var pl = {}; + pl.config = { + paths: { + source: { + patterns: patterns_dir + } + }, + outputFileSuffixes: { + rendered: '' + } + }; + pl.data = {}; + pl.data.link = {}; + pl.config.debug = false; + pl.patterns = []; + pl.partials = {}; + + var atomPattern = new Pattern('00-test/03-styled-atom.mustache'); + atomPattern.template = fs.readFileSync(patterns_dir + '/00-test/03-styled-atom.mustache', 'utf8'); + atomPattern.stylePartials = pattern_assembler.find_pattern_partials_with_style_modifiers(atomPattern); + atomPattern.parameteredPartials = pattern_assembler.find_pattern_partials_with_parameters(atomPattern); + + var anotherPattern = new Pattern('00-test/12-another-styled-atom.mustache'); + anotherPattern.template = fs.readFileSync(patterns_dir + '/00-test/12-another-styled-atom.mustache', 'utf8'); + anotherPattern.stylePartials = pattern_assembler.find_pattern_partials_with_style_modifiers(anotherPattern); + anotherPattern.parameteredPartials = pattern_assembler.find_pattern_partials_with_parameters(anotherPattern); + + pattern_assembler.addPattern(atomPattern, pl); + pattern_assembler.addPattern(anotherPattern, pl); + + //act + pattern_assembler.process_pattern_recursive('00-test' + path.sep + '12-another-styled-atom.mustache', pl, {}); + + //assert + var expectedCleanValue = ' {{message}} '; + var expectedSetValue = ' {{message}} '; + + //this is the "atom" - it should remain unchanged + test.equals(atomPattern.template.replace(/\s\s+/g, ' ').replace(/\n/g, ' ').trim(), expectedCleanValue.trim()); + test.equals(atomPattern.extendedTemplate.replace(/\s\s+/g, ' ').replace(/\n/g, ' ').trim(), expectedCleanValue.trim()); + + // this is the style modifier pattern, which should resolve correctly + test.equals(anotherPattern.template.replace(/\s\s+/g, ' ').replace(/\n/g, ' ').trim(), '{{> test-styled-atom:test_1 }}'); + test.equals(anotherPattern.extendedTemplate.replace(/\s\s+/g, ' ').replace(/\n/g, ' ').trim(), expectedSetValue.trim()); + + test.end(); +}); + +tap.test('processPatternRecursive - ensure deep-nesting works', function(test) { + //arrange + var fs = require('fs-extra'); + var pattern_assembler = new pa(); + var patterns_dir = './test/files/_patterns'; + + var pl = {}; + pl.config = { + paths: { + source: { + patterns: patterns_dir + } + }, + outputFileSuffixes: { + rendered: '' + } + }; + pl.data = {}; + pl.data.link = {}; + pl.config.debug = false; + pl.patterns = []; + pl.partials = {}; + + var atomPattern = new Pattern('00-test/01-bar.mustache'); + atomPattern.template = fs.readFileSync(patterns_dir + '/00-test/01-bar.mustache', 'utf8'); + atomPattern.stylePartials = pattern_assembler.find_pattern_partials_with_style_modifiers(atomPattern); + atomPattern.parameteredPartials = pattern_assembler.find_pattern_partials_with_parameters(atomPattern); + + var templatePattern = new Pattern('00-test/00-foo.mustache'); + templatePattern.template = fs.readFileSync(patterns_dir + '/00-test/00-foo.mustache', 'utf8'); + templatePattern.stylePartials = pattern_assembler.find_pattern_partials_with_style_modifiers(templatePattern); + templatePattern.parameteredPartials = pattern_assembler.find_pattern_partials_with_parameters(templatePattern); + + var pagesPattern = new Pattern('00-test/14-inception.mustache'); + pagesPattern.template = fs.readFileSync(patterns_dir + '/00-test/14-inception.mustache', 'utf8'); + pagesPattern.stylePartials = pattern_assembler.find_pattern_partials_with_style_modifiers(pagesPattern); + pagesPattern.parameteredPartials = pattern_assembler.find_pattern_partials_with_parameters(pagesPattern); + + pattern_assembler.addPattern(atomPattern, pl); + pattern_assembler.addPattern(templatePattern, pl); + pattern_assembler.addPattern(pagesPattern, pl); + + //act + pattern_assembler.process_pattern_recursive('00-test' + path.sep + '14-inception.mustache', pl, {}); + + //assert + var expectedCleanValue = 'bar'; + var expectedSetValue = 'bar'; + + //this is the "atom" - it should remain unchanged + test.equals(atomPattern.template.replace(/\s\s+/g, ' ').replace(/\n/g, ' ').trim(), expectedCleanValue); + test.equals(atomPattern.extendedTemplate.replace(/\s\s+/g, ' ').replace(/\n/g, ' ').trim(), expectedCleanValue); + + //this is the "template pattern" - it should have an updated extendedTemplate but an unchanged template + test.equals(templatePattern.template.replace(/\s\s+/g, ' ').replace(/\n/g, ' ').trim(), '{{> test-bar }}'); + test.equals(templatePattern.extendedTemplate.replace(/\s\s+/g, ' ').replace(/\n/g, ' ').trim(), expectedSetValue.trim()); + + //this is the "pages pattern" - it should have an updated extendedTemplate equal to the template pattern but an unchanged template + test.equals(pagesPattern.template.replace(/\s\s+/g, ' ').replace(/\n/g, ' ').trim(), '{{> test-foo }}'); + test.equals(pagesPattern.extendedTemplate.replace(/\s\s+/g, ' ').replace(/\n/g, ' ').trim(), expectedSetValue.trim()); + test.end(); +}); + +tap.test('setState - applies any patternState matching the pattern', function(test) { + //arrange + var pa = require('../core/lib/pattern_assembler'); + var pattern_assembler = new pa(); + var patternlab = {}; + patternlab.config = {}; + patternlab.config.patternStates = {}; + patternlab.config.patternStates["pages-homepage-emergency"] = "inprogress"; + + var pattern = { + patternPartial: "pages-homepage-emergency" + }; + + //act + pattern_assembler.setPatternState(pattern, patternlab); + + //assert + test.equals(pattern.patternState, "inprogress"); + test.end(); +}); + +tap.test('setState - does not apply any patternState if nothing matches the pattern', function(test) { + //arrange + var pa = require('../core/lib/pattern_assembler'); + var pattern_assembler = new pa(); + var patternlab = {}; + patternlab.config = {}; + patternlab.config.patternStates = {}; + patternlab.config.patternStates["pages-homepage-emergency"] = "inprogress"; + + var pattern = { + key: "pages-homepage", + patternState: "" + }; + + //act + pattern_assembler.setPatternState(pattern, patternlab); + + //assert + test.equals(pattern.patternState, ""); + test.end(); +}); + +tap.test('parseDataLinks - replaces found link.* data for their expanded links', function(test) { + //arrange + var diveSync = require('diveSync'); + var fs = require('fs-extra'); + var pa = require('../core/lib/pattern_assembler'); + var plMain = require('../core/lib/patternlab'); + var pattern_assembler = new pa(); + var patterns_dir = './test/files/_patterns/'; + var patternlab = {}; + //THIS IS BAD + patternlab.config = fs.readJSONSync('./patternlab-config.json'); + patternlab.config.paths.source.patterns = patterns_dir; + patternlab.config.outputFileSuffixes = {rendered: ''}; + patternlab.data = {}; + patternlab.listitems = {}; + patternlab.header = {}; + patternlab.footer = {}; + //patternlab.data = fs.readJSONSync(path.resolve(patternlab.config.paths.source.data, 'data.json')); + //patternlab.listitems = fs.readJSONSync(path.resolve(patternlab.config.paths.source.data, 'listitems.json')); + //patternlab.header = fs.readFileSync(path.resolve(patternlab.config.paths.source.patternlabFiles, 'templates/pattern-header-footer/header.html'), 'utf8'); + //patternlab.footer = fs.readFileSync(path.resolve(patternlab.config.paths.source.patternlabFiles, 'templates/pattern-header-footer/footer.html'), 'utf8'); + patternlab.patterns = [ + { + patternPartial: 'twitter-brad' + }, + { + patternPartial: 'twitter-dave' + }, + { + patternPartial: 'twitter-brian' + } + ]; + patternlab.data.link = {}; + patternlab.partials = {}; + + //diveSync once to perform iterative populating of patternlab object + plMain.process_all_patterns_iterative(pattern_assembler, patterns_dir, patternlab); + + //for the sake of the test, also imagining I have the following pages... + patternlab.data.link['twitter-brad'] = 'https://twitter.com/brad_frost'; + patternlab.data.link['twitter-dave'] = 'https://twitter.com/dmolsen'; + patternlab.data.link['twitter-brian'] = 'https://twitter.com/bmuenzenmeyer'; + + patternlab.data.brad = {url: "link.twitter-brad"}; + patternlab.data.dave = {url: "link.twitter-dave"}; + patternlab.data.brian = {url: "link.twitter-brian"}; + + + var pattern; + for (var i = 0; i < patternlab.patterns.length; i++) { + if (patternlab.patterns[i].patternPartial === 'test-nav') { + pattern = patternlab.patterns[i]; + } + } + + //assert before + test.equals(pattern.jsonFileData.brad.url, "link.twitter-brad", "brad pattern data should be found"); + test.equals(pattern.jsonFileData.dave.url, "link.twitter-dave", "dave pattern data should be found"); + test.equals(pattern.jsonFileData.brian.url, "link.twitter-brian", "brian pattern data should be found"); + + //act + pattern_assembler.parse_data_links(patternlab); + + //assert after + test.equals(pattern.jsonFileData.brad.url, "https://twitter.com/brad_frost", "brad pattern data should be replaced"); + test.equals(pattern.jsonFileData.dave.url, "https://twitter.com/dmolsen", "dave pattern data should be replaced"); + test.equals(pattern.jsonFileData.brian.url, "https://twitter.com/bmuenzenmeyer", "brian pattern data should be replaced"); + + test.equals(patternlab.data.brad.url, "https://twitter.com/brad_frost", "global brad data should be replaced"); + test.equals(patternlab.data.dave.url, "https://twitter.com/dmolsen", "global dave data should be replaced"); + test.equals(patternlab.data.brian.url, "https://twitter.com/bmuenzenmeyer", "global brian data should be replaced"); + test.end(); +}); + +tap.test('get_pattern_by_key - returns the fuzzy result when no others found', function(test) { + //arrange + var pattern_assembler = new pa(); + var patternlab = {}; + patternlab.patterns = []; + + patternlab.patterns.push({ + patternPartial: 'character-han-solo', + subdir: 'character', + fileName: 'han-solo' + }); + + //act + var result = pattern_assembler.getPartial('character-han', patternlab); + //assert + test.equals(result, patternlab.patterns[0]); + test.end(); +}); + +tap.test('get_pattern_by_key - returns the exact key if found', function(test) { + //arrange + var pattern_assembler = new pa(); + var patternlab = {}; + patternlab.patterns = []; + + patternlab.patterns.push({ + patternPartial: 'molecules-primary-nav-jagged', + subdir: 'molecules', + fileName: 'primary-nav-jagged' + }, { + patternPartial: 'molecules-primary-nav', + subdir: 'molecules', + fileName: 'molecules-primary-nav' + }); + + //act + var result = pattern_assembler.getPartial('molecules-primary-nav', patternlab); + //assert + test.equals(result, patternlab.patterns[1]); + test.end(); +}); + +tap.test('addPattern - adds pattern extended template to patternlab partial object', function(test) { + //arrange + var pattern_assembler = new pa(); + var patternlab = {}; + patternlab.patterns = []; + patternlab.partials = {}; + patternlab.data = {link: {}}; + patternlab.config = {debug: false}; + patternlab.config.outputFileSuffixes = {rendered: ''}; + + var pattern = new Pattern('00-test/01-bar.mustache'); + pattern.extendedTemplate = 'barExtended'; + pattern.template = 'bar'; + + //act + pattern_assembler.addPattern(pattern, patternlab); + + //assert + test.equals(patternlab.patterns.length, 1); + test.equals(patternlab.partials['test-bar'] != undefined, true); + test.equals(patternlab.partials['test-bar'], 'barExtended'); + test.end(); +}); + +tap.test('addPattern - adds pattern template to patternlab partial object if extendedtemplate does not exist yet', function(test){ + //arrange + var pattern_assembler = new pa(); + var patternlab = {}; + patternlab.patterns = []; + patternlab.partials = {}; + patternlab.data = {link: {}}; + patternlab.config = { debug: false }; + patternlab.config.outputFileSuffixes = {rendered : ''}; + + var pattern = new Pattern('00-test/01-bar.mustache'); + pattern.extendedTemplate = undefined; + pattern.template = 'bar'; + + //act + pattern_assembler.addPattern(pattern, patternlab); + + //assert + test.equals(patternlab.patterns.length, 1); + test.equals(patternlab.partials['test-bar'] != undefined, true); + test.equals(patternlab.partials['test-bar'], 'bar'); + 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/pattern_engines_tests.js b/test/pattern_engines_tests.js index fbf97c74c..dc55b6cf0 100644 --- a/test/pattern_engines_tests.js +++ b/test/pattern_engines_tests.js @@ -1,164 +1,164 @@ -(function () { - 'use strict'; - - var patternEngines = require('../core/lib/pattern_engines'); - var Pattern = require('../core/lib/object_factory').Pattern; - - // the mustache test pattern, stolen from object_factory unit tests - var mustacheTestPattern = new Pattern('source/_patterns/00-atoms/00-global/00-colors-alt.mustache', {d: 123}); - var mustacheTestPseudoPatternBasePattern = new Pattern('source/_patterns/04-pages/00-homepage.mustache', {d: 123}); - var mustacheTestPseudoPattern = new Pattern('source/_patterns/04-pages/00-homepage~emergency.json', {d: 123}); - mustacheTestPseudoPattern.isPseudoPattern = true; - mustacheTestPseudoPattern.basePattern = mustacheTestPseudoPatternBasePattern; - var engineNames = Object.keys(patternEngines); - - - exports['patternEngines support functions'] = { - 'getEngineNameForPattern returns "mustache" from test pattern': function (test) { - var engineName = patternEngines.getEngineNameForPattern(mustacheTestPattern); - test.equals(engineName, 'mustache'); - test.done(); - }, - 'getEngineNameForPattern returns "mustache" for a plain string template as a backwards compatibility measure': function (test) { - test.expect(1); - test.equals(patternEngines.getEngineNameForPattern('plain text string'), 'mustache'); - test.done(); - }, - 'getEngineNameForPattern returns "mustache" for an artificial empty template': function (test) { - test.expect(1); - var emptyPattern = Pattern.createEmpty(); - test.equals(patternEngines.getEngineNameForPattern(emptyPattern), 'mustache'); - test.done(); - }, - 'getEngineForPattern returns a reference to the mustache engine from test pattern': function (test) { - var engine = patternEngines.getEngineForPattern(mustacheTestPattern); - test.equals(engine, patternEngines.mustache); - test.done(); - }, - 'getEngineForPattern returns a reference to the mustache engine from test pseudo-pattern': function (test) { - var engine = patternEngines.getEngineForPattern(mustacheTestPseudoPattern); - test.equals(engine, patternEngines.mustache); - test.done(); - }, - 'isPseudoPatternJSON correctly identifies pseudo-pattern JSON filenames': function(test) { - // each test case - var filenames = { - '00-homepage~emergency.json': true, - '~emergency.json': true, - '00-homepage~emergency.js': false, - '00-homepage-emergency.js': false, - '00-homepage.hbs': false, - '00-homepage.json': false, - 'greatpic.jpg': false - }; - // expect one test per test case - test.expect(Object.keys(filenames).length); - - // loop over each test case and test it - Object.keys(filenames).forEach(function (filename) { - var expectedResult = filenames[filename], - actualResult = patternEngines.isPseudoPatternJSON(filename), - testMessage = 'isPseudoPatternJSON should return ' + expectedResult + ' for ' + filename; - test.strictEqual(actualResult, expectedResult, testMessage); - }); - - // done - test.done(); - }, - 'isPatternFile correctly identifies pattern files and rejects non-pattern files': function(test){ - // each test case - var filenames = { - '00-comment-thread.mustache': true, - '00-comment-thread.fakeextthatdoesntexist': false, - '00-comment-thread': false, - '_00-comment-thread.mustache': true, - '.00-comment-thread.mustache': false, - '00-comment-thread.json': false, - '00-homepage~emergency.json': true - }; - // expect one test per test case - test.expect(Object.keys(filenames).length); - - // loop over each test case and test it - Object.keys(filenames).forEach(function (filename) { - var expectedResult = filenames[filename], - actualResult = patternEngines.isPatternFile(filename), - testMessage = 'isPatternFile should return ' + expectedResult + ' for ' + filename; - test.strictEqual(actualResult, expectedResult, testMessage); - }); - - // done - test.done(); - } +'use strict'; + +var tap = require('tap'); + +var patternEngines = require('../core/lib/pattern_engines'); +var Pattern = require('../core/lib/object_factory').Pattern; + +// the mustache test pattern, stolen from object_factory unit tests +var mustacheTestPattern = new Pattern('source/_patterns/00-atoms/00-global/00-colors-alt.mustache', {d: 123}); +var mustacheTestPseudoPatternBasePattern = new Pattern('source/_patterns/04-pages/00-homepage.mustache', {d: 123}); +var mustacheTestPseudoPattern = new Pattern('source/_patterns/04-pages/00-homepage~emergency.json', {d: 123}); +mustacheTestPseudoPattern.isPseudoPattern = true; +mustacheTestPseudoPattern.basePattern = mustacheTestPseudoPatternBasePattern; +var engineNames = Object.keys(patternEngines); + +tap.test('getEngineNameForPattern returns "mustache" from test pattern', function (test) { + var engineName = patternEngines.getEngineNameForPattern(mustacheTestPattern); + test.equals(engineName, 'mustache'); + test.end(); +}); + +tap.test('getEngineNameForPattern returns "mustache" for a plain string template as a backwards compatibility measure', function (test) { + test.plan(1); + test.equals(patternEngines.getEngineNameForPattern('plain text string'), 'mustache'); + test.end(); +}); + +tap.test('getEngineNameForPattern returns "mustache" for an artificial empty template', function (test) { + test.plan(1); + var emptyPattern = Pattern.createEmpty(); + test.equals(patternEngines.getEngineNameForPattern(emptyPattern), 'mustache'); + test.end(); +}); + +tap.test('getEngineForPattern returns a reference to the mustache engine from test pattern', function (test) { + var engine = patternEngines.getEngineForPattern(mustacheTestPattern); + test.equals(engine, patternEngines.mustache); + test.end(); +}); + +tap.test('getEngineForPattern returns a reference to the mustache engine from test pseudo-pattern', function (test) { + var engine = patternEngines.getEngineForPattern(mustacheTestPseudoPattern); + test.equals(engine, patternEngines.mustache); + test.end(); +}); + +tap.test('isPseudoPatternJSON correctly identifies pseudo-pattern JSON filenames', function(test) { + // each test case + var filenames = { + '00-homepage~emergency.json': true, + '~emergency.json': true, + '00-homepage~emergency.js': false, + '00-homepage-emergency.js': false, + '00-homepage.hbs': false, + '00-homepage.json': false, + 'greatpic.jpg': false + }; + // expect one test per test case + test.plan(Object.keys(filenames).length); + + // loop over each test case and test it + Object.keys(filenames).forEach(function (filename) { + var expectedResult = filenames[filename], + actualResult = patternEngines.isPseudoPatternJSON(filename), + testMessage = 'isPseudoPatternJSON should return ' + expectedResult + ' for ' + filename; + test.strictEqual(actualResult, expectedResult, testMessage); + }); + + // done + test.end(); +}); + +tap.test('isPatternFile correctly identifies pattern files and rejects non-pattern files', function(test){ + // each test case + var filenames = { + '00-comment-thread.mustache': true, + '00-comment-thread.fakeextthatdoesntexist': false, + '00-comment-thread': false, + '_00-comment-thread.mustache': true, + '.00-comment-thread.mustache': false, + '00-comment-thread.json': false, + '00-homepage~emergency.json': true }; + // expect one test per test case + test.plan(Object.keys(filenames).length); + + // loop over each test case and test it + Object.keys(filenames).forEach(function (filename) { + var expectedResult = filenames[filename], + actualResult = patternEngines.isPatternFile(filename), + testMessage = 'isPatternFile should return ' + expectedResult + ' for ' + filename; + test.strictEqual(actualResult, expectedResult, testMessage); + }); + + // done + test.end(); +}); + +// testProps() utility function: given an object, and a hash of expected +// 'property name':'property type' pairs, verify that the object contains each +// expected property, and that each property is of the expected type. +function testProps(object, propTests, test) { - // testProps() utility function: given an object, and a hash of expected - // 'property name':'property type' pairs, verify that the object contains each - // expected property, and that each property is of the expected type. - function testProps(object, propTests, test) { - - // function to test each expected property is present and the correct type - function testProp(propName, types) { - - var possibleTypes; - - // handle "types" being a string or an array of strings - if (types instanceof Array) { - possibleTypes = types; - } else { - // "types" is just a single string, load it into an array; the rest of - // the code expects it! - possibleTypes = [types]; - } - - var isOneOfTheseTypes = possibleTypes.map(function (type) { - return typeof object[propName] === type; - }).reduce(function(isPrevType, isCurrentType) { - return isPrevType || isCurrentType; - }); - - test.ok(object.hasOwnProperty(propName), '"' + propName + '" prop should be present'); - test.ok(isOneOfTheseTypes, '"' + propName + '" prop should be one of types ' + possibleTypes); + // function to test each expected property is present and the correct type + function testProp(propName, types) { + + var possibleTypes; + + // handle "types" being a string or an array of strings + if (types instanceof Array) { + possibleTypes = types; + } else { + // "types" is just a single string, load it into an array; the rest of + // the code expects it! + possibleTypes = [types]; } - // go over each property test and run it - Object.keys(propTests).forEach(function (propName) { - var propType = propTests[propName]; - testProp(propName, propType); + var isOneOfTheseTypes = possibleTypes.map(function (type) { + return typeof object[propName] === type; + }).reduce(function(isPrevType, isCurrentType) { + return isPrevType || isCurrentType; }); + + test.ok(object.hasOwnProperty(propName), '"' + propName + '" prop should be present'); + test.ok(isOneOfTheseTypes, '"' + propName + '" prop should be one of types ' + possibleTypes); } - exports['patternEngines initialization'] = { - 'patternEngines object contains at least the default mustache engine': function (test) { - test.expect(1); - test.ok(patternEngines.hasOwnProperty('mustache')); - test.done(); - }, - 'patternEngines object reports that it supports the .mustache extension': function (test) { - test.expect(1); - test.ok(patternEngines.isFileExtensionSupported('.mustache')); - test.done(); - } - }; + // go over each property test and run it + Object.keys(propTests).forEach(function (propName) { + var propType = propTests[propName]; + testProp(propName, propType); + }); +} + +tap.test('patternEngines object contains at least the default mustache engine', function (test) { + test.plan(1); + test.ok(patternEngines.hasOwnProperty('mustache')); + test.end(); +}); + +tap.test('patternEngines object reports that it supports the .mustache extension', function (test) { + test.plan(1); + test.ok(patternEngines.isFileExtensionSupported('.mustache')); + test.end(); +}); + +// make one big test group for each pattern engine +engineNames.forEach(function (engineName) { + tap.test('engine ' + engineName + ' contains expected properties and methods', function (test) { + + var propertyTests = { + 'engine': ['object', 'function'], + 'engineName': 'string', + 'engineFileExtension': 'string', + 'renderPattern': 'function', + 'findPartials': 'function' + }; - // make one big test group for each pattern engine - engineNames.forEach(function (engineName) { - exports[engineName + ' patternEngine'] = { - 'engine contains expected properties and methods': function (test) { - - var propertyTests = { - 'engine': ['object', 'function'], - 'engineName': 'string', - 'engineFileExtension': 'string', - 'renderPattern': 'function', - 'findPartials': 'function' - }; - - test.expect(Object.keys(propertyTests).length * 2); - testProps(patternEngines[engineName], propertyTests, test); - test.done(); - } - }; + test.plan(Object.keys(propertyTests).length * 2); + testProps(patternEngines[engineName], propertyTests, test); + test.end(); }); +}); -})(); diff --git a/test/patternlab_tests.js b/test/patternlab_tests.js index b33144772..84eb81d2f 100644 --- a/test/patternlab_tests.js +++ b/test/patternlab_tests.js @@ -1,15 +1,15 @@ 'use strict'; -exports['test nodeunit'] = { - 'buildPatternData - should merge all JSON files in the data folder except listitems' : function(test){ - var fs = require('fs-extra'); - var plMain = require('../core/lib/patternlab'); - var data_dir = './test/files/_data/'; +var tap = require('tap'); - var dataResult = plMain.build_pattern_data(data_dir, fs); - test.equals(dataResult.data, "test"); - test.equals(dataResult.foo, "bar"); - test.equals(dataResult.test_list_item, undefined); - test.done(); - } -}; +tap.test('buildPatternData - should merge all JSON files in the data folder except listitems', function(test){ + var fs = require('fs-extra'); + var plMain = require('../core/lib/patternlab'); + var data_dir = './test/files/_data/'; + + var dataResult = plMain.build_pattern_data(data_dir, fs); + test.equals(dataResult.data, "test"); + test.equals(dataResult.foo, "bar"); + test.equals(dataResult.test_list_item, undefined); + test.end(); +}); diff --git a/test/pseudopattern_hunter_tests.js b/test/pseudopattern_hunter_tests.js index dd9b922ce..80bce5b61 100644 --- a/test/pseudopattern_hunter_tests.js +++ b/test/pseudopattern_hunter_tests.js @@ -1,5 +1,7 @@ "use strict"; +var tap = require('tap'); + var path = require('path'); var pha = require('../core/lib/pseudopattern_hunter'); var pa = require('../core/lib/pattern_assembler'); @@ -30,59 +32,57 @@ function stubPatternlab() { return pl; } -exports['pseudopattern_hunter'] = { - 'pseudpattern found and added as a pattern' : function (test) { - //arrange - var pl = stubPatternlab(); - - var atomPattern = new Pattern('00-test/03-styled-atom.mustache'); - atomPattern.template = fs.readFileSync(patterns_dir + '00-test/03-styled-atom.mustache', 'utf8'); - atomPattern.extendedTemplate = atomPattern.template; - atomPattern.stylePartials = pattern_assembler.find_pattern_partials_with_style_modifiers(atomPattern); - - pattern_assembler.addPattern(atomPattern, pl); - - //act - var patternCountBefore = pl.patterns.length; - pseudopattern_hunter.find_pseudopatterns(atomPattern, pl); - - //assert - test.equals(patternCountBefore + 1, pl.patterns.length); - test.equals(pl.patterns[1].patternPartial, 'test-styled-atom-alt'); - test.equals(pl.patterns[1].extendedTemplate.replace(/\s\s+/g, ' ').replace(/\n/g, ' ').trim(), ' {{message}} '); - test.equals(JSON.stringify(pl.patterns[1].jsonFileData), JSON.stringify({"message": "alternateMessage"})); - test.equals(pl.patterns[1].patternLink, '00-test-03-styled-atom-alt' + path.sep + '00-test-03-styled-atom-alt.html'); - - test.done(); - }, - - 'pseudpattern variant includes stylePartials and parameteredPartials' : function (test) { - //arrange - var pl = stubPatternlab(); - - var atomPattern = new Pattern('00-test/03-styled-atom.mustache'); - atomPattern.template = fs.readFileSync(patterns_dir + '00-test/03-styled-atom.mustache', 'utf8'); - atomPattern.extendedTemplate = atomPattern.template; - atomPattern.stylePartials = atomPattern.findPartialsWithStyleModifiers(atomPattern); - atomPattern.parameteredPartials = atomPattern.findPartialsWithPatternParameters(atomPattern); - - var pseudoPattern = new Pattern('00-test/474-pseudomodifier.mustache'); - pseudoPattern.template = fs.readFileSync(patterns_dir + '00-test/474-pseudomodifier.mustache', 'utf8'); - pseudoPattern.extendedTemplate = atomPattern.template; - pseudoPattern.stylePartials = pseudoPattern.findPartialsWithStyleModifiers(pseudoPattern); - pseudoPattern.parameteredPartials = pseudoPattern.findPartialsWithPatternParameters(pseudoPattern); - - pattern_assembler.addPattern(atomPattern, pl); - pattern_assembler.addPattern(pseudoPattern, pl); - - //act - pseudopattern_hunter.find_pseudopatterns(pseudoPattern, pl); - - //assert - test.equals(pl.patterns[2].patternPartial, 'test-pseudomodifier-test'); - test.equals(pl.patterns[2].stylePartials, pseudoPattern.stylePartials); - test.equals(pl.patterns[2].parameteredPartials, pseudoPattern.parameteredPartials); - - test.done(); - } -}; +tap.test('pseudpattern found and added as a pattern', function (test) { + //arrange + var pl = stubPatternlab(); + + var atomPattern = new Pattern('00-test/03-styled-atom.mustache'); + atomPattern.template = fs.readFileSync(patterns_dir + '00-test/03-styled-atom.mustache', 'utf8'); + atomPattern.extendedTemplate = atomPattern.template; + atomPattern.stylePartials = pattern_assembler.find_pattern_partials_with_style_modifiers(atomPattern); + + pattern_assembler.addPattern(atomPattern, pl); + + //act + var patternCountBefore = pl.patterns.length; + pseudopattern_hunter.find_pseudopatterns(atomPattern, pl); + + //assert + test.equals(patternCountBefore + 1, pl.patterns.length); + test.equals(pl.patterns[1].patternPartial, 'test-styled-atom-alt'); + test.equals(pl.patterns[1].extendedTemplate.replace(/\s\s+/g, ' ').replace(/\n/g, ' ').trim(), ' {{message}} '); + test.equals(JSON.stringify(pl.patterns[1].jsonFileData), JSON.stringify({"message": "alternateMessage"})); + test.equals(pl.patterns[1].patternLink, '00-test-03-styled-atom-alt' + path.sep + '00-test-03-styled-atom-alt.html'); + + test.end(); +}); + +tap.test('pseudpattern variant includes stylePartials and parameteredPartials', function (test) { + //arrange + var pl = stubPatternlab(); + + var atomPattern = new Pattern('00-test/03-styled-atom.mustache'); + atomPattern.template = fs.readFileSync(patterns_dir + '00-test/03-styled-atom.mustache', 'utf8'); + atomPattern.extendedTemplate = atomPattern.template; + atomPattern.stylePartials = atomPattern.findPartialsWithStyleModifiers(atomPattern); + atomPattern.parameteredPartials = atomPattern.findPartialsWithPatternParameters(atomPattern); + + var pseudoPattern = new Pattern('00-test/474-pseudomodifier.mustache'); + pseudoPattern.template = fs.readFileSync(patterns_dir + '00-test/474-pseudomodifier.mustache', 'utf8'); + pseudoPattern.extendedTemplate = atomPattern.template; + pseudoPattern.stylePartials = pseudoPattern.findPartialsWithStyleModifiers(pseudoPattern); + pseudoPattern.parameteredPartials = pseudoPattern.findPartialsWithPatternParameters(pseudoPattern); + + pattern_assembler.addPattern(atomPattern, pl); + pattern_assembler.addPattern(pseudoPattern, pl); + + //act + pseudopattern_hunter.find_pseudopatterns(pseudoPattern, pl); + + //assert + test.equals(pl.patterns[2].patternPartial, 'test-pseudomodifier-test'); + test.equals(pl.patterns[2].stylePartials, pseudoPattern.stylePartials); + test.equals(pl.patterns[2].parameteredPartials, pseudoPattern.parameteredPartials); + + test.end(); +}); diff --git a/test/style_modifier_hunter_tests.js b/test/style_modifier_hunter_tests.js index 4e0d7ddf3..e1a999a8b 100644 --- a/test/style_modifier_hunter_tests.js +++ b/test/style_modifier_hunter_tests.js @@ -1,86 +1,89 @@ "use strict"; +var tap = require('tap'); + var smh = require('../core/lib/style_modifier_hunter'); -exports['consume_style_modifier'] = { - 'uses the partial stylemodifer to modify the patterns extendedTemplate' : function(test){ - //arrange - var pl = {}; - pl.partials = {}; - pl.config = {}; - pl.config.debug = false; - - var pattern = { - extendedTemplate: '
' - }; - - var style_modifier_hunter = new smh(); - - //act - style_modifier_hunter.consume_style_modifier(pattern, '{{> partial:bar}}', pl); - - //assert - test.equals(pattern.extendedTemplate, '
'); - test.done(); - }, - 'replaces style modifiers with spaces in the syntax' : function(test){ - //arrange - var pl = {}; - pl.partials = {}; - pl.config = {}; - pl.config.debug = false; - - var pattern = { - extendedTemplate: '
' - }; - - var style_modifier_hunter = new smh(); - - //act - style_modifier_hunter.consume_style_modifier(pattern, '{{> partial:bar}}', pl); - - //assert - test.equals(pattern.extendedTemplate, '
'); - test.done(); - }, - 'replaces multiple style modifiers' : function(test){ - //arrange - var pl = {}; - pl.partials = {}; - pl.config = {}; - pl.config.debug = false; - - var pattern = { - extendedTemplate: '
' - }; - - var style_modifier_hunter = new smh(); - - //act - style_modifier_hunter.consume_style_modifier(pattern, '{{> partial:bar|baz|dum}}', pl); - - //assert - test.equals(pattern.extendedTemplate, '
'); - test.done(); - }, - 'does not alter pattern extendedTemplate if styleModifier not found in partial' : function(test){ - //arrange - var pl = {}; - pl.partials = {}; - pl.config = {}; - pl.config.debug = false; - - var pattern = { - extendedTemplate: '
' - }; - - var style_modifier_hunter = new smh(); - - //act - style_modifier_hunter.consume_style_modifier(pattern, '{{> partial}}', pl); - - //assert - test.equals(pattern.extendedTemplate, '
'); - test.done(); - } -}; +tap.test('uses the partial stylemodifer to modify the patterns extendedTemplate', function(test){ + //arrange + var pl = {}; + pl.partials = {}; + pl.config = {}; + pl.config.debug = false; + + var pattern = { + extendedTemplate: '
' + }; + + var style_modifier_hunter = new smh(); + + //act + style_modifier_hunter.consume_style_modifier(pattern, '{{> partial:bar}}', pl); + + //assert + test.equals(pattern.extendedTemplate, '
'); + test.end(); +}); + +tap.test('replaces style modifiers with spaces in the syntax', function(test){ + //arrange + var pl = {}; + pl.partials = {}; + pl.config = {}; + pl.config.debug = false; + + var pattern = { + extendedTemplate: '
' + }; + + var style_modifier_hunter = new smh(); + + //act + style_modifier_hunter.consume_style_modifier(pattern, '{{> partial:bar}}', pl); + + //assert + test.equals(pattern.extendedTemplate, '
'); + test.end(); +}); + +tap.test('replaces multiple style modifiers', function(test){ + //arrange + var pl = {}; + pl.partials = {}; + pl.config = {}; + pl.config.debug = false; + + var pattern = { + extendedTemplate: '
' + }; + + var style_modifier_hunter = new smh(); + + //act + style_modifier_hunter.consume_style_modifier(pattern, '{{> partial:bar|baz|dum}}', pl); + + //assert + test.equals(pattern.extendedTemplate, '
'); + test.end(); +}); + +tap.test('does not alter pattern extendedTemplate if styleModifier not found in partial', function(test){ + //arrange + var pl = {}; + pl.partials = {}; + pl.config = {}; + pl.config.debug = false; + + var pattern = { + extendedTemplate: '
' + }; + + var style_modifier_hunter = new smh(); + + //act + style_modifier_hunter.consume_style_modifier(pattern, '{{> partial}}', pl); + + //assert + test.equals(pattern.extendedTemplate, '
'); + test.end(); +}); diff --git a/test/ui_builder_tests.js b/test/ui_builder_tests.js index c677fc963..3a111ffd8 100644 --- a/test/ui_builder_tests.js +++ b/test/ui_builder_tests.js @@ -1,5 +1,7 @@ "use strict"; +var tap = require('tap'); + var eol = require('os').EOL; var Pattern = require('../core/lib/object_factory').Pattern; var extend = require('util')._extend; @@ -29,222 +31,218 @@ function createFakePatternLab(customProps) { var ui = require('../core/lib/ui_builder')(); -exports['ui_builder'] = { - - 'isPatternExcluded - returns true when pattern filename starts with underscore': function (test) { - //arrange - var patternlab = createFakePatternLab({}); - var pattern = new Pattern('00-test/_ignored-pattern.mustache'); - - //act - var result = ui.isPatternExcluded(pattern, patternlab); - - //assert - test.equals(result, true); - test.done(); - }, - - 'isPatternExcluded - returns true when pattern is defaultPattern': function (test) { - //arrange - var patternlab = createFakePatternLab({}); - var pattern = new Pattern('00-test/foo.mustache'); - patternlab.config.defaultPattern = 'test-foo'; - - //act - var result = ui.isPatternExcluded(pattern, patternlab); - - //assert - test.equals(result, true); - test.done(); - }, - - 'isPatternExcluded - returns true when pattern within underscored directory - top level': function (test) { - //arrange - var patternlab = createFakePatternLab({}); - var pattern = Pattern.createEmpty({ - relPath: '_hidden/patternsubtype/foo.mustache', - isPattern: true, - fileName : 'foo.mustache', - patternPartial: 'hidden-foo' - }); - - //act - var result = ui.isPatternExcluded(pattern, patternlab); - - //assert - test.equals(result, true); - test.done(); - }, - - 'isPatternExcluded - returns true when pattern within underscored directory - subtype level': function (test) { - //arrange - var patternlab = createFakePatternLab({}); - var pattern = Pattern.createEmpty({ - relPath: 'shown/_patternsubtype/foo.mustache', - isPattern: true, - fileName : 'foo.mustache', - patternPartial: 'shown-foo' - }); - - //act - var result = ui.isPatternExcluded(pattern, patternlab); - - //assert - test.equals(result, true); - test.done(); - }, - - 'groupPatterns - creates pattern groups correctly': function (test) { - //arrange - var patternlab = createFakePatternLab({ - patterns: [], - patternGroups: {}, - subtypePatterns: {} - }); - - patternlab.patterns.push( - new Pattern('00-test/foo.mustache'), - new Pattern('00-test/bar.mustache'), - new Pattern('patternType1/patternSubType1/blue.mustache'), - new Pattern('patternType1/patternSubType1/red.mustache'), - new Pattern('patternType1/patternSubType1/yellow.mustache'), - new Pattern('patternType1/patternSubType2/black.mustache'), - new Pattern('patternType1/patternSubType2/grey.mustache'), - new Pattern('patternType1/patternSubType2/white.mustache') - ); - ui.resetUIBuilderState(patternlab); - - //act - var result = ui.groupPatterns(patternlab); - - //assert - test.equals(result.patternGroups.patternType1.patternSubType1.blue.patternPartial, 'patternType1-blue'); - test.equals(result.patternGroups.patternType1.patternSubType1.red.patternPartial, 'patternType1-red'); - test.equals(result.patternGroups.patternType1.patternSubType1.yellow.patternPartial, 'patternType1-yellow'); - test.equals(result.patternGroups.patternType1.patternSubType2.black.patternPartial, 'patternType1-black'); - test.equals(result.patternGroups.patternType1.patternSubType2.grey.patternPartial, 'patternType1-grey'); - test.equals(result.patternGroups.patternType1.patternSubType2.white.patternPartial, 'patternType1-white'); - - test.equals(patternlab.patternTypes[0].patternItems[0].patternPartial, 'test-bar'); - test.equals(patternlab.patternTypes[0].patternItems[1].patternPartial, 'test-foo'); - - //todo: patternlab.patternTypes[0].patternItems[1] looks malformed - - test.done(); - }, - - 'groupPatterns - creates documentation patterns for each type and subtype if not exists': function (test) { - //arrange - var patternlab = createFakePatternLab({ - patterns: [], - patternGroups: {}, - subtypePatterns: {} - }); - - patternlab.patterns.push( - new Pattern('00-test/foo.mustache'), - new Pattern('00-test/bar.mustache'), - new Pattern('patternType1/patternSubType1/blue.mustache'), - new Pattern('patternType1/patternSubType1/red.mustache'), - new Pattern('patternType1/patternSubType1/yellow.mustache'), - new Pattern('patternType1/patternSubType2/black.mustache'), - new Pattern('patternType1/patternSubType2/grey.mustache'), - new Pattern('patternType1/patternSubType2/white.mustache') - ); - ui.resetUIBuilderState(patternlab); - - //act - var result = ui.groupPatterns(patternlab); - - //assert - test.equals(result.patternGroups.patternType1.patternSubType1['viewall-patternType1-patternSubType1'].patternPartial, 'viewall-patternType1-patternSubType1'); - test.equals(result.patternGroups.patternType1.patternSubType2['viewall-patternType1-patternSubType2'].patternPartial, 'viewall-patternType1-patternSubType2'); - - test.done(); - }, - - 'groupPatterns - adds each pattern to the patternPaths object': function (test) { - //arrange - var patternlab = createFakePatternLab({ - patterns: [], - patternGroups: {}, - subtypePatterns: {} - }); - - patternlab.patterns.push( - new Pattern('00-test/foo.mustache'), - new Pattern('00-test/bar.mustache'), - new Pattern('patternType1/patternSubType1/blue.mustache'), - new Pattern('patternType1/patternSubType1/red.mustache'), - new Pattern('patternType1/patternSubType1/yellow.mustache'), - new Pattern('patternType1/patternSubType2/black.mustache'), - new Pattern('patternType1/patternSubType2/grey.mustache'), - new Pattern('patternType1/patternSubType2/white.mustache') - ); - ui.resetUIBuilderState(patternlab); - - //act - var result = ui.groupPatterns(patternlab); - - //assert - test.equals(patternlab.patternPaths['test']['foo'], '00-test-foo'); - test.equals(patternlab.patternPaths['test']['bar'], '00-test-bar'); - test.equals(patternlab.patternPaths['patternType1']['blue'], 'patternType1-patternSubType1-blue'); - test.equals(patternlab.patternPaths['patternType1']['red'], 'patternType1-patternSubType1-red'); - test.equals(patternlab.patternPaths['patternType1']['yellow'], 'patternType1-patternSubType1-yellow'); - test.equals(patternlab.patternPaths['patternType1']['black'], 'patternType1-patternSubType2-black'); - test.equals(patternlab.patternPaths['patternType1']['grey'], 'patternType1-patternSubType2-grey'); - test.equals(patternlab.patternPaths['patternType1']['white'], 'patternType1-patternSubType2-white'); - - test.done(); - }, - - 'groupPatterns - adds each pattern to the view all paths object': function (test) { - //arrange - var patternlab = createFakePatternLab({ - patterns: [], - patternGroups: {}, - subtypePatterns: {} - }); - - patternlab.patterns.push( - new Pattern('00-test/foo.mustache'), - new Pattern('00-test/bar.mustache'), - new Pattern('patternType1/patternSubType1/blue.mustache'), - new Pattern('patternType1/patternSubType1/red.mustache'), - new Pattern('patternType1/patternSubType1/yellow.mustache'), - new Pattern('patternType1/patternSubType2/black.mustache'), - new Pattern('patternType1/patternSubType2/grey.mustache'), - new Pattern('patternType1/patternSubType2/white.mustache') - ); - ui.resetUIBuilderState(patternlab); - - //act - var result = ui.groupPatterns(patternlab); - - //assert - test.equals('todo', 'todo'); - - test.done(); - }, - - 'resetUIBuilderState - reset global objects' : function (test) { - //arrange - var patternlab = createFakePatternLab({ - patternPaths: { foo: 1}, - viewAllPaths: { bar: 2}, - patternTypes: ['baz'] - }); - - //act - ui.resetUIBuilderState(patternlab); - - //assert - test.equals(patternlab.patternPaths.foo, undefined); - test.equals(patternlab.viewAllPaths.bar, undefined); - test.equals(patternlab.patternTypes.length, 0); - - test.done(); - } - -}; +tap.test('isPatternExcluded - returns true when pattern filename starts with underscore', function (test) { + //arrange + var patternlab = createFakePatternLab({}); + var pattern = new Pattern('00-test/_ignored-pattern.mustache'); + + //act + var result = ui.isPatternExcluded(pattern, patternlab); + + //assert + test.equals(result, true); + test.end(); +}); + +tap.test('isPatternExcluded - returns true when pattern is defaultPattern', function (test) { + //arrange + var patternlab = createFakePatternLab({}); + var pattern = new Pattern('00-test/foo.mustache'); + patternlab.config.defaultPattern = 'test-foo'; + + //act + var result = ui.isPatternExcluded(pattern, patternlab); + + //assert + test.equals(result, true); + test.end(); +}); + +tap.test('isPatternExcluded - returns true when pattern within underscored directory - top level', function (test) { + //arrange + var patternlab = createFakePatternLab({}); + var pattern = Pattern.createEmpty({ + relPath: '_hidden/patternsubtype/foo.mustache', + isPattern: true, + fileName : 'foo.mustache', + patternPartial: 'hidden-foo' + }); + + //act + var result = ui.isPatternExcluded(pattern, patternlab); + + //assert + test.equals(result, true); + test.end(); +}); + +tap.test('isPatternExcluded - returns true when pattern within underscored directory - subtype level', function (test) { + //arrange + var patternlab = createFakePatternLab({}); + var pattern = Pattern.createEmpty({ + relPath: 'shown/_patternsubtype/foo.mustache', + isPattern: true, + fileName : 'foo.mustache', + patternPartial: 'shown-foo' + }); + + //act + var result = ui.isPatternExcluded(pattern, patternlab); + + //assert + test.equals(result, true); + test.end(); +}); + +tap.test('groupPatterns - creates pattern groups correctly', function (test) { + //arrange + var patternlab = createFakePatternLab({ + patterns: [], + patternGroups: {}, + subtypePatterns: {} + }); + + patternlab.patterns.push( + new Pattern('00-test/foo.mustache'), + new Pattern('00-test/bar.mustache'), + new Pattern('patternType1/patternSubType1/blue.mustache'), + new Pattern('patternType1/patternSubType1/red.mustache'), + new Pattern('patternType1/patternSubType1/yellow.mustache'), + new Pattern('patternType1/patternSubType2/black.mustache'), + new Pattern('patternType1/patternSubType2/grey.mustache'), + new Pattern('patternType1/patternSubType2/white.mustache') + ); + ui.resetUIBuilderState(patternlab); + + //act + var result = ui.groupPatterns(patternlab); + + //assert + test.equals(result.patternGroups.patternType1.patternSubType1.blue.patternPartial, 'patternType1-blue'); + test.equals(result.patternGroups.patternType1.patternSubType1.red.patternPartial, 'patternType1-red'); + test.equals(result.patternGroups.patternType1.patternSubType1.yellow.patternPartial, 'patternType1-yellow'); + test.equals(result.patternGroups.patternType1.patternSubType2.black.patternPartial, 'patternType1-black'); + test.equals(result.patternGroups.patternType1.patternSubType2.grey.patternPartial, 'patternType1-grey'); + test.equals(result.patternGroups.patternType1.patternSubType2.white.patternPartial, 'patternType1-white'); + + test.equals(patternlab.patternTypes[0].patternItems[0].patternPartial, 'test-bar'); + test.equals(patternlab.patternTypes[0].patternItems[1].patternPartial, 'test-foo'); + + //todo: patternlab.patternTypes[0].patternItems[1] looks malformed + + test.end(); +}); + +tap.test('groupPatterns - creates documentation patterns for each type and subtype if not exists', function (test) { + //arrange + var patternlab = createFakePatternLab({ + patterns: [], + patternGroups: {}, + subtypePatterns: {} + }); + + patternlab.patterns.push( + new Pattern('00-test/foo.mustache'), + new Pattern('00-test/bar.mustache'), + new Pattern('patternType1/patternSubType1/blue.mustache'), + new Pattern('patternType1/patternSubType1/red.mustache'), + new Pattern('patternType1/patternSubType1/yellow.mustache'), + new Pattern('patternType1/patternSubType2/black.mustache'), + new Pattern('patternType1/patternSubType2/grey.mustache'), + new Pattern('patternType1/patternSubType2/white.mustache') + ); + ui.resetUIBuilderState(patternlab); + + //act + var result = ui.groupPatterns(patternlab); + + //assert + test.equals(result.patternGroups.patternType1.patternSubType1['viewall-patternType1-patternSubType1'].patternPartial, 'viewall-patternType1-patternSubType1'); + test.equals(result.patternGroups.patternType1.patternSubType2['viewall-patternType1-patternSubType2'].patternPartial, 'viewall-patternType1-patternSubType2'); + + test.end(); +}); + +tap.test('groupPatterns - adds each pattern to the patternPaths object', function (test) { + //arrange + var patternlab = createFakePatternLab({ + patterns: [], + patternGroups: {}, + subtypePatterns: {} + }); + + patternlab.patterns.push( + new Pattern('00-test/foo.mustache'), + new Pattern('00-test/bar.mustache'), + new Pattern('patternType1/patternSubType1/blue.mustache'), + new Pattern('patternType1/patternSubType1/red.mustache'), + new Pattern('patternType1/patternSubType1/yellow.mustache'), + new Pattern('patternType1/patternSubType2/black.mustache'), + new Pattern('patternType1/patternSubType2/grey.mustache'), + new Pattern('patternType1/patternSubType2/white.mustache') + ); + ui.resetUIBuilderState(patternlab); + + //act + var result = ui.groupPatterns(patternlab); + + //assert + test.equals(patternlab.patternPaths['test']['foo'], '00-test-foo'); + test.equals(patternlab.patternPaths['test']['bar'], '00-test-bar'); + test.equals(patternlab.patternPaths['patternType1']['blue'], 'patternType1-patternSubType1-blue'); + test.equals(patternlab.patternPaths['patternType1']['red'], 'patternType1-patternSubType1-red'); + test.equals(patternlab.patternPaths['patternType1']['yellow'], 'patternType1-patternSubType1-yellow'); + test.equals(patternlab.patternPaths['patternType1']['black'], 'patternType1-patternSubType2-black'); + test.equals(patternlab.patternPaths['patternType1']['grey'], 'patternType1-patternSubType2-grey'); + test.equals(patternlab.patternPaths['patternType1']['white'], 'patternType1-patternSubType2-white'); + + test.end(); +}); + +tap.test('groupPatterns - adds each pattern to the view all paths object', function (test) { + //arrange + var patternlab = createFakePatternLab({ + patterns: [], + patternGroups: {}, + subtypePatterns: {} + }); + + patternlab.patterns.push( + new Pattern('00-test/foo.mustache'), + new Pattern('00-test/bar.mustache'), + new Pattern('patternType1/patternSubType1/blue.mustache'), + new Pattern('patternType1/patternSubType1/red.mustache'), + new Pattern('patternType1/patternSubType1/yellow.mustache'), + new Pattern('patternType1/patternSubType2/black.mustache'), + new Pattern('patternType1/patternSubType2/grey.mustache'), + new Pattern('patternType1/patternSubType2/white.mustache') + ); + ui.resetUIBuilderState(patternlab); + + //act + var result = ui.groupPatterns(patternlab); + + //assert + test.equals('todo', 'todo'); + + test.end(); +}); + +tap.test('resetUIBuilderState - reset global objects', function (test) { + //arrange + var patternlab = createFakePatternLab({ + patternPaths: { foo: 1}, + viewAllPaths: { bar: 2}, + patternTypes: ['baz'] + }); + + //act + ui.resetUIBuilderState(patternlab); + + //assert + test.equals(patternlab.patternPaths.foo, undefined); + test.equals(patternlab.viewAllPaths.bar, undefined); + test.equals(patternlab.patternTypes.length, 0); + + 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(); + } +};