Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c4930dffc5b8cfb082273a46b5fd95f545659fed (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*******************************************************************************
 * Copyright (c) 2016 Institute for Software, HSR Hochschule fuer Technik
 * Rapperswil, University of applied sciences.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *******************************************************************************/
package org.eclipse.cdt.ui.tests.text;

import java.util.ListResourceBundle;

import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.CCorePreferenceConstants;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.core.testplugin.CProjectHelper;
import org.eclipse.cdt.internal.ui.actions.AlignConstAction;
import org.eclipse.cdt.internal.ui.editor.CEditor;
import org.eclipse.cdt.ui.testplugin.EditorTestHelper;
import org.eclipse.cdt.ui.testplugin.ResourceTestHelper;
import org.eclipse.core.runtime.preferences.DefaultScope;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.source.SourceViewer;

import junit.extensions.TestSetup;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

/**
 * Test for the const alignment action.
 */
public class AlignConstActionTest extends TestCase {
	private static final String PROJECT = "AlignConstTests";

	private static final class EmptyBundle extends ListResourceBundle {
		@Override
		protected Object[][] getContents() {
			return new Object[0][];
		}
	}

	protected static class AlignConstTestSetup extends TestSetup {
		private ICProject fCProject;

		public AlignConstTestSetup(Test test) {
			super(test);
		}

		@Override
		protected void setUp() throws Exception {
			super.setUp();
			fCProject = EditorTestHelper.createCProject(PROJECT, "resources/constalign");
		}

		@Override
		protected void tearDown() throws Exception {
			if (fCProject != null)
				CProjectHelper.delete(fCProject);
			super.tearDown();
		}
	}

	public static Test suite() {
		return new AlignConstTestSetup(new TestSuite(AlignConstActionTest.class));
	}

	private CEditor fEditor;
	private SourceViewer fSourceViewer;
	private IDocument fDocument;

	@Override
	protected void setUp() throws Exception {
		IEclipsePreferences prefs = DefaultScope.INSTANCE.getNode(CCorePlugin.PLUGIN_ID);
		prefs.putBoolean(CCorePreferenceConstants.PLACE_CONST_RIGHT_OF_TYPE, getName().startsWith("testRight"));
		String filename = createFileName("Before");
		fEditor = (CEditor) EditorTestHelper.openInEditor(ResourceTestHelper.findFile(filename), true);
		fSourceViewer = EditorTestHelper.getSourceViewer(fEditor);
		fDocument = fSourceViewer.getDocument();
	}

	@Override
	protected void tearDown() throws Exception {
		EditorTestHelper.closeEditor(fEditor);
		IEclipsePreferences prefs = DefaultScope.INSTANCE.getNode(CCorePlugin.PLUGIN_ID);
		prefs.putBoolean(CCorePreferenceConstants.PLACE_CONST_RIGHT_OF_TYPE, false);
	}

	private void assertAlignConstResult() throws Exception {
		String afterFile = createFileName("After");
		String expected = ResourceTestHelper.read(afterFile).toString();

		new AlignConstAction(new EmptyBundle(), "prefix", fEditor).run();

		assertEquals(expected, fDocument.get());
	}

	private String createFileName(String qualifier) {
		String name = getName();
		name = name.substring(4, 5).toLowerCase() + name.substring(5);
		return "/" + PROJECT + "/src/" + name + "/" + qualifier + ".cpp";
	}

	private void selectAll() {
		fSourceViewer.setSelectedRange(0, fDocument.getLength());
	}

	public void testRightUnchanged() throws Exception {
		selectAll();
		assertAlignConstResult();
	}

	public void testRightChanged() throws Exception {
		selectAll();
		assertAlignConstResult();
	}

	public void testLeftUnchanged() throws Exception {
		selectAll();
		assertAlignConstResult();
	}

	public void testLeftChanged() throws Exception {
		selectAll();
		assertAlignConstResult();
	}
}

Back to the top