Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Windows line separation. #949

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.springframework.ai.assertj;

import org.assertj.core.api.AbstractAssert;
import org.assertj.core.api.AbstractCharSequenceAssert;
import org.assertj.core.api.Assertions;

import java.util.Arrays;

public class TextBlockAssertion extends AbstractCharSequenceAssert<TextBlockAssertion, String> {

protected TextBlockAssertion(String string) {
super(string, TextBlockAssertion.class);
}

public static TextBlockAssertion assertThat(String actual) {
return new TextBlockAssertion(actual);
}

@Override
public TextBlockAssertion isEqualTo(Object expected) {
Assertions.assertThat(normalizedEOL(actual)).isEqualTo(normalizedEOL((String) expected));
return this;
}

@Override
public TextBlockAssertion contains(CharSequence... values) {
Assertions.assertThat(normalizedEOL(actual)).contains(normalizedEOL(values));
return this;
}

private String normalizedEOL(CharSequence... values) {
return Arrays.stream(values).map(CharSequence::toString).map(this::normalizedEOL).reduce("", (a, b) -> a + b);
}

private String normalizedEOL(String line) {
return line.replaceAll("\r\n|\r|\n", System.lineSeparator());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import org.springframework.ai.assertj.TextBlockAssertion;
import org.springframework.core.ParameterizedTypeReference;

import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -111,78 +112,81 @@ class FormatTest {
@Test
public void formatClassType() {
var converter = new BeanOutputConverter<>(TestClass.class);
assertThat(converter.getFormat()).isEqualTo(
"""
Your response should be in JSON format.
Do not include any explanations, only provide a RFC8259 compliant JSON response following this format without deviation.
Do not include markdown code blocks in your response.
Remove the ```json markdown from the output.
Here is the JSON Schema instance your output must adhere to:
```{
"$schema" : "https://json-schema.org/draft/2020-12/schema",
"type" : "object",
"properties" : {
"someString" : {
"type" : "string"
}
}
}```
""");
TextBlockAssertion.assertThat(converter.getFormat())
.isEqualTo(
"""
Your response should be in JSON format.
Do not include any explanations, only provide a RFC8259 compliant JSON response following this format without deviation.
Do not include markdown code blocks in your response.
Remove the ```json markdown from the output.
Here is the JSON Schema instance your output must adhere to:
```{
"$schema" : "https://json-schema.org/draft/2020-12/schema",
"type" : "object",
"properties" : {
"someString" : {
"type" : "string"
}
}
}```
""");
}

@Test
public void formatTypeReference() {
var converter = new BeanOutputConverter<>(new ParameterizedTypeReference<TestClass>() {
});
assertThat(converter.getFormat()).isEqualTo(
"""
Your response should be in JSON format.
Do not include any explanations, only provide a RFC8259 compliant JSON response following this format without deviation.
Do not include markdown code blocks in your response.
Remove the ```json markdown from the output.
Here is the JSON Schema instance your output must adhere to:
```{
"$schema" : "https://json-schema.org/draft/2020-12/schema",
"type" : "object",
"properties" : {
"someString" : {
"type" : "string"
}
}
}```
""");
TextBlockAssertion.assertThat(converter.getFormat())
.isEqualTo(
"""
Your response should be in JSON format.
Do not include any explanations, only provide a RFC8259 compliant JSON response following this format without deviation.
Do not include markdown code blocks in your response.
Remove the ```json markdown from the output.
Here is the JSON Schema instance your output must adhere to:
```{
"$schema" : "https://json-schema.org/draft/2020-12/schema",
"type" : "object",
"properties" : {
"someString" : {
"type" : "string"
}
}
}```
""");
}

@Test
public void formatTypeReferenceArray() {
var converter = new BeanOutputConverter<>(new ParameterizedTypeReference<List<TestClass>>() {
});
assertThat(converter.getFormat()).isEqualTo(
"""
Your response should be in JSON format.
Do not include any explanations, only provide a RFC8259 compliant JSON response following this format without deviation.
Do not include markdown code blocks in your response.
Remove the ```json markdown from the output.
Here is the JSON Schema instance your output must adhere to:
```{
"$schema" : "https://json-schema.org/draft/2020-12/schema",
"type" : "array",
"items" : {
"type" : "object",
"properties" : {
"someString" : {
"type" : "string"
}
}
}
}```
""");
TextBlockAssertion.assertThat(converter.getFormat())
.isEqualTo(
"""
Your response should be in JSON format.
Do not include any explanations, only provide a RFC8259 compliant JSON response following this format without deviation.
Do not include markdown code blocks in your response.
Remove the ```json markdown from the output.
Here is the JSON Schema instance your output must adhere to:
```{
"$schema" : "https://json-schema.org/draft/2020-12/schema",
"type" : "array",
"items" : {
"type" : "object",
"properties" : {
"someString" : {
"type" : "string"
}
}
}
}```
""");
}

@Test
public void formatClassTypeWithAnnotations() {
var converter = new BeanOutputConverter<>(TestClassWithJsonAnnotations.class);
assertThat(converter.getFormat()).contains("""
TextBlockAssertion.assertThat(converter.getFormat()).contains("""
```{
"$schema" : "https://json-schema.org/draft/2020-12/schema",
"type" : "object",
Expand All @@ -200,7 +204,7 @@ public void formatClassTypeWithAnnotations() {
public void formatTypeReferenceWithAnnotations() {
var converter = new BeanOutputConverter<>(new ParameterizedTypeReference<TestClassWithJsonAnnotations>() {
});
assertThat(converter.getFormat()).contains("""
TextBlockAssertion.assertThat(converter.getFormat()).contains("""
```{
"$schema" : "https://json-schema.org/draft/2020-12/schema",
"type" : "object",
Expand All @@ -221,7 +225,7 @@ void normalizesLineEndingsClassType() {
String formatOutput = converter.getFormat();

// validate that output contains \n line endings
assertThat(formatOutput).contains(System.lineSeparator()).doesNotContain("\r\n").doesNotContain("\r");
TextBlockAssertion.assertThat(formatOutput).contains(System.lineSeparator());
}

@Test
Expand All @@ -232,7 +236,7 @@ void normalizesLineEndingsTypeReference() {
String formatOutput = converter.getFormat();

// validate that output contains \n line endings
assertThat(formatOutput).contains(System.lineSeparator()).doesNotContain("\r\n").doesNotContain("\r");
TextBlockAssertion.assertThat(formatOutput).contains(System.lineSeparator());
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.Map;

import org.junit.jupiter.api.Test;
import org.springframework.ai.assertj.TextBlockAssertion;

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

Expand Down Expand Up @@ -45,13 +46,14 @@ public void defaultConfigTextFormatter() {

DefaultContentFormatter defaultConfigFormatter = DefaultContentFormatter.defaultConfig();

assertThat(document.getFormattedContent(defaultConfigFormatter, MetadataMode.ALL)).isEqualTo("""
llmKey2: value4
embedKey1: value1
embedKey2: value2
embedKey3: value3
TextBlockAssertion.assertThat(document.getFormattedContent(defaultConfigFormatter, MetadataMode.ALL))
.isEqualTo("""
llmKey2: value4
embedKey1: value1
embedKey2: value2
embedKey3: value3

The World is Big and Salvation Lurks Around the Corner""");
The World is Big and Salvation Lurks Around the Corner""");

assertThat(document.getFormattedContent(defaultConfigFormatter, MetadataMode.ALL))
.isEqualTo(document.getFormattedContent());
Expand All @@ -70,7 +72,7 @@ public void customTextFormatter() {
.withMetadataTemplate("Key/Value {key}={value}")
.build();

assertThat(document.getFormattedContent(textFormatter, MetadataMode.EMBED)).isEqualTo("""
TextBlockAssertion.assertThat(document.getFormattedContent(textFormatter, MetadataMode.EMBED)).isEqualTo("""
Metadata:
Key/Value llmKey2=value4
Key/Value embedKey1=value1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.springframework.ai.assertj.TextBlockAssertion;
import org.springframework.ai.chat.messages.Message;
import org.springframework.ai.chat.prompt.PromptTemplate;
import org.springframework.core.io.InputStreamResource;
Expand Down Expand Up @@ -49,7 +50,7 @@ public void testRenderWithList() {
// don't normalize EOLs.
// It should be fine on Unix systems. In addition, Git will replace CRLF by LF by
// default.
assertEqualsWithNormalizedEOLs(expected, message.getContent());
TextBlockAssertion.assertThat(expected).isEqualTo(message.getContent());

PromptTemplate unfilledPromptTemplate = new PromptTemplate(templateString);
assertThatExceptionOfType(IllegalStateException.class).isThrownBy(unfilledPromptTemplate::render)
Expand Down Expand Up @@ -147,9 +148,4 @@ public void testRenderFailure() {
assertThrows(IllegalStateException.class, promptTemplate::render);
}

private static void assertEqualsWithNormalizedEOLs(String expected, String actual) {
assertEquals(expected.replaceAll("\\r\\n|\\r|\\n", System.lineSeparator()),
actual.replaceAll("\\r\\n|\\r|\\n", System.lineSeparator()));
}

}