Skip to content

Commit d8b4036

Browse files
committed
WIP
1 parent cac1a53 commit d8b4036

File tree

3 files changed

+63
-60
lines changed

3 files changed

+63
-60
lines changed

docstring/src/main/java/io/cucumber/docstring/DocStringTypeRegistryDocStringConverter.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,14 @@ public <T> T convert(DocString docString, Type targetType) {
6060
private List<String> suggestedContentTypes(List<DocStringType> docStringTypes) {
6161
return docStringTypes.stream()
6262
.map(DocStringType::getContentType)
63-
// Can't use the anonymous content type to resolve
64-
// the ambiguity.
65-
.filter(contentType -> !contentType.isEmpty())
63+
.map(DocStringTypeRegistryDocStringConverter::emptyToAnonymous)
6664
.sorted()
6765
.distinct()
6866
.collect(Collectors.toList());
6967
}
7068

69+
private static String emptyToAnonymous(String contentType) {
70+
return contentType.isEmpty() ? "[anonymous]" : contentType;
71+
}
72+
7173
}

docstring/src/test/java/io/cucumber/docstring/DocStringTypeRegistryDocStringConverterTest.java

+47-54
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,42 @@
1010
import static org.hamcrest.MatcherAssert.assertThat;
1111
import static org.hamcrest.Matchers.equalTo;
1212
import static org.hamcrest.Matchers.equalToCompressingWhiteSpace;
13+
import static org.junit.jupiter.api.Assertions.assertAll;
1314
import static org.junit.jupiter.api.Assertions.assertThrows;
1415

