1
2
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
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 EcmascriptParserOptions options = new EcmascriptParserOptions();
58
59 options.setSuppressMarker("foo");
60 assertEquals("foo", options.getSuppressMarker());
61 options.setSuppressMarker(null);
62 assertNull(options.getSuppressMarker());
63 }
64
65 @Test
66 public void testEqualsHashcode() throws Exception {
67 BooleanProperty[] properties = new BooleanProperty[] { EcmascriptParserOptions.RECORDING_COMMENTS_DESCRIPTOR,
68 EcmascriptParserOptions.RECORDING_LOCAL_JSDOC_COMMENTS_DESCRIPTOR };
69
70 for (int i = 0; i < properties.length; i++) {
71 BooleanProperty property = properties[i];
72
73 MyRule rule = new MyRule();
74 rule.setProperty(property, true);
75 ParserOptions options1 = rule.getParserOptions();
76 rule.setProperty(property, false);
77 ParserOptions options2 = rule.getParserOptions();
78 rule.setProperty(property, true);
79 ParserOptions options3 = rule.getParserOptions();
80 rule.setProperty(property, false);
81 ParserOptions options4 = rule.getParserOptions();
82 verifyOptionsEqualsHashcode(options1, options2, options3, options4);
83 }
84
85 EcmascriptParserOptions options1 = new EcmascriptParserOptions();
86 options1.setSuppressMarker("foo");
87 EcmascriptParserOptions options2 = new EcmascriptParserOptions();
88 options2.setSuppressMarker("bar");
89 EcmascriptParserOptions options3 = new EcmascriptParserOptions();
90 options3.setSuppressMarker("foo");
91 EcmascriptParserOptions options4 = new EcmascriptParserOptions();
92 options4.setSuppressMarker("bar");
93 verifyOptionsEqualsHashcode(options1, options2, options3, options4);
94
95 options1 = new EcmascriptParserOptions();
96 options1.setRhinoLanguageVersion(EcmascriptParserOptions.Version.VERSION_DEFAULT);
97 options2 = new EcmascriptParserOptions();
98 options2.setRhinoLanguageVersion(EcmascriptParserOptions.Version.VERSION_1_8);
99 options3 = new EcmascriptParserOptions();
100 options3.setRhinoLanguageVersion(EcmascriptParserOptions.Version.VERSION_DEFAULT);
101 options4 = new EcmascriptParserOptions();
102 options4.setRhinoLanguageVersion(EcmascriptParserOptions.Version.VERSION_1_8);
103 verifyOptionsEqualsHashcode(options1, options2, options3, options4);
104 }
105
106 private static final class MyRule extends AbstractEcmascriptRule {
107 }
108
109 public static junit.framework.Test suite() {
110 return new junit.framework.JUnit4TestAdapter(EcmascriptParserOptionsTest.class);
111 }
112 }