Aller au contenu

Module:Unité/Test

Une page de Wikipédia, l'encyclopédie libre.
Ceci est une version archivée de cette page, en date du 21 août 2017 à 12:04 et modifiée en dernier par Zebulon84 (discuter | contributions). Elle peut contenir des erreurs, des inexactitudes ou des contenus vandalisés non présents dans la version actuelle.

 Documentation[voir] [modifier] [historique] [purger]

Ce module est principalement destiné à implémenter le modèle {{Unité}}.

Utilisation

Fonctions exportables

  • unite( frame ) – implémente le modèle unité. Les paramètres sont pris soit au niveau du modèle appelant le module via #invoke, soit directement dans la table fournie lorsque la fonction est appelée depuis un autre module. Essaye de parser les deux premiers paramètres pour facilité la saisie (par exemple fonction avec p.unite{ '1.23 ±0.05 e5 m/s-2' }) ;
  • _unite( args ) – affiche l'unité à partir des paramètres classiques du modèle Unité (exemple p._unite{ '1.23', 'm', '/s', '-2', ['±'] = '0.05', e='5' }) ;
  • formatNombres( texte ) – formate tous les nombres de la chaine fournie suivant les conventions du français ;
  • formatNombre( nombre ) – transforme un nombre formaté ou non en chaine formatée suivant les conventions du français ; si la chaine n'est pas reconnue comme un nombre, elle n'est pas modifiée ;
  • _formatNum( num ) – transforme un number, ou une chaine correspondant à un number en chaine formatée suivant les conventions du français ; si le paramètre ne représente pas un number lua il est retourné sans modification ;
  • parseNombre( nombre ) – transforme si possible une chaine formatée en un chaine interprétable par tonumber() (retourne une chaine pour éviter les arrondis éventuels de lua) ; les chaines non reconnues sont retournées sans modification.

Autres fonctions

  • sanitizeNum( nombre ) – transforme les signes moins en tiret, les espaces insécables en espace simple (simplifie les pattern ultérieures) ;
  • parseUnit( texte ) – essaye de séparer une chaine en différents paramètres du modèle unité ;
  • nomUnit( unit, exposant ) – retourne si possible le nom de l'unité et son exposant en toute lettre.

Modules externes et autres éléments dont ce module a besoin pour fonctionner

  • Module:Unité/Data – Liste d'unités et de multiples, avec leur abréviation et leur nom en toute lettre.
  • Module:Delink – Utilisé pour supprimer les liens des unités pour essayer de les reconnaitre.

Exemples

Pour des exemples, voir la page de test permettant de tester diverses modifications apportées.

Voir aussi : les tests unitaires et ceux du bac à sable.

-- Test unitaire pour [[Module:Date]]. Cliquer sur « Discussion » pour voir le résultat du test.
local p = require('Module:UnitTests')

local Unit = require( 'Module:' .. mw.title.getCurrentTitle().baseText )
local template = mw.title.getCurrentTitle().baseText

local tableExemple = { '1' }
local frame = mw.getCurrentFrame()
local ref = frame:extensionTag( 'ref', 'blabla' )
local nowiki = frame:extensionTag( 'nowiki', '[[Arles]] : ' )

function p:test01_sanitizeNum()
	local function test( param, result, comment )
		local text = tostring( param )
		if comment then
			if text:match( '\n$' ) then
				text = text .. '(' .. comment .. ')'
			else
				text = text .. ' (' .. comment .. ')'
			end
		end
		self:equals( text, Unit.sanitizeNum( param ), result )
	end
	test( nil, '', 'pas de paramètre' )
	test( '', '', 'chaine vide' )
	test( '1', '1' )
	test( '1234', '1234' )
	test( ' 1234', '1234', 'espace avant' )
	test( '1234 ', '1234', 'espace après' )
	test( '1234\n', '1234', 'retour ligne après' )
	test( '	1234', '1234', 'tab avant' )
	test( '1234 ', '1234', 'espace insécable après' )
	test( '1.234', '1.234' )
	test( '1,234', '1,234' )
	test( '1,234,567', '1,234,567' )
	test( '1 234', '1 234' )
	test( '1 234', '1 234', 'espace insécable' )
	test( '1 234', '1 234', 'espace fine' )
	test( '1 234', '1 234', 'espace fine insécable' )
	test( '1 234', '1 234', ' ' )
	test( '-1234', '-1234' )
	test( '−1234', '-1234', 'signe moins' )
	test( '–1234', '-1234', 'demi cadratin' )
	test( '−1234', '-1234', 'amp;minus;' )
	test( nowiki .. '1234' .. ref, nowiki .. '1234' .. ref, 'avec strip marker' )
