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