mirror of
https://github.com/LucasVbr/interpreteur-lir.git
synced 2026-05-13 17:21:52 +00:00
Identificateur V0
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
* Identificateur.java , 08/05/2021
|
||||
* IUT Rodez 2020-2021, info1
|
||||
* pas de copyright, aucun droits
|
||||
*/
|
||||
|
||||
package donnees;
|
||||
|
||||
/**
|
||||
* @author Lucàs VABRE
|
||||
* @author Heïa DEXTER
|
||||
*/
|
||||
public class Identificateur /* extends Variable */ {
|
||||
|
||||
/**
|
||||
* Instanciation de l'identificateur
|
||||
* @param identificateur
|
||||
*/
|
||||
public Identificateur() {
|
||||
// super();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
* IdentificateurChaine.java, 08/05/2021
|
||||
* IUT Rodez 2020-2021, info1
|
||||
* pas de copyright, aucun droits
|
||||
*/
|
||||
|
||||
package donnees;
|
||||
|
||||
/**
|
||||
* Identificateur de chaine
|
||||
* @author Lucàs VABRE
|
||||
* @author Heïa DEXTER
|
||||
*/
|
||||
public class IdentificateurChaine extends Identificateur {
|
||||
|
||||
/** Nom identificateur */
|
||||
private String nom;
|
||||
|
||||
/**
|
||||
* Instantiation d'identificateur de chaine
|
||||
* @param identificateur a instancier
|
||||
* @throws IllegalAccessException si l'identificateur est invalide
|
||||
*/
|
||||
public IdentificateurChaine(String identificateur) {
|
||||
super();
|
||||
|
||||
if(!isIdentificateurChaine(identificateur)) {
|
||||
throw new IllegalArgumentException(identificateur + " n'est pas un identificateur de chaine");
|
||||
}
|
||||
|
||||
this.nom = identificateur;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prédicat attestant la validité de l'identificateur
|
||||
* @param identificateur à tester
|
||||
* @return true si l'identificateur est bien un identificateur d'entier
|
||||
* false sinon
|
||||
*/
|
||||
private boolean isIdentificateurChaine(String identificateur) {
|
||||
boolean testOk = identificateur != null
|
||||
&& identificateur.length() > 0
|
||||
&& identificateur.charAt(0) == '$';
|
||||
|
||||
for (int i = 1 ; testOk && i < identificateur.length() ; i++) {
|
||||
testOk = 'a' <= identificateur.charAt(i)
|
||||
&& identificateur.charAt(i) <= 'z' ;
|
||||
}
|
||||
|
||||
return testOk;
|
||||
}
|
||||
|
||||
/* non javadoc - @see java.lang.Object#toString() */
|
||||
@Override
|
||||
public String toString() {
|
||||
return "IdentificateurChaine [nom=" + nom + "]";
|
||||
}
|
||||
|
||||
/**
|
||||
* @return la valeur de nom
|
||||
*/
|
||||
public String getNom() {
|
||||
return nom;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
/*
|
||||
* IdentificateurEntier.java , 08/05/2021
|
||||
* IUT Rodez 2020-2021, info1
|
||||
* pas de copyright, aucun droits
|
||||
*/
|
||||
|
||||
package donnees;
|
||||
|
||||
/**
|
||||
* Identificateur d'entier
|
||||
* @author Lucàs VABRE
|
||||
* @author Heïa DEXTER
|
||||
*/
|
||||
public class IdentificateurEntier extends Identificateur {
|
||||
|
||||
/** Nom identificateur */
|
||||
private String nom;
|
||||
|
||||
/**
|
||||
* Instantiation d'identificateur d'entier
|
||||
* @param identificateur a instancier
|
||||
* @throws IllegalAccessException si l'identificateur est invalide
|
||||
*/
|
||||
public IdentificateurEntier(String identificateur) {
|
||||
super();
|
||||
|
||||
if(!isIdentificateurEntier(identificateur)) {
|
||||
throw new IllegalArgumentException(identificateur + " n'est pas un identificateur d'entier");
|
||||
}
|
||||
|
||||
this.nom = identificateur;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prédicat attestant la validité de l'identificateur
|
||||
*
|
||||
* Un identificateur d'entier est valide si
|
||||
* - Il contient au maximum 24 caractères
|
||||
* - Commence obligatoirement par une lettre (majuscule ou minuscule)
|
||||
* - suivie uniquement de lettres (majuscule ou minuscule) ou de chiffres
|
||||
*
|
||||
* @param identificateur à tester
|
||||
* @return true si l'identificateur est bien un identificateur d'entier
|
||||
* false sinon
|
||||
*/
|
||||
private boolean isIdentificateurEntier(String identificateur) {
|
||||
final int LONGUEUR_MAX = 24;
|
||||
|
||||
boolean testOk = identificateur != null
|
||||
&& 0 < identificateur.length()
|
||||
&& identificateur.length() <= LONGUEUR_MAX
|
||||
&& isLettre(identificateur.charAt(0));
|
||||
|
||||
for (int i = 1 ; testOk && i < identificateur.length() ; i++) {
|
||||
testOk = isLettre(identificateur.charAt(i))
|
||||
|| isChiffre(identificateur.charAt(i));
|
||||
}
|
||||
|
||||
return testOk;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prédicat attestant si un caractère est une lettre
|
||||
* @param aTester caractère a tester
|
||||
* @return
|
||||
*/
|
||||
private static boolean isLettre(char aTester) {
|
||||
return 'a' <= aTester && aTester <= 'z'
|
||||
|| 'A' <= aTester && aTester <= 'Z';
|
||||
}
|
||||
|
||||
/**
|
||||
* Prédicat attestant si un caractère est un chiffre
|
||||
* @param aTester caractère a tester
|
||||
*/
|
||||
private static boolean isChiffre(char aTester) {
|
||||
return '0' <= aTester && aTester <= '9';
|
||||
}
|
||||
|
||||
/* non javadoc - @see java.lang.Object#toString() */
|
||||
@Override
|
||||
public String toString() {
|
||||
return "IdentificateurEntier [nom=" + nom + "]";
|
||||
}
|
||||
|
||||
/**
|
||||
* @return la valeur de nom
|
||||
*/
|
||||
public String getNom() {
|
||||
return nom;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
/*
|
||||
* TestIdentificateurChaine.java, 08/05/2021
|
||||
* IUT Rodez 2020-2021, info1
|
||||
* pas de copyright, aucun droits
|
||||
*/
|
||||
|
||||
package donnees.tests;
|
||||
|
||||
import static outils.glg.Assertions.assertEquivalent;
|
||||
import static outils.glg.Assertions.echec;
|
||||
|
||||
import donnees.IdentificateurChaine;
|
||||
|
||||
/**
|
||||
* Tests unitaires de la classe donnees.IdentificateurEntier
|
||||
* @author Lucàs VABRE
|
||||
* @author Heïa DEXTER
|
||||
*/
|
||||
public class TestIdentificateurChaine {
|
||||
/** Jeu d'identificateurs de chaine correctement instanciés */
|
||||
private static IdentificateurChaine[] FIXTURE = {
|
||||
new IdentificateurChaine("$alpha"),
|
||||
new IdentificateurChaine("$beta"),
|
||||
new IdentificateurChaine("$gamma")
|
||||
};
|
||||
|
||||
/**
|
||||
* Tests unitaires du constructeur IdentificateurEntier(String identificateur)
|
||||
*/
|
||||
public static void testIdentificateurChaineString() {
|
||||
final String[] INVALIDE = {
|
||||
"9alpha",
|
||||
"-beta",
|
||||
"GAMMA",
|
||||
"id 3a",
|
||||
"",
|
||||
" ",
|
||||
"\t",
|
||||
"\n",
|
||||
null
|
||||
};
|
||||
|
||||
for(int noJeu = 0; noJeu < INVALIDE.length ; noJeu++) {
|
||||
try {
|
||||
new IdentificateurChaine(INVALIDE[noJeu]);
|
||||
echec();
|
||||
} catch (IllegalArgumentException lancee) {
|
||||
// test Ok
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests unitaires de getNom()
|
||||
*/
|
||||
public static void testGetNom() {
|
||||
final String[] NOM_VALIDES = {
|
||||
"$alpha",
|
||||
"$beta",
|
||||
"$gamma"
|
||||
};
|
||||
|
||||
for (int noJeu = 0 ; noJeu < NOM_VALIDES.length ; noJeu++) {
|
||||
assertEquivalent(NOM_VALIDES[noJeu], FIXTURE[noJeu].getNom());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests unitaires de toString
|
||||
*/
|
||||
public static void testToString() {
|
||||
final String[] CHAINES_VALIDES = {
|
||||
"$alpha",
|
||||
"$beta",
|
||||
"$gamma"
|
||||
};
|
||||
|
||||
for (int noJeu = 0 ; noJeu < CHAINES_VALIDES.length ; noJeu++) {
|
||||
assertEquivalent("IdentificateurChaine [nom=" + CHAINES_VALIDES[noJeu] + "]",
|
||||
FIXTURE[noJeu].toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
/*
|
||||
* TestIdentificateurEntier.java , 08/05/2021
|
||||
* IUT Rodez 2020-2021, info1
|
||||
* pas de copyright, aucun droits
|
||||
*/
|
||||
|
||||
package donnees.tests;
|
||||
|
||||
import static outils.glg.Assertions.*;
|
||||
|
||||
import donnees.IdentificateurEntier;
|
||||
|
||||
/**
|
||||
* Tests unitaires de la classe donnees.IdentificateurEntier
|
||||
* @author Lucàs VABRE
|
||||
* @author Heïa DEXTER
|
||||
*/
|
||||
public class TestIdentificateurEntier {
|
||||
|
||||
/** Jeu d'identificateurs d'entier correctement instanciés */
|
||||
private static IdentificateurEntier[] FIXTURE = {
|
||||
new IdentificateurEntier("alpha"),
|
||||
new IdentificateurEntier("beta"),
|
||||
new IdentificateurEntier("gamma")
|
||||
};
|
||||
|
||||
/**
|
||||
* Tests unitaires du constructeur IdentificateurEntier(String identificateur)
|
||||
*/
|
||||
public static void testIdentificateurEntierString() {
|
||||
final String[] INVALIDE = {
|
||||
"9alpha",
|
||||
"$beta",
|
||||
"GAMMA",
|
||||
"id 3a",
|
||||
" ",
|
||||
"",
|
||||
"\t",
|
||||
"\n",
|
||||
null
|
||||
};
|
||||
|
||||
for(int noJeu = 0; noJeu < INVALIDE.length ; noJeu++) {
|
||||
try {
|
||||
new IdentificateurEntier(INVALIDE[noJeu]);
|
||||
echec();
|
||||
} catch (IllegalArgumentException lancee) {
|
||||
// test Ok
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests unitaires de getNom()
|
||||
*/
|
||||
public static void testGetNom() {
|
||||
final String[] NOM_VALIDES = {
|
||||
"alpha",
|
||||
"beta",
|
||||
"gamma"
|
||||
};
|
||||
|
||||
for (int noJeu = 0 ; noJeu < NOM_VALIDES.length ; noJeu++) {
|
||||
assertEquivalent(NOM_VALIDES[noJeu], FIXTURE[noJeu].getNom());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests unitaires de toString
|
||||
*/
|
||||
public static void testToString() {
|
||||
final String[] CHAINES_VALIDES = {
|
||||
"alpha",
|
||||
"beta",
|
||||
"gamma"
|
||||
};
|
||||
|
||||
for (int noJeu = 0 ; noJeu < CHAINES_VALIDES.length ; noJeu++) {
|
||||
assertEquivalent("IdentificateurEntier [nom=" + CHAINES_VALIDES[noJeu] + "]",
|
||||
FIXTURE[noJeu].toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
/**
|
||||
* InterpreteurException.java 7 mai 2021
|
||||
* IUT Rodez info1 2020-2021, pas de copyright, aucun droit
|
||||
*/
|
||||
package outils;
|
||||
|
||||
/**
|
||||
* Exception levée lors d'une erreur dans l'interpreteur LIR.
|
||||
* (Erreur de syntaxe, erreur de types)
|
||||
* @author Nicolas Caminade
|
||||
* @author Sylvan Courtiol
|
||||
* @author Pierre Debas
|
||||
* @author Heïa Dexter
|
||||
* @author Lucas Vabre
|
||||
*/
|
||||
public class InterpreteurException extends RuntimeException {
|
||||
|
||||
/**
|
||||
* Une exception de syntaxe expliquée par un message
|
||||
* @param message explication succincte de cette exception
|
||||
*/
|
||||
public InterpreteurException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* Assertions.java 7 avr. 2021
|
||||
* IUT info1 2020-2021, groupe 2, aucun droit d'auteur
|
||||
*/
|
||||
package outils.glg;
|
||||
|
||||
/**
|
||||
* Propositions logiques de test qui propage EchecTest
|
||||
* si elles ne sont pas vérifiées
|
||||
* @author info1 202-2021
|
||||
*/
|
||||
public class Assertions {
|
||||
|
||||
/**
|
||||
* Assertion vérifiant qu'une expression booléenne est vraie.
|
||||
* Ce test échoue si elle est fausse
|
||||
* @param condition expression booléenne à tester
|
||||
*/
|
||||
public static void assertTrue(boolean condition) {
|
||||
if (!condition) {
|
||||
throw new EchecTest();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Assertion testant l'équivalence de 2 objets selon la relation
|
||||
* d'équivalence de base equals (@see java.lang.Object#equals)
|
||||
* @param attendu valeur attendue pour le test
|
||||
* @param obtenu valeur obtenue à tester
|
||||
*/
|
||||
public static void assertEquivalent(Object attendu, Object obtenu) {
|
||||
assertTrue(obtenu.equals(attendu));
|
||||
}
|
||||
|
||||
/**
|
||||
* Echec systématique de test
|
||||
* (signaler que une série de test est insuffisante)
|
||||
*/
|
||||
public static void echec() {
|
||||
assertTrue(false);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
/*
|
||||
* EchecTest.java 7 avr. 2021
|
||||
* IUT info1 2020-2021, groupe 2, aucun droit d'auteur
|
||||
*/
|
||||
package outils.glg;
|
||||
|
||||
/**
|
||||
* Exception lancée si un test unitaire échoue : comportement obtenu n'étant pas
|
||||
* le comportement attendu
|
||||
* @author info1 2020-2021
|
||||
*/
|
||||
public class EchecTest extends RuntimeException {
|
||||
|
||||
// constructeur par défaut généré par le compilateur
|
||||
public EchecTest() {
|
||||
super();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
/*
|
||||
* ExecuteurDeTest.java 13 avr. 2021
|
||||
* IUT info1 2020-2021, groupe 2, aucun droit d'auteur
|
||||
*/
|
||||
package outils.glg;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
|
||||
/**
|
||||
* Lanceur de test automatique ("test runner") qui exécute les méthodes de tests
|
||||
* unitaires d'une classe de test telle que :
|
||||
* <ul><li>La classe de test doit être instanciable avec des méthodes de tests
|
||||
* unitaires qui sont des méthodes d'instance</li>
|
||||
* <li>Les jeux d'essai mis en champ d'instance sont régénérés
|
||||
* automatiquement à chaque instanciation de la classe de test
|
||||
* = "fixture de test" (fix picture of test)</li>
|
||||
* <li>Chaque méthode de test s'exécute dans sa propre instance donc avec
|
||||
* la fixture remise dans son état initial</li>
|
||||
* <li>Chaque méthode de test unitaire ne doit tester qu'une seule méthode
|
||||
* de la classe à tester</li>
|
||||
* <li>Chaque méthode de test unitaire s'arrête en cas d'échec
|
||||
* de test (failure) ou de crash (error)</li>
|
||||
* </ul>
|
||||
*
|
||||
* @author info1 2020-2021
|
||||
*/
|
||||
public class ExecuteurDeTest {
|
||||
|
||||
/** préfixe des méthodes de tests unitaires à lancer */
|
||||
public final static String PREFIXE_TEST = "test";
|
||||
|
||||
/** Code erreur si la ligne de commande est mal formatée */
|
||||
public static final int ERR_NB_ARGUMENT = 1;
|
||||
|
||||
/** Code erreur si la classe de test (argument) n'est pas trouvée */
|
||||
public static final int ERR_CLASSE_INACCESSIBLE = 2;
|
||||
|
||||
/** Message d'aide à l'usage de l'outil */
|
||||
private static final String MESSAGE_USAGE
|
||||
= "usage : info1.outils.glg.ExecuteurDeTest nom.complet.de.classe.de.Test";
|
||||
|
||||
/**
|
||||
* Lancement automatique des méthodes de test
|
||||
* d'une classe de test passée en argument
|
||||
* @param args nom java complet de la classe de test à exécuter
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
|
||||
/* Analyse de la ligne de commande */
|
||||
if (args.length != 1) {
|
||||
System.err.println("Nombre d'arguments incorrects");
|
||||
System.err.println(MESSAGE_USAGE);
|
||||
System.exit(ERR_NB_ARGUMENT);
|
||||
}
|
||||
Class<?> deTest = null;
|
||||
try {
|
||||
deTest = Class.forName(args[0]);
|
||||
} catch (ClassNotFoundException e) {
|
||||
System.err.println("Classe " + args[0] + " non accessible");
|
||||
System.err.println(MESSAGE_USAGE);
|
||||
System.exit(ERR_CLASSE_INACCESSIBLE);
|
||||
}
|
||||
|
||||
/* Lancement des méthodes de tests unitaires */
|
||||
Method[] aFiltrer = deTest.getDeclaredMethods();
|
||||
for (Method aExecuter : aFiltrer) {
|
||||
String nomMethode = aExecuter.getName();
|
||||
if (nomMethode.startsWith(PREFIXE_TEST)) {
|
||||
try {
|
||||
aExecuter.invoke(deTest.getConstructor().newInstance());
|
||||
/* test Ok */
|
||||
System.out.println("Réussite de " + nomMethode);
|
||||
} catch (Exception aAnalyser) {
|
||||
if (aAnalyser.getCause() instanceof EchecTest) {
|
||||
System.out.println("Echec de " + nomMethode);
|
||||
} else {
|
||||
System.err.println("Crash de " + nomMethode + " : "
|
||||
+ aAnalyser.getCause().getMessage());
|
||||
}
|
||||
}
|
||||
System.out.println("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user