From 4901f51ee5e23b929af2e95fd5020a4bbbd83a5e Mon Sep 17 00:00:00 2001 From: "Amir E. Aharoni" Date: Tue, 22 Dec 2020 16:17:19 +0200 Subject: [PATCH] Add a simple test for validity of language codes (#132) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The reason I'm adding it is that recently I almost submitted a patch in which I wrote a language code with the Cyrillic letter ะพ instead of the corresponding Latin letter. It's not a perfect test, but it's good enough for a basic check. --- tests/js/index.js | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/tests/js/index.js b/tests/js/index.js index 09e6a76..df48e8d 100644 --- a/tests/js/index.js +++ b/tests/js/index.js @@ -2,7 +2,8 @@ var languageData = require( __dirname + '/../../src/index' ), assert = require( 'assert' ); describe( 'languagedata', function () { - var orphanScripts, badRedirects, doubleRedirects, doubleAutonyms, languagesWithoutAutonym; + var orphanScripts, badRedirects, invalidCodes, + doubleRedirects, doubleAutonyms, languagesWithoutAutonym; /* * Runs over all script codes mentioned in langdb and checks whether * they belong to the 'Other' group. @@ -32,6 +33,22 @@ describe( 'languagedata', function () { } return result; }; + /* + * Runs over all languages and checks that all redirects have a valid target. + */ + invalidCodes = function () { + var languageCode, + invalidCharsRe = /[^0-9a-z-]/, + result = []; + + for ( languageCode in languageData.getLanguages() ) { + if ( languageCode.match( invalidCharsRe ) ) { + result.push( languageCode ); + } + } + + return result; + }; /* * Runs over all languages and checks that all redirects point to a language. * There's no reason to have double redirects. @@ -89,6 +106,7 @@ describe( 'languagedata', function () { it( 'language tags', function () { assert.ok( languageData.isKnown( 'ar' ), 'Language is unknown' ); assert.ok( !languageData.isKnown( 'unknownLanguageCode!' ), 'Language is known' ); + assert.deepEqual( invalidCodes(), [], 'All language codes have no invalid characters.' ); } ); it( 'autonyms', function () {