View Javadoc

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