Skip to content

Add mappings from imports, methods and annotations to support JUnit test conversions #124

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

Merged

Conversation

javiertuya
Copy link
Contributor

This PR Closes #118 to enable the conversion of JUnit tests. This is done by converting the test annotations, imports and assert methods using a collection of mappings that are read from an optional external file.

The submitted PR includes two commits (please, tell me if you prefer to submit it as two separate sequential PRs):

  1. Read mappings file (463d247):
    A new optional CLI option --mappings-file specifies the mappings file for imports, annotations and methods (either void or non void). Example:
ImportMappings:
  org.junit.Test : Xunit
  org.junit.Assert.assertEquals : ""
  org.junit.Assert.assertTrue : ""
VoidMethodMappings:
  assertEquals : Assert.Equal
  assertTrue : Assert.True
AnnotationMappings:
  Test : Fact
  1. Apply mappings during the conversion (5c94f00)

For example, this junit4 test:

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
public class MappingsTest {
  @Test
  public void testAsserts() {
    assertEquals("a", "a");
    assertTrue(true);
    va.assertTrue(true); // non void is not mapped
  }
}

is converted using the above mappings to:

using Xunit;

public class MappingsTest
{
  [Fact]
  public virtual void TestAsserts()
  {
    Assert.Equal("a", "a");
    Assert.True(true);
    va.AssertTrue(true); // non void is not mapped
  }
}

@paulirwin
Copy link
Owner

This looks great, thank you! Can you please include in your PR an update to the README with a new section on how to use this, as well as a possible example file like you started on to convert junit tests to Xunit? Thanks!

@paulirwin paulirwin merged commit adae786 into paulirwin:master Aug 26, 2024
2 checks passed
@javiertuya
Copy link
Contributor Author

@paulirwin Thank you for your work in this project. Currently I'm working on migrating conversion of a few repos from Sharpen to JavaToCSharp and the converssion with JavaToCSharp is pretty much straightforward than when using Sharpen (which is semantic), and requires the same amount of postprocessing of the resulting C#.

I've included some instructions and a simple example (like the one in this PR). If you want to include a more complex file, this is one that I'm working on (converts Selenium and JUnit4 into NUnit4)
java-mapping.yml.txt

@paulirwin
Copy link
Owner

That is awesome, thanks! I love the idea of a custom mapping file, this will be great for projects with a lot of custom code that needs to be mapped. Glad the project helps!

@javiertuya javiertuya deleted the 118-test-annotations-and-mappings branch January 14, 2025 14:37
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

Successfully merging this pull request may close these issues.

Support junit test annotations
2 participants