end

function p:test02_parseNombre()
	local function test( param, result, comment )
		local text = tostring( param )
		if comment then
			text = text .. ' (' .. comment .. ')'
		end
		self:equals( text, Unit.parseNombre( param ), result )
	end
	test( nil, '', 'pas de paramètre' )
	test( '', '', 'chaine vide' )
	test( '1', '1' )
	test( '1234', '1234' )
	test( '1 234', '1234' )
	test( '1 234', '1234', 'espace insécable' )
	test( '1 234', '1234', ' ' )
	test( '12345678901234567890', '12345678901234567890' )
	test( '12 345 678 901 234 567 890', '12345678901234567890' )
	test( '1.234', '1.234' )
	test( '1,234', '1.234' )
	test( '1.234,567', '1.234567', 'pour être compatible avec l’ancien comportement du modèle' )
	test( '1,234.567', '1234.567' )
	test( '-1234,567', '-1234.567' )
	test( '−1234,567', '-1234.567', 'signe moins' )
end

function p:test03_formatNum()
	local function test( param, result, comment )
		local text = tostring( param )
		if type( param ) == 'table' then
			text = table_to_str( param )
		end
		if comment then
			text = text .. ' (' .. comment .. ')'
		end
		self:equals( text, Unit.formatNum( param ), result )
	end
	test( nil, nil, 'pas de paramètre' )
	test( '', '', 'chaine vide' )
	test( '1', '1' )
	test( '1234', '1 234' )
	test( '12345678901234567890', '12 345 678 901 234 567 890' )
	test( '1.234', '1,234' )
	test( '1.2345', '1,2345' )
	test( '1.234567', '1,234 567' )
	test( '1.2345678901234567890', '1,234 567 890 123 456 789 0' )
	test( '1234.56789', '1 234,567 89' )
	test( '1.230', '1,230' )
	test( '-1', '−1' )
	test( '-1234.56', '−1 234,56' )
	test( 1234, '1 234', 'number' )
	test( 1234.567890, '1 234,567 89' )
	test( -1234, '−1 234' )
	test( { '1234' }, '1 234', 'table' )
	test( { 1234 }, '1 234', 'table contenant un number' )
	test( { '1.234', round  = '2' }, '1,23' )
	test( { '1.2', round  = '2' }, '1,2' )
	test( { '1234', round  = '2' }, '1 234' )
	test( { '1234', round  = '-1' }, '1 230' )
	test( { 1.234, round  = 2 }, '1,23', 'numbers' )
	test( { '1.234', decimals = '2' }, '1,23' )
	test( { '1.2', decimals  = '2' }, '1,20' )
	test( { '1.2', decimals  = '5' }, '1,200 00' )
	test( { '1234', decimals  = '2' }, '1 234,00' )
	test( { '1234', decimals  = '-1' }, '1 230' )
	test( '1.234e5', '1,234 ×10<sup>5</sup>' )
	test( '1.234E5', '1,234 ×10<sup>5</sup>' )
	test( 1.234e5, '123 400', 'number sous forme 1.234e5' )
	test( 1.234e18, '1,234 ×10<sup>18</sup>', 'number sous forme 1.234e18' )
	test( '-1.234e-5', '−1,234 ×10<sup>−5</sup>' )
	test( { '1.234e5' }, '1,234 ×10<sup>5</sup>' )
	test( { '1.234e5', noHtml = true }, '1,234 ×10⁵' )
end

