Skip to content

Commit

Permalink
fuzzy partial matching. unit test coverage for some basic scenarios, …
Browse files Browse the repository at this point in the history
…lineage, and style modifiers.

still need to look into list items
also, may be a bug with repeated style modifiers, which i think is a regression.
  • Loading branch information
bmuenzenmeyer committed Dec 29, 2015
1 parent bf8cc0d commit 980118f
Show file tree
Hide file tree
Showing 6 changed files with 243 additions and 41 deletions.
56 changes: 25 additions & 31 deletions builder/lineage_hunter.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,47 +23,41 @@
if(matches !== null){
matches.forEach(function(match, index, matches){
//strip out the template cruft
var foundPattern = match.replace("{{> ", "").replace(" }}", "").replace("{{>", "").replace("}}", "");
var foundPatternKey = match.replace("{{> ", "").replace(" }}", "").replace("{{>", "").replace("}}", "");

// remove any potential pattern parameters. this and the above are rather brutish but I didn't want to do a regex at the time
if(foundPattern.indexOf('(') > 0){
foundPattern = foundPattern.substring(0, foundPattern.indexOf('('));
if(foundPatternKey.indexOf('(') > 0){
foundPatternKey = foundPatternKey.substring(0, foundPatternKey.indexOf('('));
}

//add if it doesnt exist
if (pattern.lineageIndex.indexOf(foundPattern) === -1){
//remove any potential stylemodifiers.
foundPatternKey = foundPatternKey.split(':')[0];

pattern.lineageIndex.push(foundPattern);
//get the ancestorPattern
var ancestorPattern = pattern_assembler.get_pattern_by_key(foundPatternKey, patternlab);

patternlab.patterns.forEach(function(ancestorPattern, index, patterns){
if (ancestorPattern && pattern.lineageIndex.indexOf(ancestorPattern.key) === -1){

//find the pattern in question
var searchPattern = ancestorPattern.patternGroup + "-" + ancestorPattern.patternName;
//add it since it didnt exist
pattern.lineageIndex.push(ancestorPattern.key);
//create the more complex patternLineage object too
var l = {
"lineagePattern": ancestorPattern.key,
"lineagePath": "../../patterns/" + ancestorPattern.patternLink
};
pattern.lineage.push(JSON.stringify(l));

if(searchPattern === foundPattern){
//create the more complex patternLineage object too
var l = {
"lineagePattern": foundPattern,
"lineagePath": "../../patterns/" + ancestorPattern.patternLink
//also, add the lineageR entry if it doesn't exist
if (ancestorPattern.lineageRIndex.indexOf(pattern.key) === -1){
ancestorPattern.lineageRIndex.push(pattern.key);

//create the more complex patternLineage object in reverse
var lr = {
"lineagePattern": pattern.key,
"lineagePath": "../../patterns/" + pattern.patternLink
};
pattern.lineage.push(JSON.stringify(l));

//also, add the lineageR entry if it doesn't exist
var patternLabel = pattern.patternGroup + "-" + pattern.patternName;
if (ancestorPattern.lineageRIndex.indexOf(patternLabel) === -1){
ancestorPattern.lineageRIndex.push(patternLabel);

//create the more complex patternLineage object in reverse
var lr = {
"lineagePattern": patternLabel,
"lineagePath": "../../patterns/" + pattern.patternLink
};
ancestorPattern.lineageR.push(JSON.stringify(lr));
}
ancestorPattern.lineageR.push(JSON.stringify(lr));
}

});

}
});
}
Expand Down
11 changes: 11 additions & 0 deletions builder/pattern_assembler.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,17 @@
case patternlab.patterns[i].subdir + '/' + patternlab.patterns[i].fileName + '.mustache':
return patternlab.patterns[i];
}
//look for exact key matches
if(key === patternlab.patterns[i].key){
return patternlab.patterns[i];
}
//return the fuzzy match within the type if it exists
var keyParts = key.split('-'),
keyType = keyParts[0],
keyName = keyParts.slice(1).join('-');
if(patternlab.patterns[i].key.split('-')[0] === keyType && patternlab.patterns[i].key.indexOf(keyName) > -1){
return patternlab.patterns[i];
}
}
throw 'Could not find pattern with key ' + key;
}
Expand Down
183 changes: 177 additions & 6 deletions test/lineage_hunter_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
var lh = require('../builder/lineage_hunter');

exports['lineage hunter '] = {
'test lineage hunter finds lineage' : function(test){
'find_lineage - finds lineage' : function(test){

//setup current pattern from what we would have during execution
var currentPattern = {
Expand All @@ -19,6 +19,7 @@
"patternGroup": "organisms",
"patternSubGroup": "organisms\\00-global",
"flatPatternPath": "02-organisms\\00-global",
"key": "organisms-header",
"patternState": "",
"lineage": [],
"lineageIndex": [],
Expand All @@ -39,6 +40,7 @@
"patternGroup": "atoms",
"patternSubGroup": "atoms\\03-images",
"flatPatternPath": "00-atoms\\03-images",
"key": "atoms-logo",
"patternState": "",
"lineage": [],
"lineageIndex": [],
Expand All @@ -57,6 +59,7 @@
"patternGroup": "molecules",
"patternSubGroup": "molecules\\05-navigation",
"flatPatternPath": "01-molecules\\05-navigation",
"key": "molecules-primary-nav",
"patternState": "",
"lineage": [],
"lineageIndex": [],
Expand All @@ -75,6 +78,7 @@
"patternGroup": "molecules",
"patternSubGroup": "molecules\\04-forms",
"flatPatternPath": "01-molecules\\04-forms",
"key": "molecules-search",
"patternState": "",
"lineage": [],
"lineageIndex": [],
Expand All @@ -95,7 +99,7 @@
test.done();
},

'test lineage hunter finds lineage with spaced pattern parameters' : function(test){
'find_lineage - finds lineage with spaced pattern parameters' : function(test){
//setup current pattern from what we would have during execution
var currentPattern = {
"name": "01-molecules-01-toast-00-error",
Expand All @@ -109,6 +113,7 @@
"patternGroup": "molecules",
"patternSubGroup": "molecules\\01-toast",
"flatPatternPath": "01-molecules\\01-toast",
"key": "molecules-error",
"patternState": "",
"lineage": [],
"lineageIndex": [],
Expand All @@ -129,6 +134,7 @@
"patternGroup": "atoms",
"patternSubGroup": "atoms\\05-alerts",
"flatPatternPath": "01-atoms\\05-alerts",
"key": "atoms-error",
"patternState": "",
"lineage": [],
"lineageIndex": [],
Expand All @@ -147,7 +153,7 @@
test.done();
},

'test lineage hunter finds lineage with unspaced pattern parameters' : function(test){
'find_lineage - finds lineage with unspaced pattern parameters' : function(test){
//setup current pattern from what we would have during execution
var currentPattern = {
"name": "01-molecules-01-toast-00-error",
Expand All @@ -161,6 +167,7 @@
"patternGroup": "molecules",
"patternSubGroup": "molecules\\01-toast",
"flatPatternPath": "01-molecules\\01-toast",
"key": "molecules-error",
"patternState": "",
"lineage": [],
"lineageIndex": [],
Expand All @@ -181,6 +188,7 @@
"patternGroup": "atoms",
"patternSubGroup": "atoms\\05-alerts",
"flatPatternPath": "01-atoms\\05-alerts",
"key": "atoms-error",
"patternState": "",
"lineage": [],
"lineageIndex": [],
Expand All @@ -201,7 +209,169 @@
test.done();
},

'test lineage hunter does not apply lineage twice' : function(test){
'find_lineage - finds lineage with spaced styleModifier' : function(test){
//setup current pattern from what we would have during execution
var currentPattern = {
"name": "01-molecules-01-toast-00-error",
"subdir": "01-molecules\\01-toast",
"filename": "00-error.mustache",
"data": null,
"template": "{{> atoms-error:foo }}",
"patternPartial": "{{> atoms-error:foo }}",
"patternName": "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",
"key": "molecules-error",
"patternState": "",
"lineage": [],
"lineageIndex": [],
"lineageR": [],
"lineageRIndex": []
};
var patternlab = {
patterns: [
{
"name": "01-atoms-05-alerts-00-error",
"subdir": "01-atoms\\05-alerts",
"filename": "00-error.mustache",
"data": null,
"template": "<h1> {{message}} </h1>",
"patternPartial": "<h1> {{message}} </h1>",
"patternName": "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",
"key": "atoms-error",
"patternState": "",
"lineage": [],
"lineageIndex": [],
"lineageR": [],
"lineageRIndex": []
}
]
};

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 = {
"name": "01-molecules-01-toast-00-error",
"subdir": "01-molecules\\01-toast",
"filename": "00-error.mustache",
"data": null,
"template": "{{> atoms-error:foo }}",
"patternPartial": "{{>atoms-error:foo}}",
"patternName": "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",
"key": "molecules-error",
"patternState": "",
"lineage": [],
"lineageIndex": [],
"lineageR": [],
"lineageRIndex": []
};
var patternlab = {
patterns: [
{
"name": "01-atoms-05-alerts-00-error",
"subdir": "01-atoms\\05-alerts",
"filename": "00-error.mustache",
"data": null,
"template": "<h1> {{message}} </h1>",
"patternPartial": "<h1> {{message}} </h1>",
"patternName": "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",
"key": "atoms-error",
"patternState": "",
"lineage": [],
"lineageIndex": [],
"lineageR": [],
"lineageRIndex": []
}
]
};

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 = {
"name": "01-molecules-01-toast-00-error",
"subdir": "01-molecules\\01-toast",
"filename": "00-error.mustache",
"data": null,
"template": "{{> atoms-e:foo }}",
"patternPartial": "{{>atoms-e:foo}}",
"patternName": "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",
"key": "molecules-error",
"patternState": "",
"lineage": [],
"lineageIndex": [],
"lineageR": [],
"lineageRIndex": []
};
var patternlab = {
patterns: [
{
"name": "01-atoms-05-alerts-00-error",
"subdir": "01-atoms\\05-alerts",
"filename": "00-error.mustache",
"data": null,
"template": "<h1> {{message}} </h1>",
"patternPartial": "<h1> {{message}} </h1>",
"patternName": "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",
"key": "atoms-error",
"patternState": "",
"lineage": [],
"lineageIndex": [],
"lineageR": [],
"lineageRIndex": []
}
]
};

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 = {
"name": "01-molecules-01-toast-00-error",
Expand All @@ -215,6 +385,7 @@
"patternGroup": "molecules",
"patternSubGroup": "molecules\\01-toast",
"flatPatternPath": "01-molecules\\01-toast",
"key": "molecules-error",
"patternState": "",
"lineage": [],
"lineageIndex": [],
Expand All @@ -235,6 +406,7 @@
"patternGroup": "atoms",
"patternSubGroup": "atoms\\05-alerts",
"flatPatternPath": "01-atoms\\05-alerts",
"key": "atoms-error",
"patternState": "",
"lineage": [],
"lineageIndex": [],
Expand All @@ -254,8 +426,7 @@
test.equals(JSON.parse(patternlab.patterns[0].lineageR).lineagePattern, 'molecules-error');

test.done();
},

}

};

Expand Down
Loading

0 comments on commit 980118f

Please sign in to comment.