1516
class DocStringTypeRegistryDocStringConverterTest {
1617

17-
private final DocStringTypeRegistry registry = new DocStringTypeRegistry();
18-
private final DocStringTypeRegistryDocStringConverter converter = new DocStringTypeRegistryDocStringConverter(
19-
registry);
18+
static final DocStringType jsonNodeForJson = new DocStringType(
19+
JsonNode.class,
20+
"json",
21+
(String s) -> new ObjectMapper().readTree(s));
22+
static final DocStringType stringForText = new DocStringType(
23+
String.class,
24+
"text",
25+
(String s) -> s);
26+
static final DocStringType stringForXml = new DocStringType(
27+
String.class,
28+
"xml",
29+
(String s) -> s);
30+
static final DocStringType stringForYaml = new DocStringType(
31+
String.class,
32+
"yml",
33+
(String s) -> s);
34+
public static final DocStringType jsonNodeForXml = new DocStringType(
35+
JsonNode.class,
36+
"xml",
37+
(String s) -> new ObjectMapper().readTree(s));
38+
public static final DocStringType stringForJson = new DocStringType(
39+
String.class,
40+
"json",
41+
(String s) -> s);
42+
43+
final DocStringTypeRegistry registry = new DocStringTypeRegistry();
44+
final DocStringTypeRegistryDocStringConverter converter = new DocStringTypeRegistryDocStringConverter(registry);
2045

2146
@Test
2247
void throws_when_uses_doc_string_type_but_downcast_conversion() {
23-
registry.defineDocStringType(new DocStringType(
24-
JsonNode.class,
25-
"json",
26-
(String s) -> new ObjectMapper().readTree(s)));
48+
registry.defineDocStringType(jsonNodeForJson);
2749

2850
DocString docString = DocString.create(
2951
"{\"hello\":\"world\"}",
@@ -39,10 +61,7 @@ void throws_when_uses_doc_string_type_but_downcast_conversion() {
3961

4062
@Test
4163
void uses_target_type_when_available() {
42-
registry.defineDocStringType(new DocStringType(
43-
JsonNode.class,
44-
"json",
45-
(String s) -> new ObjectMapper().readTree(s)));
64+
registry.defineDocStringType(jsonNodeForJson);
4665

4766
DocString docString = DocString.create(
4867
"{\"hello\":\"world\"}");
@@ -62,10 +81,7 @@ void target_type_to_string_is_predefined() {
6281

6382
@Test
6483
void default_converter_is_used_if_registered_converter_does_not_match_type() {
65-
registry.defineDocStringType(new DocStringType(
66-
JsonNode.class,
67-
"json",
68-
(String s) -> new ObjectMapper().readTree(s)));
84+
registry.defineDocStringType(jsonNodeForJson);
6985

7086
DocString docString = DocString.create(
7187
"hello world", "json");
@@ -76,10 +92,7 @@ void default_converter_is_used_if_registered_converter_does_not_match_type() {
7692

7793
@Test
7894
void default_converter_never_conflicts_with_registered_converter_for_string() {
79-
registry.defineDocStringType(new DocStringType(
80-
String.class,
81-
"text",
82-
(String s) -> s));
95+
registry.defineDocStringType(stringForText);
8396

8497
DocString docString = DocString.create("hello world");
8598

@@ -89,25 +102,20 @@ void default_converter_never_conflicts_with_registered_converter_for_string() {
89102

90103
@Test
91104
void throws_if_converter_type_conflicts_with_type() {
92-
registry.defineDocStringType(new DocStringType(
93-
JsonNode.class,
94-
"json",
95-
(String s) -> new ObjectMapper().readTree(s)));
96-
97-
registry.defineDocStringType(new DocStringType(
98-
String.class,
99-
"text",
100-
(String s) -> s));
105+
registry.defineDocStringType(jsonNodeForJson);
106+
registry.defineDocStringType(stringForText);
101107

102108
DocString docString = DocString.create("hello world", "json");
103109

104110
CucumberDocStringException exception = assertThrows(
105111
CucumberDocStringException.class,
106112
() -> converter.convert(docString, String.class));
107113

114+
// TODO: This exception should explain,
115+
108116
assertThat(exception.getMessage(),
109117
is("Multiple converters found for type java.lang.String, and the content type 'json' " +
110-
"did not match any of the registered types [text]. Change the content type of the docstring " +
118+
"did not match any of the registered types [[anonymous], text]. Change the content type of the docstring " +
111119
"or register a docstring type for 'json'"));
112120
}
113121

@@ -187,15 +195,9 @@ void converts_no_content_type_doc_string_to_registered_matching_convertor() {
187195

188196
@Test
189197
void throws_when_multiple_convertors_available() {
190-
registry.defineDocStringType(new DocStringType(
191-
JsonNode.class,
192-
"json",
193-
(String s) -> new ObjectMapper().readTree(s)));
198+
registry.defineDocStringType(jsonNodeForJson);
194199

195-
registry.defineDocStringType(new DocStringType(
196-
JsonNode.class,
197-
"xml",
198-
(String s) -> new ObjectMapper().readTree(s)));
200+
registry.defineDocStringType(jsonNodeForXml);
199201

200202
DocString docString = DocString.create(
201203
"{\"hello\":\"world\"}");
@@ -211,20 +213,9 @@ void throws_when_multiple_convertors_available() {
211213

212214
@Test
213215
void different_docstring_content_types_convert_to_matching_doc_string_types() {
214-
registry.defineDocStringType(new DocStringType(
215-
String.class,
216-
"json",
217-
(String s) -> s));
218-
219-
registry.defineDocStringType(new DocStringType(
220-
String.class,
221-
"xml",
222-
(String s) -> s));
223-
224-
registry.defineDocStringType(new DocStringType(
225-
String.class,
226-
"yml",
227-
(String s) -> s));
216+
registry.defineDocStringType(stringForJson);
217+
registry.defineDocStringType(stringForXml);
218+
registry.defineDocStringType(stringForYaml);
228219

229220
DocString docStringJson = DocString.create(
230221
"{\"content\":\"hello world\"}", "json");
@@ -237,9 +228,11 @@ void different_docstring_content_types_convert_to_matching_doc_string_types() {
237228
String convertXml = converter.convert(docStringXml, String.class);
238229
String convertYml = converter.convert(docStringYml, String.class);
239230

240-
assertThat(docStringJson.getContent(), equalTo(convertJson));
241-
assertThat(docStringXml.getContent(), equalTo(convertXml));
242-
assertThat(docStringYml.getContent(), equalTo(convertYml));
231+
assertAll(
232+
() -> assertThat(docStringJson.getContent(), equalTo(convertJson)),
233+
() -> assertThat(docStringXml.getContent(), equalTo(convertXml)),
234+
() -> assertThat(docStringYml.getContent(), equalTo(convertYml))
235+
);
243236
}
244237

245238
@Test

java/src/main/java/io/cucumber/java/DocStringType.java

+11-3
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,22 @@
1010
/**
1111
* Register doc string type.
1212
* <p>
13-
* The name of the method is used as the content type of the
14-
* {@link io.cucumber.docstring.DocStringType}.
15-
* <p>
1613
* The method must have this signature:
1714
* <ul>
1815
* <li>{@code String -> Author}</li>
1916
* </ul>
2017
* NOTE: {@code Author} is an example of the type of the parameter type.
18+
* <p>
19+
* TODO: Explain how docstring types are selected.
20+
* TODO: Explain anonymous docstring type
21+
* TODO: Explain existence of default converter
22+
* First by content type and target type.
23+
* Then by target type.
24+
*
25+
*
26+
* NOTE: When not provided the name of the method is used as the content type of the {@link io.cucumber.docstring.DocStringType}.
27+
*
28+
*
2129
*
2230
* @see io.cucumber.docstring.DocStringType
2331
*/

0 commit comments

Comments
 (0)