diff --git a/core/lib/object_factory.js b/core/lib/object_factory.js index 1a55c5760..6f7d03f7b 100644 --- a/core/lib/object_factory.js +++ b/core/lib/object_factory.js @@ -46,6 +46,7 @@ var Pattern = function (relPath, data) { // name of the pattern. UPDATE: this.key is now known as this.patternPartial this.patternPartial = this.patternGroup + '-' + this.patternBaseName; + this.patternState = ''; this.template = ''; this.patternPartialCode = ''; this.lineage = []; diff --git a/core/lib/pattern_assembler.js b/core/lib/pattern_assembler.js index 6b35aa0bf..10f1676a5 100644 --- a/core/lib/pattern_assembler.js +++ b/core/lib/pattern_assembler.js @@ -286,7 +286,7 @@ var pattern_assembler = function () { currentPattern.extendedTemplate = currentPattern.template; //find how many partials there may be for the given pattern - var foundPatternPartials = currentPattern.findPartials(currentPattern); + var foundPatternPartials = currentPattern.findPartials(); //find any listItem blocks that within the pattern, even if there are no partials list_item_hunter.process_list_item_partials(currentPattern, patternlab); @@ -294,6 +294,7 @@ var pattern_assembler = function () { // expand any partials present in this pattern; that is, drill down into // the template and replace their calls in this template with rendered // results + if (currentPattern.engine.expandPartials && (foundPatternPartials !== null && foundPatternPartials.length > 0)) { // eslint-disable-next-line expandPartials(foundPatternPartials, list_item_hunter, patternlab, currentPattern); diff --git a/package.json b/package.json index 212c34894..cc383a100 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.0.1", + "version": "2.0.2", "main": "./core/lib/patternlab.js", "dependencies": { "diveSync": "^0.3.0", diff --git a/test/engine_mustache_tests.js b/test/engine_mustache_tests.js new file mode 100644 index 000000000..da811be0e --- /dev/null +++ b/test/engine_mustache_tests.js @@ -0,0 +1,216 @@ +"use strict"; + +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; + +// fake pattern lab constructor: +// sets up a fake patternlab object, which is needed by the pattern processing +// apparatus. +function fakePatternLab() { + 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; +} + +// function for testing sets of partials +function testFindPartials(test, partialTests) { + test.expect(partialTests.length + 1); + + // setup current pattern from what we would have during execution + // docs on partial syntax are here: + // http://patternlab.io/docs/pattern-including.html + var currentPattern = Pattern.create( + '01-molecules/00-testing/00-test-mol.mustache', // relative path now + null, // data + { + template: partialTests.join(eol) + } + ); + + // act + var results = currentPattern.findPartials(); + + // assert + test.equals(results.length, partialTests.length); + partialTests.forEach(function (testString, index) { + test.equals(results[index], testString); + }); + + test.done(); +} + +function testFindPartialsWithStyleModifiers(test, partialTests) { + test.expect(partialTests.length + 1); + + // setup current pattern from what we would have during execution + // docs on partial syntax are here: + // http://patternlab.io/docs/pattern-including.html + var currentPattern = Pattern.create( + '01-molecules/00-testing/00-test-mol.mustache', // relative path now + null, // data + { + template: partialTests.join(eol) + } + ); + + // act + var results = currentPattern.findPartialsWithStyleModifiers(); + + // assert + test.equals(results.length, partialTests.length); + partialTests.forEach(function (testString, index) { + test.equals(results[index], testString); + }); + + test.done(); +} + +function testFindPartialsWithPatternParameters(test, partialTests) { + test.expect(partialTests.length + 1); + + // setup current pattern from what we would have during execution + // docs on partial syntax are here: + // http://patternlab.io/docs/pattern-including.html + var currentPattern = Pattern.create( + '01-molecules/00-testing/00-test-mol.mustache', // relative path now + null, // data + { + template: partialTests.join(eol) + } + ); + + // act + var results = currentPattern.findPartialsWithPatternParameters(); + + // assert + test.equals(results.length, partialTests.length); + partialTests.forEach(function (testString, index) { + test.equals(results[index], testString); + }); + + test.done(); +} + +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; +} diff --git a/test/object_factory_tests.js b/test/object_factory_tests.js index b2fdeab5e..685f74fbd 100644 --- a/test/object_factory_tests.js +++ b/test/object_factory_tests.js @@ -26,6 +26,7 @@ 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) { diff --git a/test/pattern_assembler_tests.js b/test/pattern_assembler_tests.js index bc649ac62..a6aad4b00 100644 --- a/test/pattern_assembler_tests.js +++ b/test/pattern_assembler_tests.js @@ -6,241 +6,86 @@ var path = require('path'); exports['pattern_assembler'] = { - 'find_pattern_partials finds partials' : function(test){ - // NOTES from GTP: - // it's nice to have so much test coverage, but it retrospect, I'm not - // happy with the structure I wound up with in this test; it's too - // difficult to add test cases and test failure reporting is not very - // granular. - - test.expect(16); - - // setup current pattern from what we would have during execution - // docs on partial syntax are here: - // http://patternlab.io/docs/pattern-including.html - var currentPattern = Pattern.create( - '01-molecules/00-testing/00-test-mol.mustache', // relative path now - null, // data - { - template: "{{> molecules-comment-header}}asdfasdf" + - "{{> molecules-comment-header}}" + - "{{> \n molecules-comment-header\n}}" + - "{{> }}" + - "{{> molecules-weird-spacing }}" + - "{{> molecules-ba_d-cha*rs }}" + - "{{> 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.") }}' + - "{{> molecules-single-comment:foo }}" + - // verbose partial syntax, introduced in v0.12.0, with file extension - "{{> 01-molecules/06-components/03-comment-header.mustache }}" + - "{{> 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.') }}" + - "{{> molecules-single-comment:foo }}" + - "{{>atoms-error(message: 'That\\'s no moon...')}}" + - '{{>atoms-error(message: \'That\\\'s no moon...\')}}' + - "{{> 00-atoms/00-global/ }}" + - // verbose partial syntax, introduced in v0.12.0, no file extension - "{{> 00-atoms/00-global/06-test }}" + - "{{> molecules-single-comment:foo_1 }}" + - "{{> molecules-single-comment:foo-1 }}" - } - ); - - var results = currentPattern.findPartials(); - test.equals(results.length, 15); - test.equals(results[0], "{{> molecules-comment-header}}"); - test.equals(results[1], "{{> molecules-comment-header}}"); - test.equals(results[2], "{{> \n molecules-comment-header\n}}"); - test.equals(results[3], "{{> molecules-weird-spacing }}"); - test.equals(results[4], "{{> molecules-single-comment(description: 'A life isn\\'t like a garden. Perfect moments can be had, but not preserved, except in memory.') }}"); - test.equals(results[5], '{{> molecules-single-comment(description: "A life is like a \\"garden\\". Perfect moments can be had, but not preserved, except in memory.") }}'); - test.equals(results[6], "{{> molecules-single-comment:foo }}"); - test.equals(results[7], "{{> 01-molecules/06-components/03-comment-header.mustache }}"); - test.equals(results[8], "{{> 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.') }}"); - test.equals(results[9], "{{> molecules-single-comment:foo }}"); - test.equals(results[10], "{{>atoms-error(message: 'That\\'s no moon...')}}"); - test.equals(results[11], "{{>atoms-error(message: 'That\\'s no moon...')}}"); - test.equals(results[12], "{{> 00-atoms/00-global/06-test }}"); - test.equals(results[13], '{{> molecules-single-comment:foo_1 }}'); - test.equals(results[14], '{{> molecules-single-comment:foo-1 }}'); - test.done(); - }, - 'find_pattern_partials finds verbose partials' : function(test){ - test.expect(3); - - //setup current pattern from what we would have during execution - var currentPattern = new Pattern( - '01-molecules/00-testing/00-test-mol.mustache', // relative path now - null // data - ); - currentPattern.template = "

{{> 01-molecules/06-components/03-comment-header.mustache }}

{{> 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.') }}
"; - - var results = currentPattern.findPartials(); - test.equals(results.length, 2); - test.equals(results[0], '{{> 01-molecules/06-components/03-comment-header.mustache }}'); - test.equals(results[1], '{{> 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.\') }}'); - test.done(); - }, - 'find_pattern_partials_with_style_modifiers finds style modifiers' : function(test){ - test.expect(4); - - //setup current pattern from what we would have during execution - - var currentPattern = new Pattern( - '01-molecules/00-testing/00-test-mol.mustache', // relative path now - null // data - ); - currentPattern.template = "

{{> molecules-comment-header}}

{{> molecules-single-comment:foo }}
{{> molecules-single-comment:foo_1 }}
{{> molecules-single-comment:foo-1 }}
"; - - var results = currentPattern.findPartialsWithStyleModifiers(); - test.equals(results.length, 3); - test.equals(results[0], '{{> molecules-single-comment:foo }}'); - test.equals(results[1], '{{> molecules-single-comment:foo_1 }}'); - test.equals(results[2], '{{> molecules-single-comment:foo-1 }}'); - - test.done(); - }, - 'find_pattern_partials_with_style_modifiers finds style modifiers with parameters present too' : function(test){ - test.expect(2); - - //setup current pattern from what we would have during execution - - var currentPattern = new Pattern( - '01-molecules/00-testing/00-test-mol.mustache', // relative path now - null // data - ); - currentPattern.template = "

{{> molecules-comment-header}}

{{> molecules-single-comment:foo(bar:'baz') }}
"; - - var results = currentPattern.findPartialsWithStyleModifiers(); - test.equals(results.length, 1); - test.equals(results[0], "{{> molecules-single-comment:foo(bar:'baz') }}"); - - test.done(); - }, - 'find_pattern_partials_with_style_modifiers finds style modifiers with verbose partials' : function(test){ - test.expect(2); - - //setup current pattern from what we would have during execution - var currentPattern = new Pattern( - '01-molecules/00-testing/00-test-mol.mustache', // relative path now - null // data - ); - currentPattern.template = "

{{> 01-molecules/06-components/molecules-comment-header}}

{{> 01-molecules/06-components/molecules-single-comment:foo }}
"; - - var results = currentPattern.findPartialsWithStyleModifiers(); - test.equals(results.length, 1); - test.equals(results[0], '{{> 01-molecules/06-components/molecules-single-comment:foo }}'); - - test.done(); - }, - 'find_pattern_partials_with_style_modifiers finds no style modifiers when only partials present' : function(test){ - test.expect(1); - - //setup current pattern from what we would have during execution - var currentPattern = new Pattern( - '01-molecules/00-testing/00-test-mol.mustache', // relative path now - null // data - ); - currentPattern.template = "

{{> molecules-comment-header}}

{{> molecules-single-comment }}
"; - - var results = currentPattern.findPartialsWithStyleModifiers(); - test.equals(results, null); - - test.done(); - }, - 'find_pattern_partials_with_style_modifiers finds no style modifiers when only partials with pattern parameters present' : function(test){ - test.expect(1); - - //setup current pattern from what we would have during execution - var currentPattern = new Pattern( - '01-molecules/00-testing/00-test-mol.mustache', // relative path now - null // data - ); - currentPattern.template = "

{{> molecules-comment-header}}

{{> molecules-single-comment(foo: 'bar') }}
"; - var results = currentPattern.findPartialsWithStyleModifiers(); - test.equals(results, null); - - test.done(); - }, - 'find_pattern_partials_with_parameters finds parameters' : function(test){ - test.expect(2); - - //setup current pattern from what we would have during execution - var currentPattern = new Pattern( - '01-molecules/00-testing/00-test-mol.mustache', // relative path now - null // data - ); - currentPattern.template = "

{{> molecules-comment-header}}

{{> molecules-single-comment(bar:'baz') }}
"; - - var results = currentPattern.findPartialsWithPatternParameters(); - test.equals(results.length, 1); - test.equals(results[0], "{{> molecules-single-comment(bar:'baz') }}"); - - test.done(); - - }, - 'find_pattern_partials_with_parameters finds parameters when stylemodifiers present too' : function(test){ - test.expect(2); - - //setup current pattern from what we would have during execution - var currentPattern = new Pattern( - '01-molecules/00-testing/00-test-mol.mustache', // relative path now - null // data - ); - currentPattern.template = "

{{> molecules-comment-header}}

{{> molecules-single-comment:foo(bar:'baz') }}
"; - - var results = currentPattern.findPartialsWithPatternParameters(); - test.equals(results.length, 1); - test.equals(results[0], "{{> molecules-single-comment:foo(bar:'baz') }}"); - - test.done(); - }, - 'find_pattern_partials_with_parameters finds parameters with verbose partials' : function(test){ - test.expect(2); - - //setup current pattern from what we would have during execution - var currentPattern = new Pattern( - '01-molecules/00-testing/00-test-mol.mustache', // relative path now - null // data - ); - currentPattern.template = "

{{> 01-molecules/06-components/molecules-comment-header}}

{{> 01-molecules/06-components/molecules-single-comment(bar:'baz') }}
"; - - var results = currentPattern.findPartialsWithPatternParameters(); - test.equals(results.length, 1); - test.equals(results[0], "{{> 01-molecules/06-components/molecules-single-comment(bar:'baz') }}"); - - test.done(); - }, - 'find_pattern_partials_with_parameters finds no style modifiers when only partials present' : function(test){ - test.expect(1); - - //setup current pattern from what we would have during execution - var currentPattern = new Pattern( - '01-molecules/00-testing/00-test-mol.mustache', // relative path now - null // data - ); - currentPattern.template = "

{{> molecules-comment-header}}

{{> molecules-single-comment }}
"; - - var results = currentPattern.findPartialsWithPatternParameters(); - test.equals(results, null); - - test.done(); - }, - 'find_pattern_partials_with_parameters finds no style modifiers when only partials with style modifiers present' : function(test){ - test.expect(1); - - //setup current pattern from what we would have during execution - var currentPattern = new Pattern( - '01-molecules/00-testing/00-test-mol.mustache', // relative path now - null // data - ); - currentPattern.template = "

{{> molecules-comment-header}}

{{> molecules-single-comment:foo }}
"; - - var results = currentPattern.findPartialsWithPatternParameters(); - test.equals(results, null); - - test.done(); - }, + //'find_pattern_partials_with_parameters finds parameters' : function(test){ + //test.expect(2); + // + // //setup current pattern from what we would have during execution + //var currentPattern = new Pattern( + // '01-molecules/00-testing/00-test-mol.mustache', // relative path now + // null // data + //); + // currentPattern.template = "

{{> molecules-comment-header}}

{{> molecules-single-comment(bar:'baz') }}
"; + // + //var results = currentPattern.findPartialsWithPatternParameters(); + // test.equals(results.length, 1); + // test.equals(results[0], "{{> molecules-single-comment(bar:'baz') }}"); + // + // test.done(); + // + //}, + //'find_pattern_partials_with_parameters finds parameters when stylemodifiers present too' : function(test){ + //test.expect(2); + // + // //setup current pattern from what we would have during execution + //var currentPattern = new Pattern( + // '01-molecules/00-testing/00-test-mol.mustache', // relative path now + // null // data + //); + // currentPattern.template = "

{{> molecules-comment-header}}

{{> molecules-single-comment:foo(bar:'baz') }}
"; + // + //var results = currentPattern.findPartialsWithPatternParameters(); + // test.equals(results.length, 1); + // test.equals(results[0], "{{> molecules-single-comment:foo(bar:'baz') }}"); + // + // test.done(); + //}, + //'find_pattern_partials_with_parameters finds parameters with verbose partials' : function(test){ + //test.expect(2); + // + // //setup current pattern from what we would have during execution + //var currentPattern = new Pattern( + // '01-molecules/00-testing/00-test-mol.mustache', // relative path now + // null // data + //); + // currentPattern.template = "

{{> 01-molecules/06-components/molecules-comment-header}}

{{> 01-molecules/06-components/molecules-single-comment(bar:'baz') }}
"; + // + //var results = currentPattern.findPartialsWithPatternParameters(); + // test.equals(results.length, 1); + // test.equals(results[0], "{{> 01-molecules/06-components/molecules-single-comment(bar:'baz') }}"); + // + // test.done(); + //}, + //'find_pattern_partials_with_parameters finds no style modifiers when only partials present' : function(test){ + //test.expect(1); + // + // //setup current pattern from what we would have during execution + //var currentPattern = new Pattern( + // '01-molecules/00-testing/00-test-mol.mustache', // relative path now + // null // data + //); + // currentPattern.template = "

{{> molecules-comment-header}}

{{> molecules-single-comment }}
"; + // + //var results = currentPattern.findPartialsWithPatternParameters(); + // test.equals(results, null); + // + // test.done(); + //}, + //'find_pattern_partials_with_parameters finds no style modifiers when only partials with style modifiers present' : function(test){ + //test.expect(1); + // + // //setup current pattern from what we would have during execution + //var currentPattern = new Pattern( + // '01-molecules/00-testing/00-test-mol.mustache', // relative path now + // null // data + //); + // currentPattern.template = "

{{> molecules-comment-header}}

{{> molecules-single-comment:foo }}
"; + // + //var results = currentPattern.findPartialsWithPatternParameters(); + // test.equals(results, null); + // + // test.done(); + //}, 'process_pattern_recursive recursively includes partials' : function(test){ test.expect(3);