function p:test04_formatNombre()
	local function test( param, param2, param3, result, comment )
		local text = tostring( param )
		if param2 or param3 then
			text = param ..  ' / ' .. tostring( param2 ) ..  ' / ' .. tostring( param3 )
		end
		if comment then
			text = text .. ' (' .. comment .. ')'
		end
		self:equals( text, Unit.formatNombre( param, param2, param3 ), result )
	end
	test( nil, nil, nil, '', 'pas de paramètre' )
	test( '', nil, nil, '', 'chaine vide' )
	test( '1', nil, nil, '1' )
	test( '−1&nbsp;234', nil, nil, '−1 234' )
	test( '1.234', '2', nil, '1,23' )
	test( '1.234', nil, '2', '1,23' )
	test( '1.234', nil, '5', '1,234 00' )
	test( '1', nil, '2', '1,00' )
	test( '', '2', nil, '' )
	test( '', nil, '2', '' )
end

function p:test05_formatNombres()
	local function test( param, param2, param3, result, comment )
		local text = tostring( param )
		if param2 or param3 then
			text = param ..  ' / ' .. tostring( param2 ) ..  ' / ' .. tostring( param3 )
		end
		if comment then
			text = text .. ' (' .. comment .. ')'
		end
		self:equals( text, Unit.formatNombres( param, param2, param3 ), result )
	end
	test( nil, nil, nil, '', 'pas de paramètre' )
	test( '', nil, nil, '', 'chaine vide' )
	test( '1', nil, nil, '1' )
	test( '−1&nbsp;234.5', nil, nil, '−1 234,5' )
	test( '5.678e3', nil, nil, '5,678 ×10<sup>3</sup>' )
	test( '1.567,89', nil, nil, '1,567 89' )
	test( '1.234', '2', nil, '1,23' )
	test( '1.234', nil, '2', '1,23' )
	test( '1.234', nil, '5', '1,234 00' )
	test( 'environ 1234.5', nil, nil, 'environ 1 234,5' )
	test( 'entre −1&nbsp;234.5 et 5.678e3', nil, nil, 'entre −1 234,5 et 5,678 ×10<sup>3</sup>' )
	test( 'de 0,2 jusqu\'à 1.567,89', nil, 5, 'de 0,200 00 jusqu\'à 1,567 89' )
	test( '1234-5678', nil, nil, '1 234-5 678' )
	test( nowiki .. '1234', nil, nil, nowiki .. '1 234', 'avec nowiki' )
	test( '1234' .. ref, nil, nil, '1 234' .. ref, 'avec ref' )
end

