From bc9a57807705afa45bf09c08427553c8a06201d3 Mon Sep 17 00:00:00 2001
From: Suraj Devatha <42767118+surajdm123@users.noreply.github.com>
Date: Thu, 17 Jul 2025 14:17:34 -0700
Subject: [PATCH] Added JUnit Test Cases for Replacement
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
### Changes
This PR adds the missing unit test case for Replacement.
**Before:**
**After:**
### Test Plan
> mvn -Dtest=ReplacementTest test
```
...
[INFO] Running com.google.googlejavaformat.java.ReplacementTest
[INFO] Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.068 s - in com.google.googlejavaformat.java.ReplacementTest
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 4, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
```
----
> mvn test
```
[INFO] Results:
[INFO]
[INFO] Tests run: 1533, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 12.168 s
[INFO] Finished at: 2025-07-16T22:30:47-07:00
[INFO] ------------------------------------------------------------------------
```
### Next Steps
- Add more missing test cases to make this codebase more resilient.
Fixes #1268
COPYBARA_INTEGRATE_REVIEW=https://github.com/google/google-java-format/pull/1268 from surajdm123:add-test 1bad87cbc8f513a475d9917ec0d7615c550c6ddb
PiperOrigin-RevId: 784313467
---
.../java/ReplacementTest.java | 51 +++++++++++++++++++
1 file changed, 51 insertions(+)
create mode 100644 core/src/test/java/com/google/googlejavaformat/java/ReplacementTest.java
diff --git a/core/src/test/java/com/google/googlejavaformat/java/ReplacementTest.java b/core/src/test/java/com/google/googlejavaformat/java/ReplacementTest.java
new file mode 100644
index 000000000..177af9f1f
--- /dev/null
+++ b/core/src/test/java/com/google/googlejavaformat/java/ReplacementTest.java
@@ -0,0 +1,51 @@
+/*
+ * Copyright 2025 Google Inc.
+ *
+ * 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
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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 com.google.googlejavaformat.java;
+
+import static com.google.common.truth.Truth.assertThat;
+import static org.junit.Assert.assertThrows;
+
+import com.google.common.collect.Range;
+import com.google.common.testing.EqualsTester;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+/** {@link Replacement}Test */
+@RunWith(JUnit4.class)
+public class ReplacementTest {
+
+ @Test
+ public void testCreateWithValidInput() {
+ Replacement replacement = Replacement.create(3, 7, "replacementText");
+ assertThat(replacement.getReplaceRange()).isEqualTo(Range.closedOpen(3, 7));
+ assertThat(replacement.getReplacementString()).isEqualTo("replacementText");
+ }
+
+ @Test
+ public void invalidReplacementRange() {
+ assertThrows(IllegalArgumentException.class, () -> Replacement.create(-1, 5, "text"));
+ assertThrows(IllegalArgumentException.class, () -> Replacement.create(10, 5, "text"));
+ }
+
+ @Test
+ public void testEqualsAndHashCode() {
+ new EqualsTester()
+ .addEqualityGroup(Replacement.create(0, 4, "abc"), Replacement.create(0, 4, "abc"))
+ .addEqualityGroup(Replacement.create(1, 4, "abc"))
+ .addEqualityGroup(Replacement.create(0, 4, "def"))
+ .testEquals();
+ }
+}