Skip to content

Commit 93d7c05

Browse files
committed
Feature: Do not add section without attached rows
1 parent a5c1cae commit 93d7c05

File tree

2 files changed

+51
-4
lines changed

2 files changed

+51
-4
lines changed

lib/QMBForm/src/main/java/com/quemb/qmbform/annotation/FormDescriptorAnnotationFactory.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,9 @@ public int compare(Field lhs, Field rhs) {
173173
}
174174
}
175175

176-
formDescriptor.addSection(sectionDescriptor);
176+
if (sectionDescriptor.getRowCount()>0){
177+
formDescriptor.addSection(sectionDescriptor);
178+
}
177179
}
178180

179181
return formDescriptor;

lib/QMBForm/src/test/java/com/quemb/qmbform/AnnotationFormTest.java

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.quemb.qmbform.annotation.FormDescriptorAnnotationFactory;
44
import com.quemb.qmbform.annotation.FormElement;
5+
import com.quemb.qmbform.annotation.FormElementDelegate;
56
import com.quemb.qmbform.annotation.FormOptionsObjectElement;
67
import com.quemb.qmbform.descriptor.FormDescriptor;
78
import com.quemb.qmbform.descriptor.RowDescriptor;
@@ -17,11 +18,14 @@
1718
import android.app.Activity;
1819
import android.widget.ListView;
1920

21+
import java.lang.reflect.Field;
2022
import java.util.Map;
2123

2224
import static junit.framework.Assert.assertTrue;
2325
import static org.hamcrest.MatcherAssert.assertThat;
2426
import static org.hamcrest.core.Is.is;
27+
import static org.hamcrest.core.IsNull.notNullValue;
28+
import static org.hamcrest.core.IsNull.nullValue;
2529

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

35-
public class TestUserClass {
39+
public class TestUserClass implements FormElementDelegate {
3640
@FormElement(
3741
rowDescriptorType = RowDescriptor.FormRowDescriptorTypeInteger,
38-
required = true
42+
required = true,
43+
section = android.R.string.unknownName
3944
)
4045
public int age;
4146

4247
@FormElement(
4348
rowDescriptorType = RowDescriptor.FormRowDescriptorTypeText,
44-
required = true
49+
required = true,
50+
section = android.R.string.unknownName
4551
)
4652
public String name;
4753

54+
@FormElement(
55+
rowDescriptorType = RowDescriptor.FormRowDescriptorTypeText,
56+
section = android.R.string.untitled
57+
)
58+
public String option;
59+
4860

4961
public TestUserClass(int age, String name, int bodyfat) {
5062
this.age = age;
5163
this.name = name;
5264
}
65+
66+
@Override
67+
public boolean shouldAddRowDescriptorForField(RowDescriptor rowDescriptor, Field field) {
68+
69+
if (rowDescriptor.getTag().equals("option")){
70+
return this.option != null;
71+
}
72+
73+
return true;
74+
}
5375
}
5476

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

98+
@Test
99+
public void shouldNotIncludeSection() {
100+
101+
FormDescriptorAnnotationFactory factory = new FormDescriptorAnnotationFactory(activity);
102+
FormDescriptor formDescriptor = factory.createFormDescriptorFromAnnotatedClass(testUserClass);
103+
104+
assertThat(formDescriptor.countOfSections(), is(1));
105+
assertThat(formDescriptor.findRowDescriptor("option"), nullValue());
106+
107+
}
108+
109+
@Test
110+
public void shouldIncludeSection() {
111+
112+
FormDescriptorAnnotationFactory factory = new FormDescriptorAnnotationFactory(activity);
113+
testUserClass.option = "mock";
114+
FormDescriptor formDescriptor = factory.createFormDescriptorFromAnnotatedClass(testUserClass);
115+
116+
assertThat(formDescriptor.countOfSections(), is(2));
117+
assertThat(formDescriptor.findRowDescriptor("option"), notNullValue());
118+
119+
}
120+
76121
@After
77122
public void tearDown() {
78123

0 commit comments

Comments
 (0)