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

Refactor codegen unit test #884

Merged
merged 9 commits into from
Sep 4, 2024
Merged
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,43 @@
package run.mone.ai.codegen.util;

import lombok.extern.slf4j.Slf4j;

/**
* @author zhangxiaowei6
* @Date 2024/9/4 16:31
*/

@Slf4j
public class StrUtil {

public static String toCamelCase(String input) {
if (input == null || input.isEmpty()) {
return input;
}

StringBuilder result = new StringBuilder();
boolean capitalizeNext = false;

for (int i = 0; i < input.length(); i++) {
char currentChar = input.charAt(i);

if (currentChar == ' ') {
capitalizeNext = true;
} else {
if (capitalizeNext) {
result.append(Character.toUpperCase(currentChar));
capitalizeNext = false;
} else {
result.append(Character.toLowerCase(currentChar));
}
}
}

// 确保第一个字符是小写的
if (result.length() > 0) {
result.setCharAt(0, Character.toLowerCase(result.charAt(0)));
}

return result.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static String renderTemplate(String template, Map<String, ? extends Objec
}
StringTemplateResourceLoader resourceLoader = new StringTemplateResourceLoader();
Configuration cfg = Configuration.defaultConfiguration();
GroupTemplate gt = new GroupTemplate(resourceLoader, cfg);
GroupTemplate gt = new GroupTemplate(resourceLoader, cfg, cfg.getClass().getClassLoader());
functionList.forEach(it -> gt.registerFunction(it.getKey(), it.getValue()));
Template t = gt.getTemplate(template);
m.forEach((k, v) -> t.binding(k, v));
Expand Down
4 changes: 2 additions & 2 deletions jcommon/codegen/src/main/resources/tlp/testSpring.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
public class ${testName} {


@Resource
private ${serviceName} ${strutil.toLowerCase(serviceName)}Service;
@Resource
private ${serviceName} ${className}Service;


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.xiaomi.youpin.codegen.test;

import org.junit.Test;
import run.mone.ai.codegen.util.StrUtil;

import static org.junit.Assert.assertEquals;


public class StrTest {




@Test
public void testToCamelCase() {
String input = "hello world";
String expected = "helloWorld";
String actual = StrUtil.toCamelCase(input);
assertEquals(expected, actual);

input = "java programming language";
expected = "javaProgrammingLanguage";
actual = StrUtil.toCamelCase(input);
assertEquals(expected, actual);

input = " leading and trailing spaces ";
expected = "leadingAndTrailingSpaces";
actual = StrUtil.toCamelCase(input);
assertEquals(expected, actual);

input = "";
expected = "";
actual = StrUtil.toCamelCase(input);
assertEquals(expected, actual);

input = null;
expected = null;
actual = StrUtil.toCamelCase(input);
assertEquals(expected, actual);
}


}
Loading