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

trouble with coercision in test #1160

Open
peutch opened this issue Feb 17, 2024 · 0 comments
Open

trouble with coercision in test #1160

peutch opened this issue Feb 17, 2024 · 0 comments

Comments

@peutch
Copy link

peutch commented Feb 17, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant