Skip to content

Commit 17b1e9e

Browse files
committed
Add tests and refactoring
1 parent a0de13e commit 17b1e9e

File tree

4 files changed

+94
-37
lines changed

4 files changed

+94
-37
lines changed

core/esmf-aspect-meta-model-java/src/main/java/org/eclipse/esmf/aspectmodel/utils/DescriptionsUtils.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
import java.util.ArrayList;
1717
import java.util.List;
18-
import java.util.Set;
1918
import java.util.regex.Matcher;
2019
import java.util.regex.Pattern;
2120

@@ -37,7 +36,7 @@ private DescriptionsUtils() {
3736
* Matches lines beginning with {@code > NOTE:}, {@code > EXAMPLE:}, or {@code > SOURCE:},
3837
* optionally followed by a number (e.g., {@code > EXAMPLE 2: ...}).
3938
*/
40-
private static final Pattern BLOCK_PATTERN = Pattern.compile(
39+
static final Pattern BLOCK_PATTERN = Pattern.compile(
4140
"^>\\s*(NOTE|EXAMPLE|SOURCE)(\\s+\\d+)?:\\s*(.*)",
4241
Pattern.CASE_INSENSITIVE
4342
);

core/esmf-aspect-meta-model-java/src/main/java/org/eclipse/esmf/aspectmodel/utils/MarkdownHtmlRenderer.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import java.util.LinkedHashMap;
1818
import java.util.List;
1919
import java.util.Map;
20-
import java.util.Set;
2120
import java.util.regex.Matcher;
2221
import java.util.regex.Pattern;
2322
import java.util.stream.Collectors;
@@ -140,14 +139,13 @@ private static String renderSpecialBlock( final String type, final List<String>
140139
* @return A map of special block types to their associated content.
141140
*/
142141
private static Map<String, List<String>> collectSpecialBlocks( final String[] lines, final StringBuilder markdownBuffer ) {
143-
Pattern pattern = Pattern.compile( "^>\\s*(NOTE|EXAMPLE|SOURCE)(\\s+\\d+)?:\\s*(.*)", Pattern.CASE_INSENSITIVE );
144142
Map<String, List<String>> specialBlocks = new LinkedHashMap<>();
145143

146144
String currentType = null;
147145
StringBuilder block = new StringBuilder();
148146

149147
for ( String line : lines ) {
150-
Matcher matcher = pattern.matcher( line );
148+
Matcher matcher = DescriptionsUtils.BLOCK_PATTERN.matcher( line );
151149
if ( matcher.find() ) {
152150
flushBlock( currentType, block, specialBlocks );
153151
currentType = matcher.group( 1 ).toUpperCase();

core/esmf-aspect-meta-model-java/src/test/java/org/eclipse/esmf/aspectmodel/utils/DescriptionsUtilsTest.java

Lines changed: 76 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.eclipse.esmf.aspectmodel.utils;
22

3+
import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
34
import static org.junit.jupiter.api.Assertions.assertEquals;
45
import static org.junit.jupiter.api.Assertions.assertTrue;
56

@@ -9,15 +10,15 @@
910

1011
class DescriptionsUtilsTest {
1112
@Test
12-
void testExtractNotes_singleNote() {
13+
void testExtractNotesSingleNote() {
1314
final String description = "> NOTE: This is a note.\n> Continued on the next line.";
1415
final List<String> notes = DescriptionsUtils.notes( description );
15-
assertEquals( 1, notes.size() );
16+
assertThat( notes ).hasSize( 1 );
1617
assertEquals( "This is a note.\nContinued on the next line.", notes.get( 0 ) );
1718
}
1819

1920
@Test
20-
void testExtractExamples_multipleExamples() {
21+
void testExtractExamplesMultipleExamples() {
2122
final String description =
2223
"""
2324
> EXAMPLE 1: First example.
@@ -33,11 +34,11 @@ void testExtractExamples_multipleExamples() {
3334
}
3435

3536
@Test
36-
void testExtractSources_withLink() {
37+
void testExtractSourcesWithLink() {
3738
final String description = "> SOURCE: Source with [link](https://example.com)";
3839
final List<String> sources = DescriptionsUtils.sources( description );
3940
assertEquals( 1, sources.size() );
40-
assertTrue( sources.get( 0 ).contains( "[link](https://example.com)" ) );
41+
assertThat( sources.get( 0 ) ).contains( "[link](https://example.com)" );
4142
}
4243

4344
@Test
@@ -57,13 +58,13 @@ void testMixedBlockTypes() {
5758
@Test
5859
void testNoBlocks() {
5960
final String description = "This is a plain description without any special blocks.";
60-
assertTrue( DescriptionsUtils.notes( description ).isEmpty() );
61-
assertTrue( DescriptionsUtils.examples( description ).isEmpty() );
62-
assertTrue( DescriptionsUtils.sources( description ).isEmpty() );
61+
assertThat( DescriptionsUtils.notes( description ) ).isEmpty();
62+
assertThat( DescriptionsUtils.examples( description ) ).isEmpty();
63+
assertThat( DescriptionsUtils.sources( description ) ).isEmpty();
6364
}
6465

6566
@Test
66-
void testToHtml_withAllBlockTypes() {
67+
void testToHtmlWithAllBlockTypes() {
6768
final String description =
6869
"""
6970
> NOTE: This is a note.
@@ -83,13 +84,72 @@ void testToHtml_withAllBlockTypes() {
8384

8485
final String html = DescriptionsUtils.toHtml( description );
8586

86-
assertTrue( html.contains( "<div class=\"note\">" ) );
87-
assertTrue( html.contains( "This is a note." ) );
87+
assertThat( html ).contains( "<div class=\"note\">" );
88+
assertThat( html ).contains( "This is a note." );
8889
assertTrue( html.contains( "<ul class=\"example-list\">" ) || html.contains( "<div class=\"example\">" ) );
89-
assertTrue( html.contains( "First example." ) );
90-
assertTrue( html.contains( "<div class=\"source\">" ) );
91-
assertTrue( html.contains( "Source information here." ) );
92-
assertTrue( html.contains( "<strong>markdown</strong>" ) );
93-
assertTrue( html.contains( "<ol>" ) );
90+
assertThat( html ).contains( "First example." );
91+
assertThat( html ).contains( "<div class=\"source\">" );
92+
assertThat( html ).contains( "Source information here." );
93+
assertThat( html ).contains( "<strong>markdown</strong>" );
94+
assertThat( html ).contains( "<ol>" );
95+
}
96+
97+
@Test
98+
void testMarkdownRenderingBulletList() {
99+
String description = """
100+
This is a list:
101+
* Item A
102+
* Item B
103+
* Item C
104+
""";
105+
String html = DescriptionsUtils.toHtml( description );
106+
assertThat( html ).contains( "<ul>" );
107+
assertThat( html ).contains( "<li>Item A</li>" );
108+
assertThat( html ).contains( "<li>Item B</li>" );
109+
assertThat( html ).contains( "<li>Item C</li>" );
110+
}
111+
112+
@Test
113+
void testMarkdownRenderingOrderedList() {
114+
String description = """
115+
Steps:
116+
1. First
117+
2. Second
118+
3. Third
119+
""";
120+
String html = DescriptionsUtils.toHtml( description );
121+
assertThat( html ).contains( "<ol>" );
122+
assertThat( html ).contains( "<li>First</li>" );
123+
assertThat( html ).contains( "<li>Second</li>" );
124+
assertThat( html ).contains( "<li>Third</li>" );
125+
}
126+
127+
@Test
128+
void testMarkdownRenderingSpecialBlock() {
129+
String description =
130+
"""
131+
> NOTE: This is a note.
132+
> Continued here.
133+
""";
134+
String html = DescriptionsUtils.toHtml( description );
135+
assertThat( html ).contains( "<div class=\"note\">" );
136+
assertThat( html ).contains( "This is a note." );
137+
assertThat( html ).contains( "Continued here." );
138+
}
139+
140+
@Test
141+
void testMarkdownRenderingWithLink() {
142+
String description =
143+
"Here is a [link](https://example.com) in the text.";
144+
String html = DescriptionsUtils.toHtml( description );
145+
assertThat( html ).contains( "<a href=\"https://example.com\">link</a>" );
146+
}
147+
148+
@Test
149+
void testHtmlOutputDoesNotContainMarkdownSyntax() {
150+
String description =
151+
"This is a [link](https://example.com).";
152+
String html = DescriptionsUtils.toHtml( description );
153+
assertThat( html ).doesNotContain( "[link](https://example.com)" );
94154
}
95155
}

core/esmf-aspect-model-document-generators/src/test/java/org/eclipse/esmf/aspectmodel/generator/docu/AspectModelDocumentationGeneratorTest.java

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@
2828
import org.junit.jupiter.params.ParameterizedTest;
2929
import org.junit.jupiter.params.provider.EnumSource;
3030

31-
class AspectModelDocumentationGeneratorTest {
31+
public class AspectModelDocumentationGeneratorTest {
3232
@ParameterizedTest
3333
@EnumSource( value = TestAspect.class )
34-
void testGeneration( final TestAspect testAspect ) {
34+
public void testGeneration( final TestAspect testAspect ) {
3535
assertThatCode( () -> {
3636
final String html = generateHtmlDocumentation( testAspect );
3737
assertThat( html ).doesNotContain( "UnnamedCharacteristic" );
@@ -41,7 +41,7 @@ void testGeneration( final TestAspect testAspect ) {
4141
}
4242

4343
@Test
44-
void testAspectWithEntityCollection() throws Throwable {
44+
public void testAspectWithEntityCollection() throws Throwable {
4545
final String htmlResult = generateHtmlDocumentation( TestAspect.ASPECT_WITH_ENTITY_COLLECTION );
4646

4747
assertThat( htmlResult ).isNotEmpty();
@@ -53,7 +53,7 @@ void testAspectWithEntityCollection() throws Throwable {
5353
}
5454

5555
@Test
56-
void testAspectWithCollectionOfSimpleType() throws Throwable {
56+
public void testAspectWithCollectionOfSimpleType() throws Throwable {
5757
final String htmlResult = generateHtmlDocumentation( TestAspect.ASPECT_WITH_COLLECTION_OF_SIMPLE_TYPE );
5858

5959
assertThat( htmlResult ).isNotEmpty();
@@ -65,14 +65,14 @@ void testAspectWithCollectionOfSimpleType() throws Throwable {
6565
}
6666

6767
@Test
68-
void testScriptTagIsEscaped() throws IOException {
68+
public void testScriptTagIsEscaped() throws IOException {
6969
assertThat( generateHtmlDocumentation( TestAspect.ASPECT_WITH_SCRIPT_TAGS ) )
7070
.isNotEmpty()
7171
.doesNotContain( "<script>alert('Should not be alerted');</script>" );
7272
}
7373

7474
@Test
75-
void testRubyGemUpdateCommandIsNotExecuted() throws IOException {
75+
public void testRubyGemUpdateCommandIsNotExecuted() throws IOException {
7676
try ( final ByteArrayOutputStream stdOut = new ByteArrayOutputStream() ) {
7777
System.setOut( new PrintStream( stdOut ) );
7878
generateHtmlDocumentation( TestAspect.ASPECT_WITH_RUBY_GEM_UPDATE_COMMAND );
@@ -81,7 +81,7 @@ void testRubyGemUpdateCommandIsNotExecuted() throws IOException {
8181
}
8282

8383
@Test
84-
void testHtmlTagsAreEscaped() throws IOException {
84+
public void testHtmlTagsAreEscaped() throws IOException {
8585
assertThat( generateHtmlDocumentation( TestAspect.ASPECT_WITH_HTML_TAGS ) )
8686
.isNotEmpty()
8787
.doesNotContain( "<img src=xss.png onerror=alert('Boom!')>" )
@@ -90,20 +90,20 @@ void testHtmlTagsAreEscaped() throws IOException {
9090
}
9191

9292
@Test
93-
void testEncodedTextIsNotDecoded() throws IOException {
93+
public void testEncodedTextIsNotDecoded() throws IOException {
9494
assertThat( generateHtmlDocumentation( TestAspect.ASPECT_WITH_ENCODED_STRINGS ) )
9595
.doesNotContain( "This is an Aspect with encoded text." )
9696
.contains( "VGhpcyBpcyBhbiBBc3BlY3Qgd2l0aCBlbmNvZGVkIHRleHQu" );
9797
}
9898

9999
@Test
100-
void testAspectModelUrnIsDisplayed() throws IOException {
100+
public void testAspectModelUrnIsDisplayed() throws IOException {
101101
assertThat( generateHtmlDocumentation( TestAspect.ASPECT_WITH_HTML_TAGS ) )
102102
.contains( "urn:samm:org.eclipse.esmf.test:1.0.0#AspectWithHtmlTags" );
103103
}
104104

105105
@Test
106-
void testDocInfosAreDisplayed() throws IOException {
106+
public void testDocInfosAreDisplayed() throws IOException {
107107
assertThat( generateHtmlDocumentation( TestAspect.ASPECT_WITH_HTML_TAGS ) )
108108
.contains( ".toc-list" )
109109
.contains( "aspect-model-diagram" )
@@ -115,13 +115,13 @@ void testDocInfosAreDisplayed() throws IOException {
115115
}
116116

117117
@Test
118-
void testDocumentationIsNotEmptyForModelWithoutLanguageTags() throws IOException {
118+
public void testDocumentationIsNotEmptyForModelWithoutLanguageTags() throws IOException {
119119
final String aspectWithoutLanguageTags = generateHtmlDocumentation( TestAspect.ASPECT_WITHOUT_LANGUAGE_TAGS );
120120
assertThat( aspectWithoutLanguageTags ).isNotEmpty();
121121
}
122122

123123
@Test
124-
void testAspectWithAbstractSingleEntityExpectSuccess() throws IOException {
124+
public void testAspectWithAbstractSingleEntityExpectSuccess() throws IOException {
125125
final String documentation = generateHtmlDocumentation( TestAspect.ASPECT_WITH_ABSTRACT_SINGLE_ENTITY );
126126
assertThat( documentation ).contains(
127127
"<h3 id=\"org-eclipse-esmf-test-AspectWithAbstractSingleEntity-org-eclipse-esmf-test-testProperty-property\">testProperty</h3"
@@ -134,7 +134,7 @@ void testAspectWithAbstractSingleEntityExpectSuccess() throws IOException {
134134
}
135135

136136
@Test
137-
void testAspectWithAbstractEntityExpectSuccess() throws IOException {
137+
public void testAspectWithAbstractEntityExpectSuccess() throws IOException {
138138
final String documentation = generateHtmlDocumentation( TestAspect.ASPECT_WITH_ABSTRACT_ENTITY );
139139
assertThat( documentation ).contains(
140140
"<h3 id=\"org-eclipse-esmf-test-AspectWithAbstractEntity-org-eclipse-esmf-test-testProperty-property\">Test Property</h3>" );
@@ -146,7 +146,7 @@ void testAspectWithAbstractEntityExpectSuccess() throws IOException {
146146
}
147147

148148
@Test
149-
void testAspectWithCollectionWithAbstractEntityExpectSuccess() throws IOException {
149+
public void testAspectWithCollectionWithAbstractEntityExpectSuccess() throws IOException {
150150
final String documentation = generateHtmlDocumentation( TestAspect.ASPECT_WITH_COLLECTION_WITH_ABSTRACT_ENTITY );
151151
assertThat( documentation ).contains(
152152
"<h3 id=\"org-eclipse-esmf-test-AspectWithCollectionWithAbstractEntity-org-eclipse-esmf-test-testProperty-property"
@@ -159,7 +159,7 @@ void testAspectWithCollectionWithAbstractEntityExpectSuccess() throws IOExceptio
159159
}
160160

161161
@Test
162-
void testAspectWithQuantifiableWithoutUnit() throws IOException {
162+
public void testAspectWithQuantifiableWithoutUnit() throws IOException {
163163
try ( final ByteArrayOutputStream stdOut = new ByteArrayOutputStream() ) {
164164
System.setOut( new PrintStream( stdOut ) );
165165
assertThatCode( () -> generateHtmlDocumentation( TestAspect.ASPECT_WITH_QUANTIFIABLE_WITHOUT_UNIT ) )
@@ -168,7 +168,7 @@ void testAspectWithQuantifiableWithoutUnit() throws IOException {
168168
}
169169

170170
@Test
171-
void testAspectWithConstraintWithSeeAttribute() throws IOException {
171+
public void testAspectWithConstraintWithSeeAttribute() throws IOException {
172172
final String documentation = generateHtmlDocumentation( TestAspect.ASPECT_WITH_CONSTRAINT_WITH_SEE_ATTRIBUTE );
173173
assertThat( documentation ).contains(
174174
"<h3 id=\"org-eclipse-esmf-test-AspectWithConstraintWithSeeAttribute-org-eclipse-esmf-test-testPropertyTwo-property"

0 commit comments

Comments
 (0)