Skip to content

Commit

Permalink
add integration tests. Fix some 1.1 issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
elliotstokes committed May 12, 2014
1 parent 0cf96fb commit 45eef40
Show file tree
Hide file tree
Showing 5 changed files with 31,054 additions and 27 deletions.
63 changes: 37 additions & 26 deletions lib/gpx-parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ var _getWayPoints = function(gpxWaypoints) {
var waypoints = [],
currentWaypoint = null,
point = null;

//grab waypoints
for (var i = 0, il = gpxWaypoints.length; i < il; i++) {
currentWaypoint = gpxWaypoints[i];
point = new GpxWaypoint(currentWaypoint.$.lat, currentWaypoint.$.lon, currentWaypoint.ele, currentWaypoint.time);
waypoints.push(point);
if (gpxWaypoints !== null && gpxWaypoints !== undefined) {
//grab waypoints
for (var i = 0, il = gpxWaypoints.length; i < il; i++) {
currentWaypoint = gpxWaypoints[i];
point = new GpxWaypoint(currentWaypoint.$.lat, currentWaypoint.$.lon, currentWaypoint.ele, currentWaypoint.time);
waypoints.push(point);
}
}

return waypoints;
Expand All @@ -34,33 +35,40 @@ var _getRoutes = function(gpxRoutes) {
//grab routes
var routes = [],
route = null;
if (gpxRoutes !== null && gpxRoutes !== undefined) {
for (var i = 0, il = gpxRoutes.length; i < il; i++) {
//clear out route points
var routePoints = [],
currentRoute = gpxRoutes[i];

for (var j = 0, jl = currentRoute.rtept.length; j < jl; j++) {
routePoints.push(new GpxWaypoint(currentRoute.rtept[j].$.lat, currentRoute.rtept[j].$.lon));
}

for (var i = 0, il = gpxRoutes.length; i < il; i++) {
//clear out route points
var routePoints = [],
currentRoute = gpxRoutes[i];

for (var j = 0, jl = currentRoute.rtept.length; j < jl; j++) {
routePoints.push(new GpxWaypoint(currentRoute.rtept[j].$.lat, currentRoute.rtept[j].$.lon));
}

route = new GpxRoute(gpxRoutes.name, gpxRoutes.cmt, gpxRoutes.desc, routePoints);
route = new GpxRoute(gpxRoutes.name, gpxRoutes.cmt, gpxRoutes.desc, routePoints);


routes.push(route);
routes.push(route);
}
}

return routes;
};

var _getTracks = function(gpxTracks) {
//grab tracks
var tracks = [];
var tracks = [],
trackName = null;

for (var i = 0, il = gpxTracks.length; i < il; i++) {

var trackSegments = [],
currentTrack = gpxTracks[i];
currentTrack = gpxTracks[i],
trackName;

if (currentTrack.name) {
trackName = currentTrack.name[0];
}

for (var j = 0, jl = currentTrack.trkseg.length; j < jl; j++) {

Expand All @@ -78,7 +86,7 @@ var _getTracks = function(gpxTracks) {
trackSegments.push(trackSegement);
}

tracks.push(new GpxTrack(trackSegments));
tracks.push(new GpxTrack(trackSegments, trackName));
}

return tracks;
Expand All @@ -94,19 +102,23 @@ var _ParseV10 = function(gpx) {

extent = new GpxExtent();
metadata = new GpxMetaData(gpx.$.creator, gpx.time, extent);

return new GpxResult(metadata, _getWayPoints(gpx.wpt), _getRoutes(gpx.rte), _getTracks(gpx.trk));
};

var _ParseV11 = function(gpx) {
var metadata;
var metadata,
time;
if (gpx.metadata && gpx.metadata[0]) {
metadata = new GpxMetaData(gpx.$.creator, gpx.metadata[0].time[0]);
if (gpx.metadata[0].time) {
time = gpx.metadata[0].time[0];
}
metadata = new GpxMetaData(gpx.$.creator, time);
} else {
metadata = new GpxMetaData();
}

return new GpxResult(metadata);
return new GpxResult(metadata, _getWayPoints(gpx.wpt), _getRoutes(gpx.rte), _getTracks(gpx.trk));
};

/**
Expand All @@ -115,13 +127,12 @@ var _ParseV11 = function(gpx) {
* @param {gpxParseCompleteCallback} callback Callback function to call when parse has completed.
*/
exports.parseGpx = function(gpxString, callback) {

var parseString = require('xml2js').parseString,
gpxResult = null,
version = null;

parseString(gpxString, function(error, data) {

if (error) {
callback(error, null);
return;
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
},
"scripts": {
"test": "node_modules/.bin/nodeunit tests/",
"integration" : "node_modules/.bin/nodeunit tests/integration/",
"coveralls": "jscoverage lib && GPXPARSE_COV=1 nodeunit --reporter=lcov tests | coveralls",
"lcov": "jscoverage lib && GPXPARSE_COV=1 nodeunit --reporter=lcov tests"
}
Expand Down
4 changes: 3 additions & 1 deletion tests/gpx-v1.1-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ module.exports = {
gpxParse.parseGpx(successfulGpx, function(error, result) {
test.equal(result.metadata.creator, "Oregon 400t");
test.equal(result.metadata.time, "2009-10-17T22:58:43Z");
test.equal(result.tracks.length, 1);
test.equal(result.tracks[0].name, 'Example GPX Document');
test.done();
});

Expand All @@ -76,4 +78,4 @@ module.exports = {
//});

}
};
};
Loading

0 comments on commit 45eef40

Please sign in to comment.