Skip to content

Commit

Permalink
Feature: Do not add section without attached rows
Browse files Browse the repository at this point in the history
  • Loading branch information
Toni Möckel committed Mar 14, 2017
1 parent a5c1cae commit 93d7c05
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,9 @@ public int compare(Field lhs, Field rhs) {
}
}

formDescriptor.addSection(sectionDescriptor);
if (sectionDescriptor.getRowCount()>0){
formDescriptor.addSection(sectionDescriptor);
}
}

return formDescriptor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.quemb.qmbform.annotation.FormDescriptorAnnotationFactory;
import com.quemb.qmbform.annotation.FormElement;
import com.quemb.qmbform.annotation.FormElementDelegate;
import com.quemb.qmbform.annotation.FormOptionsObjectElement;
import com.quemb.qmbform.descriptor.FormDescriptor;
import com.quemb.qmbform.descriptor.RowDescriptor;
Expand All @@ -17,11 +18,14 @@
import android.app.Activity;
import android.widget.ListView;

import java.lang.reflect.Field;
import java.util.Map;

import static junit.framework.Assert.assertTrue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsNull.notNullValue;
import static org.hamcrest.core.IsNull.nullValue;

/**
* Created by pmaccamp on 9/14/2015.
Expand All @@ -32,24 +36,42 @@ public class AnnotationFormTest {
private Activity activity;
private TestUserClass testUserClass;

public class TestUserClass {
public class TestUserClass implements FormElementDelegate {
@FormElement(
rowDescriptorType = RowDescriptor.FormRowDescriptorTypeInteger,
required = true
required = true,
section = android.R.string.unknownName
)
public int age;

@FormElement(
rowDescriptorType = RowDescriptor.FormRowDescriptorTypeText,
required = true
required = true,
section = android.R.string.unknownName
)
public String name;

@FormElement(
rowDescriptorType = RowDescriptor.FormRowDescriptorTypeText,
section = android.R.string.untitled
)
public String option;


public TestUserClass(int age, String name, int bodyfat) {
this.age = age;
this.name = name;
}

@Override
public boolean shouldAddRowDescriptorForField(RowDescriptor rowDescriptor, Field field) {

if (rowDescriptor.getTag().equals("option")){
return this.option != null;
}

return true;
}
}

@Before
Expand All @@ -73,6 +95,29 @@ public void hasCorrectFormValues() {
assertThat((String) formValues.get("name"), is("John"));
}

@Test
public void shouldNotIncludeSection() {

FormDescriptorAnnotationFactory factory = new FormDescriptorAnnotationFactory(activity);
FormDescriptor formDescriptor = factory.createFormDescriptorFromAnnotatedClass(testUserClass);

assertThat(formDescriptor.countOfSections(), is(1));
assertThat(formDescriptor.findRowDescriptor("option"), nullValue());

}

@Test
public void shouldIncludeSection() {

FormDescriptorAnnotationFactory factory = new FormDescriptorAnnotationFactory(activity);
testUserClass.option = "mock";
FormDescriptor formDescriptor = factory.createFormDescriptorFromAnnotatedClass(testUserClass);

assertThat(formDescriptor.countOfSections(), is(2));
assertThat(formDescriptor.findRowDescriptor("option"), notNullValue());

}

@After
public void tearDown() {

Expand Down

0 comments on commit 93d7c05

Please sign in to comment.