function p:test06_parseUnit()
	local function test( param, result, comment )
		local text = tostring( param )
		if comment then
			text = text .. ' (' .. comment .. ')'
		end
		self:equals_deep( text, Unit.parseUnit( param ), result )
	end
	test( nil, {}, 'pas de paramètre' )
	test( '', {}, 'chaine vide' )
	test( '1', { '1' } )
	test( '1,234.567', { '1,234.567' } )
	test( '1 234', { '1 234' } )
	test( '&minus;1&nbsp;234', { '-1 234' } )
	test( '1 m', { '1', 'm' } )
	test( '1 m2', { '1', 'm', '2' } )
	test( '1 km²', { '1', 'km', '2' } )
	test( '1 m³', { '1', 'm', '3' } )
	test( '1 m<sup>2</sup>', { '1', 'm', '2' }, 'HTML <nowiki><sup></nowiki>' )
	test( '1 m2 s-1', { '1', 'm', '2', 's', '-1' } )
	test( '1 m2/s', { '1', 'm', '2', '/s' } )
	test( '1 J2K3s-1', { '1', 'J', '2', 'K', '3', 's', '-1' } )
	test( '1 J kg m−2', { '1', 'J', '', 'kg', '', 'm', '-2' } )
	test( '1 kilomètres par heure', { '1', 'kilomètres', '', 'par', '', 'heure' } )
	test( '1 [[mètre carré|m{{exp|2}}]]', { '1', '[[mètre carré|m{{exp|2}}]]' } )
	test( '1 [[mètre carré|m]]2', { '1', '[[mètre carré|m]]', '2' } )
	test( '10 [[parsec]]s', { '10', '[[parsec]]s' } )
	test( '1.23456789e15', { '1.23456789', e='15' } )
	test( '1.23456789 e15', { '1.23456789', e='15' } )
	test( '1.23456789x10e-15', { '1.23456789', e='-15' } )
	test( '40.234°C', { '40.234', '°C' } )
	test( 'm', { false, 'm' } )
	test( 'm2', { false, 'm', '2' } )
	test( 'e3 m2', { false, 'm', '2', e='3' } )
	test( 'x10e3 m2', { false, 'm', '2', e='3' } )
	test( '10{{3}} m2', { false, 'm', '2', e='3' } )
	test( '55 à 56 cal', { '55', 'cal', ['à'] = '56' } )
	test( '55 et 56 cal', { '55', 'cal', et = '56' } )
	test( '55-56 cal', { '55', 'cal', ['–'] = '56' } )
	test( '100±9 mm', { '100', 'mm', ['±'] = '9' } )
	test( '100+-9 mm', { '100', 'mm', ['±'] = '9' } )
	test( '100,5 +/- 9.3 e5 mm', { '100,5', 'mm', ['±'] = '9.3', e = '5' } )
	test( '95+5-4 m', { '95', 'm', ['+'] = '5', ['−'] = '4' } )
	test( '29.7 x 21 cm', { '29.7', 'cm', ['×'] = '21' } )
	test( '50x25x3 m', { '50', 'm', ['×'] = '25', ['××'] = '3' } )
	test( '[[Kilojoule|kJ]] [[Mole (unité)|mol]]-1', { false, '[[Kilojoule|kJ]]', '', '[[Mole (unité)|mol]]', '-1' } )
	test( 'hab./km2', { false, 'hab.', '', '/km', '2' } )
	test( 'fl oz', { false, 'fl oz' } )
	test( 'fl. oz.', { false, 'fl. oz.' } )
	test( 'fl.oz.', { false, 'fl.oz.' } )
	test( '1.266,865,3 e17 m3s−2', { '1.266,865,3', 'm', '3', 's', '-2', e = "17" } )
	test( '2.518 021 002… eV', { '2.518 021 002…', 'eV' } )
	test( '2 1/2 lb', { '2', 'lb', fraction = '1/2' } )
	test( '2 ½ lb', { '2', 'lb', fraction = '½' } )
	test( '1/2 lb', { '', 'lb', fraction = '1/2' } )
	test( '1/2', { '', fraction = '1/2' } )
	test( '1 / 2 lb', { '1', 'lb', ['/'] = '2' } )
	test( '(6~10)', { '(6~10)' } )
	test( '? m', { '?', 'm' } )
	test( '2 €', { '2', '€' } )
	test( '€', { false, '€' } )
	test( '+1.23 m', { '+1.23', 'm' } )
	test( '~2 m', { '~2', 'm' } )
	test( '~ 2 m', { '~ 2', 'm' } )
	test( '1.23 « m »', { '1.23', '«','', 'm','', '»' } )
	test( '« m »', { false, '«','', 'm','', '»' } )
	test( '3 km/€', { '3', 'km', '', '/€' } )
	test( '> 2 km', { '> 2', 'km' } )
	test( '>2 km', { '>2', 'km' } )
	
end

function p:test07_nomUnit()
		
end

function p:test08__unite()

end

