mirror of
https://github.com/LucasVbr/interpreteur-lir.git
synced 2026-07-09 15:08:05 +00:00
Prototype + programme de démo + exécutable
This commit is contained in:
@@ -19,7 +19,7 @@ import interpreteurlir.programmes.Programme;
|
||||
* @author Heïa Dexter
|
||||
* @author Lucas Vabre
|
||||
*/
|
||||
public class Commande {
|
||||
public abstract class Commande {
|
||||
|
||||
/** référence du programme global */
|
||||
protected static Programme programmeGlobal;
|
||||
@@ -52,10 +52,7 @@ public class Commande {
|
||||
* @return true si la commande affiche un feedback directement sur la sortie
|
||||
* standard, sinon false
|
||||
*/
|
||||
public boolean executer() {
|
||||
// pas de comportement pour une Commande générale
|
||||
return false; // pas de feedback
|
||||
}
|
||||
public abstract boolean executer();
|
||||
|
||||
/**
|
||||
* Référence le programme pour accéder et modifier le programme chargé.
|
||||
|
||||
@@ -0,0 +1,167 @@
|
||||
/**
|
||||
* CommandeCharge.java 21 mai 2021
|
||||
* IUT Rodez info1 2020-2021, pas de copyright, aucun droit
|
||||
*/
|
||||
package interpreteurlir.motscles;
|
||||
|
||||
import interpreteurlir.Contexte;
|
||||
import interpreteurlir.ExecutionException;
|
||||
import interpreteurlir.InterpreteurException;
|
||||
import interpreteurlir.motscles.instructions.Instruction;
|
||||
import interpreteurlir.programmes.Etiquette;
|
||||
import interpreteurlir.Analyseur;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
|
||||
/**
|
||||
* Charge les lignes de programme dans le fichier texte indiqué en argument
|
||||
*
|
||||
* @author Nicolas Caminade
|
||||
* @author Sylvan Courtiol
|
||||
* @author Pierre Debas
|
||||
* @author Heia Dexter
|
||||
* @author Lucas Vabre
|
||||
*/
|
||||
public class CommandeCharge extends Commande{
|
||||
|
||||
/** Chemin du fichier dans lequel sera le programme chargé */
|
||||
private String cheminFichier;
|
||||
|
||||
/**
|
||||
* Initialise la commande Charge a partir de ses argument et
|
||||
* de son contexte passé en paramètre.
|
||||
* @param arguments Chemin du fichier dans lequel sera le programme chargé
|
||||
* @param contexte Contexte du programme
|
||||
* @throws InterpreteurException Si l'argument est null, vide,
|
||||
* contient uniquement des espaces ou
|
||||
* ne se termine pas par ".lir".
|
||||
*/
|
||||
public CommandeCharge(String arguments, Contexte contexte ) {
|
||||
super(arguments, contexte);
|
||||
|
||||
final String extension = ".lir";
|
||||
|
||||
if (arguments == null
|
||||
|| arguments.isEmpty()
|
||||
|| arguments.isBlank()
|
||||
|| !arguments.trim().endsWith(extension)) {
|
||||
|
||||
throw new InterpreteurException("\t" + arguments
|
||||
+ " n'est pas un chemin valide");
|
||||
}
|
||||
|
||||
this.cheminFichier = arguments.trim();
|
||||
}
|
||||
|
||||
/**
|
||||
* Charge le programme contenu dans le fichier de this
|
||||
* @return false car elle n'affiche aucun feedback directement
|
||||
* @throws InterpreteurException Si le fichier a charger n'as pas été trouvé
|
||||
*/
|
||||
public boolean executer() {
|
||||
|
||||
programmeGlobal.raz();
|
||||
|
||||
/* Chemin du fichier */
|
||||
String nomFichier = new File(cheminFichier).getAbsolutePath();
|
||||
|
||||
/* Fichier logique en entrée */
|
||||
BufferedReader entree;
|
||||
|
||||
entree = null;
|
||||
try {
|
||||
entree = new BufferedReader(
|
||||
new InputStreamReader(
|
||||
new FileInputStream(nomFichier)));
|
||||
analyserFichier(entree);
|
||||
entree.close();
|
||||
} catch (IOException e) {
|
||||
throw new InterpreteurException(nomFichier + " est introuvable");
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Analyse chaque ligne de l'entrée et les ajoute dans programme global
|
||||
* @param entree Tampon du fichier à lire
|
||||
* @throws IOException Problème de lecture du fichier
|
||||
*/
|
||||
private void analyserFichier(BufferedReader entree) throws IOException {
|
||||
|
||||
/* Index de la ligne découpée */
|
||||
final int ETIQUETTE = 0;
|
||||
final int MOT_CLE = 1;
|
||||
final int ARGUMENT = 2;
|
||||
|
||||
String ligneLue;
|
||||
int numLigne = 0;
|
||||
|
||||
do {
|
||||
ligneLue = entree.readLine();
|
||||
if (ligneLue != null && !ligneLue.isBlank()) {
|
||||
numLigne++;
|
||||
|
||||
String[] decoupage = splitter(ligneLue);
|
||||
|
||||
Class<?> aAjouter;
|
||||
try {
|
||||
aAjouter = Analyseur
|
||||
.rechercheInstruction(decoupage[MOT_CLE]);
|
||||
|
||||
Class<?> classeArg = String.class;
|
||||
Class<?> classeContexte = Contexte.class;
|
||||
Instruction inst = (Instruction)aAjouter
|
||||
.getConstructor(classeArg, classeContexte)
|
||||
.newInstance(decoupage[ARGUMENT], contexte);
|
||||
|
||||
Etiquette etiquette = new Etiquette(decoupage[ETIQUETTE]);
|
||||
|
||||
programmeGlobal.ajouterLigne(etiquette, inst);
|
||||
} catch (InvocationTargetException| IllegalAccessException
|
||||
|InstantiationException | NoSuchMethodException
|
||||
|InterpreteurException | ExecutionException lancee) {
|
||||
programmeGlobal.raz();
|
||||
throw new InterpreteurException(ligneLue + " => ligne "
|
||||
+ numLigne);
|
||||
}
|
||||
}
|
||||
} while (ligneLue != null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sépare la ligne en etiquette/mot clé/argument
|
||||
* @param ligneLue
|
||||
* @return Tableau comportant en :
|
||||
* <ul><li>indice 1 : l'étiquette</li>
|
||||
* <li>indice 2 : mot clé</li>
|
||||
* <li>indice 3 : argument</li></ul>
|
||||
* @throws InterpreteurException Si la ligne ne contient pas les 2 éléments:
|
||||
* <ul><li>Etiquette</li>
|
||||
* <li>Mot clé</li></ul>
|
||||
*/
|
||||
private static String[] splitter(String ligneLue) {
|
||||
/* Sépare l'étiquette, la commande et l'argument */
|
||||
String[] ligne = ligneLue.split(" ", 3);
|
||||
|
||||
if (ligne.length < 2) {
|
||||
programmeGlobal.raz();
|
||||
throw new InterpreteurException(ligneLue + " n'est pas "
|
||||
+ "une ligne valide");
|
||||
}
|
||||
|
||||
String[] decoupage = new String[3];
|
||||
|
||||
/* Ajouter la ligne dans le contexte */
|
||||
decoupage[0] = ligne[0];
|
||||
decoupage[1] = ligne[1];
|
||||
decoupage[2] = ligne.length >= 3 ? ligne[2] : "";
|
||||
|
||||
return decoupage;
|
||||
}
|
||||
}
|
||||
@@ -39,7 +39,6 @@ public class CommandeDebut extends Commande {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Commande d'exécution de la commande.
|
||||
* Efface le contexte.
|
||||
@@ -52,4 +51,4 @@ public class CommandeDebut extends Commande {
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -21,7 +21,7 @@ public class CommandeEfface extends Commande {
|
||||
|
||||
/** Erreur nombre incorrect d'arguments */
|
||||
private static final String ERREUR_NB_ARGS =
|
||||
"nombre d'arguments incorrect. Syntaxe attendue ==> <debut>:<fin>";
|
||||
"usage efface <étiquette_début>:<étiquette_fin>";
|
||||
|
||||
/** Plage de suppression des lignes de code */
|
||||
private Etiquette[] plageSuppression;
|
||||
@@ -31,7 +31,7 @@ public class CommandeEfface extends Commande {
|
||||
* contexte passés en paramètres. Modifie le programme global référencé
|
||||
* par l'analyseur.
|
||||
* @param arguments lignes à effacer (tout le programme si vide)
|
||||
* @param contexte
|
||||
* @param contexte référence du contexte global
|
||||
*/
|
||||
public CommandeEfface(String arguments, Contexte contexte) {
|
||||
super(arguments, contexte);
|
||||
|
||||
@@ -35,7 +35,6 @@ public class CommandeFin extends Commande {
|
||||
throw new InterpreteurException(ERREUR_ARGUMENTS);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Commande d'exécution de la commande.
|
||||
@@ -51,4 +50,4 @@ public class CommandeFin extends Commande {
|
||||
System.exit(0);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,6 @@ package interpreteurlir.motscles;
|
||||
|
||||
import interpreteurlir.Contexte;
|
||||
import interpreteurlir.InterpreteurException;
|
||||
import interpreteurlir.programmes.Programme;
|
||||
import interpreteurlir.programmes.Etiquette;
|
||||
|
||||
/**
|
||||
@@ -25,8 +24,8 @@ public class CommandeLance extends Commande {
|
||||
|
||||
/**
|
||||
* Initialise la commande lance avec ses arguments et le contexte
|
||||
* @param arguments
|
||||
* @param contexte
|
||||
* @param arguments vide ou étiquette de lancement
|
||||
* @param contexte référence du contexte global
|
||||
* @throws InterpreteurException en cas d'erreur de syntaxe à
|
||||
* la création d'une étiquette
|
||||
*/
|
||||
@@ -67,4 +66,4 @@ public class CommandeLance extends Commande {
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -28,8 +28,8 @@ public class CommandeListe extends Commande {
|
||||
/**
|
||||
* Initialise la commande liste avec ses arguments et le contexte
|
||||
*
|
||||
* @param arguments
|
||||
* @param contexte
|
||||
* @param arguments arguments vide ou contenant les étiquettes à afficher
|
||||
* @param contexte référence du contexte global
|
||||
* @throws InterpreteurException en cas d'erreur de syntaxe lors
|
||||
* de l'instanciation des étiquettes
|
||||
*/
|
||||
@@ -39,10 +39,10 @@ public class CommandeListe extends Commande {
|
||||
final int ARGS_DEBUT = 0;
|
||||
final int ARGS_FIN = 1;
|
||||
|
||||
final String ERREUR_INTERVALLE = "usage <etiquette debut>:"
|
||||
+ "<etiquette fin> avec "
|
||||
+ "<etiquette debut> < "
|
||||
+ "<etiquette fin> ";
|
||||
final String ERREUR_INTERVALLE = "usage liste <étiquette_début>:"
|
||||
+ "<étiquette_fin> avec "
|
||||
+ "<étiquette_début> <= "
|
||||
+ "<étiquette_fin> ";
|
||||
|
||||
if (arguments.isBlank()) {
|
||||
debut = new Etiquette(VALEUR_ETIQUETTE_MIN);
|
||||
@@ -75,16 +75,16 @@ public class CommandeListe extends Commande {
|
||||
* dans la classe {@link Commande}
|
||||
*/
|
||||
public boolean executer() {
|
||||
|
||||
final String ERREUR = "erreur exécution";
|
||||
|
||||
if (programmeGlobal == null) {
|
||||
throw new RuntimeException(ERREUR);
|
||||
}
|
||||
|
||||
if (debut != null || fin != null) {
|
||||
System.out.print(programmeGlobal.listeBornee(debut, fin));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
/**
|
||||
* CommandeSauve.java 21 mai 2021
|
||||
* IUT Rodez info1 2020-2021, pas de copyright, aucun droit
|
||||
*/
|
||||
package interpreteurlir.motscles;
|
||||
|
||||
import java.io.PrintStream;
|
||||
|
||||
import interpreteurlir.Contexte;
|
||||
import interpreteurlir.ExecutionException;
|
||||
import interpreteurlir.InterpreteurException;
|
||||
import interpreteurlir.programmes.Programme;
|
||||
|
||||
/**
|
||||
* Commande sauve prenant en argument le chemin du fichier
|
||||
* (avec une extension .lir) dans lequel est enregistré le programme chargé.
|
||||
* @author Nicolas Caminade
|
||||
* @author Sylvan Courtiol
|
||||
* @author Pierre Debas
|
||||
* @author Heia Dexter
|
||||
* @author Lucas Vabre
|
||||
*/
|
||||
public class CommandeSauve extends Commande {
|
||||
|
||||
/** chemin du fichier d'extension .lir pour la sauvegarde */
|
||||
private String cheminFichier;
|
||||
|
||||
/**
|
||||
* Initialise une commande sauve avec un chemin de fichier .lir passé en
|
||||
* argument.
|
||||
* @param arguments chemin du fichier dans lequel
|
||||
* la sauvegarde sera effectuée
|
||||
* @param contexte référence du contexte global
|
||||
* @throws InterpreteurException si le chemin du fichier n'a pas
|
||||
* l'extension .lir
|
||||
* ou si arguments est chaîne blanche
|
||||
*/
|
||||
public CommandeSauve(String arguments, Contexte contexte) {
|
||||
super(arguments, contexte);
|
||||
|
||||
final String EXTENSION = ".lir";
|
||||
final String USAGE = "usage sauve <chemin_et_nom_du_fichier>.lir";
|
||||
|
||||
arguments = arguments.trim();
|
||||
|
||||
if (arguments.isBlank() || !arguments.endsWith(EXTENSION)) {
|
||||
throw new InterpreteurException(USAGE);
|
||||
}
|
||||
// else
|
||||
|
||||
cheminFichier = arguments;
|
||||
}
|
||||
|
||||
/**
|
||||
* Commande d'exécution de la commande.
|
||||
* Sauvegarde le programme référencé dans la classe Commande
|
||||
* dans le fichier de cette CommandeSauve.
|
||||
* @return false car aucun feedback afficher directement
|
||||
* @throws ExecutionException si l'enregistrement est impossible
|
||||
* @throws RuntimeException si aucun programme référencé dans la classe
|
||||
* Commande avec la méthode
|
||||
* {@link Commande#referencerProgramme(Programme)}
|
||||
*/
|
||||
@Override
|
||||
public boolean executer() {
|
||||
final String MSG_ERREUR = "impossible de sauvegarder le programme "
|
||||
+ "dans le fichier (le chemin est peut-être invalide)";
|
||||
|
||||
if (programmeGlobal == null) {
|
||||
throw new RuntimeException("Programme non référencé dans Commande");
|
||||
}
|
||||
|
||||
PrintStream aEcrire = null;
|
||||
|
||||
try {
|
||||
aEcrire = new PrintStream(cheminFichier);
|
||||
aEcrire.print(programmeGlobal.toString());
|
||||
aEcrire.close();
|
||||
} catch (Exception lancee) {
|
||||
throw new ExecutionException(MSG_ERREUR);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -17,10 +17,7 @@ import interpreteurlir.motscles.Commande;
|
||||
* @author Heïa Dexter
|
||||
* @author Lucas Vabre
|
||||
*/
|
||||
public class Instruction extends Commande {
|
||||
|
||||
/** Contexte d'exécution de cette instruction */
|
||||
protected Contexte contexteGlobal;
|
||||
public abstract class Instruction extends Commande {
|
||||
|
||||
/** Expression qui sera exécutée par la commande */
|
||||
protected Expression aExecuter;
|
||||
@@ -41,16 +38,12 @@ public class Instruction extends Commande {
|
||||
* @see interpreteurlir.motscles.Commande#executer()
|
||||
*/
|
||||
@Override
|
||||
public boolean executer() {
|
||||
return super.executer();
|
||||
}
|
||||
public abstract boolean executer();
|
||||
|
||||
/*
|
||||
* Non - javadoc
|
||||
* @see java.lang.Object#toString()
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return getClass().getSimpleName() + " " + aExecuter;
|
||||
}
|
||||
public abstract String toString();
|
||||
}
|
||||
|
||||
@@ -7,7 +7,6 @@ package interpreteurlir.motscles.instructions;
|
||||
import interpreteurlir.Contexte;
|
||||
import interpreteurlir.InterpreteurException;
|
||||
import interpreteurlir.expressions.Expression;
|
||||
import interpreteurlir.expressions.ExpressionChaine;
|
||||
|
||||
/**
|
||||
* Affiche sur la sortie standard une expression passée en argument. Cette
|
||||
@@ -21,8 +20,9 @@ import interpreteurlir.expressions.ExpressionChaine;
|
||||
*/
|
||||
public class InstructionAffiche extends Instruction {
|
||||
|
||||
/** Erreur d'affectation illegale */
|
||||
private static final String AFFECTATION_ILLEGALE = "affectation illegale";
|
||||
/** Erreur d'affectation illégale */
|
||||
private static final String AFFECTATION_ILLEGALE =
|
||||
"affectation impossible avec la commande affiche";
|
||||
|
||||
/**
|
||||
* Initialise cette InstructionAffiche à partir de son contexte global
|
||||
@@ -37,8 +37,9 @@ public class InstructionAffiche extends Instruction {
|
||||
public InstructionAffiche(String arguments, Contexte contexte) {
|
||||
super(arguments, contexte);
|
||||
|
||||
if (ExpressionChaine.indexOperateur(arguments, '=') >= 0)
|
||||
if (Expression.detecterCaractere(arguments, '=') >= 0) {
|
||||
throw new InterpreteurException(AFFECTATION_ILLEGALE);
|
||||
}
|
||||
|
||||
aExecuter = arguments.isBlank()
|
||||
? null
|
||||
@@ -70,5 +71,4 @@ public class InstructionAffiche extends Instruction {
|
||||
: " " + aExecuter.toString());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -44,7 +44,7 @@ public class InstructionEntre extends Instruction {
|
||||
*/
|
||||
public InstructionEntre(String arguments, Contexte contexte) {
|
||||
super(arguments, contexte);
|
||||
final String ERREUR_ARG = "Entre attend un identificateur en argument";
|
||||
final String ERREUR_ARG = "usage entre <identificateur>";
|
||||
|
||||
if (arguments.isBlank()) {
|
||||
throw new InterpreteurException(ERREUR_ARG);
|
||||
@@ -68,8 +68,10 @@ public class InstructionEntre extends Instruction {
|
||||
*/
|
||||
public boolean executer() {
|
||||
|
||||
final String MESSAGE_ERREUR_TYPE = "Le type saisi ne correspond"
|
||||
+ " pas au type demandé";
|
||||
final String MESSAGE_ERREUR_TYPE = "type saisi "
|
||||
+ "et type demandé incompatibles";
|
||||
|
||||
@SuppressWarnings("resource") // ne pas fermer sinon crash
|
||||
Scanner entree = new Scanner(System.in);
|
||||
|
||||
String valeurSaisie = entree.nextLine();
|
||||
@@ -84,6 +86,7 @@ public class InstructionEntre extends Instruction {
|
||||
} catch (InterpreteurException lancee) {
|
||||
throw new ExecutionException(MESSAGE_ERREUR_TYPE);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -94,4 +97,4 @@ public class InstructionEntre extends Instruction {
|
||||
public String toString() {
|
||||
return "entre " + id;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,6 @@
|
||||
package interpreteurlir.motscles.instructions;
|
||||
|
||||
import interpreteurlir.Contexte;
|
||||
import interpreteurlir.ExecutionException;
|
||||
import interpreteurlir.InterpreteurException;
|
||||
import interpreteurlir.programmes.Etiquette;
|
||||
|
||||
@@ -36,7 +35,7 @@ public class InstructionProcedure extends Instruction {
|
||||
public InstructionProcedure(String arguments, Contexte contexte) {
|
||||
super(arguments, contexte);
|
||||
|
||||
final String ERREUR_ARG = "procedure attend une étiquette en argument";
|
||||
final String ERREUR_ARG = "usage procedure <étiquette>";
|
||||
|
||||
if(arguments.isBlank()) {
|
||||
throw new InterpreteurException(ERREUR_ARG);
|
||||
@@ -63,7 +62,6 @@ public class InstructionProcedure extends Instruction {
|
||||
* de classe de Commande.
|
||||
*/
|
||||
public boolean executer() {
|
||||
|
||||
final String ERREUR_REFERENCEMENT = "Le programme doit être référencé "
|
||||
+ "dans la classe commande";
|
||||
|
||||
|
||||
@@ -32,7 +32,7 @@ public class InstructionRetour extends Instruction {
|
||||
public InstructionRetour(String arguments, Contexte contexte) {
|
||||
super(arguments, contexte);
|
||||
|
||||
final String ERREUR_ARG = "L'instruction retour n'a pas d'arguments";
|
||||
final String ERREUR_ARG = "l'instruction retour n'a pas d'arguments";
|
||||
|
||||
if (!arguments.isBlank()) {
|
||||
throw new InterpreteurException(ERREUR_ARG);
|
||||
@@ -70,4 +70,4 @@ public class InstructionRetour extends Instruction {
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
/**
|
||||
* InstructionSi.java 22 mai 2021
|
||||
* IUT Rodez info1 2020-2021, pas de copyright, aucun droit
|
||||
*/
|
||||
package interpreteurlir.motscles.instructions;
|
||||
|
||||
import interpreteurlir.Contexte;
|
||||
import interpreteurlir.ExecutionException;
|
||||
import interpreteurlir.InterpreteurException;
|
||||
import interpreteurlir.expressions.ExpressionBooleenne;
|
||||
import interpreteurlir.programmes.Etiquette;
|
||||
|
||||
/**
|
||||
* Instruction de saut conditionnel.
|
||||
* La syntaxe est "si expression_booléenne vaen etiquette".
|
||||
* @author Nicolas Caminade
|
||||
* @author Sylvan Courtiol
|
||||
* @author Pierre Debas
|
||||
* @author Heïa Dexter
|
||||
* @author Lucas Vabre
|
||||
*/
|
||||
public class InstructionSi extends Instruction {
|
||||
|
||||
/** expression booléenne de this qui est la condition du saut */
|
||||
private ExpressionBooleenne condition;
|
||||
|
||||
/** etiquette pour le saut conditionnel */
|
||||
private Etiquette saut;
|
||||
|
||||
/**
|
||||
* Initialise une instruction de saut conditionnel à partir des arguments
|
||||
* @param arguments chaîne argument de la forme
|
||||
* "si expression_booléenne vaen etiquette"
|
||||
* @param contexte contexte global
|
||||
* @throws InterpreteurException si syntaxe invalide (si ... vaen ...)
|
||||
* ou si expression_booléenne invalide
|
||||
* ou si etiquette invalide
|
||||
*/
|
||||
public InstructionSi(String arguments, Contexte contexte) {
|
||||
super(arguments, contexte);
|
||||
final String ERR_SYNTAXE = "usage si <expression_booléenne>"
|
||||
+ " vaen <étiquette>";
|
||||
|
||||
arguments = arguments.trim();
|
||||
if (arguments.isBlank()) {
|
||||
throw new InterpreteurException(ERR_SYNTAXE);
|
||||
}
|
||||
|
||||
int indexVaen = arguments.lastIndexOf("vaen");
|
||||
if (indexVaen < 1) {
|
||||
throw new InterpreteurException(ERR_SYNTAXE);
|
||||
}
|
||||
|
||||
String expression = arguments.substring(0, indexVaen);
|
||||
String etiquette = arguments.substring(indexVaen + 4,
|
||||
arguments.length());
|
||||
|
||||
condition = new ExpressionBooleenne(expression);
|
||||
saut = new Etiquette(etiquette);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Execution de l'instruction :
|
||||
* Realise un saut a l'étiquette spécifiée
|
||||
* si l'expression booléenne est true.
|
||||
* @return false car aucun feedback affiché directement
|
||||
* @throws RuntimeException si un programme n'est pas référencé en membre
|
||||
* de classe de Commande.
|
||||
* @throws ExecutionException si l'étiquette n'existe pas dans le programme
|
||||
*/
|
||||
@Override
|
||||
public boolean executer() {
|
||||
final String ERREUR_REFERENCEMENT = "Le programme doit être référencé "
|
||||
+ "dans la classe commande";
|
||||
|
||||
if (programmeGlobal == null) {
|
||||
throw new RuntimeException(ERREUR_REFERENCEMENT);
|
||||
}
|
||||
|
||||
if (condition.calculer().getValeur()) {
|
||||
programmeGlobal.vaen(saut);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/* non javadoc
|
||||
* @see interpreteurlir.motscles.instructions.Instruction#toString()
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return "si " + condition + " vaen " + saut;
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,6 @@ package interpreteurlir.motscles.instructions;
|
||||
|
||||
import interpreteurlir.Contexte;
|
||||
import interpreteurlir.InterpreteurException;
|
||||
import interpreteurlir.programmes.Programme;
|
||||
|
||||
/**
|
||||
* Instruction stop servant à marquer la fin d'un programme de l'interpréteur
|
||||
@@ -22,7 +21,8 @@ public class InstructionStop extends Instruction {
|
||||
|
||||
/** Message d'erreur si instruction passée avec des arguments */
|
||||
private static final String ERREUR_ARGUMENTS =
|
||||
"l'instruction stop n'accepte pas d'arguments";
|
||||
"l'instruction stop n'a pas d'arguments";
|
||||
|
||||
/**
|
||||
* Initialise cette instruction stop à partir des arguments, du contexte
|
||||
* et du programme passés en paramètres. Cette instruction ne modifie que
|
||||
@@ -46,7 +46,7 @@ public class InstructionStop extends Instruction {
|
||||
public boolean executer() {
|
||||
|
||||
final String ERREUR_REFERENCEMENT = "Le programme doit être référencé "
|
||||
+ "dans la classe commande";
|
||||
+ "dans la classe commande";
|
||||
|
||||
if (programmeGlobal == null) {
|
||||
throw new RuntimeException(ERREUR_REFERENCEMENT);
|
||||
@@ -64,4 +64,4 @@ public class InstructionStop extends Instruction {
|
||||
public String toString() {
|
||||
return "stop";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -30,7 +30,7 @@ public class InstructionVaen extends Instruction {
|
||||
public InstructionVaen(String arguments, Contexte contexte) {
|
||||
super(arguments, contexte);
|
||||
|
||||
final String ERREUR_ARG = "vaen attend une étiquette en argument";
|
||||
final String ERREUR_ARG = "usage vaen <étiquette>";
|
||||
|
||||
if (arguments.isBlank()) {
|
||||
throw new InterpreteurException(ERREUR_ARG);
|
||||
@@ -49,16 +49,15 @@ public class InstructionVaen extends Instruction {
|
||||
|
||||
/**
|
||||
* Execution de l'instruction :
|
||||
* Realise un saut a l'étiquette spécifiée.
|
||||
* L'appel s'empile sur le contexte appellant pour ce qui est du
|
||||
* compteur ordinal.
|
||||
* Réalise un saut à l'étiquette spécifiée.
|
||||
* @return false car aucun feedback affiché directement
|
||||
* @throws RuntimeException si un programme n'est pas référencé en membre
|
||||
* de classe de Commande.
|
||||
*/
|
||||
public boolean executer() {
|
||||
|
||||
final String ERREUR = "erreur exécution";
|
||||
final String ERREUR = "Le programme doit être référencé "
|
||||
+ "dans la classe commande";
|
||||
|
||||
if (programmeGlobal == null) {
|
||||
throw new RuntimeException(ERREUR);
|
||||
@@ -69,4 +68,4 @@ public class InstructionVaen extends Instruction {
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -7,7 +7,6 @@ package interpreteurlir.motscles.instructions;
|
||||
import interpreteurlir.Contexte;
|
||||
import interpreteurlir.InterpreteurException;
|
||||
import interpreteurlir.expressions.Expression;
|
||||
import interpreteurlir.expressions.ExpressionChaine;
|
||||
|
||||
/**
|
||||
* Instruction de déclaration et d'affectation de variables. La syntaxe de
|
||||
@@ -30,10 +29,11 @@ public class InstructionVar extends Instruction {
|
||||
*/
|
||||
public InstructionVar(String arguments, Contexte contexte) {
|
||||
super(arguments, contexte);
|
||||
final String USAGE = "usage var <identificateur> = <expression>";
|
||||
|
||||
if (arguments == null || arguments.isBlank()
|
||||
|| ExpressionChaine.indexOperateur(arguments, '=') <= 0)
|
||||
throw new InterpreteurException("erreur de syntaxe");
|
||||
|| Expression.detecterCaractere(arguments, '=') <= 0)
|
||||
throw new InterpreteurException(USAGE);
|
||||
|
||||
aExecuter = Expression.determinerTypeExpression(arguments.trim());
|
||||
}
|
||||
@@ -56,4 +56,4 @@ public class InstructionVar extends Instruction {
|
||||
public String toString() {
|
||||
return "var " + aExecuter;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,52 +1,45 @@
|
||||
/**
|
||||
* TestInstruction.java 9 mai 2021
|
||||
* IUT info1 2020-2021, pas de copyright, aucun droit
|
||||
*/
|
||||
package interpreteurlir.motscles.instructions.tests;
|
||||
// Classe testée passé en abstract
|
||||
|
||||
import interpreteurlir.Contexte;
|
||||
import interpreteurlir.motscles.instructions.Instruction;
|
||||
|
||||
/**
|
||||
* Tests unitaires des instructions
|
||||
* @author Nicolas Caminade
|
||||
* @author Sylvan Courtiol
|
||||
* @author Pierre Debas
|
||||
* @author Heïa Dexter
|
||||
* @author Lucas Vabre
|
||||
*/
|
||||
public class TestInstruction {
|
||||
|
||||
/**
|
||||
* Test du constructeur
|
||||
*/
|
||||
public static void testInstruction() {
|
||||
System.out.println("Test du constructeur");
|
||||
Instruction aTester = new Instruction("Bonjour", new Contexte());
|
||||
System.out.println("==> OK\n");
|
||||
}
|
||||
|
||||
/**
|
||||
* Test de toString()
|
||||
*/
|
||||
public static void testToString() {
|
||||
System.out.println("Test de toString()");
|
||||
Instruction aTester = new Instruction("Bonjour", new Contexte());
|
||||
|
||||
if (!aTester.toString().equals("Instruction null"))
|
||||
System.err.println("Echec du test");
|
||||
else
|
||||
System.out.println("==> OK\n");
|
||||
|
||||
System.out.println(aTester);
|
||||
}
|
||||
|
||||
/**
|
||||
* Lancement des tests
|
||||
* @param args non utilisé
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
testInstruction();
|
||||
testToString();
|
||||
}
|
||||
}
|
||||
///**
|
||||
// * TestInstruction.java 9 mai 2021
|
||||
// * IUT info1 2020-2021, pas de copyright, aucun droit
|
||||
// */
|
||||
//package interpreteurlir.motscles.instructions.tests;
|
||||
//
|
||||
//import static info1.outils.glg.Assertions.*;
|
||||
//import interpreteurlir.Contexte;
|
||||
//import interpreteurlir.InterpreteurException;
|
||||
//import interpreteurlir.motscles.instructions.Instruction;
|
||||
//
|
||||
///**
|
||||
// * Tests unitaires des instructions
|
||||
// *
|
||||
// * @author Nicolas Caminade
|
||||
// * @author Sylvan Courtiol
|
||||
// * @author Pierre Debas
|
||||
// * @author Heïa Dexter
|
||||
// * @author Lucas Vabre
|
||||
// */
|
||||
//public class TestInstruction {
|
||||
//
|
||||
// /**
|
||||
// * Test unitaire de {@link Instruction#Instruction(String, Contexte)}
|
||||
// */
|
||||
// public static void testInstruction() {
|
||||
// System.out.println("\tExécution du test de Instruction()");
|
||||
// try {
|
||||
// new Instruction("Bonjour", new Contexte());
|
||||
// } catch (InterpreteurException lancee) {
|
||||
// echec();
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * Test unitaire de {@link Instruction#toString()}
|
||||
// */
|
||||
// public static void testToString() {
|
||||
// System.out.println("\tExécution du test de toString()");
|
||||
// Instruction aTester = new Instruction("Bonjour", new Contexte());
|
||||
// assertEquivalence(aTester.toString(), "Instruction null");
|
||||
// }
|
||||
//}
|
||||
|
||||
@@ -56,7 +56,7 @@ public class TestInstructionAffiche {
|
||||
};
|
||||
|
||||
Expression.referencerContexte(CONTEXTE_GBL);
|
||||
System.out.println("Exécution du test de InstructionAffiche(String"
|
||||
System.out.println("\tExécution du test de InstructionAffiche(String"
|
||||
+ ", Contexte)");
|
||||
for (String argInvalide : INVALIDES) {
|
||||
try {
|
||||
@@ -73,12 +73,14 @@ public class TestInstructionAffiche {
|
||||
*/
|
||||
public static void testExecuter() {
|
||||
|
||||
System.out.println("Exécution du test de executer()\nTEST VISUEL SUR "
|
||||
System.out.println("\tExécution du test de executer()\nTEST VISUEL SUR "
|
||||
+ "CONSOLE :");
|
||||
|
||||
Expression.referencerContexte(CONTEXTE_GBL);
|
||||
for (InstructionAffiche aLancer : FIXTURE)
|
||||
aLancer.executer();
|
||||
for (InstructionAffiche aLancer : FIXTURE) {
|
||||
System.out.println("\n\ttest visuel suivant : ");
|
||||
aLancer.executer();
|
||||
}
|
||||
|
||||
System.out.println();
|
||||
}
|
||||
@@ -100,7 +102,7 @@ public class TestInstructionAffiche {
|
||||
"affiche \"300000000000000000 ça passe\""
|
||||
};
|
||||
|
||||
System.out.println("Exécution du test de toString()");
|
||||
System.out.println("\tExécution du test de toString()");
|
||||
for (int i = 0 ; i < FIXTURE.length ; i++) {
|
||||
assertTrue(FIXTURE[i].toString().compareTo(ATTENDUS[i]) == 0);
|
||||
}
|
||||
|
||||
@@ -8,7 +8,6 @@ import interpreteurlir.motscles.instructions.InstructionEntre;
|
||||
import interpreteurlir.Contexte;
|
||||
import interpreteurlir.ExecutionException;
|
||||
import interpreteurlir.InterpreteurException;
|
||||
import interpreteurlir.donnees.litteraux.Entier;
|
||||
|
||||
import static info1.outils.glg.Assertions.*;
|
||||
/**
|
||||
@@ -117,5 +116,4 @@ public class TestInstructionEntre {
|
||||
System.out.println("Contexte : \n" + CONTEXTE_GLB);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,184 @@
|
||||
/**
|
||||
* TestInstructionSi.java 22 mai 2021
|
||||
* IUT Rodez info1 2020-2021, pas de copyright, aucun droit
|
||||
*/
|
||||
package interpreteurlir.motscles.instructions.tests;
|
||||
|
||||
import interpreteurlir.motscles.Commande;
|
||||
import interpreteurlir.motscles.instructions.InstructionSi;
|
||||
import interpreteurlir.motscles.instructions.InstructionVar;
|
||||
import interpreteurlir.programmes.*;
|
||||
import interpreteurlir.Contexte;
|
||||
import interpreteurlir.InterpreteurException;
|
||||
import interpreteurlir.donnees.*;
|
||||
import interpreteurlir.donnees.litteraux.*;
|
||||
import interpreteurlir.expressions.Expression;
|
||||
|
||||
import static info1.outils.glg.Assertions.*;
|
||||
|
||||
/**
|
||||
* Tests unitaires de {@link InstructionSi}
|
||||
* @author Nicolas Caminade
|
||||
* @author Sylvan Courtiol
|
||||
* @author Pierre Debas
|
||||
* @author Heïa Dexter
|
||||
* @author Lucas Vabre
|
||||
*/
|
||||
public class TestInstructionSi {
|
||||
|
||||
/** contexte pour les tests */
|
||||
private Contexte contexte = new Contexte();
|
||||
|
||||
/** programme pour les tests */
|
||||
private Programme prog = new Programme();
|
||||
|
||||
/** Jeu de donnée d'instruction si vaen valides pour les tests*/
|
||||
private InstructionSi[] fixture = {
|
||||
new InstructionSi("45 = 2 vaen 15", contexte),
|
||||
new InstructionSi("age >= 130 vaen 1000", contexte),
|
||||
new InstructionSi("$prenom <>\"défaut\" vaen 16", contexte),
|
||||
new InstructionSi("resultat < 20 vaen 17", contexte),
|
||||
new InstructionSi("resultat < moyenne vaen 18", contexte),
|
||||
new InstructionSi("age > 20 vaen 19", contexte),
|
||||
new InstructionSi("\"tata\" = \"tata\"vaen 20", contexte),
|
||||
new InstructionSi("\"toto \" > $toto vaen1502", contexte),
|
||||
new InstructionSi("-5 <= 0 vaen 21", contexte),
|
||||
new InstructionSi("resultat < 20 vaen 22", contexte),
|
||||
};
|
||||
|
||||
/**
|
||||
* Tests unitaires de {@link InstructionSi#InstructionSi(String, Contexte)}
|
||||
*/
|
||||
public void testInstructionSiStringContexte() {
|
||||
final String[] ARGS_INVALIDES = {
|
||||
"",
|
||||
" \t",
|
||||
" entier < index",
|
||||
"vaen 1050",
|
||||
"age = 10 vaen",
|
||||
" $prenom = \"défaut\" va 10",
|
||||
"$prenom <> $nom goto 45",
|
||||
"$prenom <> $nom vaen dix",
|
||||
"$prenom != $nom vaen 45",
|
||||
/* erreur de type */
|
||||
"$prenom <> 5 vaen 450",
|
||||
"age > \"\" vaen 450",
|
||||
"age >= $prenom vaen 450",
|
||||
"\"dix\" = 10 vaen 4500",
|
||||
};
|
||||
|
||||
System.out.println("\tExécution du test de "
|
||||
+ "InstructionSi#InstructionSi(String, Contexte)");
|
||||
|
||||
for (String aTester : ARGS_INVALIDES) {
|
||||
try {
|
||||
new InstructionSi(aTester, contexte);
|
||||
echec();
|
||||
} catch (InterpreteurException lancee) {
|
||||
// testok
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
new InstructionSi("45 = 2 vaen 15", contexte);
|
||||
new InstructionSi("age >= 130 vaen 1000", contexte);
|
||||
new InstructionSi("$prenom <>\"défaut\" vaen 15", contexte);
|
||||
new InstructionSi("resultat < 20 vaen 15", contexte);
|
||||
new InstructionSi("resultat < moyenne vaen 15", contexte);
|
||||
new InstructionSi("age > 20 vaen 15", contexte);
|
||||
new InstructionSi("\"tata\" = \"tata\"vaen 15", contexte);
|
||||
new InstructionSi("\"toto \" > $toto vaen1502", contexte);
|
||||
new InstructionSi("-5 <= 0 vaen 15", contexte);
|
||||
new InstructionSi("resultat < 20 vaen 15", contexte);
|
||||
new InstructionSi("$chaine <= \"vaen 15\" vaen 15", contexte);
|
||||
} catch (InterpreteurException lancee) {
|
||||
echec();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests unitaires de {@link InstructionSi#toString()}
|
||||
*/
|
||||
public void testToString() {
|
||||
final String[] ATTENDU = {
|
||||
"si 45 = 2 vaen 15",
|
||||
"si age >= 130 vaen 1000",
|
||||
"si $prenom <> \"défaut\" vaen 16",
|
||||
"si resultat < 20 vaen 17",
|
||||
"si resultat < moyenne vaen 18",
|
||||
"si age > 20 vaen 19",
|
||||
"si \"tata\" = \"tata\" vaen 20",
|
||||
"si \"toto \" > $toto vaen 1502",
|
||||
"si -5 <= 0 vaen 21",
|
||||
"si resultat < 20 vaen 22",
|
||||
};
|
||||
System.out.println("\tExécution du test de InstructionSi#toString()");
|
||||
|
||||
for (int numTest = 0 ; numTest < ATTENDU.length ; numTest++) {
|
||||
assertEquivalence(ATTENDU[numTest], fixture[numTest].toString());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests unitaires de {@link InstructionSi#executer()}
|
||||
*/
|
||||
public void testExecuter() {
|
||||
Commande.referencerProgramme(prog);
|
||||
prog.ajouterLigne(new Etiquette(15),
|
||||
new InstructionVar("valeur = valeur -1", contexte));
|
||||
prog.ajouterLigne(new Etiquette(16),
|
||||
new InstructionVar("valeur = valeur -1", contexte));
|
||||
prog.ajouterLigne(new Etiquette(17),
|
||||
new InstructionVar("valeur = valeur -1", contexte));
|
||||
prog.ajouterLigne(new Etiquette(18),
|
||||
new InstructionVar("valeur = valeur -1", contexte));
|
||||
prog.ajouterLigne(new Etiquette(19),
|
||||
new InstructionVar("valeur = valeur -1", contexte));
|
||||
prog.ajouterLigne(new Etiquette(20),
|
||||
new InstructionVar("valeur = valeur -1", contexte));
|
||||
prog.ajouterLigne(new Etiquette(21),
|
||||
new InstructionVar("valeur = valeur -1", contexte));
|
||||
prog.ajouterLigne(new Etiquette(22),
|
||||
new InstructionVar("valeur = valeur -1", contexte));
|
||||
prog.ajouterLigne(new Etiquette(1000),
|
||||
new InstructionVar("valeur = valeur -1", contexte));
|
||||
prog.ajouterLigne(new Etiquette(1502),
|
||||
new InstructionVar("valeur = valeur -1", contexte));
|
||||
Expression.referencerContexte(contexte);
|
||||
|
||||
|
||||
final int[] VALEUR_ATTENDU = {
|
||||
0, // pas de saut
|
||||
0,
|
||||
-9, // saut en 16
|
||||
-8, // saut en 17
|
||||
0,
|
||||
-6, // saut en 19
|
||||
-5, // saut en 20
|
||||
-1, // saut en 1502
|
||||
-4, // saut en 21
|
||||
-3, // saut en 22
|
||||
};
|
||||
|
||||
System.out.println("\tExécution du test de InstructionSi#executer()");
|
||||
|
||||
for (int numTest = 0 ; numTest < VALEUR_ATTENDU.length ; numTest++) {
|
||||
/* initialisation du contexte */
|
||||
contexte.raz();
|
||||
contexte.ajouterVariable(new IdentificateurEntier("moyenne"),
|
||||
new Entier("-2"));
|
||||
contexte.ajouterVariable(new IdentificateurEntier("age"),
|
||||
new Entier("99"));
|
||||
contexte.ajouterVariable(new IdentificateurChaine("$toto"),
|
||||
new Chaine("\"toto\""));
|
||||
|
||||
fixture[numTest].executer();
|
||||
assertEquivalence(VALEUR_ATTENDU[numTest],
|
||||
((Integer)contexte.lireValeurVariable(
|
||||
new IdentificateurEntier("valeur"))
|
||||
.getValeur()).intValue());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -44,7 +44,7 @@ public class TestInstructionStop {
|
||||
"entier = 2 + 3"
|
||||
};
|
||||
|
||||
System.out.println("Exécution du test de InstructionStop"
|
||||
System.out.println("\tExécution du test de InstructionStop"
|
||||
+ "(String, Contexte)");
|
||||
for (String aTester : INVALIDES) {
|
||||
try {
|
||||
@@ -59,32 +59,34 @@ public class TestInstructionStop {
|
||||
/** Test de executer() */
|
||||
public static void testExecuter() {
|
||||
Programme pgmTest = new Programme();
|
||||
System.out.println("Exécution du test de executer()\ntestVisuel\n");
|
||||
System.out.println("\tExécution du test de executer()\nTest Visuels\n");
|
||||
Commande.referencerProgramme(pgmTest);
|
||||
Expression.referencerContexte(CONTEXTE_TESTS);
|
||||
pgmTest.ajouterLigne(new Etiquette(10),
|
||||
new InstructionAffiche("Bonjour", CONTEXTE_TESTS));
|
||||
new InstructionAffiche("\"Bonjour\"", CONTEXTE_TESTS));
|
||||
pgmTest.ajouterLigne(new Etiquette(20),
|
||||
new InstructionAffiche("Comment", CONTEXTE_TESTS));
|
||||
new InstructionAffiche("\"Comment\"", CONTEXTE_TESTS));
|
||||
pgmTest.ajouterLigne(new Etiquette(30),
|
||||
new InstructionAffiche("Allez", CONTEXTE_TESTS));
|
||||
new InstructionAffiche("\"Allez\"", CONTEXTE_TESTS));
|
||||
pgmTest.ajouterLigne(new Etiquette(40),
|
||||
new InstructionAffiche("Vous", CONTEXTE_TESTS));
|
||||
new InstructionAffiche("\"Vous\"", CONTEXTE_TESTS));
|
||||
pgmTest.ajouterLigne(new Etiquette(45),
|
||||
new InstructionStop("", CONTEXTE_TESTS));
|
||||
pgmTest.ajouterLigne(new Etiquette(50),
|
||||
new InstructionAffiche("foobar", CONTEXTE_TESTS));
|
||||
new InstructionAffiche("\"foobar\"", CONTEXTE_TESTS));
|
||||
System.out.println(pgmTest);
|
||||
System.out.println("lancement du programme : ne doit pas "
|
||||
+ "afficher foobar");
|
||||
pgmTest.lancer();
|
||||
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
/** Tests de toString() */
|
||||
public static void testToString() {
|
||||
|
||||
final String ATTENDUE = "stop";
|
||||
System.out.println("Exécution du test de toString()");
|
||||
System.out.println("\tExécution du test de toString()");
|
||||
for (InstructionStop valide : FIXTURE)
|
||||
assertTrue(valide.toString().compareTo(ATTENDUE) == 0);
|
||||
}
|
||||
|
||||
@@ -4,12 +4,14 @@
|
||||
*/
|
||||
package interpreteurlir.motscles.instructions.tests;
|
||||
|
||||
import static info1.outils.glg.Assertions.*;
|
||||
import interpreteurlir.Contexte;
|
||||
import interpreteurlir.InterpreteurException;
|
||||
import interpreteurlir.motscles.instructions.InstructionVar;
|
||||
|
||||
/**
|
||||
* Tests unitaires de l'instruction var
|
||||
* Tests unitaires de la classe InstructionVar
|
||||
*
|
||||
* @author Nicolas Caminade
|
||||
* @author Sylvan Courtiol
|
||||
* @author Pierre Debas
|
||||
@@ -25,59 +27,50 @@ public class TestInstructionVar {
|
||||
};
|
||||
|
||||
/**
|
||||
* Test du constructeur InstructionVar
|
||||
* Test unitaire de {@link InstructionVar#InstructionVar(String, Contexte)}
|
||||
*/
|
||||
public static void testInstructionVar() {
|
||||
final String[] EXPRESSIONS_INVALIDES = {
|
||||
"bonjour", "", null, "$toto $tata",
|
||||
"bonjour", "", "$toto $tata",
|
||||
};
|
||||
|
||||
System.out.println("Test du constructeur\navec expressions invalides");
|
||||
System.out.println("\tExécution du test de InstructionVar(String, "
|
||||
+ "Contexte)");
|
||||
|
||||
for (String aTester : EXPRESSIONS_INVALIDES) {
|
||||
try {
|
||||
new InstructionVar(aTester, new Contexte());
|
||||
throw new RuntimeException("Echec du test");
|
||||
echec();
|
||||
} catch (InterpreteurException lancee) {
|
||||
System.out.println(aTester + " ==> OK");
|
||||
// Test OK
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println("Avec expressions valides");
|
||||
for (String aTester : VALIDES)
|
||||
new InstructionVar(aTester, new Contexte());
|
||||
|
||||
System.out.println("Fin du test\n");
|
||||
for (String aTester : VALIDES) {
|
||||
try {
|
||||
new InstructionVar(aTester, new Contexte());
|
||||
} catch (InterpreteurException lancee) {
|
||||
echec();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test de toString
|
||||
* Test unitaire de {@link InstructionVar#toString()}
|
||||
*/
|
||||
public static void testToString() {
|
||||
final String[] CHAINES_ATTENDUES = {
|
||||
"var $toto = $tata", "var entier=2+2",
|
||||
"var $coucou = $toto + \\\"titi\\\"",
|
||||
"var $toto = $tata",
|
||||
"var entier = 2 + 2",
|
||||
"var $coucou = $toto + \"titi\"",
|
||||
"var anneeNaissance = 1898"
|
||||
};
|
||||
|
||||
System.out.println("Test de toString()");
|
||||
System.out.println("\tExécution du tes de toString()");
|
||||
for (int i = 0 ; i < VALIDES.length ; i++) {
|
||||
|
||||
try {
|
||||
assert VALIDES[i].toString().equals(CHAINES_ATTENDUES[i]);
|
||||
} catch (AssertionError lancee) {
|
||||
System.err.println("Echec du test à l'indice " + i);
|
||||
}
|
||||
InstructionVar aTester = new InstructionVar(VALIDES[i],
|
||||
new Contexte());
|
||||
assertTrue(CHAINES_ATTENDUES[i].equals(aTester.toString()));
|
||||
}
|
||||
System.out.println("Fin du test\n");
|
||||
}
|
||||
|
||||
/**
|
||||
* Lancement des tests
|
||||
* @param args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
testInstructionVar();
|
||||
testToString();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,7 @@ package interpreteurlir.motscles.tests;
|
||||
import interpreteurlir.Contexte;
|
||||
import interpreteurlir.InterpreteurException;
|
||||
import interpreteurlir.motscles.*;
|
||||
import interpreteurlir.programmes.Programme;
|
||||
|
||||
/**
|
||||
* Essais des commandes (création + éxécution)
|
||||
@@ -24,6 +25,7 @@ public class EssaiCommande {
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
Contexte contexte = new Contexte();
|
||||
Commande.referencerProgramme(new Programme());
|
||||
|
||||
/* Erreur dans commande */
|
||||
System.out.println("? debut args");
|
||||
@@ -49,9 +51,11 @@ public class EssaiCommande {
|
||||
System.out.println("? debut");
|
||||
feedback(new CommandeDebut("", contexte).executer());
|
||||
System.out.println("? defs");
|
||||
feedback(new CommandeDefs( "", contexte).executer());
|
||||
feedback(new CommandeDefs("", contexte).executer());
|
||||
System.out.println("? liste");
|
||||
feedback(new CommandeListe("", contexte).executer());
|
||||
System.out.println("? fin");
|
||||
feedback(new CommandeFin( "", contexte).executer());
|
||||
feedback(new CommandeFin("", contexte).executer());
|
||||
|
||||
System.err.println("Erreur, la commande fin n'a pas quitter");
|
||||
|
||||
|
||||
@@ -1,88 +1,90 @@
|
||||
/**
|
||||
* TestCommande.java 7 mai 2021
|
||||
* IUT Rodez info1 2020-2021, pas de copyright, aucun droit
|
||||
*/
|
||||
package interpreteurlir.motscles.tests;
|
||||
// Classe testée passé en abstract
|
||||
|
||||
import static info1.outils.glg.Assertions.*;
|
||||
|
||||
import interpreteurlir.Contexte;
|
||||
import interpreteurlir.expressions.Expression;
|
||||
import interpreteurlir.motscles.Commande;
|
||||
import interpreteurlir.programmes.Programme;
|
||||
|
||||
/**
|
||||
* Tests unitaires de {@link interpreteurlir.motscles.Commande}
|
||||
* @author Nicolas Caminade
|
||||
* @author Sylvan Courtiol
|
||||
* @author Pierre Debas
|
||||
* @author Heïa Dexter
|
||||
* @author Lucas Vabre
|
||||
*/
|
||||
public class TestCommande {
|
||||
|
||||
/** Jeux d'essais de Commande valides pour les tests */
|
||||
private Commande[] fixture = {
|
||||
new Commande("", new Contexte()),
|
||||
new Commande("coucou", new Contexte()),
|
||||
new Commande("$chaine = \"toto\" + $tata", new Contexte())
|
||||
};
|
||||
|
||||
/**
|
||||
* Tests unitaires de {@link Commande#referencerProgramme(Programme)}
|
||||
*/
|
||||
public void testReferencerProgramme() {
|
||||
|
||||
Programme reference = new Programme();
|
||||
Programme[] programmes = {
|
||||
null, reference, reference, new Programme()
|
||||
};
|
||||
|
||||
boolean[] resultatAttendu = { false, true, true, false };
|
||||
|
||||
System.out.println("\tExécution du test de "
|
||||
+ "Commande#referencerProgramme(Programme)");
|
||||
for (int numTest = 0 ; numTest < programmes.length ; numTest++) {
|
||||
assertTrue( Commande.referencerProgramme(programmes[numTest])
|
||||
== resultatAttendu[numTest]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests unitaires de {@link Commande#Commande(String, Contexte)}
|
||||
*/
|
||||
public void testCommandeStringContexte() {
|
||||
System.out.println(
|
||||
"\tExécution du test de Commande#Commande(String, Contexte)");
|
||||
|
||||
/* Tests Commande invalide */
|
||||
String[] arguments = { null, null, "" };
|
||||
Contexte[] contexte = { null, new Contexte(), null};
|
||||
for (int numTest = 0 ; numTest < arguments.length ; numTest++) {
|
||||
try {
|
||||
new Commande(arguments[numTest], contexte[numTest]);
|
||||
echec();
|
||||
} catch (NullPointerException lancee) {
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
new Commande("", new Contexte());
|
||||
new Commande("coucou", new Contexte());
|
||||
new Commande("$chaine = \"toto\" + $tata", new Contexte());
|
||||
} catch (NullPointerException e) {
|
||||
echec();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Tests unitaires de {@link Commande#executer()}
|
||||
*/
|
||||
public void testExecuter() {
|
||||
System.out.println("\tExécution du test de Commande#executer()");
|
||||
for (Commande aTester : fixture) {
|
||||
assertFalse(aTester.executer());
|
||||
}
|
||||
}
|
||||
}
|
||||
///**
|
||||
// * TestCommande.java 7 mai 2021
|
||||
// * IUT Rodez info1 2020-2021, pas de copyright, aucun droit
|
||||
// */
|
||||
//package interpreteurlir.motscles.tests;
|
||||
//
|
||||
//import static info1.outils.glg.Assertions.*;
|
||||
//
|
||||
//import interpreteurlir.Contexte;
|
||||
//import interpreteurlir.expressions.Expression;
|
||||
//import interpreteurlir.motscles.Commande;
|
||||
//import interpreteurlir.programmes.Programme;
|
||||
//
|
||||
///**
|
||||
// * Tests unitaires de {@link interpreteurlir.motscles.Commande}
|
||||
// * @author Nicolas Caminade
|
||||
// * @author Sylvan Courtiol
|
||||
// * @author Pierre Debas
|
||||
// * @author Heïa Dexter
|
||||
// * @author Lucas Vabre
|
||||
// */
|
||||
//public class TestCommande {
|
||||
//
|
||||
// /** Jeux d'essais de Commande valides pour les tests */
|
||||
// private Commande[] fixture = {
|
||||
// new Commande("", new Contexte()),
|
||||
// new Commande("coucou", new Contexte()),
|
||||
// new Commande("$chaine = \"toto\" + $tata", new Contexte())
|
||||
// };
|
||||
//
|
||||
// /**
|
||||
// * Tests unitaires de {@link Commande#referencerProgramme(Programme)}
|
||||
// */
|
||||
// public void testReferencerProgramme() {
|
||||
//
|
||||
// Programme reference = new Programme();
|
||||
// Programme[] programmes = {
|
||||
// null, reference, reference, new Programme()
|
||||
// };
|
||||
//
|
||||
// boolean[] resultatAttendu = { false, true, true, false };
|
||||
//
|
||||
// System.out.println("\tExécution du test de "
|
||||
// + "Commande#referencerProgramme(Programme)");
|
||||
// for (int numTest = 0 ; numTest < programmes.length ; numTest++) {
|
||||
// assertTrue( Commande.referencerProgramme(programmes[numTest])
|
||||
// == resultatAttendu[numTest]);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * Tests unitaires de {@link Commande#Commande(String, Contexte)}
|
||||
// */
|
||||
// public void testCommandeStringContexte() {
|
||||
// System.out.println(
|
||||
// "\tExécution du test de Commande#Commande(String, Contexte)");
|
||||
//
|
||||
// /* Tests Commande invalide */
|
||||
// String[] arguments = { null, null, "" };
|
||||
// Contexte[] contexte = { null, new Contexte(), null};
|
||||
// for (int numTest = 0 ; numTest < arguments.length ; numTest++) {
|
||||
// try {
|
||||
// new Commande(arguments[numTest], contexte[numTest]);
|
||||
// echec();
|
||||
// } catch (NullPointerException lancee) {
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// try {
|
||||
// new Commande("", new Contexte());
|
||||
// new Commande("coucou", new Contexte());
|
||||
// new Commande("$chaine = \"toto\" + $tata", new Contexte());
|
||||
// } catch (NullPointerException e) {
|
||||
// echec();
|
||||
// }
|
||||
// }
|
||||
//
|
||||
//
|
||||
// /**
|
||||
// * Tests unitaires de {@link Commande#executer()}
|
||||
// */
|
||||
// public void testExecuter() {
|
||||
// System.out.println("\tExécution du test de Commande#executer()");
|
||||
// for (Commande aTester : fixture) {
|
||||
// assertFalse(aTester.executer());
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
@@ -0,0 +1,111 @@
|
||||
/**
|
||||
* TestCommandeCharge.java 21 mai 2021
|
||||
* IUT Rodez info1 2020-2021, pas de copyright, aucun droit
|
||||
*/
|
||||
package interpreteurlir.motscles.tests;
|
||||
|
||||
import interpreteurlir.Contexte;
|
||||
import interpreteurlir.InterpreteurException;
|
||||
import interpreteurlir.motscles.Commande;
|
||||
import interpreteurlir.motscles.CommandeCharge;
|
||||
import interpreteurlir.motscles.CommandeListe;
|
||||
import interpreteurlir.programmes.Programme;
|
||||
|
||||
import static info1.outils.glg.Assertions.*;
|
||||
|
||||
/**
|
||||
* Tests unitaires de {@link interpreteurlir.motscles.CommandeCharge}
|
||||
* @author Nicolas Caminade
|
||||
* @author Sylvan Courtiol
|
||||
* @author Pierre Debas
|
||||
* @author Heia Dexter
|
||||
* @author Lucas Vabre
|
||||
*/
|
||||
public class TestCommandeCharge {
|
||||
|
||||
/** Contexte pour tests */
|
||||
private final static Contexte CONTEXTE_TESTS = new Contexte();
|
||||
|
||||
/** Programme global pour tests */
|
||||
private static Programme progGlobal = new Programme();
|
||||
|
||||
/** jeu de test valide */
|
||||
public static final CommandeCharge[] FIXTURE = {
|
||||
new CommandeCharge("F:\\Programmation\\WorkspaceInterpreteurLIR"
|
||||
+ "\\outilTest\\dossierFichier\\"
|
||||
+ "lefichier1.lir", CONTEXTE_TESTS),
|
||||
new CommandeCharge("dossierFichier\\lefichier2.lir",
|
||||
CONTEXTE_TESTS),
|
||||
new CommandeCharge("dossierFichier\\lefichier3.lir",
|
||||
CONTEXTE_TESTS),
|
||||
new CommandeCharge("dossierFichier\\test\\lefichier4.lir",
|
||||
CONTEXTE_TESTS),
|
||||
new CommandeCharge("dossierFichier\\test\\test2\\lefichier5.lir",
|
||||
CONTEXTE_TESTS),
|
||||
new CommandeCharge(" dossierFichier\\lefichier6.lir ",
|
||||
CONTEXTE_TESTS),
|
||||
new CommandeCharge("dossierFichier\\test\\test2\\..\\lefichier7.lir",
|
||||
CONTEXTE_TESTS)
|
||||
};
|
||||
|
||||
/**
|
||||
* Tests unitaires de
|
||||
* {@link CommandeCharge#CommandeCharge(String, Contexte)}
|
||||
*/
|
||||
public static void testCommandeCharge() {
|
||||
|
||||
final String[] INVALIDE = {
|
||||
null,
|
||||
" ",
|
||||
"",
|
||||
"lefichier",
|
||||
"dossier\\ lefichier",
|
||||
"dossier \\lefichier",
|
||||
};
|
||||
|
||||
for (int i = 0; i < INVALIDE.length ; i++) {
|
||||
try {
|
||||
new CommandeCharge(INVALIDE[i], CONTEXTE_TESTS);
|
||||
echec();
|
||||
} catch (InterpreteurException | NullPointerException lancee) {
|
||||
// Test OK
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests unitaires de
|
||||
* {@link CommandeCharge#executer()}
|
||||
*/
|
||||
public static void testExecuter() {
|
||||
|
||||
Commande.referencerProgramme(progGlobal);
|
||||
|
||||
final CommandeCharge[] ERREUR = {
|
||||
new CommandeCharge("dossierFichier\\erreur1.lir",
|
||||
CONTEXTE_TESTS),
|
||||
new CommandeCharge("dossierFichier\\erreur2.lir",
|
||||
CONTEXTE_TESTS)
|
||||
};
|
||||
|
||||
final int NB_TESTS = FIXTURE.length;
|
||||
System.out.println("\nTest valides de CommandeCharge#executer():");
|
||||
for (int i = 0; i < NB_TESTS ; i++) {
|
||||
System.out.println("Test " + (i+1) + '\\' + NB_TESTS + ":");
|
||||
FIXTURE[i].executer();
|
||||
new CommandeListe("", CONTEXTE_TESTS).executer();
|
||||
}
|
||||
|
||||
System.out.println("\nTest invalides de CommandeCharge#executer():");
|
||||
for(int i = 0; i < ERREUR.length ; i++) {
|
||||
try {
|
||||
ERREUR[i].executer();
|
||||
echec();
|
||||
} catch (InterpreteurException lancee) {
|
||||
// Test OK
|
||||
new CommandeListe("", CONTEXTE_TESTS).executer();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -8,7 +8,9 @@ import static info1.outils.glg.Assertions.*;
|
||||
|
||||
import interpreteurlir.InterpreteurException;
|
||||
import interpreteurlir.Contexte;
|
||||
import interpreteurlir.motscles.Commande;
|
||||
import interpreteurlir.motscles.CommandeDebut;
|
||||
import interpreteurlir.programmes.Programme;
|
||||
|
||||
/**
|
||||
* Tests unitaires de {@link interpreteurlir.motscles.CommandeDebut}
|
||||
@@ -59,6 +61,7 @@ public class TestCommandeDebut {
|
||||
* Tests unitaires de {@link CommandeDebut#executer()}
|
||||
*/
|
||||
public void testExecuter() {
|
||||
Commande.referencerProgramme(new Programme());
|
||||
System.out.println("\tExécution du test de CommandeDebut#executer()");
|
||||
for (CommandeDebut cmd : fixture) {
|
||||
assertFalse(cmd.executer());
|
||||
|
||||
@@ -54,14 +54,14 @@ public class TestCommandeEfface {
|
||||
"\'a\' : 99"
|
||||
};
|
||||
|
||||
System.out.println("Exécution du test de CommandeEfface"
|
||||
System.out.println("\tExécution du test de CommandeEfface"
|
||||
+ "(String, Contexte)");
|
||||
for (String aTester : INVALIDES) {
|
||||
try {
|
||||
new CommandeEfface(aTester, CONTEXTE_TESTS);
|
||||
echec();
|
||||
} catch (InterpreteurException e) {
|
||||
// TODO: handle exception
|
||||
// test OK
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -69,7 +69,7 @@ public class TestCommandeEfface {
|
||||
/** Test de executer() */
|
||||
public static void testExecuter() {
|
||||
|
||||
System.out.println("Exécution du test d'executer()\nTest visuel :");
|
||||
System.out.println("\tExécution du test d'executer()\nTest visuel :");
|
||||
Commande.referencerProgramme(PGM_TESTS);
|
||||
PGM_TESTS.ajouterLigne(new Etiquette(10),
|
||||
new InstructionAffiche("Bonjour", CONTEXTE_TESTS));
|
||||
|
||||
@@ -19,11 +19,6 @@ import interpreteurlir.motscles.CommandeFin;
|
||||
* @author Lucas Vabre
|
||||
*/
|
||||
public class TestCommandeFin {
|
||||
|
||||
/** Jeux d'essais de Commande valides pour les tests */
|
||||
private CommandeFin[] fixture = {
|
||||
new CommandeFin("", new Contexte()),
|
||||
};
|
||||
|
||||
/**
|
||||
* Tests unitaires de {@link CommandeFin#CommandeFin(String, Contexte)}
|
||||
@@ -61,7 +56,6 @@ public class TestCommandeFin {
|
||||
System.out.println("\tLe programme doit s'éteindre en affichant un "
|
||||
+ "message d'aurevoir :");
|
||||
System.out.println("Test exécuter désactiver");
|
||||
// fixture[0].executer();
|
||||
echec();
|
||||
//fixture[0].executer();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,10 +8,6 @@ import interpreteurlir.Contexte;
|
||||
import interpreteurlir.InterpreteurException;
|
||||
import interpreteurlir.expressions.Expression;
|
||||
import interpreteurlir.motscles.CommandeLance;
|
||||
import interpreteurlir.motscles.instructions.Instruction;
|
||||
import interpreteurlir.motscles.instructions.InstructionVar;
|
||||
import interpreteurlir.programmes.Etiquette;
|
||||
import interpreteurlir.programmes.Programme;
|
||||
|
||||
import static info1.outils.glg.Assertions.*;
|
||||
|
||||
@@ -29,7 +25,6 @@ import info1.outils.glg.TestException;
|
||||
public class TestCommandeLance {
|
||||
|
||||
private Contexte contexteTest = new Contexte();
|
||||
private Programme programmeTest = new Programme();
|
||||
|
||||
private final CommandeLance[] FIXTURE = {
|
||||
new CommandeLance("", contexteTest),
|
||||
@@ -47,37 +42,7 @@ public class TestCommandeLance {
|
||||
"20",
|
||||
"70",
|
||||
"40"
|
||||
};
|
||||
|
||||
private final Etiquette[] JEU_ETIQUETTES = {
|
||||
new Etiquette(1),
|
||||
new Etiquette(10),
|
||||
new Etiquette(13),
|
||||
new Etiquette(25),
|
||||
new Etiquette(31),
|
||||
new Etiquette(40),
|
||||
new Etiquette(78),
|
||||
new Etiquette(89)
|
||||
};
|
||||
|
||||
private final Instruction[] JEU_INSTRUCTIONS = {
|
||||
new InstructionVar("$res = \"1 \"", contexteTest),
|
||||
new InstructionVar("$res = $res + \"10 \"", contexteTest),
|
||||
new InstructionVar("$res = $res + \"13 \"", contexteTest),
|
||||
new InstructionVar("$res = $res + \"25 \"", contexteTest),
|
||||
new InstructionVar("$res = $res + \"31 \"", contexteTest),
|
||||
new InstructionVar("$res = $res + \"40 \"", contexteTest),
|
||||
new InstructionVar("$res = $res + \"78 \"", contexteTest),
|
||||
new InstructionVar("$res = $res + \"89 \"", contexteTest)
|
||||
};
|
||||
|
||||
private void ecrireProgrammeTest() {
|
||||
for (int i = 0 ; i < JEU_ETIQUETTES.length ; i++) {
|
||||
programmeTest.ajouterLigne(JEU_ETIQUETTES[i],
|
||||
JEU_INSTRUCTIONS[i]);
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* Test unitaire de
|
||||
@@ -95,6 +60,9 @@ public class TestCommandeLance {
|
||||
|
||||
Expression.referencerContexte(contexteTest);
|
||||
|
||||
System.out.println("\tExécution du test de "
|
||||
+ "CommandeLance#CommandeLance(String, Contexte)");
|
||||
|
||||
for (int i = 0; i < ARGS_INVALIDES.length; i++) {
|
||||
try {
|
||||
new CommandeLance(ARGS_INVALIDES[i], contexteTest);
|
||||
@@ -122,6 +90,7 @@ public class TestCommandeLance {
|
||||
//ecrireProgrammeTest();
|
||||
Expression.referencerContexte(contexteTest);
|
||||
|
||||
System.out.println("\tExécution du test de CommandeLance#executer()");
|
||||
for (int i = 0 ; i < FIXTURE.length ; i++) {
|
||||
try {
|
||||
FIXTURE[i].executer();
|
||||
|
||||
@@ -91,6 +91,9 @@ public class TestCommandeListe {
|
||||
"1:100000"
|
||||
};
|
||||
|
||||
System.out.println("\tExécution du test de "
|
||||
+ "CommandeListe#CommandeListe(String, Contexte)");
|
||||
|
||||
for (int i = 0; i < ARGS_INVALIDES.length; i++) {
|
||||
try {
|
||||
new CommandeListe(ARGS_INVALIDES[i], contexteTest);
|
||||
@@ -130,6 +133,9 @@ public class TestCommandeListe {
|
||||
Commande.referencerProgramme(programmeTest);
|
||||
Expression.referencerContexte(contexteTest);
|
||||
|
||||
System.out.println("\tExécution du test de "
|
||||
+ "CommandeListe#executer()");
|
||||
|
||||
for (int i = 0 ; i < FIXTURE.length ; i++) {
|
||||
try {
|
||||
FIXTURE[i].executer();
|
||||
|
||||
@@ -0,0 +1,176 @@
|
||||
/**
|
||||
* TestCommandeSauve.java 21 mai 2021
|
||||
* IUT Rodez info1 2020-2021, pas de copyright, aucun droit
|
||||
*/
|
||||
package interpreteurlir.motscles.tests;
|
||||
|
||||
import static info1.outils.glg.Assertions.*;
|
||||
|
||||
import interpreteurlir.motscles.Commande;
|
||||
import interpreteurlir.motscles.CommandeSauve;
|
||||
import interpreteurlir.programmes.Programme;
|
||||
import interpreteurlir.Contexte;
|
||||
import interpreteurlir.ExecutionException;
|
||||
import interpreteurlir.InterpreteurException;
|
||||
import interpreteurlir.tests.ProgrammeDeTest;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.InputStreamReader;
|
||||
|
||||
/**
|
||||
* Tests unitaires de {@link CommandeSauve}
|
||||
* @author Nicolas Caminade
|
||||
* @author Sylvan Courtiol
|
||||
* @author Pierre Debas
|
||||
* @author Heia Dexter
|
||||
* @author Lucas Vabre
|
||||
*/
|
||||
public class TestCommandeSauve {
|
||||
|
||||
/** contexte pour les tests */
|
||||
private Contexte contexte = new Contexte();
|
||||
|
||||
/** Programme pour les tests */
|
||||
private Programme progGlobal = new Programme();
|
||||
|
||||
/** Jeu de donnée de commandeSauve valides pour les tests */
|
||||
private CommandeSauve[] fixture = {
|
||||
/* chemin valide */
|
||||
new CommandeSauve("monProgramme.lir", contexte),
|
||||
new CommandeSauve("programmationLIR\\monProgramme.lir", contexte),
|
||||
new CommandeSauve("D:\\testInterpreteurLIR\\test1.lir", contexte),
|
||||
new CommandeSauve(" D:\\testInterpreteurLIR\\test2.lir\t", contexte),
|
||||
|
||||
/* chemin invalide à l'exécution*/
|
||||
new CommandeSauve("\\\\monProgramme.lir", contexte),
|
||||
new CommandeSauve("monPro//??!gr<>amme.lir", contexte),
|
||||
/* chemin inexistant */
|
||||
new CommandeSauve("D:\\testInterpreteurLIR\\dossierNonCree\\test1.lir",
|
||||
contexte),
|
||||
/* lecteur inexistant */
|
||||
new CommandeSauve("X:\\testInterpreteurLIR\\test1.lir", contexte),
|
||||
};
|
||||
|
||||
/**
|
||||
* Tests unitaires de {@link CommandeSauve#CommandeSauve(String, Contexte)}
|
||||
*/
|
||||
public void testCommandeSauveStringContexte() {
|
||||
final String[] ARGS_INVALIDES = {
|
||||
"",
|
||||
" \t ",
|
||||
"D:\\utilisateurs\\defaut\\bureau\\",
|
||||
"D:\\utilisateurs\\defaut\\bureau\\monProgramme.txt",
|
||||
"D:\\utilisateurs\\defaut\\bureau\\monProgramme",
|
||||
"nouveau dossier\\monProgramme.java",
|
||||
"nouveau dossier\\monProgramme",
|
||||
"monProgramme.class",
|
||||
"monProgramme"
|
||||
};
|
||||
|
||||
System.out.println("\tExécution du test de "
|
||||
+ "CommandeSauve#CommandeSauve(String, Contexte)");
|
||||
|
||||
for (String aTester : ARGS_INVALIDES) {
|
||||
try {
|
||||
new CommandeSauve(aTester, contexte);
|
||||
echec();
|
||||
} catch (InterpreteurException e) {
|
||||
// test ok
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
/* chemin valide */
|
||||
new CommandeSauve("monProgramme.lir", contexte);
|
||||
new CommandeSauve("programmationLIR\\monProgramme.lir", contexte);
|
||||
new CommandeSauve("D:\\testInterpreteurLIR\\test1.lir", contexte);
|
||||
new CommandeSauve(" D:\\testInterpreteurLIR\\test2.lir\t",
|
||||
contexte);
|
||||
/* chemin invalide à l'exécution*/
|
||||
new CommandeSauve("\\\\monProgramme.lir", contexte);
|
||||
new CommandeSauve("monPro//??!gr<>amme.lir", contexte);
|
||||
/* chemin inexistant */
|
||||
new CommandeSauve("D:\\testInterpreteurLIR\\dossierNonCree\\"
|
||||
+ "test1.lir", contexte);
|
||||
/* lecteur inexistant */
|
||||
new CommandeSauve("X:\\testInterpreteurLIR\\test1.lir", contexte);
|
||||
} catch (InterpreteurException e) {
|
||||
echec();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests unitaires de {@link CommandeSauve#executer()}
|
||||
*/
|
||||
public void testExecuter() {
|
||||
final int INDEX_INVALIDES = 4;
|
||||
|
||||
Commande.referencerProgramme(progGlobal);
|
||||
System.out.println("\tExécution du test de CommandeSauve#executer()");
|
||||
|
||||
/* Tests des chemins invalides */
|
||||
for (int index = INDEX_INVALIDES ; index < fixture.length ; index++) {
|
||||
try {
|
||||
fixture[index].executer();
|
||||
echec();
|
||||
} catch (ExecutionException lancee) {
|
||||
// test OK
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
try {
|
||||
assertFalse(fixture[0].executer());
|
||||
assertEquivalence(progGlobal.toString(),
|
||||
lireFichier("monProgramme.lir"));
|
||||
assertFalse(fixture[2].executer());
|
||||
assertEquivalence(progGlobal.toString(),
|
||||
lireFichier("D:\\testInterpreteurLIR\\test1.lir"));
|
||||
|
||||
ProgrammeDeTest.genererProgramme(progGlobal, contexte);
|
||||
|
||||
assertFalse(fixture[1].executer());
|
||||
assertEquivalence(progGlobal.toString(),
|
||||
lireFichier("programmationLIR\\monProgramme.lir"));
|
||||
|
||||
assertFalse(fixture[3].executer());
|
||||
assertEquivalence(progGlobal.toString(),
|
||||
lireFichier("D:\\testInterpreteurLIR\\test2.lir"));
|
||||
|
||||
} catch (ExecutionException lancee) {
|
||||
echec();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Lit un fichier et retourne le contenu entier du fichier
|
||||
* @param cheminFichier chemin du fichier à lire
|
||||
* @return contenu du fichier
|
||||
*/
|
||||
private static String lireFichier(String cheminFichier) {
|
||||
BufferedReader aTester;
|
||||
StringBuilder contenu = new StringBuilder("");
|
||||
|
||||
aTester = null;
|
||||
try {
|
||||
aTester = new BufferedReader(
|
||||
new InputStreamReader(
|
||||
new FileInputStream(cheminFichier)));
|
||||
String ligneLue;
|
||||
do {
|
||||
ligneLue = aTester.readLine();
|
||||
if (ligneLue != null) {
|
||||
contenu.append(ligneLue).append("\n");
|
||||
}
|
||||
} while (ligneLue != null);
|
||||
aTester.close();
|
||||
} catch (Exception e) {
|
||||
echec();
|
||||
}
|
||||
|
||||
return contenu.toString();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user