Skip to content

Commit

Permalink
Use triple quotes for multi line literals on all platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
atextor committed Dec 10, 2024
1 parent 0072d12 commit 0c29d85
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ private String serialize( final RDFNode rdfNode, final int indentationLevel ) {
private String quoteValue( final String value ) {
final int approximateSize = value.length() <= 50 ? 64 : 256;
final StringBuilder buffer = new StringBuilder( approximateSize );
if ( value.contains( System.lineSeparator() ) ) {
if ( value.contains( "\n" ) ) {
buffer.append( TRIPLE_QUOTE );
final String[] lines = value.split( LINE_BREAK );
for ( int i = 0; i < lines.length; i++ ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@

import static org.assertj.core.api.Assertions.assertThat;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
Expand All @@ -32,6 +30,7 @@
import org.eclipse.esmf.test.TestResources;

import org.apache.jena.rdf.model.Model;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
Expand All @@ -42,17 +41,27 @@ public class PrettyPrinterTest {
void testPrettyPrinter( final TestModel testModel ) {
final AspectModel aspectModel = TestResources.load( testModel );
final AspectModelFile originalFile = aspectModel.files().iterator().next();
final String formattedModel = formatAspectModelFile( originalFile );
final Model prettyPrintedModel = TurtleLoader.loadTurtle( formattedModel ).get();
assertThat( RdfComparison.hash( originalFile.sourceModel() ).equals( RdfComparison.hash( prettyPrintedModel ) ) ).isTrue();
}

@Test
void testPrintMultiLineDescription() {
final AspectModel aspectModel = TestResources.load( TestAspect.ASPECT_WITH_MULTI_LINE_DESCRIPTION );
final AspectModelFile originalFile = aspectModel.files().iterator().next();
final String formattedModel = formatAspectModelFile( originalFile );
assertThat( formattedModel ).contains( "\"\"\"" );
final Model prettyPrintedModel = TurtleLoader.loadTurtle( formattedModel ).get();
assertThat( RdfComparison.hash( originalFile.sourceModel() ).equals( RdfComparison.hash( prettyPrintedModel ) ) ).isTrue();
}

private static String formatAspectModelFile( final AspectModelFile originalFile ) {
final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
final PrintWriter writer = new PrintWriter( buffer, false, StandardCharsets.UTF_8 );
new PrettyPrinter( originalFile, writer ).print();
writer.flush();

final InputStream bufferInput = new ByteArrayInputStream( buffer.toByteArray() );
final String formattedModel = buffer.toString( StandardCharsets.UTF_8 );
final Model prettyPrintedModel = TurtleLoader.loadTurtle( formattedModel ).get();

assertThat( RdfComparison.hash( originalFile.sourceModel() ).equals( RdfComparison.hash( prettyPrintedModel ) ) ).isTrue();
return buffer.toString( StandardCharsets.UTF_8 );
}

static Stream<Arguments> testModels() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ public enum TestAspect implements TestModel {
ASPECT_WITH_ENUMERATION_WITH_SEE_ATTRIBUTE,
ASPECT_WITH_ENUM_AND_OPTIONAL_ENUM_PROPERTIES,
ASPECT_WITH_ENUM_HAVING_NESTED_ENTITIES,
ASPECT_WITH_ENUM_ONLY_ONE_SEE,
ASPECT_WITH_ERROR_COLLECTION,
ASPECT_WITH_EVENT,
ASPECT_WITH_EXCLUSIVE_RANGE_CONSTRAINT,
Expand Down Expand Up @@ -124,10 +125,11 @@ public enum TestAspect implements TestModel {
ASPECT_WITH_MULTIPLE_ENUMERATIONS_ON_MULTIPLE_LEVELS,
ASPECT_WITH_MULTIPLE_SEE_ATTRIBUTES,
ASPECT_WITH_MULTI_LANGUAGE_TEXT,
ASPECT_WITH_MULTI_LINE_DESCRIPTION,
ASPECT_WITH_NAMESPACE_DESCRIPTION,
ASPECT_WITH_NESTED_ENTITY,
ASPECT_WITH_NESTED_ENTITY_LIST,
ASPECT_WITH_NESTED_ENTITY_ENUMERATION_WITH_NOT_IN_PAYLOAD,
ASPECT_WITH_NESTED_ENTITY_LIST,
ASPECT_WITH_NESTED_ENTITY_LIST_ENUMERATION_WITH_NOT_IN_PAYLOAD,
ASPECT_WITH_NUMERIC_REGULAR_EXPRESSION_CONSTRAINT,
ASPECT_WITH_NUMERIC_STRUCTURED_VALUE,
Expand Down Expand Up @@ -191,7 +193,6 @@ public enum TestAspect implements TestModel {
ENTITY_INSTANCE_TEST2,
ENTITY_INSTANCE_TEST3,
ENTITY_INSTANCE_TEST4,
ASPECT_WITH_ENUM_ONLY_ONE_SEE,

MODEL_WITH_BROKEN_CYCLES,

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Copyright (c) 2024 Robert Bosch Manufacturing Solutions GmbH
#
# See the AUTHORS file(s) distributed with this work for additional
# information regarding authorship.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
#
# SPDX-License-Identifier: MPL-2.0

@prefix : <urn:samm:org.eclipse.esmf.test:1.0.0#> .
@prefix samm: <urn:samm:org.eclipse.esmf.samm:meta-model:2.1.0#> .
@prefix samm-c: <urn:samm:org.eclipse.esmf.samm:characteristic:2.1.0#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

:AspectWithMultiLineDescription a samm:Aspect ;
samm:description """This
is
a
test"""@en ;
samm:properties ( ) ;
samm:operations ( ) .

0 comments on commit 0c29d85

Please sign in to comment.