View Javadoc

1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd.lang.ecmascript;
5   
6   import static net.sourceforge.pmd.lang.ParserOptionsTest.verifyOptionsEqualsHashcode;
7   import static org.junit.Assert.assertEquals;
8   import static org.junit.Assert.assertFalse;
9   import static org.junit.Assert.assertNull;
10  import static org.junit.Assert.assertTrue;
11  import net.sourceforge.pmd.lang.ParserOptions;
12  import net.sourceforge.pmd.lang.ecmascript.rule.AbstractEcmascriptRule;
13  import net.sourceforge.pmd.lang.rule.properties.BooleanProperty;
14  import net.sourceforge.pmd.lang.xml.XmlParserOptions;
15  
16  import org.junit.Test;
17  
18  public class EcmascriptParserOptionsTest {
19  
20      @Test
21      public void testDefaults() throws Exception {
22  	EcmascriptParserOptions parserOptions = new EcmascriptParserOptions();
23  	assertTrue(parserOptions.isRecordingComments());
24  	assertTrue(parserOptions.isRecordingLocalJsDocComments());
25  	assertEquals(EcmascriptParserOptions.Version.VERSION_DEFAULT, parserOptions.getRhinoLanguageVersion());
26  
27  	MyRule rule = new MyRule();
28  	parserOptions = (EcmascriptParserOptions) rule.getParserOptions();
29  	assertTrue(parserOptions.isRecordingComments());
30  	assertTrue(parserOptions.isRecordingLocalJsDocComments());
31  	assertEquals(EcmascriptParserOptions.Version.VERSION_DEFAULT, parserOptions.getRhinoLanguageVersion());
32      }
33  
34      @Test
35      public void testConstructor() throws Exception {
36  	MyRule rule = new MyRule();
37  
38  	rule.setProperty(EcmascriptParserOptions.RECORDING_COMMENTS_DESCRIPTOR, true);
39  	assertTrue(((EcmascriptParserOptions) rule.getParserOptions()).isRecordingComments());
40  	rule.setProperty(EcmascriptParserOptions.RECORDING_COMMENTS_DESCRIPTOR, false);
41  	assertFalse(((EcmascriptParserOptions) rule.getParserOptions()).isRecordingComments());
42  
43  	rule.setProperty(EcmascriptParserOptions.RECORDING_LOCAL_JSDOC_COMMENTS_DESCRIPTOR, true);
44  	assertTrue(((EcmascriptParserOptions) rule.getParserOptions()).isRecordingLocalJsDocComments());
45  	rule.setProperty(EcmascriptParserOptions.RECORDING_LOCAL_JSDOC_COMMENTS_DESCRIPTOR, false);
46  	assertFalse(((EcmascriptParserOptions) rule.getParserOptions()).isRecordingLocalJsDocComments());
47  
48  	rule.setProperty(EcmascriptParserOptions.RHINO_LANGUAGE_VERSION, "default");
49  	assertEquals(EcmascriptParserOptions.Version.VERSION_DEFAULT, ((EcmascriptParserOptions) rule
50  		.getParserOptions()).getRhinoLanguageVersion());
51  	rule.setProperty(EcmascriptParserOptions.RHINO_LANGUAGE_VERSION, "1.8");
52  	assertEquals(EcmascriptParserOptions.Version.VERSION_1_8, ((EcmascriptParserOptions) rule.getParserOptions())
53  		.getRhinoLanguageVersion());
54      }
55  
56      @Test
57      public void testSetters() {
58  	XmlParserOptions options = new XmlParserOptions();
59  
60  	options.setSuppressMarker("foo");
61  	assertEquals("foo", options.getSuppressMarker());
62  	options.setSuppressMarker(null);
63  	assertNull(options.getSuppressMarker());
64  
65  	options.setCoalescing(true);
66  	assertTrue(options.isCoalescing());
67  	options.setCoalescing(false);
68  	assertFalse(options.isCoalescing());
69  
70  	options.setExpandEntityReferences(true);
71  	assertTrue(options.isExpandEntityReferences());
72  	options.setExpandEntityReferences(false);
73  	assertFalse(options.isExpandEntityReferences());
74  
75  	options.setIgnoringComments(true);
76  	assertTrue(options.isIgnoringComments());
77  	options.setIgnoringComments(false);
78  	assertFalse(options.isIgnoringComments());
79      }
80      @Test
81      public void testEqualsHashcode() throws Exception {
82  	BooleanProperty[] properties = new BooleanProperty[] { EcmascriptParserOptions.RECORDING_COMMENTS_DESCRIPTOR,
83  		EcmascriptParserOptions.RECORDING_LOCAL_JSDOC_COMMENTS_DESCRIPTOR };
84  
85  	for (int i = 0; i < properties.length; i++) {
86  	    BooleanProperty property = properties[i];
87  
88  	    MyRule rule = new MyRule();
89  	    rule.setProperty(property, true);
90  	    ParserOptions options1 = rule.getParserOptions();
91  	    rule.setProperty(property, false);
92  	    ParserOptions options2 = rule.getParserOptions();
93  	    rule.setProperty(property, true);
94  	    ParserOptions options3 = rule.getParserOptions();
95  	    rule.setProperty(property, false);
96  	    ParserOptions options4 = rule.getParserOptions();
97  	    verifyOptionsEqualsHashcode(options1, options2, options3, options4);
98  	}
99  
100 	EcmascriptParserOptions options1 = new EcmascriptParserOptions();
101 	options1.setSuppressMarker("foo");
102 	EcmascriptParserOptions options2 = new EcmascriptParserOptions();
103 	options2.setSuppressMarker("bar");
104 	EcmascriptParserOptions options3 = new EcmascriptParserOptions();
105 	options3.setSuppressMarker("foo");
106 	EcmascriptParserOptions options4 = new EcmascriptParserOptions();
107 	options4.setSuppressMarker("bar");
108 	verifyOptionsEqualsHashcode(options1, options2, options3, options4);
109 
110 	options1 = new EcmascriptParserOptions();
111 	options1.setRhinoLanguageVersion(EcmascriptParserOptions.Version.VERSION_DEFAULT);
112 	options2 = new EcmascriptParserOptions();
113 	options2.setRhinoLanguageVersion(EcmascriptParserOptions.Version.VERSION_1_8);
114 	options3 = new EcmascriptParserOptions();
115 	options3.setRhinoLanguageVersion(EcmascriptParserOptions.Version.VERSION_DEFAULT);
116 	options4 = new EcmascriptParserOptions();
117 	options4.setRhinoLanguageVersion(EcmascriptParserOptions.Version.VERSION_1_8);
118 	verifyOptionsEqualsHashcode(options1, options2, options3, options4);
119     }
120 
121     private static final class MyRule extends AbstractEcmascriptRule {
122     }
123 
124     public static junit.framework.Test suite() {
125 	return new junit.framework.JUnit4TestAdapter(EcmascriptParserOptionsTest.class);
126     }
127 }