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 static org.junit.Assert.assertEquals;
7   import net.sourceforge.pmd.PMD;
8   
9   import org.junit.Test;
10  
11  public class CPPTokenizerTest {
12  
13      @Test
14      public void testMultiLineMacros() throws Throwable {
15          Tokens tokens = parse(TEST1);
16          assertEquals(7, tokens.size());
17      }
18  
19      @Test
20      public void testDollarSignInIdentifier() {
21          parse(TEST2);
22      }
23  
24      @Test
25      public void testDollarSignStartingIdentifier() {
26          parse(TEST3);
27      }
28  
29      @Test
30      public void testWideCharacters() {
31          parse(TEST4);
32      }
33  
34      @Test
35      public void testContinuation_IntraToken() {
36      	Tokens tokens = parse(TEST5);
37          assertEquals(7, tokens.size());
38      }
39      
40      @Test
41      public void testContinuation_InterToken() {
42      	Tokens tokens = parse(TEST6);
43      	assertEquals(17, tokens.size());
44      }
45  
46      private Tokens parse(String snippet) {
47          CPPTokenizer tokenizer = new CPPTokenizer();
48          SourceCode code = new SourceCode(new SourceCode.StringCodeLoader(snippet));
49          Tokens tokens = new Tokens();
50          tokenizer.tokenize(code, tokens);
51          return tokens;
52      }
53  
54      private static final String TEST1 =
55              "#define FOO a +\\" + PMD.EOL +
56              "            b +\\" + PMD.EOL +
57              "            c +\\" + PMD.EOL +
58              "            d +\\" + PMD.EOL +
59              "            e +\\" + PMD.EOL +
60              "            f +\\" + PMD.EOL +
61              "            g" + PMD.EOL +
62              " void main() {}";
63  
64      private static final String TEST2 =
65              " void main() { int x$y = 42; }";
66  
67      private static final String TEST3 =
68              " void main() { int $x = 42; }";
69  
70      private static final String TEST4 =
71              " void main() { char x = L'a'; }";
72      
73      private static final String TEST5 =
74              "v\\" + PMD.EOL +
75              "o\\" + PMD.EOL +
76              "i\\" + PMD.EOL +
77              "d\\" + PMD.EOL +
78              " \\" + PMD.EOL +
79              "m\\" + PMD.EOL +
80              "a\\" + PMD.EOL +
81              "i\\" + PMD.EOL +
82              "n\\" + PMD.EOL +
83              "(\\" + PMD.EOL +
84              ")\\" + PMD.EOL +
85              " \\" + PMD.EOL +
86              "{\\" + PMD.EOL +
87              " \\" + PMD.EOL +
88              "}\\" + PMD.EOL;
89      
90      private static final String TEST6 =
91              "#include <iostream>" + PMD.EOL +
92              PMD.EOL +
93              "int main()" + PMD.EOL +
94              "{" + PMD.EOL +
95              "   std::cout << \"Hello, \" \\" + PMD.EOL +
96              "                \"world!\\n\";" + PMD.EOL +
97              "   return 0;" + PMD.EOL +
98              "}";
99  
100     public static junit.framework.Test suite() {
101         return new junit.framework.JUnit4TestAdapter(CPPTokenizerTest.class);
102     }
103 }