Skip to content

Commit 8d753c6

Browse files
committed
Update tests and refactoring
1 parent 5258e3c commit 8d753c6

File tree

4 files changed

+68
-27
lines changed

4 files changed

+68
-27
lines changed

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import java.util.List;
1818
import java.util.regex.Matcher;
1919
import java.util.regex.Pattern;
20+
import java.util.stream.Collectors;
2021

2122
/**
2223
* Utility class for extracting and rendering structured content blocks (such as NOTE, EXAMPLE, SOURCE)
@@ -88,13 +89,13 @@ public static String toHtml( final String description ) {
8889
* <p>Each block is expected to begin with a {@code > TYPE:} line and may span multiple lines,
8990
* each of which begins with {@code >}.
9091
*
91-
* @param descriptions A set of multi-line Markdown description strings.
92+
* @param description A line Markdown description string.
9293
* @param type The type of block to extract ("NOTE", "EXAMPLE", or "SOURCE").
9394
* @return A list of extracted block contents for the specified type.
9495
*/
95-
private static List<String> extractBlock( final String descriptions, final String type ) {
96+
private static List<String> extractBlock( final String description, final String type ) {
9697
List<String> result = new ArrayList<>();
97-
extractFromDescription( descriptions, type, result );
98+
extractFromDescription( stripIndent( description ), type, result );
9899
return result;
99100
}
100101

@@ -141,5 +142,17 @@ private static void flushBlock( boolean[] insideBlock, StringBuilder blockConten
141142
insideBlock[0] = false;
142143
}
143144
}
145+
146+
static String stripIndent( final String string ) {
147+
final int indent = string.lines()
148+
.filter( line -> !line.isEmpty() )
149+
.map( line -> line.indexOf( line.trim() ) )
150+
.filter( offset -> offset > 0 )
151+
.min( Integer::compareTo )
152+
.orElse( 0 );
153+
return string.lines()
154+
.map( line -> indent <= line.length() ? line.substring( indent ) : line )
155+
.collect( Collectors.joining( "\n" ) );
156+
}
144157
}
145158

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,12 @@ void testHtmlOutputDoesNotContainMarkdownSyntax() {
132132
String html = DescriptionsUtils.toHtml( testDescription );
133133
assertThat( html ).doesNotContain( "[Visit Example](https://example.com)" );
134134
}
135+
136+
@Test
137+
void testStripIndentSingleLine() {
138+
String input = " only one line";
139+
String expected = "only one line";
140+
String result = DescriptionsUtils.stripIndent( input );
141+
assertEquals( expected, result );
142+
}
135143
}

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@
2020
import java.io.IOException;
2121
import java.io.PrintStream;
2222

23+
import org.eclipse.esmf.aspectmodel.utils.DescriptionsUtils;
2324
import org.eclipse.esmf.metamodel.Aspect;
2425
import org.eclipse.esmf.test.TestAspect;
2526
import org.eclipse.esmf.test.TestResources;
2627

28+
import org.assertj.core.api.AssertionsForClassTypes;
2729
import org.junit.jupiter.api.Test;
2830
import org.junit.jupiter.params.ParameterizedTest;
2931
import org.junit.jupiter.params.provider.EnumSource;
@@ -179,6 +181,24 @@ void testAspectWithConstraintWithSeeAttribute() throws IOException {
179181
"<li>http://example.com/me2</li>" );
180182
}
181183

184+
@Test
185+
void testMarkdownRenderingWithLink() throws IOException {
186+
final String htmlResult = generateHtmlDocumentation( TestAspect.ASPECT_WITH_MARKDOWN_DESCRIPTION );
187+
AssertionsForClassTypes.assertThat( htmlResult ).contains( "<a href=\"https://example.com\">Visit Example</a>" );
188+
}
189+
190+
@Test
191+
void testAspectWithMarkdownDescription() throws IOException {
192+
final String htmlResult = generateHtmlDocumentation( TestAspect.ASPECT_WITH_MARKDOWN_DESCRIPTION );
193+
AssertionsForClassTypes.assertThat( htmlResult ).doesNotContain( "[link](https://www.example.com/spec)" );
194+
}
195+
196+
@Test
197+
void testHtmlOutputDoesNotContainMarkdownSyntax() throws IOException {
198+
final String htmlResult = generateHtmlDocumentation( TestAspect.ASPECT_WITH_MARKDOWN_DESCRIPTION );
199+
AssertionsForClassTypes.assertThat( htmlResult ).doesNotContain( "[Visit Example](https://example.com)" );
200+
}
201+
182202
private String generateHtmlDocumentation( final TestAspect testAspect ) throws IOException {
183203
final Aspect aspect = TestResources.load( testAspect ).aspect();
184204
final AspectModelDocumentationGenerator aspectModelDocumentationGenerator = new AspectModelDocumentationGenerator( aspect );

core/esmf-test-aspect-models/src/main/resources/valid/org.eclipse.esmf.test/1.0.0/AspectWithMarkdownDescription.ttl

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,38 +14,38 @@
1414
@prefix samm-c: <urn:samm:org.eclipse.esmf.samm:characteristic:2.2.0#> .
1515

1616
:AspectWithMarkdownDescription a samm:Aspect ;
17-
samm:properties ( :myProperty ) ;
18-
samm:operations ( ) .
17+
samm:properties ( :myProperty ) ;
18+
samm:operations ( ) .
1919

2020
:myProperty a samm:Property ;
21-
samm:description """
22-
This is a sample concept demonstrating **Markdown** support in samm:description.
21+
samm:description """
22+
This is a sample concept demonstrating **Markdown** support in samm:description.
2323
24-
> NOTE: This is a note block.
25-
> It supports multiple lines.
26-
> Here's a second line of the note.
24+
> NOTE: This is a note block.
25+
> It supports multiple lines.
26+
> Here's a second line of the note.
2727
28-
> EXAMPLE 1: This is the first example block.
29-
> It can span several lines, and supports *italic* and **bold** text.
28+
> EXAMPLE 1: This is the first example block.
29+
> It can span several lines, and supports *italic* and **bold** text.
3030
31-
> EXAMPLE 2: This is the second example.
32-
> Also multiline, for testing multiple example entries.
31+
> EXAMPLE 2: This is the second example.
32+
> Also multiline, for testing multiple example entries.
3333
34-
> SOURCE: ISO 12345:2023, section 4.2.1
35-
> with an inline [link](https://www.example.com/spec).
34+
> SOURCE: ISO 12345:2023, section 4.2.1
35+
> with an inline [link](https://www.example.com/spec).
3636
37-
Unordered list:
38-
* Item A
39-
* Item B
40-
* Item C
37+
Unordered list:
38+
* Item A
39+
* Item B
40+
* Item C
4141
42-
Ordered list:
43-
1. First
44-
2. Second
45-
3. Third
42+
Ordered list:
43+
1. First
44+
2. Second
45+
3. Third
4646
47-
You can also include inline links like [Visit Example](https://example.com).
47+
You can also include inline links like [Visit Example](https://example.com).
4848
49-
Another paragraph after a blank line to simulate text flow and paragraph breaks.
50-
"""@en ;
49+
Another paragraph after a blank line to simulate text flow and paragraph breaks.
50+
"""@en ;
5151
samm:characteristic samm-c:Text .

0 commit comments

Comments
 (0)