Skip to content

Commit

Permalink
[461506] Improve the Dot Color sub-grammar validation/content assistant.
Browse files Browse the repository at this point in the history
- Modify the DotColorJavaValidator and the DotColorProposalProvider to
consider the globally set colorscheme attribute value.
- Modify the DotColorJavaValidator and the DotColorProposalProvider to
treat the colorscheme attribute value case insensitively.
- Move the Dot ast walking logic from the DotProposalProvider class into
the DotAstHelper class.
- Implement corresponding validation and content assistant test cases.
  • Loading branch information
miklossy committed Aug 5, 2017
1 parent 9f22901 commit db353bd
Show file tree
Hide file tree
Showing 9 changed files with 425 additions and 83 deletions.
33 changes: 33 additions & 0 deletions org.eclipse.gef.dot.tests/resources/colorscheme.dot
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*******************************************************************************
* Copyright (c) 2017 itemis AG and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Tamas Miklossy (itemis AG) - Initial text
*
*******************************************************************************/

// Sample graph from
// https://github.com/ellson/graphviz/blob/master/rtest/graphs/colorscheme.gv

digraph G {

node [shape=box, style=filled]

subgraph {
node [colorscheme=spectral11]
1 [color=1]
4 [color=4]
8 [color=8]
11 [color="//11"]
}

ylgn7 [color="/ylgn7/5"]
X11 [color="/X11/thistle"]
indigo [color="/X11/indigo"]
magenta [color="magenta"]
}
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,20 @@ public void edge_color() throws Exception {
"azure3", "azure4")
.applyProposal(27, "azure2")
.expectContent("digraph {1->2[ color=\"azure2\" ]}");

// test local attribute value with local color scheme value
newBuilder().append("graph{1--2[color=; colorscheme=brbg10]}")
.assertTextAtCursorPosition(17, "#", "/", "1", "2", "3", "4",
"5", "6", "7", "8", "9", "10")
.applyProposal(17, "10")
.expectContent("graph{1--2[color=10; colorscheme=brbg10]}");

// test local attribute value with global color scheme value
newBuilder().append("graph{edge[colorscheme=brbg10] 1--2[color=]}")
.assertTextAtCursorPosition(42, "#", "/", "1", "2", "3", "4",
"5", "6", "7", "8", "9", "10")
.applyProposal(42, "10").expectContent(
"graph{edge[colorscheme=brbg10] 1--2[color=10]}");
}

@Test
Expand Down Expand Up @@ -620,6 +634,13 @@ public void edge_labelfontcolor() throws Exception {
.applyProposal(46, "/").expectContent(
"digraph {1->2[ colorscheme=svg labelfontcolor=/ ]}");

// test local attribute values (case insensitive color scheme)
newBuilder().append("digraph {1->2[ colorscheme=SVG labelfontcolor= ]}")
.assertTextAtCursorPosition(46,
combine(expectedSvgColorNames, "#", "/"))
.applyProposal(46, "/").expectContent(
"digraph {1->2[ colorscheme=SVG labelfontcolor=/ ]}");

// test local attribute values with quotes
newBuilder()
.append("digraph {1->2[ colorscheme=svg labelfontcolor=\"\" ]}")
Expand Down Expand Up @@ -892,6 +913,20 @@ public void graph_bgcolor() throws Exception {
"darkslategrey", "darkturquoise", "darkviolet")
.applyProposal(37, "darkturquoise").expectContent(
"graph { colorscheme=svg bgcolor=\"darkturquoise\" }");

// test local attribute value with local color scheme value
newBuilder().append("graph{colorscheme=brbg10 bgcolor= 1}")
.assertTextAtCursorPosition(33, "#", "/", "1", "2", "3", "4",
"5", "6", "7", "8", "9", "10")
.applyProposal(33, "10")
.expectContent("graph{colorscheme=brbg10 bgcolor=10 1}");

// test local attribute value with global color scheme value
newBuilder().append("graph{graph[colorscheme=brbg10] bgcolor= 1}")
.assertTextAtCursorPosition(40, "#", "/", "1", "2", "3", "4",
"5", "6", "7", "8", "9", "10")
.applyProposal(40, "10")
.expectContent("graph{graph[colorscheme=brbg10] bgcolor=10 1}");
}

@Test
Expand Down Expand Up @@ -1330,6 +1365,20 @@ public void node_color() throws Exception {
"lightsteelblue3", "lightsteelblue4")
.applyProposal(23, "lightskyblue")
.expectContent("graph {1[ color=\"lightskyblue\" ]}");

