Skip to content

Commit

Permalink
fixes #29 check for instance of Double in FeatureResult#isOn() (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
tinahollygb committed Jul 6, 2023
1 parent 3f17ab8 commit aa04bfa
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 2 deletions.
4 changes: 4 additions & 0 deletions lib/src/main/java/growthbook/sdk/java/FeatureResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ public Boolean isOn() {
return (Float) value != 0.0f;
}

if (value instanceof Double) {
return (Double) value != 0;
}

return false;
}

Expand Down
109 changes: 107 additions & 2 deletions lib/src/test/java/growthbook/sdk/java/FeatureResultTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.*;

class FeatureResultTest {
@Test
Expand Down Expand Up @@ -62,4 +61,110 @@ void featureResultSourceOutputsCorrectlyToJson() {
subject.setSource(FeatureResultSource.DEFAULT_VALUE);
assertEquals("{\"on\":false,\"off\":true,\"source\":\"defaultValue\"}", GrowthBookJsonUtils.getInstance().gson.toJson(subject));
}

// region isOn() isOff()

@Test
void featureResult_isOn_withNonZeroValue_returnsTrue_forIntegers() {
FeatureResult<Integer> subject = FeatureResult
.<Integer>builder()
.value(1)
.source(FeatureResultSource.FORCE)
.build();

assertTrue(subject.isOn());
assertFalse(subject.isOff());
}

@Test
void featureResult_isOn_withZeroValue_returnsFalse_forIntegers() {
FeatureResult<Integer> subject = FeatureResult
.<Integer>builder()
.value(0)
.source(FeatureResultSource.FORCE)
.build();

assertFalse(subject.isOn());
assertTrue(subject.isOff());
}

// floats

@Test
void featureResult_isOn_withNonZeroValue_returnsTrue_forFloats() {
FeatureResult<Float> subject = FeatureResult
.<Float>builder()
.value(1.0f)
.source(FeatureResultSource.FORCE)
.build();

assertTrue(subject.isOn());
assertFalse(subject.isOff());
}

@Test
void featureResult_isOn_withZeroValue_returnsFalse_forFloats() {
FeatureResult<Float> subject = FeatureResult
.<Float>builder()
.value(0.0f)
.source(FeatureResultSource.FORCE)
.build();

assertFalse(subject.isOn());
assertTrue(subject.isOff());
}

// doubles

@Test
void featureResult_isOn_withNonZeroValue_returnsTrue_forDoubles() {
FeatureResult<Double> subject = FeatureResult
.<Double>builder()
.value(1.0)
.source(FeatureResultSource.FORCE)
.build();

assertTrue(subject.isOn());
assertFalse(subject.isOff());
}

@Test
void featureResult_isOn_withZeroValue_returnsFalse_forDoubles() {
FeatureResult<Double> subject = FeatureResult
.<Double>builder()
.value(0)
.source(FeatureResultSource.FORCE)
.build();

assertFalse(subject.isOn());
assertTrue(subject.isOff());
}

// strings

@Test
void featureResult_isOn_withNonEmptyValue_returnsTrue_forStrings() {
FeatureResult<String> subject = FeatureResult
.<String>builder()
.value("hello, world!")
.source(FeatureResultSource.FORCE)
.build();

assertTrue(subject.isOn());
assertFalse(subject.isOff());
}

@Test
void featureResult_isOn_withEmptyValue_returnsFalse_forStrings() {
FeatureResult<String> subject = FeatureResult
.<String>builder()
.value("")
.source(FeatureResultSource.FORCE)
.build();

assertFalse(subject.isOn());
assertTrue(subject.isOff());
}

// endregion isOn() isOff()
}

0 comments on commit aa04bfa

Please sign in to comment.