View Javadoc

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