View Javadoc

    1   /**
    2    *
    3    */
    4   package net.sourceforge.pmd.cpd;
    5
    6   import java.io.IOException;
    7
    8   import net.sourceforge.pmd.PMD;
    9   import net.sourceforge.pmd.cpd.FortranTokenizer;
    10  import net.sourceforge.pmd.cpd.SourceCode;
    11  import net.sourceforge.pmd.testframework.AbstractTokenizerTest;
    12
    13  import org.junit.Before;
    14  import org.junit.Test;
    15
    16
    17  /**
    18   * @author rpelisse
    19   *
    20   */
    21  public class FortranTokenizerTest extends AbstractTokenizerTest {
    22
    23  	@Before
    24  	@Override
    25  	public void buildTokenizer() {
    26  		this.tokenizer = new FortranTokenizer();
    27  		this.sourceCode = new SourceCode(new SourceCode.StringCodeLoader(this.getSampleCode(), "sample.for"));
    28  	}
    29
    30  	@Override
    31  	public String getSampleCode() {
    32  		 return "       options/extend_source" + PMD.EOL +
    33  		 "       program tp3" + PMD.EOL +
    34  		 "       implicit none" + PMD.EOL +
    35  		 "" + PMD.EOL +
    36  		 "! Ce programme va demander la saisie de la commande, puis on va separer les differentes" + PMD.EOL +
    37  		 "!parties de la chaine en plusieurs variables, ensuite selon l'action demandee on appelera le" + PMD.EOL +
    38  		 "!sous programme correspondant." + PMD.EOL +
    39  		 "" + PMD.EOL +
    40  		 "       character*60 COMMANDE" + PMD.EOL +
    41  		 "       integer*4 IOS," + PMD.EOL +
    42  		 "       1         COMPTEUR," + PMD.EOL +
    43  		 "       1         SORTIE," + PMD.EOL +
    44  		 "       1         ERRONE," + PMD.EOL +
    45  		 "       1         CONF," + PMD.EOL +
    46  		 "       1         POSITION_ESPACE," + PMD.EOL +
    47  		 "       1         DEBUT_MOT," + PMD.EOL +
    48  		 "       1         FIN_MOT," + PMD.EOL +
    49  		 "       1         NB_MOTS," + PMD.EOL +
    50  		 "       1         NB_MOTS_MAX," + PMD.EOL +
    51  		 "       1         FIN_CHAINE," + PMD.EOL +
    52  		 "       1         TROUVER_FIN," + PMD.EOL +
    53  		 "       1         NUM_CARACTERE," + PMD.EOL +
    54  		 "       1         ACTION," + PMD.EOL +
    55  		 "       1         PREMIERE_LETTRE," + PMD.EOL +
    56  		 "       1         DERNIERE_LETTRE," + PMD.EOL +
    57  		 "       1         INTERVALLE_MAJ_MIN," + PMD.EOL +
    58  		 "       1         APRES_MAJ," + PMD.EOL +
    59  		 "       1         TAILLE_COLONNE," + PMD.EOL +
    60  		 "       1         TAILLE_LIGNE," + PMD.EOL +
    61  		 "       1         LIGNES_DESC" + PMD.EOL +
    62  		 "" + PMD.EOL +
    63  		 "       parameter(NB_MOTS_MAX = 9) !une saisie correcte ne contient pas plus de 8 mots, si" + PMD.EOL +
    64  		 "!elle en contient 9, alors la saisie sera jugee incorrecte." + PMD.EOL +
    65  		 "       parameter(ERRONE = 1)" + PMD.EOL +
    66  		 "       parameter(SORTIE = - 1)" + PMD.EOL +
    67  		 "       parameter(ACTION = 1)  !il s'agit du 1er mot de la chaine de caracteres" + PMD.EOL +
    68  		 "       parameter(PREMIERE_LETTRE = 1)  !correspond a la 1ere lettre d'un mot" + PMD.EOL +
    69  		 "       parameter(DERNIERE_LETTRE = 18)  !correspond a la derniere lettre d'un mot" + PMD.EOL +
    70  		 "       parameter(INTERVALLE_MAJ_MIN = 32)  !nombre separant un meme caractere" + PMD.EOL +
    71  		 "!minuscule de son majuscule" + PMD.EOL +
    72  		 "       parameter(APRES_MAJ = 96)  !correspond au dernier caractere avant les MIN" + PMD.EOL +
    73  		 "       parameter(TAILLE_COLONNE = 7)" + PMD.EOL +
    74  		 "       parameter(TAILLE_LIGNE = 12)" + PMD.EOL +
    75  		 "       parameter(LIGNES_DESC = 11)" + PMD.EOL +
    76  		 "" + PMD.EOL +
    77  		 "        character*19 N(TAILLE_COLONNE,TAILLE_LIGNE)" + PMD.EOL +
    78  		 "       character*19 MOTS_COMMANDE(NB_MOTS_MAX)" + PMD.EOL +
    79  		 "       character*60 DESC(LIGNES_DESC)" + PMD.EOL +
    80  		 "" + PMD.EOL +
    81  		 "       write(*,*) ' '" + PMD.EOL +
    82  		 "       write(*,*) '      -----------------------------------------------------'" + PMD.EOL +
    83  		 "       write(*,*) '      | Bonjour, et bienvenue dans le programme DASHBOARD |'" + PMD.EOL +
    84  		 "       write(*,*) '      -----------------------------------------------------'" + PMD.EOL +
    85  		 "       write(*,*) ' '" + PMD.EOL +
    86  		 "       write(*,*) ' '" + PMD.EOL +
    87  		 "       write(*,*) ' Voici un rappel des fonctions disponibles pour ce DASHBOARD : '" + PMD.EOL +
    88  		 "       write(*,*) ' '" + PMD.EOL +
    89  		 "       write(*,*) '   _ TASK pour creer une tache (ex : TASK IDTACHE CIBLE AUTEUR)'" + PMD.EOL +
    90  		 "       write(*,*) ' '" + PMD.EOL +
    91  		 "       write(*,*) '   _ SHOW pour voir la description (ex : SHOW IDTACHE)'" + PMD.EOL +
    92  		 "       write(*,*) ' '" + PMD.EOL +
    93  		 "       write(*,*) '   _ REMOVE pour enlever une tache (ex : REMOVE IDTACHE)'" + PMD.EOL +
    94  		 "       write(*,*) ' '" + PMD.EOL +
    95  		 "       write(*,*) '   _ CLEAR pour effacer le DASHBOARD (ex : CLEAR)'" + PMD.EOL +
    96  		 "       write(*,*) ' '" + PMD.EOL +
    97  		 "       write(*,*) '   _ CANCEL, DONE, TODO pour modifier lHEREetat de la tache (ex : DONE IDTACHE)'" + PMD.EOL +
    98  		 "       write(*,*) ' '" + PMD.EOL +
    99  		 "" + PMD.EOL +
    100 		 "! La boucle de sortie pour quitter si l'on appuie sur F10" + PMD.EOL +
    101 		 "       do while (IOS .ne. SORTIE)" + PMD.EOL +
    102 		 "" + PMD.EOL +
    103 		 "! Initialisons les variables, afin de ne pas garder les anciennes valeurs pour chaque variable." + PMD.EOL +
    104 		 "               POSITION_ESPACE = 0" + PMD.EOL +
    105 		 "               DEBUT_MOT = 0" + PMD.EOL +
    106 		 "               FIN_MOT = 0" + PMD.EOL +
    107 		 "               NB_MOTS = 0" + PMD.EOL +
    108 		 "               FIN_CHAINE = 0" + PMD.EOL +
    109 		 "" + PMD.EOL +
    110 		 "! Initialisons aussi le tableau des MOTS_COMMANDE" + PMD.EOL +
    111 		 "               do COMPTEUR = ACTION, NB_MOTS_MAX" + PMD.EOL +
    112 		 "                       MOTS_COMMANDE (COMPTEUR) = ' '" + PMD.EOL +
    113 		 "               end do" + PMD.EOL +
    114 		 "" + PMD.EOL +
    115 		 "! Appelons le sous prgramme qui gere la saisie de la commande et aussi la sortie, si " + PMD.EOL +
    116 		 "!l'utilisateur le demande" + PMD.EOL +
    117 		 "               call SAISIE(COMMANDE, IOS)" + PMD.EOL +
    118 		 "" + PMD.EOL +
    119 		 "               if (IOS .eq. 0) then" + PMD.EOL +
    120 		 "" + PMD.EOL +
    121 		 "! Trouvons la fin de la chaine" + PMD.EOL +
    122 		 "                       FIN_CHAINE = TROUVER_FIN (COMMANDE)" + PMD.EOL +
    123 		 "                       COMPTEUR = 1" + PMD.EOL +
    124 		 "                       do while (POSITION_ESPACE .lt. FIN_CHAINE .and. NB_MOTS .lt. NB_MOTS_MAX)" + PMD.EOL +
    125 		 "                               DEBUT_MOT = POSITION_ESPACE + 1" + PMD.EOL +
    126 		 "" + PMD.EOL +
    127 		 "! Decoupons les mots" + PMD.EOL +
    128 		 "                               POSITION_ESPACE = POSITION_ESPACE + index (COMMANDE (DEBUT_MOT:), ' ')" + PMD.EOL +
    129 		 "                               FIN_MOT = POSITION_ESPACE - 1" + PMD.EOL +
    130 		 "" + PMD.EOL +
    131 		 "! Ensuite on les enregistre dans MOTS_COMMANDE" + PMD.EOL +
    132 		 "                               MOTS_COMMANDE (COMPTEUR) = COMMANDE (DEBUT_MOT : FIN_MOT)" + PMD.EOL +
    133 		 "" + PMD.EOL +
    134 		 "! Comptons les mots" + PMD.EOL +
    135 		 "                               if (MOTS_COMMANDE (COMPTEUR) .ne. ' ') then" + PMD.EOL +
    136 		 "                                       NB_MOTS = NB_MOTS + 1" + PMD.EOL +
    137 		 "                                       COMPTEUR = COMPTEUR + 1" + PMD.EOL +
    138 		 "                               end if" + PMD.EOL +
    139 		 "                       end do" + PMD.EOL +
    140 		 "" + PMD.EOL +
    141 		 "! Le programme ne doit pas tenir compte de la casse, ainsi peu importe la maniere" + PMD.EOL +
    142 		 "!dont est ecrit le mot, il sera mis en majuscule" + PMD.EOL +
    143 		 "                       do COMPTEUR = 1, NB_MOTS" + PMD.EOL +
    144 		 "                               do NUM_CARACTERE = PREMIERE_LETTRE, DERNIERE_LETTRE" + PMD.EOL +
    145 		 "                                       if (ichar(MOTS_COMMANDE (COMPTEUR)(NUM_CARACTERE:NUM_CARACTERE))" + PMD.EOL +
    146 		 "       1 .gt. APRES_MAJ) then" + PMD.EOL +
    147 		 "                                               MOTS_COMMANDE (COMPTEUR)(NUM_CARACTERE:NUM_CARACTERE) =" + PMD.EOL +
    148 		 "       1 char(ichar(MOTS_COMMANDE (COMPTEUR)(NUM_CARACTERE:NUM_CARACTERE)) - INTERVALLE_MAJ_MIN)" + PMD.EOL +
    149 		 "                                       end if" + PMD.EOL +
    150 		 "                               end do" + PMD.EOL +
    151 		 "                       end do" + PMD.EOL +
    152 		 "" + PMD.EOL +
    153 		 "!! Affichons les mots (provisoire)" + PMD.EOL +
    154 		 "!!                     do COMPTEUR = 1, NB_MOTS" + PMD.EOL +
    155 		 "!!                             write(*,*) COMPTEUR, ': ', MOTS_COMMANDE (COMPTEUR)" + PMD.EOL +
    156 		 "!!                     end do" + PMD.EOL +
    157 		 "!!" + PMD.EOL +
    158 		 "!! Testons si le mot est bien en majuscule (etape provisoire)" + PMD.EOL +
    159 		 "!!                     write(*,*) MOTS_COMMANDE (ACTION), ': voila lHEREaction'" + PMD.EOL +
    160 		 "" + PMD.EOL +
    161 		 "" + PMD.EOL +
    162 		 "! Si la commande contient plus de 8 mots, on demande de recommencer" + PMD.EOL +
    163 		 "" + PMD.EOL +
    164 		 "                       if (NB_MOTS .eq. NB_MOTS_MAX) then" + PMD.EOL +
    165 		 "                               write(*,*) ' '" + PMD.EOL +
    166 		 "                               write(*,*) 'ERR> Trop de mot, veuillez ressaisir'" + PMD.EOL +
    167 		 "                       else" + PMD.EOL +
    168 		 "" + PMD.EOL +
    169 		 "! Maintenant, en fonction du premier mot entre, on va appeler le sous programme correspondant" + PMD.EOL +
    170 		 "                               if (MOTS_COMMANDE (ACTION) .eq. 'TASK') then" + PMD.EOL +
    171 		 "                                       call TACHE(MOTS_COMMANDE, DESC, N)" + PMD.EOL +
    172 		 "                               else if (MOTS_COMMANDE (ACTION) .eq. 'SHOW') then" + PMD.EOL +
    173 		 "!                                      write(*,*) 'on appelle le sous prgrm SHOW'" + PMD.EOL +
    174 		 "                                       call SHOW(MOTS_COMMANDE, N)" + PMD.EOL +
    175 		 "                               else if (MOTS_COMMANDE (ACTION) .eq. 'REMOVE') then" + PMD.EOL +
    176 		 "!                                      write(*,*) 'on appelle le sous prgrm REMOVE'" + PMD.EOL +
    177 		 "                                       call REMOVE(MOTS_COMMANDE, DESC, N)" + PMD.EOL +
    178 		 "                               else if (MOTS_COMMANDE (ACTION) .eq. 'CLEAR') then" + PMD.EOL +
    179 		 "!                                      write(*,*) 'on appelle le sous prgrm CLEAR'" + PMD.EOL +
    180 		 "                                       call CLEAR(MOTS_COMMANDE, N)" + PMD.EOL +
    181 		 "                               else if (MOTS_COMMANDE (ACTION) .eq. 'CANCEL') then" + PMD.EOL +
    182 		 "!                                      write(*,*) 'on appelle le sous prgrm CANCEL'" + PMD.EOL +
    183 		 "                                       call CANCEL(MOTS_COMMANDE, N)" + PMD.EOL +
    184 		 "                               else if (MOTS_COMMANDE (ACTION) .eq. 'DONE') then" + PMD.EOL +
    185 		 "!                                      write(*,*) 'on appelle le sous prgrm DONE'" + PMD.EOL +
    186 		 "                                       call DONE(MOTS_COMMANDE, N)" + PMD.EOL +
    187 		 "                               else if (MOTS_COMMANDE (ACTION) .eq. 'TODO') then" + PMD.EOL +
    188 		 "!                                      write(*,*) 'on appelle le sous prgrm TODO'" + PMD.EOL +
    189 		 "                                       call TODO(MOTS_COMMANDE, N)" + PMD.EOL +
    190 		 "                               else" + PMD.EOL +
    191 		 "                                       write(*,*) ' '" + PMD.EOL +
    192 		 "                                       write(*,*) 'L''action suivante n''a pas ete'," + PMD.EOL +
    193 		 "       1 ' comprise: ', MOTS_COMMANDE (ACTION)" + PMD.EOL +
    194 		 "                               end if" + PMD.EOL +
    195 		 "                       end if" + PMD.EOL +
    196 		 "               end if" + PMD.EOL +
    197 		 "       end do" + PMD.EOL +
    198 		 "       end" + PMD.EOL;
    199 	 }
    200
    201 	@Test
    202 	public void tokenizeTest() throws IOException {
    203 		this.expectedTokenCount = 434;
    204 		super.tokenizeTest();
    205 	}
    206
    207     public static junit.framework.Test suite() {
    208         return new junit.framework.JUnit4TestAdapter(FortranTokenizerTest.class);
    209     }
    210 }