Skip to content

Commit

Permalink
#268 Add FloatProperty
Browse files Browse the repository at this point in the history
  • Loading branch information
ljacqu committed Jun 20, 2023
1 parent 9b83180 commit 2b51fcb
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/main/java/ch/jalu/configme/properties/FloatProperty.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package ch.jalu.configme.properties;

import ch.jalu.configme.properties.types.PrimitivePropertyType;

/**
* Float property. This extension exists for convenience.
*/
public class FloatProperty extends TypeBasedProperty<Float> {

public FloatProperty(String path, float defaultValue) {
super(path, defaultValue, PrimitivePropertyType.FLOAT);
}
}
11 changes: 11 additions & 0 deletions src/main/java/ch/jalu/configme/properties/PropertyInitializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,17 @@ public static Property<Double> newProperty(String path, double defaultValue) {
return new DoubleProperty(path, defaultValue);
}

/**
* Creates a new float property.
*
* @param path the property's path
* @param defaultValue the default value
* @return the created property
*/
public static Property<Float> newProperty(String path, float defaultValue) {
return new FloatProperty(path, defaultValue);
}

/**
* Creates a new String property.
*
Expand Down
63 changes: 63 additions & 0 deletions src/test/java/ch/jalu/configme/properties/FloatPropertyTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package ch.jalu.configme.properties;

import ch.jalu.configme.properties.convertresult.PropertyValue;
import ch.jalu.configme.resource.PropertyReader;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import static ch.jalu.configme.TestUtils.isErrorValueOf;
import static ch.jalu.configme.TestUtils.isValidValueOf;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.mockito.BDDMockito.given;

/**
* Test for {@link FloatProperty}.
*/
@ExtendWith(MockitoExtension.class)
class FloatPropertyTest {

@Mock
private PropertyReader reader;

@Test
void shouldReturnFloatFromResource() {
// given
Property<Float> property = new FloatProperty("test.path", -4.11f);
given(reader.getObject("test.path")).willReturn(-2508.346);

// when
PropertyValue<Float> result = property.determineValue(reader);

// then
assertThat(result, isValidValueOf(-2508.346f));
}

@Test
void shouldReturnDefaultValue() {
// given
Property<Float> property = new FloatProperty("property.path", 140f);
given(reader.getObject("property.path")).willReturn(null);

// when
PropertyValue<Float> result = property.determineValue(reader);

// then
assertThat(result, isErrorValueOf(140f));
}

@Test
void shouldReturnValueAsExportValue() {
// given
Property<Float> property = new FloatProperty("property.path", 44f);
float givenValue = 0.25f;

// when
Object exportValue = property.toExportValue(givenValue);

// then
assertThat(exportValue, equalTo(givenValue));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ void shouldInstantiateProperties() {
assertThat(newProperty("my.path", (short) 5), instanceOf(ShortProperty.class));
assertThat(newProperty("my.path", 12), instanceOf(IntegerProperty.class));
assertThat(newProperty("my.path", 10L), instanceOf(LongProperty.class));
assertThat(newProperty("my.path", 3.5f), instanceOf(FloatProperty.class));
assertThat(newProperty("my.path", -8.4), instanceOf(DoubleProperty.class));
assertThat(newProperty("my.path", "default"), instanceOf(StringProperty.class));
assertThat(newProperty(TestEnum.class, "my.path", TestEnum.FIRST), instanceOf(EnumProperty.class));
Expand Down

0 comments on commit 2b51fcb

Please sign in to comment.