1
2
3
4
5
6
7
8
9
10 package de.funfried.netbeans.plugins.external.formatter.javascript.eclipse;
11
12 import java.util.prefs.Preferences;
13
14 import javax.swing.text.StyledDocument;
15
16 import org.junit.Assert;
17 import org.junit.Test;
18 import org.netbeans.junit.NbTestCase;
19 import org.netbeans.modules.editor.NbEditorDocument;
20
21 import de.funfried.netbeans.plugins.external.formatter.ui.options.Settings;
22
23
24
25
26
27 public class EclipseJavascriptFormatterServiceTest extends NbTestCase {
28 public EclipseJavascriptFormatterServiceTest(String name) {
29 super(name);
30 }
31
32
33
34
35
36
37 @Test
38 public void testFormat() throws Exception {
39 final String text = "function foo(bar) { return Str('', { '' : bar }, []); }\n";
40 final String expected = "function foo(bar) {\n"
41 + " return Str('', {\n"
42 + " '' : bar\n"
43 + " }, []);\n"
44 + "}\n";
45
46 StyledDocument document = new NbEditorDocument("text/javascript");
47 document.insertString(0, text, null);
48
49 Preferences prefs = Settings.getActivePreferences(document);
50 prefs.putBoolean(Settings.ENABLE_USE_OF_INDENTATION_SETTINGS, true);
51 prefs.putBoolean(Settings.OVERRIDE_TAB_SIZE, false);
52
53 EclipseJavascriptFormatterService instance = new EclipseJavascriptFormatterService();
54 Assert.assertEquals("Eclipse Javascript Code Formatter", instance.getDisplayName());
55 Assert.assertNotNull(instance.createOptionsPanel(null));
56 Assert.assertEquals((long) 80L, (long) instance.getRightMargin(document));
57
58 Assert.assertEquals((long) 8L, (long) instance.getContinuationIndentSize(document));
59 Assert.assertEquals((long) 4L, (long) instance.getIndentSize(document));
60 Assert.assertEquals((long) 4L, (long) instance.getSpacesPerTab(document));
61 Assert.assertTrue(instance.isExpandTabToSpaces(document));
62
63 Assert.assertNull(instance.getContinuationIndentSize(null));
64 Assert.assertNull(instance.getIndentSize(null));
65 Assert.assertNull(instance.getSpacesPerTab(null));
66 Assert.assertNull(instance.isExpandTabToSpaces(null));
67
68 prefs.putBoolean(Settings.ENABLE_USE_OF_INDENTATION_SETTINGS, false);
69
70 Assert.assertNull(instance.getContinuationIndentSize(document));
71 Assert.assertNull(instance.getIndentSize(document));
72 Assert.assertNull(instance.getSpacesPerTab(document));
73 Assert.assertNull(instance.isExpandTabToSpaces(document));
74
75 instance.format(document, null);
76
77 String actual = document.getText(0, document.getLength());
78
79 Assert.assertEquals("Formatting should change the code", expected, actual);
80 }
81
82
83
84
85
86
87
88 @Test
89 public void testUnsupportedFileType() throws Exception {
90 final String text = "function foo(bar) { return Str('', { '' : bar }, []); }\n";
91
92 StyledDocument document = new NbEditorDocument("text/xml");
93 document.insertString(0, text, null);
94
95 EclipseJavascriptFormatterService instance = new EclipseJavascriptFormatterService();
96
97 try {
98 instance.format(document, null);
99
100 Assert.assertFalse("Formatting should not be possible for the given file type!", true);
101 } catch (Exception ex) {
102 Assert.assertTrue("Formatting should not be possible for the given file type", ex.getMessage().contains("The file type 'text/xml' is not supported"));
103 }
104 }
105 }