Skip to content

Commit

Permalink
Added IndentWidthTest.
Browse files Browse the repository at this point in the history
  • Loading branch information
gbr committed Mar 25, 2023
1 parent 4b666a2 commit f23525d
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 9 deletions.
11 changes: 11 additions & 0 deletions net.certiv.tools.indentguide.plugin.test/.classpath
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src/test/java">
<attributes>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="src/test/resources">
<attributes>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/5"/>
<classpathentry kind="output" path="target/classes"/>
</classpath>
1 change: 1 addition & 0 deletions net.certiv.tools.indentguide.plugin.test/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/attic/
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package net.certiv.tools.indentguide.painter;

import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.LinkedHashMap;
import java.util.Map;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

class IndentWidthTest {

public final static boolean isWin = SWT.getPlatform().startsWith("win32");
public final static boolean isCocoa = SWT.getPlatform().startsWith("cocoa");
public final static boolean isGTK = SWT.getPlatform().equals("gtk");
public final static boolean isWinOS = System.getProperty("os.name").startsWith("Windows");
public final static boolean isLinux = System.getProperty("os.name").equals("Linux");

String[] fontnames = new String[] { //
"Consolas", "Courier New", "Menlo", //
"Fira Code", "Source Code Pro", "Liberation Mono" //
};

Display display;
Shell shell;
GC gc;

StyledText widget;

@BeforeEach
public void setUp() {
// display = new Display();
shell = new Shell(display);
gc = new GC(shell);

shell.setSize(200, 200);
shell.setLayout(new FillLayout());

widget = new StyledText(shell, SWT.BORDER);
widget.setText("This is some dummy text. Sufficient text to have the scroll bars appear.");
}

@AfterEach
public void tearDown() {
if (gc != null) gc.dispose();
if (shell != null) shell.dispose();
}

@Test
void test_stringExtent() {
for (int size = 7; size < 15; size++) {
for (String name : fontnames) {
FontData fd = new FontData(name, size, SWT.NORMAL);
Font font = new Font(widget.getDisplay(), fd);
widget.setFont(font);
gc.setFont(font);

Map<Integer, Integer> widths0 = new LinkedHashMap<>();
Map<Integer, Integer> widths1 = new LinkedHashMap<>();
for (int col = 1; col <= 10; col++) {
Point p = gc.stringExtent(" ");
widths0.put(col, p.x * col);

p = gc.stringExtent(" ".repeat(col));
widths1.put(col, p.x);
}
System.out.printf("Font %-20s: size [%02d] multiply widths %s\n", name, size, widths0);
System.out.printf("Font %-20s: size [%02d] *repeat* widths %s\n", name, size, widths1);

gc.setFont(null);
widget.setFont(null);
font.dispose();

assertTrue(widths0.equals(widths1), String.format("Font %s did not match.", name));
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package net.certiv.tools.indentguide.util;
package net.certiv.tools.indentguide.painter;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvFileSource;

import net.certiv.tools.indentguide.painter.Line;
import net.certiv.tools.indentguide.util.Utils;

class LineTest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public GuidePage() {
Set<String> exclude = Utils.undelimit(getPreferenceStore().getString(Pref.CONTENT_TYPES));
excludeTypes = exclude.stream() //
.map(e -> Utils.getPlatformTextType(e)) //
.filter(t -> !t.equals(txtType)) //
.filter(t -> t != null && !txtType.equals(t)) //
.collect(Collectors.toCollection(LinkedHashSet::new));
}

Expand Down Expand Up @@ -131,7 +131,8 @@ private void createContentTypesGroup(Composite parent) {
Composite comp = createGroup(parent, Messages.contenttype_group_label, true, 1);
blocks.add(comp);

CheckboxTreeViewer viewer = new CheckboxTreeViewer(comp, SWT.SINGLE | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
CheckboxTreeViewer viewer = new CheckboxTreeViewer(comp,
SWT.SINGLE | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
viewer.getControl().setFont(comp.getFont());
GridDataFactory.fillDefaults().hint(SWT.DEFAULT, 150).grab(true, true).applyTo(viewer.getControl());
parts.add(viewer);
Expand Down Expand Up @@ -186,7 +187,8 @@ private void createVerticalSpacer(Composite comp, int lines) {
private void createVerticalSpacer(Composite comp, int lines, int span) {
Label spacer = new Label(comp, SWT.NONE);
int height = Utils.lineHeight(comp, lines);
GridDataFactory.fillDefaults().hint(SWT.DEFAULT, height).grab(true, false).span(span, 1).applyTo(spacer);
GridDataFactory.fillDefaults().hint(SWT.DEFAULT, height).grab(true, false).span(span, 1)
.applyTo(spacer);
}

private Label createLabel(Composite comp, String text) {
Expand All @@ -207,7 +209,8 @@ private Button createLabeledCheckbox(Composite comp, String label, String key) {
return btn;
}

private Combo createLabeledCombo(Composite comp, String label, String trail, String[] styles, String key) {
private Combo createLabeledCombo(Composite comp, String label, String trail, String[] styles,
String key) {
createLabel(comp, label);
Combo combo = new Combo(comp, SWT.READ_ONLY);
combo.setData(key);
Expand All @@ -224,7 +227,8 @@ private Combo createLabeledCombo(Composite comp, String label, String trail, Str
return combo;
}

private Spinner createLabeledSpinner(Composite comp, String label, String trail, int min, int max, String key) {
private Spinner createLabeledSpinner(Composite comp, String label, String trail, int min, int max,
String key) {
createLabel(comp, label);

Spinner spin = new Spinner(comp, SWT.BORDER);
Expand Down Expand Up @@ -332,7 +336,8 @@ public boolean performOk() {
}

/**
* Returns the types of the checked, and optionally not grayed, elements in the tree viewer.
* Returns the types of the checked, and optionally not grayed, elements in the tree
* viewer.
*
* @param viewer the tree viewer
* @param grayed include grayed if {@code true}; exclude grayed if {@code false}
Expand All @@ -343,7 +348,8 @@ private Set<IContentType> getChecked(CheckboxTreeViewer viewer, boolean grayed)
.collect(Collectors.toCollection(LinkedHashSet::new));

if (grayed) return checked;
return checked.stream().filter(t -> !viewer.getGrayed(t)).collect(Collectors.toCollection(LinkedHashSet::new));
return checked.stream().filter(t -> !viewer.getGrayed(t))
.collect(Collectors.toCollection(LinkedHashSet::new));
}

/** Returns the types of the unchecked tree viewer items */
Expand Down

0 comments on commit f23525d

Please sign in to comment.