trouble with coercision in test
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 35/100
Research direction
Start with the TestJinjava.testCoerc reproduction and the Jinjava.render entry point named in the stack trace. Run the test with numeric and string macro arguments, then trace the conditional comparison and coercion behavior. Done means the reproduced case renders both Fedora entries with the expected output without the coercion exception.
Written by the indexing model from the issue text.
Description
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
- Dominant language
- Java
- Stars
- 785
- Forks
- 184
- Avg merge
- 6d 22h
- Merged PRs (30d)
- 1
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from HubSpot/jinjava
-
Difficulty 2/5 1-3 hours Newbie friendliness 64/100
-
Jinjava 3.0 Open
-
Difficulty 4/5 3-5 days Newbie friendliness 35/100
-
Difficulty 3/5 1-2 days Newbie friendliness 68/100
-
Difficulty 3/5 1-2 days Newbie friendliness 45/100
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
elastic/gradle-plugins#157 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
cryptomator/hub#497 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
johanhaleby/occurrent#1120 ·