Skip to content

Commit

Permalink
Add rewrite-test
Browse files Browse the repository at this point in the history
  • Loading branch information
jkschneider committed Aug 20, 2024
1 parent 83e3bfc commit 015d2bd
Show file tree
Hide file tree
Showing 50 changed files with 3,786 additions and 0 deletions.
114 changes: 114 additions & 0 deletions rewrite/rewrite-test/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions rewrite/rewrite-test/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[tool.poetry]
name = "rewrite-test"
version = "0.0.0" # This will be replaced by the GitHub Actions workflow
description = "Test functionality for the OpenRewrite library"
authors = ["Moderne Inc. <support@moderne.io>"]
license = "Apache-2.0"
packages = [
{ include = "rewrite/test", from = "src" }
]

[tool.poetry.dependencies]
python = "^3.8"
rewrite-core = { path = "../rewrite-core", develop = true }

[tool.poetry.group.dev.dependencies]
pytest = "^8.3.2"

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"
Empty file.
16 changes: 16 additions & 0 deletions rewrite/rewrite-test/src/rewrite/test/source_specs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from dataclasses import dataclass
from uuid import UUID

@dataclass(frozen=True, eq=False)
class SourceSpec:
_id: UUID

@property
def id(self) -> UUID:
return self._id

_parser: ParserBuilder

@property
def parser(self) -> ParserBuilder:
return self._parser
5 changes: 5 additions & 0 deletions rewrite/rewrite-test/src/rewrite/test/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from abc import ABC

class RewriteTest(ABC):
def rewrite_run(self):
pass
5 changes: 5 additions & 0 deletions rewrite/rewrite-test/tests/java/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
root = true

[*.java]
indent_size = 4
ij_continuation_indent_size = 2
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2023 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.python;

import org.junit.jupiter.api.Test;
import org.openrewrite.DocumentExample;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

import static org.openrewrite.python.Assertions.python;

class ChangeMethodNameTest implements RewriteTest {

@Override
public void defaults(RecipeSpec spec) {
spec.recipe(new ChangeMethodName("print", "println", true));
}

@DocumentExample
@Test
void renameMethod() {
rewriteRun(
python(
"""
class Foo:
def foo() :
print("hello")
""",
"""
class Foo:
def foo() :
println("hello")
"""
)
);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* Copyright 2024 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.python.cleanup;

import org.junit.jupiter.api.Test;
import org.openrewrite.DocumentExample;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

import static org.openrewrite.python.Assertions.python;

@SuppressWarnings({"PyInterpreter", "PyUnresolvedReferences"})
class StartsWithEndsWithTest implements RewriteTest {
@Override
public void defaults(RecipeSpec spec) {
spec.recipe(new StartsWithEndsWith());
}

@Test
@DocumentExample
void startswithAndEndswith() {
rewriteRun(
python(
"""
a_string = "ababab"
if a_string.startswith("ab") or a_string.startswith("aba"):
print("Starts with ab")
if a_string.startswith("ab") or a_string.startswith("aba") or a_string.startswith("abab"):
print("Starts with ab")
if a_string.endswith("ab") or a_string.endswith("bab"):
print("Ends with ab")
""",
"""
a_string = "ababab"
if a_string.startswith(("ab", "aba")):
print("Starts with ab")
if a_string.startswith(("ab", "aba", "abab")):
print("Starts with ab")
if a_string.endswith(("ab", "bab")):
print("Ends with ab")
"""
)
);
}

@Test
void morePositiveCases() {
rewriteRun(
python(
"""
a_string = "ababab"
a_string.startswith("ab") or a_string.startswith("aba") or a_string.startswith("abab") or a_string.startswith("ababa")
a_string.startswith("ab") or a_string.startswith(("aba", "abab"))
a_string.startswith(("ab", "aba")) or a_string.startswith(("abab", "ababa"))
""",
"""
a_string = "ababab"
a_string.startswith(("ab", "aba", "abab", "ababa"))
a_string.startswith(("ab", "aba", "abab"))
a_string.startswith(("ab", "aba", "abab", "ababa"))
"""
)
);
}

@Test
void negativeCases() {
rewriteRun(
python(
"""
a_string = "ababab"
b_string = "babab"
a_string.startswith("ab")
a_string.startswith(("ab", "aba"))
a_string.startswith("aba") and a_string.startswith("ab")
a_string.startswith("aba") and a_string.startswith("ab") or True
a_string.startswith("aba") or b_string.startswith("ba")
a_string.startswith("aba") or a_string.endswith("bab")
"""
)
);
}
}
Loading

0 comments on commit 015d2bd

Please sign in to comment.