Skip to content

Commit

Permalink
Refactor codegen unit test (#884)
Browse files Browse the repository at this point in the history
* update

* refactor codegen

* change pom

* change infra pom

* change template

* refactor: fix template
  • Loading branch information
wodiwudi authored Sep 4, 2024
1 parent 82c2441 commit b156aab
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 3 deletions.
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);
}


}

0 comments on commit b156aab

Please sign in to comment.