From 57a6ecf5eef35b63e1b77d735e2ddf3937e993de Mon Sep 17 00:00:00 2001 From: Shannon Pamperl Date: Sat, 19 Oct 2024 23:24:34 -0400 Subject: [PATCH] feat(test): added failing test for RemoveUnusedLocalVariables --- .../launchdarkly/RemoveBoolVariationTest.java | 83 +++++++++++++++++++ 1 file changed, 83 insertions(+) diff --git a/src/test/java/org/openrewrite/featureflags/launchdarkly/RemoveBoolVariationTest.java b/src/test/java/org/openrewrite/featureflags/launchdarkly/RemoveBoolVariationTest.java index b2bd753..b5735e5 100644 --- a/src/test/java/org/openrewrite/featureflags/launchdarkly/RemoveBoolVariationTest.java +++ b/src/test/java/org/openrewrite/featureflags/launchdarkly/RemoveBoolVariationTest.java @@ -178,6 +178,89 @@ void bar() { ); } + @Test + void removeUnusedLDContextWithBuilder() { + rewriteRun( + // language=java + java( + """ + import com.launchdarkly.sdk.*; + import com.launchdarkly.sdk.server.*; + class Foo { + LDClient client = new LDClient("sdk-key-123abc"); + void bar() { + LDContext context = LDContext.builder("context-key-123abc") + .name("Sandy") + .build(); + if (client.boolVariation("flag-key-123abc", context, false)) { + // Application code to show the feature + System.out.println("Feature is on"); + } + else { + // The code to run if the feature is off + System.out.println("Feature is off"); + } + } + } + """, + """ + import com.launchdarkly.sdk.*; + import com.launchdarkly.sdk.server.*; + class Foo { + LDClient client = new LDClient("sdk-key-123abc"); + void bar() { + // Application code to show the feature + System.out.println("Feature is on"); + } + } + """ + ) + ); + } + + @Test + void removeUnusedLDContextWithBuilderContext() { + rewriteRun( + // language=java + java( + """ + import com.launchdarkly.sdk.*; + import com.launchdarkly.sdk.server.*; + class Foo { + LDClient client = new LDClient("sdk-key-123abc"); + void bar() { + LDContext ldContext = LDContext.create("newValue") + LDContext context = LDContext.builderFromContext(ldContext) + .anonymous(false) + .name("name") + .set("email", "email@gmail.com") + .build(); + if (client.boolVariation("flag-key-123abc", context, false)) { + // Application code to show the feature + System.out.println("Feature is on"); + } + else { + // The code to run if the feature is off + System.out.println("Feature is off"); + } + } + } + """, + """ + import com.launchdarkly.sdk.*; + import com.launchdarkly.sdk.server.*; + class Foo { + LDClient client = new LDClient("sdk-key-123abc"); + void bar() { + // Application code to show the feature + System.out.println("Feature is on"); + } + } + """ + ) + ); + } + @Test void enablePermanentlyWithParameters() { rewriteRun(