Skip to content

Commit

Permalink
test: use assertj
Browse files Browse the repository at this point in the history
  • Loading branch information
sebthom committed Jan 20, 2025
1 parent fef6ccb commit 17f46fd
Show file tree
Hide file tree
Showing 43 changed files with 659 additions and 627 deletions.
3 changes: 2 additions & 1 deletion org.eclipse.tm4e.core.tests/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ Require-Bundle: org.apache.batik.css;resolution:=optional,
org.apache.batik.util;resolution:=optional,
com.google.gson;resolution:=optional,
org.eclipse.core.runtime,
org.eclipse.tm4e.core
org.eclipse.tm4e.core,
assertj-core
Import-Package: org.junit.jupiter.api,
org.junit.jupiter.api.function,
org.w3c.css.sac;resolution:=optional,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
package org.eclipse.tm4e.core.grammar.internal;

import static java.util.stream.Collectors.*;
import static org.junit.jupiter.api.Assertions.*;
import static org.assertj.core.api.Assertions.*;

import java.io.File;
import java.util.Arrays;
Expand Down Expand Up @@ -136,16 +136,15 @@ private static void deepEqual(final List<RawToken> actualTokens, final List<RawT
final var actualTokensStr = actualTokens.stream().map(Object::toString).collect(joining("\n"));
final var expextedTokensStr = expextedTokens.stream().map(Object::toString).collect(joining("\n"));

assertEquals(expextedTokensStr, actualTokensStr,
message + " (collection size problem: actual=" + actualTokens.size() + " expected=" + expextedTokens.size() + ")");
assertThat(actualTokensStr).as(message + " (collection size)").isEqualTo(expextedTokensStr);
}

