Skip to content

Commit 4afc5dc

Browse files
committed
improve Label & its tests
Signed-off-by: Stefan Niederhauser <[email protected]>
1 parent e467730 commit 4afc5dc

File tree

2 files changed

+88
-4
lines changed

2 files changed

+88
-4
lines changed

graphviz-java/src/main/java/guru/nidi/graphviz/attribute/Label.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@
2525
import static java.util.stream.Collectors.joining;
2626

2727
public final class Label extends SimpleLabel implements Attributes<ForAll> {
28+
public static final String NODE_NAME = "\\N";
29+
public static final String GRAPH_NAME = "\\G";
30+
public static final String HEAD_NAME = "\\H";
31+
public static final String TAIL_NAME = "\\T";
32+
2833
public enum Justification {
2934
LEFT, MIDDLE, RIGHT
3035
}
@@ -67,7 +72,7 @@ public static Label of(String value) {
6772
* @return the Label
6873
*/
6974
public static Label nodeName() {
70-
return of("\\N");
75+
return of(NODE_NAME);
7176
}
7277

7378
/**
@@ -76,7 +81,7 @@ public static Label nodeName() {
7681
* @return the Label
7782
*/
7883
public static Label graphName() {
79-
return of("\\G");
84+
return of(GRAPH_NAME);
8085
}
8186

8287
/**
@@ -85,7 +90,7 @@ public static Label graphName() {
8590
* @return the Label
8691
*/
8792
public static Label headName() {
88-
return of("\\H");
93+
return of(HEAD_NAME);
8994
}
9095

9196
/**
@@ -94,7 +99,7 @@ public static Label headName() {
9499
* @return the Label
95100
*/
96101
public static Label tailName() {
97-
return of("\\T");
102+
return of(TAIL_NAME);
98103
}
99104

100105
/**

graphviz-java/src/test/java/guru/nidi/graphviz/attribute/LabelTest.java

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,83 @@ void locate() {
6060
assertEquals(attrs(attr("label", label.locate(BOTTOM)), attr("labelloc", "b")), attrs(label.locate(BOTTOM)));
6161
}
6262

63+
@Test
64+
void nodeName() {
65+
assertEquals("\\N", Label.nodeName().value());
66+
}
67+
68+
@Test
69+
void graphName() {
70+
assertEquals("\\G", Label.graphName().value());
71+
}
72+
73+
@Test
74+
void headName() {
75+
assertEquals("\\H", Label.headName().value());
76+
}
77+
78+
@Test
79+
void tailName() {
80+
assertEquals("\\T", Label.tailName().value());
81+
}
82+
83+
@Test
84+
void lines() {
85+
assertEquals("a\nb\nc\n", Label.lines("a", "b", "c").value);
86+
assertEquals("a\\lb\\lc\\l", Label.lines(LEFT, "a", "b", "c").value);
87+
assertEquals("a\\rb\\rc\\r", Label.lines(RIGHT, "a", "b", "c").value);
88+
}
89+
90+
@Test
91+
void htmlLines() {
92+
assertEquals("a<br/>b<br/>", Label.htmlLines("a", "b").value);
93+
assertEquals("a<br align=\"left\"/>b<br align=\"left\"/>", Label.htmlLines(LEFT, "a", "b").value);
94+
assertEquals("a<br align=\"right\"/>b<br align=\"right\"/>", Label.htmlLines(RIGHT, "a", "b").value);
95+
}
96+
97+
@Test
98+
void html() {
99+
assertEquals("<html>", Label.html("html").serialized());
100+
}
101+
102+
@Test
103+
void backslashAtEnd() {
104+
assertEquals("\"a\\\\\"", Label.of("a\\").serialized());
105+
assertEquals("\"a\\\\\"", Label.of("a\\\\").serialized());
106+
}
107+
108+
@Test
109+
void newlines() {
110+
assertEquals("\"1\\n2\\n3\\n4\\n\\n5\"", Label.of("1\n2\r3\r\n4\n\r5").serialized());
111+
}
112+
113+
@Test
114+
void markdown() {
115+
assertEquals("a<br/>b <b>b<i>ol</i>d</b> <s>s</s> <sub>sub</sub> <sup>sup</sup> <o>o</o> *",
116+
Label.markdown("a\nb **b*ol*d** ~~s~~ __sub__ ^^sup^^ ^o^ \\*").value);
117+
}
118+
119+
@Test
120+
void raw() {
121+
assertEquals("\"hula\"", Label.raw("hula").serialized());
122+
assertEquals("<hula>", Label.raw("<hula>").serialized());
123+
assertEquals("<hula<b>bold</b>**star**>", Label.raw("<hula<b>bold</b>**star**>").serialized());
124+
assertEquals("<hula<b>bold</b>>", Label.raw("<hula**bold**>").serialized());
125+
}
126+
127+
@Test
128+
void head() {
129+
assertEquals(attrs(attr("headlabel", EndLabel.head(Label.of("hula"), null, null))),
130+
attrs(label.head()));
131+
assertEquals(attrs(attr("headlabel", EndLabel.head(Label.of("hula"), 1.2, 3.4)), attr("labelangle", 1.2), attr("labeldistance", 3.4)),
132+
attrs(label.head(1.2, 3.4)));
133+
}
134+
135+
@Test
136+
void tail() {
137+
assertEquals(attrs(attr("taillabel", EndLabel.tail(Label.of("hula"), null, null))),
138+
attrs(label.tail()));
139+
assertEquals(attrs(attr("taillabel", EndLabel.tail(Label.of("hula"), 1.2, 3.4)), attr("labelangle", 1.2), attr("labeldistance", 3.4)),
140+
attrs(label.tail(1.2, 3.4)));
141+
}
63142
}

0 commit comments

Comments
 (0)