Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix incomplete questionnaire entry removal when invalidating a pre-condition #115

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions lib/models/questionnaire.dart
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ class Questionnaire {
_entries[_activeIndex].question,
answer
);
_updateWorkingElement();
_removeObsoleteEntries();
_updateWorkingElement();
_insertMatchingEntries();
}
}
Expand Down Expand Up @@ -118,8 +118,14 @@ class Questionnaire {


void _removeObsoleteEntries() {
// Note: do not use reverse iteration here
// because removing a previous entry might result in the removal of a following entry (and so forth)
// which wouldn't be detected when iterating in reverse

// only iterate over all elements after the current active index
for (var i = _entries.length - 1; i > _activeIndex; i--) {
var i = _activeIndex + 1;

while (i < _entries.length) {
final entry = _entries[i];
// create working element from all preceding entries excluding the current entry
// this needs to be done, because otherwise an entry can get obsolete by its own answer or answers of succeeding questions
Expand All @@ -140,6 +146,10 @@ class Questionnaire {
// remove the answer
_entries.removeAt(i);
}
else {
// only increment if no element was removed, because the removal will modify the indexes
i++;
}
}
}

Expand Down
79 changes: 79 additions & 0 deletions test/questionnaire_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:open_stop/models/answer.dart';
import 'package:open_stop/models/osm_condition.dart';
import 'package:open_stop/models/question_catalog/answer_constructor.dart';
import 'package:open_stop/models/question_catalog/answer_definition.dart';
import 'package:open_stop/models/question_catalog/question_catalog.dart';
import 'package:open_stop/models/question_catalog/question_definition.dart';
import 'package:open_stop/models/questionnaire.dart';
import 'package:osm_api/osm_api.dart';

void main() async {

const stringAnswer = StringAnswerDefinition(
input: StringInputDefinition(),
constructor: AnswerConstructor({ 'tag_a': [r'$input'] }),
);

const dummyCatalog = QuestionCatalog([
QuestionDefinition(id: 0, name: 'q1', question: 'q1', isProfessional: false,
conditions: [
OsmCondition({ 'tag_a': false }, [])
],
answer: stringAnswer,
),
QuestionDefinition(id: 0, name: 'q2', question: 'q2', isProfessional: false,
conditions: [
OsmCondition({ 'tag_a': '1' }, [])
],
answer: stringAnswer,
),
QuestionDefinition(id: 0, name: 'q2', question: 'q2', isProfessional: false,
conditions: [
OsmCondition({ 'tag_a': '2' }, [])
],
answer: stringAnswer,
),
]);


test('test if succeeding questions get correctly removed when pre-condition changes', () {
final element = OSMNode(0, 0, tags: {
'other_tag': 'val',
});

final questionnaire = Questionnaire(osmElement: element, questionCatalog: dummyCatalog);

expect(questionnaire.length, equals(1));
expect(questionnaire.activeIndex, equals(0));
expect(questionnaire.workingElement.tags, equals({ 'other_tag': 'val' }));

questionnaire.update(const StringAnswer(definition: stringAnswer, value: '1'));
questionnaire.next();

expect(questionnaire.length, equals(2));
expect(questionnaire.activeIndex, equals(1));
expect(questionnaire.workingElement.tags, equals({ 'other_tag': 'val', 'tag_a': '1' }));

questionnaire.update(const StringAnswer(definition: stringAnswer, value: '2'));
questionnaire.next();

expect(questionnaire.length, equals(3));
expect(questionnaire.activeIndex, equals(2));
expect(questionnaire.workingElement.tags, equals({ 'other_tag': 'val', 'tag_a': '2' }));

questionnaire.jumpTo(0);

expect(questionnaire.length, equals(3));
expect(questionnaire.activeIndex, equals(0));
expect(questionnaire.workingElement.tags, equals({ 'other_tag': 'val', 'tag_a': '2' }));

questionnaire.update(const StringAnswer(definition: stringAnswer, value: 'OTHER'));

expect(questionnaire.length, equals(1));
expect(questionnaire.activeIndex, equals(0));
expect(questionnaire.workingElement.tags, equals({ 'other_tag': 'val', 'tag_a': 'OTHER' }));

expect(questionnaire.next(), isFalse);
});
}