// compare item
for (int i = 0; i < expextedTokens.size(); i++) {
final var expected = expextedTokens.get(i);
final var actual = actualTokens.get(i);
assertEquals(expected.getValue(), actual.getValue(), message + " (value of item '" + i + "' problem)");
assertEquals(expected.getScopes(), actual.getScopes(), message + " (tokens of item '" + i + "' problem)");
assertThat(actual.getValue()).as(message + " (value of item '" + i + "' problem)").isEqualTo(expected.getValue());
assertThat(actual.getScopes()).as(message + " (scopes of item '" + i + "' problem)").isEqualTo(expected.getScopes());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*/
package org.eclipse.tm4e.core.internal.matcher;

import static org.junit.jupiter.api.Assertions.*;
import static org.assertj.core.api.Assertions.*;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -41,8 +41,7 @@ static final class MatcherTestImpl implements Executable {
@Override
public void execute() {
final var matcher = Matcher.createMatchers(expression);
final boolean result = matcher.stream().anyMatch(m -> m.matcher.matches(input));
assertEquals(result, this.result);
assertThat(matcher.stream().anyMatch(m -> m.matcher.matches(input))).isEqualTo(this.result);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@

import org.eclipse.tm4e.core.internal.theme.raw.RawThemeReader;
import org.eclipse.tm4e.core.registry.IThemeSource;
import org.junit.jupiter.api.Assertions;

public abstract class AbstractThemeTest extends Assertions {
public abstract class AbstractThemeTest extends org.assertj.core.api.Assertions {

protected static final int _NOT_SET = 0;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ void testGivesHigherPriorityToDeeperMatches() throws Exception {
]}""");

final var actual = theme.match(ScopeStack.from("punctuation.definition.string.begin.html"));
assertEquals(theme.getColorMap().get(actual.foregroundId), "#300000");
assertThat(theme.getColorMap().get(actual.foregroundId)).isEqualTo("#300000");
}

@Test
Expand All @@ -58,7 +58,7 @@ void testGivesHigherPriorityToParentMatches1() throws Exception {

final var map = theme.getColorMap();

assertEquals(map.get(theme.match(ScopeStack.from("d", "a.b")).foregroundId), "#400000");
assertThat(map.get(theme.match(ScopeStack.from("d", "a.b")).foregroundId)).isEqualTo("#400000");
}

@Test
Expand All @@ -80,7 +80,7 @@ void testGivesHigherPriorityToParentMatches2() throws Exception {
"entity.name.tag.structure.any.html"));

final var colorMap = theme.getColorMap();
assertEquals(colorMap.get(result.foregroundId), "#300000");
assertThat(colorMap.get(result.foregroundId)).isEqualTo("#300000");
}

private Map<String, String> match(final Theme theme, final String... path) {
Expand Down Expand Up @@ -118,41 +118,41 @@ void testCanMatch() throws Exception {
]}""");

// simpleMatch1..25
assertEquals(match(theme, "source"), map("background", "#100000", "fontStyle", "not set"));
assertEquals(match(theme, "source"), map("background", "#100000", "fontStyle", "not set"));
assertEquals(match(theme, "source.ts"), map("background", "#100000", "fontStyle", "not set"));
assertEquals(match(theme, "source.tss"), map("background", "#100000", "fontStyle", "not set"));
assertEquals(match(theme, "something"), map("background", "#100000", "fontStyle", "not set"));
assertEquals(match(theme, "something.ts"), map("background", "#100000", "fontStyle", "not set"));
assertEquals(match(theme, "something.tss"), map("background", "#100000", "fontStyle", "not set"));
assertEquals(match(theme, "baz"), map("background", "#200000", "fontStyle", "not set"));
assertEquals(match(theme, "baz.ts"), map("background", "#200000", "fontStyle", "not set"));
assertEquals(match(theme, "baz.tss"), map("background", "#200000", "fontStyle", "not set"));
assertEquals(match(theme, "constant"), map("foreground", "#300000", "fontStyle", "italic"));
assertEquals(match(theme, "constant.string"), map("foreground", "#300000", "fontStyle", "italic"));
assertEquals(match(theme, "constant.hex"), map("foreground", "#300000", "fontStyle", "italic"));
assertEquals(match(theme, "constant.numeric"), map("foreground", "#400000", "fontStyle", "italic"));
assertEquals(match(theme, "constant.numeric.baz"), map("foreground", "#400000", "fontStyle", "italic"));
assertEquals(match(theme, "constant.numeric.hex"), map("foreground", "#400000", "fontStyle", "bold"));
assertEquals(match(theme, "constant.numeric.hex.baz"), map("foreground", "#400000", "fontStyle", "bold"));
assertEquals(match(theme, "constant.numeric.oct"), map("foreground", "#400000", "fontStyle", "italic bold underline"));
assertEquals(match(theme, "constant.numeric.oct.baz"), map("foreground", "#400000", "fontStyle", "italic bold underline"));
assertEquals(match(theme, "constant.numeric.dec"), map("foreground", "#500000", "fontStyle", "none"));
assertEquals(match(theme, "constant.numeric.dec.baz"), map("foreground", "#500000", "fontStyle", "none"));
assertEquals(match(theme, "storage.object.bar"), map("foreground", "#600000", "fontStyle", "none"));
assertEquals(match(theme, "storage.object.bar.baz"), map("foreground", "#600000", "fontStyle", "none"));
assertEquals(match(theme, "storage.object.bart"), map("fontStyle", "not set"));
assertEquals(match(theme, "storage.object"), map("fontStyle", "not set"));
assertEquals(match(theme, "storage"), map("fontStyle", "not set"));
assertThat(match(theme, "source")).isEqualTo(map("background", "#100000", "fontStyle", "not set"));
assertThat(match(theme, "source")).isEqualTo(map("background", "#100000", "fontStyle", "not set"));
assertThat(match(theme, "source.ts")).isEqualTo(map("background", "#100000", "fontStyle", "not set"));
assertThat(match(theme, "source.tss")).isEqualTo(map("background", "#100000", "fontStyle", "not set"));
assertThat(match(theme, "something")).isEqualTo(map("background", "#100000", "fontStyle", "not set"));
assertThat(match(theme, "something.ts")).isEqualTo(map("background", "#100000", "fontStyle", "not set"));
assertThat(match(theme, "something.tss")).isEqualTo(map("background", "#100000", "fontStyle", "not set"));
assertThat(match(theme, "baz")).isEqualTo(map("background", "#200000", "fontStyle", "not set"));
assertThat(match(theme, "baz.ts")).isEqualTo(map("background", "#200000", "fontStyle", "not set"));
assertThat(match(theme, "baz.tss")).isEqualTo(map("background", "#200000", "fontStyle", "not set"));
assertThat(match(theme, "constant")).isEqualTo(map("foreground", "#300000", "fontStyle", "italic"));
assertThat(match(theme, "constant.string")).isEqualTo(map("foreground", "#300000", "fontStyle", "italic"));
assertThat(match(theme, "constant.hex")).isEqualTo(map("foreground", "#300000", "fontStyle", "italic"));
assertThat(match(theme, "constant.numeric")).isEqualTo(map("foreground", "#400000", "fontStyle", "italic"));
assertThat(match(theme, "constant.numeric.baz")).isEqualTo(map("foreground", "#400000", "fontStyle", "italic"));
assertThat(match(theme, "constant.numeric.hex")).isEqualTo(map("foreground", "#400000", "fontStyle", "bold"));
assertThat(match(theme, "constant.numeric.hex.baz")).isEqualTo(map("foreground", "#400000", "fontStyle", "bold"));
assertThat(match(theme, "constant.numeric.oct")).isEqualTo(map("foreground", "#400000", "fontStyle", "italic bold underline"));
assertThat(match(theme, "constant.numeric.oct.baz")).isEqualTo(map("foreground", "#400000", "fontStyle", "italic bold underline"));
assertThat(match(theme, "constant.numeric.dec")).isEqualTo(map("foreground", "#500000", "fontStyle", "none"));
assertThat(match(theme, "constant.numeric.dec.baz")).isEqualTo(map("foreground", "#500000", "fontStyle", "none"));
assertThat(match(theme, "storage.object.bar")).isEqualTo(map("foreground", "#600000", "fontStyle", "none"));
assertThat(match(theme, "storage.object.bar.baz")).isEqualTo(map("foreground", "#600000", "fontStyle", "none"));
assertThat(match(theme, "storage.object.bart")).isEqualTo(map("fontStyle", "not set"));
assertThat(match(theme, "storage.object")).isEqualTo(map("fontStyle", "not set"));
assertThat(match(theme, "storage")).isEqualTo(map("fontStyle", "not set"));

// defaultMatch1..3
assertEquals(match(theme, ""), map("fontStyle", "not set"));
assertEquals(match(theme, "bazz"), map("fontStyle", "not set"));
assertEquals(match(theme, "asdfg"), map("fontStyle", "not set"));
assertThat(match(theme, "")).isEqualTo(map("fontStyle", "not set"));
assertThat(match(theme, "bazz")).isEqualTo(map("fontStyle", "not set"));
assertThat(match(theme, "asdfg")).isEqualTo(map("fontStyle", "not set"));

// multiMatch1..2
assertEquals(match(theme, "bar"), map("background", "#200000", "fontStyle", "not set"));
assertEquals(match(theme, "source.css", "selector", "bar"), map("background", "#200000", "fontStyle", "bold"));
assertThat(match(theme, "bar")).isEqualTo(map("background", "#200000", "fontStyle", "not set"));
assertThat(match(theme, "source.css", "selector", "bar")).isEqualTo(map("background", "#200000", "fontStyle", "bold"));
}

@Test
Expand Down Expand Up @@ -191,6 +191,6 @@ void testMicrosoft_vscode_23460() throws Exception {
"meta.structure.dictionary.value.json",
"string.quoted.double.json");
final var result = theme.match(path);
assertEquals(theme.getColorMap().get(result.foregroundId), "#FF410D");
assertThat(theme.getColorMap().get(result.foregroundId)).isEqualTo("#FF410D");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import static org.eclipse.tm4e.core.internal.theme.FontStyle.*;

import java.util.List;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
Expand All @@ -36,7 +38,7 @@ public class ThemeParsingTest extends AbstractThemeTest {
@Order(1)
@DisplayName("Theme parsing can parse")
public void testCanParse() throws Exception {
final var actual = parseTheme("""
final List<ParsedThemeRule> actual = parseTheme("""
{ "settings": [
{ "settings": { "foreground": "#F8F8F2", "background": "#272822" } },
{ "scope": "source, something", "settings": { "background": "#100000" } },
Expand All @@ -51,22 +53,21 @@ public void testCanParse() throws Exception {
{ "scope": "foo", "settings": { "fontStyle": "", "foreground": "#CFA" } }
]}""");

final var expected = new ParsedThemeRule[] {
new ParsedThemeRule("", null, 0, NotSet, "#F8F8F2", "#272822"),
new ParsedThemeRule("source", null, 1, NotSet, null, "#100000"),
new ParsedThemeRule("something", null, 1, NotSet, null, "#100000"),
new ParsedThemeRule("bar", null, 2, NotSet, null, "#010000"),
new ParsedThemeRule("baz", null, 2, NotSet, null, "#010000"),
new ParsedThemeRule("bar", list("selector", "source.css"), 3, Bold, null, null),
new ParsedThemeRule("constant", null, 4, Italic, "#ff0000", null),
new ParsedThemeRule("constant.numeric", null, 5, NotSet, "#00ff00", null),
new ParsedThemeRule("constant.numeric.hex", null, 6, Bold, null, null),
new ParsedThemeRule("constant.numeric.oct", null, 7, Bold | Italic | Underline, null, null),
new ParsedThemeRule("constant.numeric.bin", null, 8, Bold | Strikethrough, null, null),
new ParsedThemeRule("constant.numeric.dec", null, 9, None, "#0000ff", null),
new ParsedThemeRule("foo", null, 10, None, "#CFA", null),
};
final var expected = list(
new ParsedThemeRule("", null, 0, NotSet, "#F8F8F2", "#272822"),
new ParsedThemeRule("source", null, 1, NotSet, null, "#100000"),
new ParsedThemeRule("something", null, 1, NotSet, null, "#100000"),
new ParsedThemeRule("bar", null, 2, NotSet, null, "#010000"),
new ParsedThemeRule("baz", null, 2, NotSet, null, "#010000"),
new ParsedThemeRule("bar", list("selector", "source.css"), 3, Bold, null, null),
new ParsedThemeRule("constant", null, 4, Italic, "#ff0000", null),
new ParsedThemeRule("constant.numeric", null, 5, NotSet, "#00ff00", null),
new ParsedThemeRule("constant.numeric.hex", null, 6, Bold, null, null),
new ParsedThemeRule("constant.numeric.oct", null, 7, Bold | Italic | Underline, null, null),
new ParsedThemeRule("constant.numeric.bin", null, 8, Bold | Strikethrough, null, null),
new ParsedThemeRule("constant.numeric.dec", null, 9, None, "#0000ff", null),
new ParsedThemeRule("foo", null, 10, None, "#CFA", null));

assertArrayEquals(expected, actual.toArray());
assertThat(actual).isEqualTo(expected);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public class ThemeResolvingTest extends AbstractThemeTest {
private static final ThemeTrieElement NOTSET_THEME_TRIE_ELEMENT = new ThemeTrieElement(NOTSET_THEME_TRIE_ELEMENT_RULE);

private static void assertStrArrCmp(final String testCase, final List<String> a, final List<String> b, final int expected) {
assertEquals(expected, StringUtils.strArrCmp(a, b), testCase);
assertThat(StringUtils.strArrCmp(a, b)).as(testCase).isEqualTo(expected);
}

@Test
Expand All @@ -57,7 +57,7 @@ public void testStrcmpWorks() {
actual.sort(StringUtils::strcmp);

final var expected = list("", "a", "ab", "bar", "z", "zu");
assertArrayEquals(expected.toArray(), actual.toArray());
assertThat(actual).isEqualTo(expected);
}

@Test
Expand Down Expand Up @@ -96,10 +96,10 @@ public boolean shouldSkipClass(final Class<?> clazz) {

private static void assertThemeEqual(final Theme actual, final Theme expected) {
// if this fails, we get a nice visual representation of the difference:
assertEquals(THEME_GSON.toJson(expected), THEME_GSON.toJson(actual));
assertThat(THEME_GSON.toJson(actual)).isEqualTo(THEME_GSON.toJson(expected));

// this ensures hashCode/equals are properly implemented:
assertEquals(expected, actual);
assertThat(actual).isEqualTo(expected);
}

@Test
Expand Down Expand Up @@ -372,10 +372,10 @@ interface Matcher {
return colorMap.get(result.foregroundId);
};

assertEquals(matcher.match("b", "a"), "#300000", "b a");
assertEquals(matcher.match("b", "c", "a"), "#200000", "b c a");
assertEquals(matcher.match("c", "b", "a"), "#400000", "c b a");
assertEquals(matcher.match("c", "b", "d", "a"), "#200000", "c b d a");
assertThat(matcher.match("b", "a")).as("b a").isEqualTo("#300000");
assertThat(matcher.match("b", "c", "a")).as("b c a").isEqualTo("#200000");
assertThat(matcher.match("c", "b", "a")).as("c b a").isEqualTo("#400000");
assertThat(matcher.match("c", "b", "d", "a")).as("c b d a").isEqualTo("#200000");
}

@Test
Expand All @@ -402,14 +402,14 @@ interface Matcher {
return colorMap.get(result.foregroundId);
};

assertEquals(matcher.match("x", "a.b"), null, "x a.b");
assertEquals(matcher.match("y", "a.b"), null, "y a.b");
assertEquals(matcher.match("y.z", "a"), null, "y.z a");
assertEquals(matcher.match("x", "y", "a.b"), "#300000", "x y a.b");
assertThat(matcher.match("x", "a.b")).as("x a.b").isNull();
assertThat(matcher.match("y", "a.b")).as("y a.b").isNull();
assertThat(matcher.match("y.z", "a")).as("y.z a").isNull();
assertThat(matcher.match("x", "y", "a.b")).as("x y a.b").isEqualTo("#300000");

// Even though the "x y a.b" rule has more scopes in its path, the "y.z a.b" rule has
// a deeper match, so it should take precedence.
assertEquals(matcher.match("x", "y.z", "a.b"), "#200000", "y.z a.b");
assertThat(matcher.match("x", "y.z", "a.b")).as("y.z a.b").isEqualTo("#200000");
}

@Test
Expand Down Expand Up @@ -467,7 +467,7 @@ public void testIssue_38_ignores_rules_with_invalid_colors() throws Exception {
new ParsedThemeRule("variable.other", null, 4, None, null, null),
new ParsedThemeRule("variable.parameter.function.coffee", null, 5, Italic, "#F9D423", null));

assertArrayEquals(expected.toArray(), actual.toArray());
assertThat(actual).isEqualTo(expected);
}

@Test
Expand Down Expand Up @@ -505,6 +505,6 @@ public void testIssue_35_Trailing_comma_in_a_tmTheme_scope_selector() throws Exc
new ParsedThemeRule("meta.at-rule.if.scss", null, 1, NotSet, "#CC7832", null),
new ParsedThemeRule("punctuation.definition", list("meta.at-rule.if.scss"), 1, NotSet, "#CC7832", null));

assertArrayEquals(expected.toArray(), actual.toArray());
assertThat(actual).isEqualTo(expected);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/
package org.eclipse.tm4e.core.internal.theme.css;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.assertj.core.api.Assertions.*;

import org.eclipse.tm4e.core.theme.RGB;
import org.eclipse.tm4e.core.theme.css.CSSParser;
Expand All @@ -27,9 +27,9 @@ void testCSSParser() throws Exception {
.storage.invalid { background-color: red; }
""");

assertEquals(null, parser.getBestStyle("undefined"));
assertEquals(new RGB(255,128,128), parser.getBestStyle("invalid").getBackgroundColor());
assertEquals(new RGB(255,0,0), parser.getBestStyle("storage", "invalid").getBackgroundColor());
assertEquals(new RGB(255,0,0), parser.getBestStyle("storage" , "modifier", "invalid", "deprecated").getBackgroundColor());
assertThat(parser.getBestStyle("undefined")).isNull();
assertThat(parser.getBestStyle("invalid").getBackgroundColor()).isEqualTo(new RGB(255, 128, 128));
assertThat(parser.getBestStyle("storage", "invalid").getBackgroundColor()).isEqualTo(new RGB(255, 0, 0));
assertThat(parser.getBestStyle("storage", "modifier", "invalid", "deprecated").getBackgroundColor()).isEqualTo(new RGB(255, 0, 0));
}
}
2 changes: 1 addition & 1 deletion org.eclipse.tm4e.core/build.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ bin.includes = META-INF/,\
about.html

# JDT Null Analysis for Eclipse
additional.bundles = org.eclipse.jdt.annotation
additional.bundles = org.eclipse.jdt.annotation,assertj-core
Loading

0 comments on commit 17f46fd

Please sign in to comment.