Skip to content

Commit

Permalink
copy tests from osm-adiff-parser
Browse files Browse the repository at this point in the history
  • Loading branch information
Ajith Ranka committed Mar 9, 2017
1 parent 2251d00 commit 4fc3acb
Show file tree
Hide file tree
Showing 11 changed files with 1,106,394 additions and 93 deletions.
501,308 changes: 501,308 additions & 0 deletions tests/data/2360001.json

Large diffs are not rendered by default.

127,626 changes: 127,626 additions & 0 deletions tests/data/2360001.osc

Large diffs are not rendered by default.

28,929 changes: 28,929 additions & 0 deletions tests/data/2360002.json

Large diffs are not rendered by default.

7,160 changes: 7,160 additions & 0 deletions tests/data/2360002.osc

Large diffs are not rendered by default.

38,425 changes: 38,425 additions & 0 deletions tests/data/2360003.json

Large diffs are not rendered by default.

10,127 changes: 10,127 additions & 0 deletions tests/data/2360003.osc

Large diffs are not rendered by default.

297,925 changes: 297,925 additions & 0 deletions tests/data/2360004.json

Large diffs are not rendered by default.

75,872 changes: 75,872 additions & 0 deletions tests/data/2360004.osc

Large diffs are not rendered by default.

15,146 changes: 15,146 additions & 0 deletions tests/data/2360005.json

Large diffs are not rendered by default.

3,853 changes: 3,853 additions & 0 deletions tests/data/2360005.osc

Large diffs are not rendered by default.

116 changes: 23 additions & 93 deletions tests/index.js
Original file line number Diff line number Diff line change
@@ -1,93 +1,23 @@
var R = require('ramda');
var sax = require('sax');

// Returns elements grouped by changeset ID.

function AugmentedDiffParser (xmlData, changesetsFilter, callback) {
var xmlParser = sax.parser(true, { lowercase: true });
var currentAction = '';
var currentElement = {};
var oldElement = {};
var currentMember = {};
var currentMode = '';
var changesetMap = {};

function isElement (symbol) {
return (symbol === 'node' || symbol === 'way' || symbol === 'relation');
}

function endTag (symbol) {
if (symbol === 'action') {
var changeset = currentElement.changeset;
if (changesetsFilter && changesetsFilter.length) {
if (changesetsFilter.indexOf(changeset) !== -1) {
if (changesetMap[changeset]) {
changesetMap[changeset].push(currentElement);
} else {
changesetMap[changeset] = [currentElement];
}
}
} else {
if (changesetMap[changeset]) {
changesetMap[changeset].push(currentElement);
} else {
changesetMap[changeset] = [currentElement];
}
}
}
if (symbol === 'osm') {
callback(null, changesetMap);
}
}

function startTag (node) {
var symbol = node.name;
var attrs = node.attributes;

if (symbol === 'action') {
currentAction = attrs.type;
}
if (symbol === 'new' || symbol === 'old') {
currentMode = symbol;
}
if (isElement(symbol)) {
if (currentMode === 'new' && (currentAction === 'modify' ||
currentAction === 'delete')) {
oldElement = R.clone(currentElement);
currentElement = attrs;
currentElement.old = oldElement;
} else {
currentElement = attrs;
}
currentElement.action = currentAction;
currentElement.type = symbol;
currentElement.tags = {};
if (symbol === 'way') {currentElement.nodes = []; }
if (symbol === 'relation') {currentElement.members = []; currentMember = {};}
}
if (symbol === 'tag' && currentElement) {
currentElement.tags[attrs.k] = attrs.v;
}

if (symbol === 'nd' && currentElement && currentElement.type === 'way') {
currentElement.nodes.push(attrs);
}

if (symbol === 'nd' && currentElement && currentElement.type === 'relation') {
currentMember.nodes.push(attrs);
}

if (symbol === 'member' && currentElement && currentElement.type === 'relation') {
currentMember = R.clone(attrs);
currentMember.nodes = [];
currentElement.members.push(currentMember);
}
}

xmlParser.onopentag = startTag;
xmlParser.onclosetag = endTag;
xmlParser.onerror = function(err) { callback(err, null); };
xmlParser.write(xmlData);
}

module.exports = AugmentedDiffParser;
const fs = require('fs');
const tape = require('tape');
const parser = require('../index.js');

const isOSCFile = (filename) => (/^.*\.osc$/).test(filename);
const stripExtension = (filename) => filename.split('.')[0];

const filenames =
fs.readdirSync('tests/data', { encoding: 'utf-8' })
.filter(isOSCFile)
.map(stripExtension);

filenames.forEach(function(filename) {
tape(`testing file: ${filename}`, function(t) {
const xml = fs.readFileSync(`tests/data/${filename}.osc`, { encoding: 'utf-8' });
const expectedJSON = JSON.parse(fs.readFileSync(`tests/data/${filename}.json`));

parser(xml, null, function(error, actualJSON) {
t.deepEqual(actualJSON, expectedJSON, 'parsed correctly');
t.end();
});
});
});

0 comments on commit 4fc3acb

Please sign in to comment.