View Javadoc
1   /*
2    * Copyright (c) 2020 bahlef.
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the Eclipse Public License v2.0
5    * which accompanies this distribution, and is available at
6    * http://www.eclipse.org/legal/epl-v20.html
7    * Contributors:
8    * bahlef - initial API and implementation and/or initial documentation
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   * @author bahlef
26   */
27  public class EclipseJavascriptFormatterServiceTest extends NbTestCase {
28  	public EclipseJavascriptFormatterServiceTest(String name) {
29  		super(name);
30  	}
31  
32  	/**
33  	 * Test of format method, of class {@link EclipseJavascriptFormatterService}.
34  	 *
35  	 * @throws Exception if an error occurs
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  	 * Test of {@link EclipseJavascriptFormatterService#format(javax.swing.text.StyledDocument, java.util.SortedSet)}
84  	 * method, of class {@link EclipseJavascriptFormatterService}.
85  	 *
86  	 * @throws Exception if an error occurs
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 }