trouble with coercision in test
还没有人认领这个 Issue。
评估
调研方向
从 TestJinjava.testCoerc 的复现以及堆栈跟踪中指出的 Jinjava.render 入口点开始。使用数字和字符串宏参数运行测试,然后跟踪条件比较和强制转换行为。当复现的案例在没有发生强制转换异常的情况下,以预期输出渲染两个 Fedora 条目时,即表示完成。
由索引模型根据 Issue 内容生成。
描述
Hi,
Here is a unit test class that reflect my problem:
import static org.junit.Assert.assertEquals;
import java.util.Collections;
import org.junit.Test;
import com.hubspot.jinjava.Jinjava;
public class TestJinjava {
@Test
public void testCoerc() {
String output = new Jinjava().render("{% macro bug(version) %}"
+ "{% set fversion = 'rawhide' if version == 'rawhide' else 'f' + version|string %}"
+ "- name: fedora_{{version}}\n"
+ " url: 'https://src.fedoraproject.org/rpms/{srcname}/tree/{{fversion}}'\n"
+ "{% endmacro %}{{ bug('39') }}{{ bug('rawhide') }}", Collections.emptyMap());
assertEquals("- name: fedora_39\n url: 'https://src.fedoraproject.org/rpms/{srcname}/tree/f39'\n"
+ "- name: fedora_rawhide\n url: 'https://src.fedoraproject.org/rpms/{srcname}/tree/rawhide'\n",
output);
}
}
If bug('19') is replaced by bug(19), test is broken:
com.hubspot.jinjava.interpret.FatalTemplateErrorsException: Cannot coerce 'rawhide' of class java.lang.String to class java.lang.Long (incompatible value)
at com.hubspot.jinjava.Jinjava.render(Jinjava.java:193)
at repology.TestJinjava.testCoerc(TestJinjava.java:15)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:93)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:40)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:529)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:756)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:452)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:210)
To me this is strange and it is also not possible to replace the condition by something like:
{% set fversion = 'rawhide' if version|string == 'rawhide' else 'f' + version|string %}
isn't it?
This test case was extracted from:
https://github.com/repology/repology-updater/blob/master/repos.d/rpm/fedora.yaml
that use Python Jinja2.
Regards,
Patrice
- 主要语言
- Java
- 星标
- 785
- 派生
- 184
- 平均合并
- 6 天 22 小时
- 30 天内合并 PR
- 1
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
HubSpot/jinjava 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 64/100
-
Jinjava 3.0 未关闭
-
难度 4/5 3-5 天 新手友好度 35/100
-
难度 3/5 1-2 天 新手友好度 68/100
-
难度 3/5 1-2 天 新手友好度 45/100
相似的 Issue
-
area/plugin
难度 2/5 1-3 小时 新手友好度 75/100
kestra-io/plugin-kestra#190 ·
-
难度 2/5 1-3 小时 新手友好度 70/100
google-ai-edge/LiteRT-LM#3739 ·
-
难度 2/5 1-3 小时 新手友好度 75/100
integra-team-red/meet-map#249 ·
-
[Studio][Bug] Cancelled create-user dialog keeps the password and admin switch for the next attempt 未关闭
难度 2/5 1-3 小时 新手友好度 75/100
apache/rocketmq-dashboard#5064 ·
-
难度 2/5 1-3 小时 新手友好度 75/100
wso2/dpdp-accelerator#287 ·