Skip to content

Commit

Permalink
added ExerciseViewHelperTest
Browse files Browse the repository at this point in the history
  • Loading branch information
avalax committed Aug 18, 2014
1 parent 49bea2f commit adfedc7
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public interface Exercise extends Serializable {

void setName(String name);

@Deprecated
void addSet(Set set);

void removeSet(Set set);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import de.avalax.fitbuddy.presentation.workout.MainActivity;

import javax.inject.Singleton;
import java.util.Locale;

@Module(injects = {
MainActivity.class,
Expand Down Expand Up @@ -86,7 +87,8 @@ WorkoutApplicationService provideWorkoutApplicationService(WorkoutSession workou
@Provides
@Singleton
ExerciseViewHelper provideExerciseViewHelper() {
return new ExerciseViewHelper();
Locale locale = context.getResources().getConfiguration().locale;
return new ExerciseViewHelper(locale);
}

@Provides
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,50 @@
import de.avalax.fitbuddy.domain.model.exercise.Exercise;

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Locale;

public class ExerciseViewHelper {
private DecimalFormat decimalFormat;

public ExerciseViewHelper() {
this.decimalFormat = new DecimalFormat("###.#");
public ExerciseViewHelper(Locale locale) {
DecimalFormatSymbols symbols = new DecimalFormatSymbols(locale);
this.decimalFormat = new DecimalFormat("###.###", symbols);
}

public String weightOfExercise(Exercise exercise) {
if (exercise == null) {
return "-";
}
if (exercise.getSets().isEmpty()) {
return "-";
}
double weight = exercise.getCurrentSet().getWeight();
if (weight > 0) {
if (weight != 0) {
return decimalFormat.format(weight);
} else {
return "-";
}
}

public String nameOfExercise(Exercise exercise) {
return exercise.getName().length() > 0 ? exercise.getName() : "unnamed exercise";
if (exercise == null || exercise.getName().isEmpty()) {
return "unnamed exercise";
}
return exercise.getName();
}

public int maxRepsOfExercise(Exercise exercise) {
return exercise.getSets().isEmpty() ? 0 : exercise.getCurrentSet().getMaxReps();
if (exercise == null || exercise.getSets().isEmpty()) {
return 0;
}
return exercise.getCurrentSet().getMaxReps();
}

public int setCountOfExercise(Exercise exercise) {
return exercise.getSets().isEmpty() ? 0 : exercise.getSets().size();
if (exercise == null || exercise.getSets().isEmpty()) {
return 0;
}
return exercise.getSets().size();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package de.avalax.fitbuddy.presentation.helper;

import de.avalax.fitbuddy.domain.model.exercise.BasicExercise;
import de.avalax.fitbuddy.domain.model.exercise.Exercise;
import de.avalax.fitbuddy.domain.model.set.BasicSet;
import de.avalax.fitbuddy.domain.model.set.Set;
import org.junit.Before;
import org.junit.Test;

import java.util.Locale;

import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.assertThat;

public class ExerciseViewHelperTest {
private ExerciseViewHelper exerciseViewHelper;

private BasicExercise createExerciseWithWeight(double weight) {
BasicExercise exercise = new BasicExercise();
BasicSet set = new BasicSet();
set.setWeight(weight);
exercise.addSet(set);
return exercise;
}

private BasicExercise createExerciseWithName(String name) {
BasicExercise exercise = new BasicExercise();
exercise.setName(name);
return exercise;
}

private Exercise createExerciseWithMaxReps(int maxReps) {
Exercise exercise = new BasicExercise();
Set set = new BasicSet();
set.setMaxReps(maxReps);
exercise.addSet(set);
return exercise;
}

private BasicExercise createExerciseWithOneSet() {
BasicExercise exercise = new BasicExercise();
exercise.addSet(new BasicSet());
return exercise;
}

@Before
public void setUp() throws Exception {
exerciseViewHelper = new ExerciseViewHelper(Locale.ENGLISH);
}

@Test
public void testWeightOfExercise() throws Exception {
assertThat(exerciseViewHelper.weightOfExercise(null), equalTo("-"));
assertThat(exerciseViewHelper.weightOfExercise(new BasicExercise()), equalTo("-"));
assertThat(exerciseViewHelper.weightOfExercise(createExerciseWithWeight(0.0)), equalTo("-"));
assertThat(exerciseViewHelper.weightOfExercise(createExerciseWithWeight(10.0)), equalTo("10"));
assertThat(exerciseViewHelper.weightOfExercise(createExerciseWithWeight(0.725)), equalTo("0.725"));
assertThat(exerciseViewHelper.weightOfExercise(createExerciseWithWeight(-0.725)), equalTo("-0.725"));
}

@Test
public void testNameOfExercise() throws Exception {
assertThat(exerciseViewHelper.nameOfExercise(null), equalTo("unnamed exercise"));
assertThat(exerciseViewHelper.nameOfExercise(new BasicExercise()), equalTo("unnamed exercise"));
assertThat(exerciseViewHelper.nameOfExercise(createExerciseWithName("my new exercise")), equalTo("my new exercise"));
}

@Test
public void testMaxRepsOfExercise() throws Exception {
assertThat(exerciseViewHelper.maxRepsOfExercise(null), equalTo(0));
assertThat(exerciseViewHelper.maxRepsOfExercise(new BasicExercise()), equalTo(0));
assertThat(exerciseViewHelper.maxRepsOfExercise(createExerciseWithMaxReps(12)), equalTo(12));
}

@Test
public void testSetCountOfExercise() throws Exception {
assertThat(exerciseViewHelper.setCountOfExercise(null), equalTo(0));
assertThat(exerciseViewHelper.setCountOfExercise(new BasicExercise()), equalTo(0));
assertThat(exerciseViewHelper.setCountOfExercise(createExerciseWithOneSet()), equalTo(1));
}
}

0 comments on commit adfedc7

Please sign in to comment.