p['test09_modèle_unité'] = function ( self )
	self:preprocess_equals_many( '{{' .. template .. '|', '}}', {
        { '1', '1' },
        { '1234567', '1 234 567' },
        { '-1234567.89', '−1 234 567,89' },
        { '1.23456789|e=15', '<span class="nowrap">1,234 567 89 × 10<sup>15</sup></span>' },
        { '1.234 567 89|e=15', '<span class="nowrap">1,234 567 89 × 10<sup>15</sup></span>' },
        { '1.234,567,89|e=15', '<span class="nowrap">1,234 567 89 × 10<sup>15</sup></span>' },
        { '10000|km', '<span class="nowrap">10 000 <abbr class=abbr title="kilomètre">km</abbr></span>' },
        { '10000|km/h', '<span class="nowrap"><span title="2 777,78 m/s">10 000</span> <abbr class=abbr title="kilomètre par heure">km/h</abbr></span>' },
        { '10000|km|2', '<span class="nowrap">10 000 <abbr class=abbr title="kilomètre carré">km<sup>2</sup></abbr></span>' },
        { '10000|km|3', '<span class="nowrap">10 000 <abbr class=abbr title="kilomètre cube">km<sup>3</sup></abbr></span>' },
        { '10000|kilomètres par heure', '<span class="nowrap"><span title="2 777,78 m/s">10 000</span> kilomètres par heure</span>' },
        { '10000|km||h|-1', '<span class="nowrap"><span title="2 777,78 m/s">10 000</span> <abbr class=abbr title="kilomètre par heure">km h<sup>−1</sup></abbr></span>' },
        { '10000|J|2|K|3|s|-1', '<span class="nowrap">10 000 <abbr class=abbr title="joule carré kelvin cube par seconde">J<sup>2</sup> K<sup>3</sup> s<sup>−1</sup></abbr></span>' },
        { '10000|J||kg||m|-2', '<span class="nowrap">10 000 <abbr class=abbr title="joule kilogramme par mètre carré">J kg m<sup>−2</sup></abbr></span>' },
        { '−40.234|°C', '<span class="nowrap"><span title="−40,4 °F ou 232,9 K">−40,234</span> <abbr class=abbr title="degré Celsius">°C</abbr></span>' },
        { '1|[[mètre carré|m{{exp|2}}]]', '<span class="nowrap">1 [[mètre carré|m<sup>2</sup>]]</span>' },
        { '1|[[mètre carré|m]]|2', '<span class="nowrap">1 <abbr class=abbr title="mètre carré">[[mètre carré|m]]<sup>2</sup></abbr></span>' },
        { '10{{exp|−1}}|m', '<span class="nowrap">10<sup>−1</sup> <abbr class=abbr title="mètre">m</abbr></span>' },
        { '|e=-1|m', '<span class="nowrap">10<sup>−1</sup> <abbr class=abbr title="mètre">m</abbr></span>' },
        { 'e=-1|m', '<span class="nowrap">10<sup>−1</sup> <abbr class=abbr title="mètre">m</abbr></span>' },
        { '55|à=56|cal', '<span class="nowrap"><span title="230,4511 J">55</span> à <span title="234,641 12 J">56</span> <abbr class=abbr title="calorie">cal</abbr></span>' },
        { '100|±=9|mm', '<span class="nowrap">100 ± 9 <abbr class=abbr title="millimètre">mm</abbr></span>' },
        { '1.23|±=0.09|e=5|Bq', '<span class="nowrap">(1,23 ± 0,09) × 10<sup>5</sup> <abbr class=abbr title="becquerel">Bq</abbr></span>' },
        { '100|+=5|−=4|mm', '<span class="nowrap">100<span style="display:inline-block; padding-left:0.2em; vertical-align:top; line-height:1em; font-size:80%; text-align:left;">+5<br> −4</span> <abbr class=abbr title="millimètre">mm</abbr></span>' },
        { '18.23|g||l|-1|M=36.46', '<span class="nowrap"><span title="0,5 mol/l">18,23</span> <abbr class=abbr title="gramme par litre">g l<sup>−1</sup></abbr></span>' },
        { '1.82|g||cm|-3|M=30.973762', '<span class="nowrap"><span title="1 820 kg/m³ ou 1,701 855 ×10⁻⁵ m³/mol">1,82</span> <abbr class=abbr title="gramme par centimètre cube">g cm<sup>−3</sup></abbr></span>' },
        { '1.23456789 e15', '<span class="nowrap">1,234 567 89 × 10<sup>15</sup></span>' },
        { '-1.23456789 x10e-15', '<span class="nowrap">−1,234 567 89 × 10<sup>−15</sup></span>' },
        { '10000 km/h', '<span class="nowrap"><span title="2 777,78 m/s">10 000</span> <abbr class=abbr title="kilomètre par heure">km/h</abbr></span>' },
        { '10000 km2', '<span class="nowrap">10 000 <abbr class=abbr title="kilomètre carré">km<sup>2</sup></abbr></span>' },
        { '10000 km²', '<span class="nowrap">10 000 <abbr class=abbr title="kilomètre carré">km<sup>2</sup></abbr></span>' },
        { '10000 m3', '<span class="nowrap">10 000 <abbr class=abbr title="mètre cube">m<sup>3</sup></abbr></span>' },
        { '10000 km h-1', '<span class="nowrap"><span title="2 777,78 m/s">10 000</span> <abbr class=abbr title="kilomètre par heure">km h<sup>−1</sup></abbr></span>' },
        { '10 000 J2 K3 s-1', '<span class="nowrap">10 000 <abbr class=abbr title="joule carré kelvin cube par seconde">J<sup>2</sup> K<sup>3</sup> s<sup>−1</sup></abbr></span>' },
        { '−40,234 °C', '<span class="nowrap"><span title="−40,4 °F ou 232,9 K">−40,234</span> <abbr class=abbr title="degré Celsius">°C</abbr></span>' },
        { 'e-1 m', '<span class="nowrap">10<sup>−1</sup> <abbr class=abbr title="mètre">m</abbr></span>' },
        { 'x10e3 m', '<span class="nowrap">10<sup>3</sup> <abbr class=abbr title="mètre">m</abbr></span>' },
        { '55 ou 56 cal', '<span class="nowrap"><span title="230,4511 J">55</span> ou <span title="234,641 12 J">56</span> <abbr class=abbr title="calorie">cal</abbr></span>' },
        { '100±9 mm', '<span class="nowrap">100 ± 9 <abbr class=abbr title="millimètre">mm</abbr></span>' },
        { '1.23 +/-0.09 e5 Bq', '<span class="nowrap">(1,23 ± 0,09) × 10<sup>5</sup> <abbr class=abbr title="becquerel">Bq</abbr></span>' },
        { '100+5-4 mm', '<span class="nowrap">100<span style="display:inline-block; padding-left:0.2em; vertical-align:top; line-height:1em; font-size:80%; text-align:left;">+5<br> −4</span> <abbr class=abbr title="millimètre">mm</abbr></span>' },
        { '29.7 x 21 cm', '<span class="nowrap">29,7 × 21 <abbr class=abbr title="centimètre">cm</abbr></span>' },
        { '50x25x3 m', '<span class="nowrap">50 × 25 × 3 <abbr class=abbr title="mètre">m</abbr></span>' },
        { '123,45678 | décimales=1', '123,5' },
        { '123,45678 | décimales=-1', '120' },
        { '123,45678 | décimales=6', '123,456 780' },
        { '1,2345 e5 m3 | décimales=1', '<span class="nowrap">1,2 × 10<sup>5</sup> <abbr class=abbr title="mètre cube">m<sup>3</sup></abbr></span>' },
        { '1,2345 e5 m3 | décimales=6', '<span class="nowrap">1,234 500 × 10<sup>5</sup> <abbr class=abbr title="mètre cube">m<sup>3</sup></abbr></span>' },
        { '2.518 021 002…|e=-8|W||m|-2', '<span class="nowrap">2,518 021 002… × 10<sup>−8</sup> <abbr class=abbr title="watt par mètre carré">W m<sup>−2</sup></abbr></span>' },
        { '1000|[[Paire de bases|bp]]||s|-1', '<span class="nowrap">1 000 <abbr class=abbr title="bp par seconde">[[Paire de bases|bp]] s<sup>−1</sup></abbr></span>' },
        { '10{{exp|34}}|cm|-2|s|-1', '<span class="nowrap">10<sup>34</sup> <abbr class=abbr title="par centimètre carré seconde">cm<sup>−2</sup> s<sup>−1</sup></abbr></span>' },
        { '-61.9|[[Kilojoule|kJ]]||[[Mole (unité)|mol]]|-1', '<span class="nowrap"><span title="−0,641 548 eV ou −14 794,4714 cal(th)/mol">−61,9</span> <abbr class=abbr title="kilojoule par mole">[[Kilojoule|kJ]] [[Mole (unité)|mol]]<sup>−1</sup></abbr></span>' },
        { '12.03 hab./km2', '<span class="nowrap">12,03 <abbr class=abbr title="habitants par kilomètre carré">hab./km<sup>2</sup></abbr></span>' },
        { '320 / 270 km/h', '<span class="nowrap"><span title="88,888 96 m/s">320</span> / <span title="75,000 06 m/s">270</span> <abbr class=abbr title="kilomètre par heure">km/h</abbr></span>' },
        { '', '' },
    })
end
	
	
	
return p