Skip to content

Commit 06ea883

Browse files
markpollacksobychacko
authored andcommitted
Improve support for running tests on windows
Signed-off-by: [email protected] Signed-off-by: Mark Pollack <[email protected]>
1 parent 9774b4e commit 06ea883

File tree

4 files changed

+182
-69
lines changed

4 files changed

+182
-69
lines changed

spring-ai-commons/src/test/java/org/springframework/ai/document/ContentFormatterTests.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@
2525
/**
2626
* @author Christian Tzolov
2727
*/
28-
public class ContentFormatterTests {
28+
class ContentFormatterTests {
2929

3030
Document document = new Document("The World is Big and Salvation Lurks Around the Corner",
3131
Map.of("embedKey1", "value1", "embedKey2", "value2", "embedKey3", "value3", "llmKey2", "value4"));
3232

3333
@Test
34-
public void noExplicitlySetFormatter() {
35-
assertThat(this.document.getText()).isEqualTo("""
34+
void noExplicitlySetFormatter() {
35+
TextBlockAssertion.assertThat(this.document.getText()).isEqualTo("""
3636
The World is Big and Salvation Lurks Around the Corner""");
3737

3838
assertThat(this.document.getFormattedContent()).isEqualTo(this.document.getFormattedContent(MetadataMode.ALL));
@@ -42,17 +42,18 @@ public void noExplicitlySetFormatter() {
4242
}
4343

4444
@Test
45-
public void defaultConfigTextFormatter() {
45+
void defaultConfigTextFormatter() {
4646

4747
DefaultContentFormatter defaultConfigFormatter = DefaultContentFormatter.defaultConfig();
4848

49-
assertThat(this.document.getFormattedContent(defaultConfigFormatter, MetadataMode.ALL)).isEqualTo("""
50-
llmKey2: value4
51-
embedKey1: value1
52-
embedKey2: value2
53-
embedKey3: value3
49+
TextBlockAssertion.assertThat(this.document.getFormattedContent(defaultConfigFormatter, MetadataMode.ALL))
50+
.isEqualTo("""
51+
llmKey2: value4
52+
embedKey1: value1
53+
embedKey2: value2
54+
embedKey3: value3
5455
55-
The World is Big and Salvation Lurks Around the Corner""");
56+
The World is Big and Salvation Lurks Around the Corner""");
5657

5758
assertThat(this.document.getFormattedContent(defaultConfigFormatter, MetadataMode.ALL))
5859
.isEqualTo(this.document.getFormattedContent());
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright 2024-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.ai.document;
18+
19+
import java.util.Arrays;
20+
21+
import org.assertj.core.api.AbstractCharSequenceAssert;
22+
import org.assertj.core.api.Assertions;
23+
24+
public class TextBlockAssertion extends AbstractCharSequenceAssert<TextBlockAssertion, String> {
25+
26+
protected TextBlockAssertion(String string) {
27+
super(string, TextBlockAssertion.class);
28+
}
29+
30+
public static TextBlockAssertion assertThat(String actual) {
31+
return new TextBlockAssertion(actual);
32+
}
33+
34+
@Override
35+
public TextBlockAssertion isEqualTo(Object expected) {
36+
Assertions.assertThat(normalizedEOL(actual)).isEqualTo(normalizedEOL((String) expected));
37+
return this;
38+
}
39+
40+
@Override
41+
public TextBlockAssertion contains(CharSequence... values) {
42+
Assertions.assertThat(normalizedEOL(actual)).contains(normalizedEOL(values));
43+
return this;
44+
}
45+
46+
private String normalizedEOL(CharSequence... values) {
47+
return Arrays.stream(values).map(CharSequence::toString).map(this::normalizedEOL).reduce("", (a, b) -> a + b);
48+
}
49+
50+
private String normalizedEOL(String line) {
51+
return line.replaceAll("\r\n|\r|\n", System.lineSeparator());
52+
}
53+
54+
}

spring-ai-model/src/test/java/org/springframework/ai/converter/BeanOutputConverterTest.java

Lines changed: 63 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.mockito.junit.jupiter.MockitoExtension;
3939
import org.slf4j.LoggerFactory;
4040

41+
import org.springframework.ai.util.TextBlockAssertion;
4142
import org.springframework.core.ParameterizedTypeReference;
4243

4344
import static org.assertj.core.api.Assertions.assertThat;
@@ -246,83 +247,86 @@ class FormatTest {
246247
@Test
247248
void formatClassType() {
248249
var converter = new BeanOutputConverter<>(TestClass.class);
249-
assertThat(converter.getFormat()).isEqualTo(
250-
"""
251-
Your response should be in JSON format.
252-
Do not include any explanations, only provide a RFC8259 compliant JSON response following this format without deviation.
253-
Do not include markdown code blocks in your response.
254-
Remove the ```json markdown from the output.
255-
Here is the JSON Schema instance your output must adhere to:
256-
```{
257-
"$schema" : "https://json-schema.org/draft/2020-12/schema",
258-
"type" : "object",
259-
"properties" : {
260-
"someString" : {
261-
"type" : "string"
262-
}
263-
},
264-
"additionalProperties" : false
265-
}```
266-
""");
250+
TextBlockAssertion.assertThat(converter.getFormat())
251+
.isEqualTo(
252+
"""
253+
Your response should be in JSON format.
254+
Do not include any explanations, only provide a RFC8259 compliant JSON response following this format without deviation.
255+
Do not include markdown code blocks in your response.
256+
Remove the ```json markdown from the output.
257+
Here is the JSON Schema instance your output must adhere to:
258+
```{
259+
"$schema" : "https://json-schema.org/draft/2020-12/schema",
260+
"type" : "object",
261+
"properties" : {
262+
"someString" : {
263+
"type" : "string"
264+
}
265+
},
266+
"additionalProperties" : false
267+
}```
268+
""");
267269
}
268270

269271
@Test
270272
void formatTypeReference() {
271273
var converter = new BeanOutputConverter<>(new ParameterizedTypeReference<TestClass>() {
272274

273275
});
274-
assertThat(converter.getFormat()).isEqualTo(
275-
"""
276-
Your response should be in JSON format.
277-
Do not include any explanations, only provide a RFC8259 compliant JSON response following this format without deviation.
278-
Do not include markdown code blocks in your response.
279-
Remove the ```json markdown from the output.
280-
Here is the JSON Schema instance your output must adhere to:
281-
```{
282-
"$schema" : "https://json-schema.org/draft/2020-12/schema",
283-
"type" : "object",
284-
"properties" : {
285-
"someString" : {
286-
"type" : "string"
287-
}
288-
},
289-
"additionalProperties" : false
290-
}```
291-
""");
276+
TextBlockAssertion.assertThat(converter.getFormat())
277+
.isEqualTo(
278+
"""
279+
Your response should be in JSON format.
280+
Do not include any explanations, only provide a RFC8259 compliant JSON response following this format without deviation.
281+
Do not include markdown code blocks in your response.
282+
Remove the ```json markdown from the output.
283+
Here is the JSON Schema instance your output must adhere to:
284+
```{
285+
"$schema" : "https://json-schema.org/draft/2020-12/schema",
286+
"type" : "object",
287+
"properties" : {
288+
"someString" : {
289+
"type" : "string"
290+
}
291+
},
292+
"additionalProperties" : false
293+
}```
294+
""");
292295
}
293296

294297
@Test
295298
void formatTypeReferenceArray() {
296299
var converter = new BeanOutputConverter<>(new ParameterizedTypeReference<List<TestClass>>() {
297300

298301
});
299-
assertThat(converter.getFormat()).isEqualTo(
300-
"""
301-
Your response should be in JSON format.
302-
Do not include any explanations, only provide a RFC8259 compliant JSON response following this format without deviation.
303-
Do not include markdown code blocks in your response.
304-
Remove the ```json markdown from the output.
305-
Here is the JSON Schema instance your output must adhere to:
306-
```{
307-
"$schema" : "https://json-schema.org/draft/2020-12/schema",
308-
"type" : "array",
309-
"items" : {
310-
"type" : "object",
311-
"properties" : {
312-
"someString" : {
313-
"type" : "string"
314-
}
315-
},
316-
"additionalProperties" : false
317-
}
318-
}```
319-
""");
302+
TextBlockAssertion.assertThat(converter.getFormat())
303+
.isEqualTo(
304+
"""
305+
Your response should be in JSON format.
306+
Do not include any explanations, only provide a RFC8259 compliant JSON response following this format without deviation.
307+
Do not include markdown code blocks in your response.
308+
Remove the ```json markdown from the output.
309+
Here is the JSON Schema instance your output must adhere to:
310+
```{
311+
"$schema" : "https://json-schema.org/draft/2020-12/schema",
312+
"type" : "array",
313+
"items" : {
314+
"type" : "object",
315+
"properties" : {
316+
"someString" : {
317+
"type" : "string"
318+
}
319+
},
320+
"additionalProperties" : false
321+
}
322+
}```
323+
""");
320324
}
321325

322326
@Test
323327
void formatClassTypeWithAnnotations() {
324328
var converter = new BeanOutputConverter<>(TestClassWithJsonAnnotations.class);
325-
assertThat(converter.getFormat()).contains("""
329+
TextBlockAssertion.assertThat(converter.getFormat()).contains("""
326330
```{
327331
"$schema" : "https://json-schema.org/draft/2020-12/schema",
328332
"type" : "object",
@@ -342,7 +346,7 @@ void formatTypeReferenceWithAnnotations() {
342346
var converter = new BeanOutputConverter<>(new ParameterizedTypeReference<TestClassWithJsonAnnotations>() {
343347

344348
});
345-
assertThat(converter.getFormat()).contains("""
349+
TextBlockAssertion.assertThat(converter.getFormat()).contains("""
346350
```{
347351
"$schema" : "https://json-schema.org/draft/2020-12/schema",
348352
"type" : "object",
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright 2024-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.ai.util;
18+
19+
import java.util.Arrays;
20+
21+
import org.assertj.core.api.AbstractCharSequenceAssert;
22+
import org.assertj.core.api.Assertions;
23+
24+
public class TextBlockAssertion extends AbstractCharSequenceAssert<TextBlockAssertion, String> {
25+
26+
protected TextBlockAssertion(String string) {
27+
super(string, TextBlockAssertion.class);
28+
}
29+
30+
public static TextBlockAssertion assertThat(String actual) {
31+
return new TextBlockAssertion(actual);
32+
}
33+
34+
@Override
35+
public TextBlockAssertion isEqualTo(Object expected) {
36+
Assertions.assertThat(normalizedEOL(actual)).isEqualTo(normalizedEOL((String) expected));
37+
return this;
38+
}
39+
40+
@Override
41+
public TextBlockAssertion contains(CharSequence... values) {
42+
Assertions.assertThat(normalizedEOL(actual)).contains(normalizedEOL(values));
43+
return this;
44+
}
45+
46+
private String normalizedEOL(CharSequence... values) {
47+
return Arrays.stream(values).map(CharSequence::toString).map(this::normalizedEOL).reduce("", (a, b) -> a + b);
48+
}
49+
50+
private String normalizedEOL(String line) {
51+
return line.replaceAll("\r\n|\r|\n", System.lineSeparator());
52+
}
53+
54+
}

0 commit comments

Comments
 (0)