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 EcmascriptTokenizerTest {
12  
13      @Test
14      public void test1() throws Throwable {
15          Tokenizer tokenizer = new EcmascriptTokenizer();
16          SourceCode sourceCode = new SourceCode( new SourceCode.StringCodeLoader( getCode1() ) );
17          Tokens tokens = new Tokens();
18          tokenizer.tokenize( sourceCode, tokens );
19          assertEquals( 22, tokens.size() );
20      }
21  
22      @Test
23      public void test2() throws Throwable {
24          Tokenizer t = new EcmascriptTokenizer();
25          SourceCode sourceCode = new SourceCode( new SourceCode.StringCodeLoader( getCode2() ) );
26          Tokens tokens = new Tokens();
27          t.tokenize( sourceCode, tokens );
28          assertEquals( 22, tokens.size() );
29      }
30  
31      // no semi-colons
32      private String getCode1() {
33          StringBuilder sb = new StringBuilder();
34          sb.append( "function switchToRealPassword() {" ).append(PMD.EOL);
35          sb.append( "   var real = $('realPass')" ).append(PMD.EOL);
36          sb.append( "   var prompt = $('promptPass')" ).append(PMD.EOL);
37          sb.append( "   real.style.display = 'inline'" ).append(PMD.EOL);
38          sb.append( "   prompt.style.display = 'none'" ).append(PMD.EOL);
39          sb.append( "   real.focus()" ).append(PMD.EOL);
40          sb.append( "}" ).append(PMD.EOL);
41          return sb.toString();
42      }
43  
44      // same as getCode1, but lines are ended with semi-colons
45      private String getCode2() {
46          StringBuilder sb = new StringBuilder();
47          sb.append( "function switchToRealPassword() {" ).append(PMD.EOL);
48          sb.append( "   var real = $('realPass');" ).append(PMD.EOL);
49          sb.append( "   var prompt = $('promptPass');" ).append(PMD.EOL);
50          sb.append( "   real.style.display = 'inline';" ).append(PMD.EOL);
51          sb.append( "   prompt.style.display = 'none';" ).append(PMD.EOL);
52          sb.append( "   real.focus();" ).append(PMD.EOL);
53          sb.append( "}" ).append(PMD.EOL);
54          return sb.toString();
55      }
56  
57      public static junit.framework.Test suite() {
58          return new junit.framework.JUnit4TestAdapter( EcmascriptTokenizerTest.class );
59      }
60  }