// test local attribute value with local color scheme value
newBuilder().append("graph{1[colorscheme=brbg10 color=]}")
.assertTextAtCursorPosition(33, "#", "/", "1", "2", "3", "4",
"5", "6", "7", "8", "9", "10")
.applyProposal(33, "10")
.expectContent("graph{1[colorscheme=brbg10 color=10]}");

// test local attribute value with global color scheme value
newBuilder().append("graph{node[colorscheme=brbg10] 1[color=]}")
.assertTextAtCursorPosition(39, "#", "/", "1", "2", "3", "4",
"5", "6", "7", "8", "9", "10")
.applyProposal(39, "10")
.expectContent("graph{node[colorscheme=brbg10] 1[color=10]}");
}

@Test
Expand Down Expand Up @@ -1461,7 +1510,13 @@ public void node_fontcolor() throws Exception {
newBuilder().append("graph {1[ fontcolor=\"/accent3/\" ]}")
.assertTextAtCursorPosition(30, "/", "1", "2", "3")
.applyProposal(30, "1")
.expectContent("graph {1[ fontcolor=\"/accent3/1\" ]}"); //
.expectContent("graph {1[ fontcolor=\"/accent3/1\" ]}");

// test local attribute values with quotes (case insensitively)
newBuilder().append("graph {1[ fontcolor=\"/ACCENT3/\" ]}")
.assertTextAtCursorPosition(30, "/", "1", "2", "3")
.applyProposal(30, "1")
.expectContent("graph {1[ fontcolor=\"/ACCENT3/1\" ]}");

// test local attribute values with prefix
newBuilder().append("graph {1[ colorscheme=svg fontcolor=w ]}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,11 @@ public void testColoredGraph() {
testFile("colored_graph.dot");
}

@Test
public void testColorSchemeGraph() {
testFile("colorscheme.dot");
}

@Test
public void testHtmlLikeLabels1() {
testFile("html_like_labels1.dot");
Expand All @@ -286,6 +291,24 @@ public void testHtmlLikeLabels4() {
testFile("html_like_labels4.dot");
}

@Test
public void testGraphColorWithCustomColorScheme() {
testString("graph{graph[colorscheme=brbg10] bgcolor=5 1}");
testString("graph{colorscheme=brbg10 bgcolor=5 1}");
}

@Test
public void testNodeColorWithCustomColorScheme() {
testString("graph{node[colorscheme=brbg10] 1[color=5]}");
testString("graph{1[colorscheme=brbg10 color=5]}");
}

@Test
public void testEdgeColorWithCustomColorScheme() {
testString("graph{edge[colorscheme=brbg10] 1--2[color=5]}");
testString("graph{1--2[color=5 colorscheme=brbg10]}");
}

private void testFile(String fileName) {
String fileContents = DotFileUtils
.read(new File(DotTestUtils.RESOURCES_TESTS + fileName));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,40 @@ public void testWrongGraphBackgroundColor() throws Exception {
Assert.assertEquals(1, validationTestHelper.validate(dotAst).size());
}

@Test
public void testGraphBackgroundColorDoesNotCorrespondToLocalColorScheme()
throws Exception {
registerColorPackage();

String text = "graph { colorscheme=brbg10 bgcolor=blue}";

DotAst dotAst = parserHelper.parse(text);

validationTestHelper.assertError(dotAst,
DotPackage.eINSTANCE.getAttribute(), DotAttributes.BGCOLOR__GC,
"The color value 'blue' is not semantically correct: The 'blue' color is not valid within the 'brbg10' color scheme.");

// verify that this is the only reported issues
Assert.assertEquals(1, validationTestHelper.validate(dotAst).size());
}

@Test
public void testGraphBackgroundColorDoesNotCorrespondToGlobalColorScheme()
throws Exception {
registerColorPackage();

String text = "graph { graph[colorscheme=brbg10] bgcolor=blue}";

DotAst dotAst = parserHelper.parse(text);

validationTestHelper.assertError(dotAst,
DotPackage.eINSTANCE.getAttribute(), DotAttributes.BGCOLOR__GC,
"The color value 'blue' is not semantically correct: The 'blue' color is not valid within the 'brbg10' color scheme.");

// verify that this is the only reported issues
Assert.assertEquals(1, validationTestHelper.validate(dotAst).size());
}

@Test
public void testWrongNodeColor() throws Exception {
registerColorPackage();
Expand All @@ -288,6 +322,93 @@ public void testWrongNodeColor() throws Exception {
Assert.assertEquals(1, validationTestHelper.validate(dotAst).size());
}

@Test
public void testNodeColorDoesNotCorrespondToLocalColorScheme()
throws Exception {
registerColorPackage();

String text = "graph { 1[colorscheme=brbg10 color=blue]}";

DotAst dotAst = parserHelper.parse(text);

validationTestHelper.assertError(dotAst,
DotPackage.eINSTANCE.getAttribute(), DotAttributes.COLOR__CNE,
"The color value 'blue' is not semantically correct: The 'blue' color is not valid within the 'brbg10' color scheme.");

// verify that this is the only reported issues
Assert.assertEquals(1, validationTestHelper.validate(dotAst).size());
}

@Test
public void testNodeColorDoesNotCorrespondToGlobalColorScheme()
throws Exception {
registerColorPackage();

String text = "graph { node[colorscheme=brbg10] 1[color=blue]}";

DotAst dotAst = parserHelper.parse(text);

validationTestHelper.assertError(dotAst,
DotPackage.eINSTANCE.getAttribute(), DotAttributes.COLOR__CNE,
"The color value 'blue' is not semantically correct: The 'blue' color is not valid within the 'brbg10' color scheme.");

// verify that this is the only reported issues
Assert.assertEquals(1, validationTestHelper.validate(dotAst).size());
}

@Test
public void testWrongEdgeFillColor() throws Exception {
registerColorPackage();

String text = "digraph { 1->2[fillcolor=\"#fffff\"]}";

DotAst dotAst = parserHelper.parse(text);

validationTestHelper.assertError(dotAst,
DotPackage.eINSTANCE.getAttribute(),
DotAttributes.FILLCOLOR__CNE,
"The value '#fffff' is not a syntactically correct color: Mismatched input '<EOF>' expecting RULE_HEXADECIMAL_DIGIT.");

// verify that this is the only reported issues
Assert.assertEquals(1, validationTestHelper.validate(dotAst).size());
}

@Test
public void testEdgeFillColorDoesNotCorrespondToLocalColorScheme()
throws Exception {
registerColorPackage();

String text = "digraph { 1->2[colorscheme=brbg10 fillcolor=white]}";

DotAst dotAst = parserHelper.parse(text);

validationTestHelper.assertError(dotAst,
DotPackage.eINSTANCE.getAttribute(),
DotAttributes.FILLCOLOR__CNE,
"The color value 'white' is not semantically correct: The 'white' color is not valid within the 'brbg10' color scheme.");

// verify that this is the only reported issues
Assert.assertEquals(1, validationTestHelper.validate(dotAst).size());
}

@Test
public void testEdgeFillColorDoesNotCorrespondToGlobalColorScheme()
throws Exception {
registerColorPackage();

String text = "digraph { edge[colorscheme=brbg10] 1->2[fillcolor=red]}";

DotAst dotAst = parserHelper.parse(text);

validationTestHelper.assertError(dotAst,
DotPackage.eINSTANCE.getAttribute(),
DotAttributes.FILLCOLOR__CNE,
"The color value 'red' is not semantically correct: The 'red' color is not valid within the 'brbg10' color scheme.");

// verify that this is the only reported issues
Assert.assertEquals(1, validationTestHelper.validate(dotAst).size());
}

@Test
public void testWrongNodeDistortion() throws Exception {
String text = "graph { 1[distortion=foo] 2[distortion=\"-100.0001\"]}";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@
public class DotColorProposalProvider extends
org.eclipse.gef.dot.internal.ui.language.contentassist.AbstractDotColorProposalProvider {

/**
* Represents the color scheme that is defined in the DOT ast. If this color
* scheme is not defined, the default color scheme should be used in the
* proposal provider.
*/
public static String globalColorScheme = null;

private final String defaultColorScheme = "x11"; //$NON-NLS-1$

@Override
public void completeStringColor_Scheme(EObject model, Assignment assignment,
ContentAssistContext context,
Expand All @@ -43,16 +52,19 @@ public void completeStringColor_Name(EObject model, Assignment assignment,
ContentAssistContext context,
ICompletionProposalAcceptor acceptor) {
super.completeStringColor_Name(model, assignment, context, acceptor);
if (model instanceof StringColor) {
StringColor stringColor = (StringColor) model;
String colorScheme = stringColor.getScheme();
if (colorScheme != null) {
for (String colorName : DotColors.getColorNames(colorScheme)) {
acceptor.accept(
createCompletionProposal(colorName, context));
}
}
// start with the default color scheme
String colorScheme = defaultColorScheme;

if (model instanceof StringColor
&& ((StringColor) model).getScheme() != null) {
colorScheme = ((StringColor) model).getScheme();
} else if (globalColorScheme != null) {
colorScheme = globalColorScheme;
}
}

for (String colorName : DotColors
.getColorNames(colorScheme.toLowerCase())) {
acceptor.accept(createCompletionProposal(colorName, context));
}
}
}
Loading

0 comments on commit db353bd

Please sign in to comment.