Fix kebab and snake case formatting of all upper case words
Closes gh-658
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2019 the original author or authors.
|
||||
* Copyright 2014-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -16,9 +16,6 @@
|
||||
|
||||
package org.springframework.restdocs.snippet;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.springframework.restdocs.RestDocumentationContext;
|
||||
import org.springframework.util.PropertyPlaceholderHelper.PlaceholderResolver;
|
||||
|
||||
@@ -51,8 +48,6 @@ import org.springframework.util.PropertyPlaceholderHelper.PlaceholderResolver;
|
||||
*/
|
||||
public class RestDocumentationContextPlaceholderResolver implements PlaceholderResolver {
|
||||
|
||||
private static final Pattern CAMEL_CASE_PATTERN = Pattern.compile("([A-Z])");
|
||||
|
||||
private final RestDocumentationContext context;
|
||||
|
||||
/**
|
||||
@@ -130,14 +125,18 @@ public class RestDocumentationContextPlaceholderResolver implements PlaceholderR
|
||||
}
|
||||
|
||||
private String camelCaseToSeparator(String string, String separator) {
|
||||
Matcher matcher = CAMEL_CASE_PATTERN.matcher(string);
|
||||
StringBuffer result = new StringBuffer();
|
||||
while (matcher.find()) {
|
||||
String replacement = (matcher.start() > 0) ? separator + matcher.group(1).toLowerCase()
|
||||
: matcher.group(1).toLowerCase();
|
||||
matcher.appendReplacement(result, replacement);
|
||||
char[] chars = string.toCharArray();
|
||||
for (int i = 0; i < chars.length; i++) {
|
||||
char current = chars[i];
|
||||
if (Character.isUpperCase(current) && i > 0) {
|
||||
if (Character.isLowerCase(chars[i - 1])
|
||||
|| (i < chars.length - 1 && Character.isLowerCase(chars[i + 1]))) {
|
||||
result.append(separator);
|
||||
}
|
||||
}
|
||||
result.append(Character.toLowerCase(chars[i]));
|
||||
}
|
||||
matcher.appendTail(result);
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2019 the original author or authors.
|
||||
* Copyright 2014-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -38,12 +38,48 @@ public class RestDocumentationContextPlaceholderResolverTests {
|
||||
.isEqualTo("dash-separated-method-name");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void kebabCaseMethodNameWithUpperCaseOpeningSection() throws Exception {
|
||||
assertThat(createResolver("URIDashSeparatedMethodName").resolvePlaceholder("method-name"))
|
||||
.isEqualTo("uri-dash-separated-method-name");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void kebabCaseMethodNameWithUpperCaseMidSection() throws Exception {
|
||||
assertThat(createResolver("dashSeparatedMethodNameWithURIInIt").resolvePlaceholder("method-name"))
|
||||
.isEqualTo("dash-separated-method-name-with-uri-in-it");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void kebabCaseMethodNameWithUpperCaseEndSection() throws Exception {
|
||||
assertThat(createResolver("dashSeparatedMethodNameWithURI").resolvePlaceholder("method-name"))
|
||||
.isEqualTo("dash-separated-method-name-with-uri");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void snakeCaseMethodName() throws Exception {
|
||||
assertThat(createResolver("underscoreSeparatedMethodName").resolvePlaceholder("method_name"))
|
||||
.isEqualTo("underscore_separated_method_name");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void snakeCaseMethodNameWithUpperCaseOpeningSection() throws Exception {
|
||||
assertThat(createResolver("URIUnderscoreSeparatedMethodName").resolvePlaceholder("method_name"))
|
||||
.isEqualTo("uri_underscore_separated_method_name");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void snakeCaseMethodNameWithUpperCaseMidSection() throws Exception {
|
||||
assertThat(createResolver("underscoreSeparatedMethodNameWithURIInIt").resolvePlaceholder("method_name"))
|
||||
.isEqualTo("underscore_separated_method_name_with_uri_in_it");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void snakeCaseMethodNameWithUpperCaseEndSection() throws Exception {
|
||||
assertThat(createResolver("underscoreSeparatedMethodNameWithURI").resolvePlaceholder("method_name"))
|
||||
.isEqualTo("underscore_separated_method_name_with_uri");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void camelCaseMethodName() throws Exception {
|
||||
assertThat(createResolver("camelCaseMethodName").resolvePlaceholder("methodName"))
|
||||
|
||||
Reference in New Issue
Block a user