From 50110390870187c837406d657aa1cdee2d33ffef Mon Sep 17 00:00:00 2001 From: Quincy Morgan Date: Thu, 9 Jan 2020 09:49:31 -0500 Subject: [PATCH] Clarify some variable names in osmEntity.deprecatedTags and add more code tests --- modules/osm/entity.js | 20 +++++++++++--------- test/spec/osm/entity.js | 40 +++++++++++++++++++++++++++++++++++++--- 2 files changed, 48 insertions(+), 12 deletions(-) diff --git a/modules/osm/entity.js b/modules/osm/entity.js index a86635564..f7b3595ef 100644 --- a/modules/osm/entity.js +++ b/modules/osm/entity.js @@ -194,20 +194,22 @@ osmEntity.prototype = { var deprecated = []; dataDeprecated.forEach(function(d) { - var matchesDeprecatedTags = Object.keys(d.old).every(function(key) { - if (!tags[key]) return false; - if (d.old[key] === '*') return true; + var oldKeys = Object.keys(d.old); + var matchesDeprecatedTags = oldKeys.every(function(oldKey) { + if (!tags[oldKey]) return false; + if (d.old[oldKey] === '*') return true; - var vals = tags[key].split(';').filter(Boolean); + var vals = tags[oldKey].split(';').filter(Boolean); if (vals.length === 0) { return false; } else if (vals.length > 1) { - return vals.indexOf(d.old[key]) !== -1; + return vals.indexOf(d.old[oldKey]) !== -1; } else { - if (tags[key] === d.old[key]) { - if (d.replace && d.old[key] === d.replace[key]) { - return !Object.keys(d.replace).every(function(key) { - return tags[key] === d.replace[key]; + if (tags[oldKey] === d.old[oldKey]) { + if (d.replace && d.old[oldKey] === d.replace[oldKey]) { + var replaceKeys = Object.keys(d.replace); + return !replaceKeys.every(function(replaceKey) { + return tags[replaceKey] === d.replace[replaceKey]; }); } else { return true; diff --git a/test/spec/osm/entity.js b/test/spec/osm/entity.js index 6926b1afd..5a74916df 100644 --- a/test/spec/osm/entity.js +++ b/test/spec/osm/entity.js @@ -221,16 +221,50 @@ describe('iD.osmEntity', function () { }); }); - describe('#hasDeprecatedTags', function () { - it('returns false if entity has no tags', function () { + describe('#deprecatedTags', function () { + it('returns none if entity has no tags', function () { expect(iD.osmEntity().deprecatedTags()).to.eql([]); }); - it('returns true if entity has deprecated tags', function () { + it('returns none when no tags are deprecated', function () { + expect(iD.osmEntity({ tags: { amenity: 'toilets' } }).deprecatedTags()).to.eql([]); + }); + + it('returns 1:0 replacement', function () { + expect(iD.osmEntity({ tags: { highway: 'no' } }).deprecatedTags()).to.eql( + [{ old: { highway: 'no' } }] + ); + }); + + it('returns 1:1 replacement', function () { expect(iD.osmEntity({ tags: { amenity: 'toilet' } }).deprecatedTags()).to.eql( [{ old: { amenity: 'toilet' }, replace: { amenity: 'toilets' } }] ); }); + + it('returns 1:1 wildcard', function () { + expect(iD.osmEntity({ tags: { speedlimit: '50' } }).deprecatedTags()).to.eql( + [{ old: { speedlimit: '*' }, replace: { maxspeed: '$1' } }] + ); + }); + + it('returns 1:2 total replacement', function () { + expect(iD.osmEntity({ tags: { man_made: 'water_tank' } }).deprecatedTags()).to.eql( + [{ old: { man_made: 'water_tank' }, replace: { man_made: 'storage_tank', content: 'water' } }] + ); + }); + + it('returns 1:2 partial replacement', function () { + expect(iD.osmEntity({ tags: { man_made: 'water_tank', content: 'water' } }).deprecatedTags()).to.eql( + [{ old: { man_made: 'water_tank' }, replace: { man_made: 'storage_tank', content: 'water' } }] + ); + }); + + it('returns 2:1 replacement', function () { + expect(iD.osmEntity({ tags: { amenity: 'gambling', gambling: 'casino' } }).deprecatedTags()).to.eql( + [{ old: { amenity: 'gambling', gambling: 'casino' }, replace: { amenity: 'casino' } }] + ); + }); }); describe('#hasWikidata', function () {