Test parsing Google Maps Response
by Aubrey Taylor
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0-beta1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
<button class="js-run-btn">Run</button>
JavaScript
//////////////////////////////
// Throw open that console! //
//////////////////////////////
suite = {};
function assert(condition, message) {
if (!condition) {
throw message || "Assertion failed";
}
}
function test(testName, callback) {
id = testName.split(" ").join("_");
suite[id] = {
testName: testName,
callback: callback,
isAsync: testName.toLowerCase().indexOf("async") > -1,
}
}
function runTest(test) {
var callback = test.callback;
try {
callback();
console.log("[Passed]", test.testName);
}
catch(error) {
console.log("[Failed] ", test.testName + ", ", error);
}
}
function runTestAsync(test) {
var callback = test.callback;
var dfd = new $.Deferred();
callback(dfd);
dfd.done(function(done){
try {
done();
console.log("[Passed]", test.testName);
}
catch(error) {
console.log("[Failed] ", test.testName + ", ", error);
}
})
}
function runTestSuite() {
var count = 0;
for(testName in suite) {
var test = suite[testName];
var isAsync = test.isAsync;
if(isAsync){
runTestAsync(test);
}
else {
runTest(test);
}
count++;
}
}
///////////
// SETUP //
///////////
function gmParseFormattedAddress(gmResponse) {
// captures "(Ackworth), (GA) (30101), (USA)"
var regex = /^(.*)+,\s(\w*)+\s(\d*)+,\s(\w*)+$/g;
var results = gmResponse["results"][0];
try {
var formattedAddress = results["formatted_address"];
var matches = regex.exec(formattedAddress);
var city = matches[1];
var state = matches[2];
var zip = matches[3];
var country = matches[4];
var every = _.every([city, state, zip, country], function(d){ return !!d; });
}
catch(error) {
var e = new Error();
e.message = "Parsing Error: " + error.message;
throw e;
}
if(!every) {
var e = new Error();
e.message = "Parsing Error:...