From eda69ca6d71e79d96218c211dfd529768c72965d Mon Sep 17 00:00:00 2001 From: Brady James Garvin <bgarvin@cse.unl.edu> Date: Tue, 3 Aug 2021 10:38:24 -0500 Subject: [PATCH] Matched code to the latest version of the testability examples. --- .gitignore | 1 + fuzzing/fuzz_find_smallest_positive.py | 3 +- main.py | 57 +++++++++++++++++--------- smallestpositive.kv | 16 ++++---- 4 files changed, 48 insertions(+), 29 deletions(-) diff --git a/.gitignore b/.gitignore index edc7dfb..99dc3fb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ *~ *.pyc *.pyo +__pycache__ .idea diff --git a/fuzzing/fuzz_find_smallest_positive.py b/fuzzing/fuzz_find_smallest_positive.py index 346c410..6cea2c0 100644 --- a/fuzzing/fuzz_find_smallest_positive.py +++ b/fuzzing/fuzz_find_smallest_positive.py @@ -1,6 +1,7 @@ from math import inf from random import randrange, choice + SUITE_TEMPLATE = ''' from math import inf from unittest import TestCase @@ -37,7 +38,7 @@ def generate_padding(smallest_positive): def generate_random_test(number): - smallest_positive = choice((randrange(VALUE_LIMIT), inf)) + smallest_positive = choice((randrange(1, VALUE_LIMIT), inf)) inputs = [smallest_positive] # TODO return TEST_TEMPLATE.format(number=number, inputs=inputs, expected=smallest_positive) diff --git a/main.py b/main.py index 17cd025..605d4a8 100644 --- a/main.py +++ b/main.py @@ -2,42 +2,59 @@ from math import inf from kivy.app import App from kivy.uix.boxlayout import BoxLayout -from kivy.properties import NumericProperty, StringProperty +from kivy.properties import NumericProperty, StringProperty, ListProperty class Field(BoxLayout): - label_text = StringProperty() + index = NumericProperty() + entry = StringProperty() + + +class Fields(BoxLayout): + entries = ListProperty([]) # elements are strings (some may not be valid numbers) + + def rebuild(self): + while len(self.children) > len(self.entries): + self.remove_widget(self.children[0]) + while len(self.children) < len(self.entries): + self.add_widget(Field(index=len(self.children))) + + def on_entries(self, _, __): + self.rebuild() + for field, entry in zip(reversed(self.children), self.entries): + field.entry = entry class SmallestPositiveApp(App): - smallest_positive = NumericProperty(inf) + entries = ListProperty([]) # elements are strings (some may not be valid numbers) + result = NumericProperty(inf) def add_field(self): - container = self.root.ids.fields - container.add_widget(Field(label_text=f'Field #{len(container.children)}: ')) + self.entries.append('') def remove_field(self): - container = self.root.ids.fields - if len(container.children) > 0: - container.remove_widget(container.children[0]) + try: + self.entries.pop() + except IndexError: + pass @staticmethod - def _find_smallest_positive(values): + def _find_smallest_positive(entries): smallest_positive = inf - for value in values: - if 0 < value < smallest_positive: - smallest_positive = value - return smallest_positive - - def update(self): - container = self.root.ids.fields - values = [] - for field in container.children: + for entry in entries: try: - values.append(float(field.ids.input.text)) + value = float(entry) + if 0 < value < smallest_positive: + smallest_positive = value except ValueError: pass - self.smallest_positive = SmallestPositiveApp._find_smallest_positive(values) + return smallest_positive + + def on_entries(self, _, __): + self.result = SmallestPositiveApp._find_smallest_positive(self.entries) + + def set_entry(self, index, entry): + self.entries[index] = entry if __name__ == '__main__': diff --git a/smallestpositive.kv b/smallestpositive.kv index 2ea9ff2..dd39ccf 100644 --- a/smallestpositive.kv +++ b/smallestpositive.kv @@ -1,8 +1,7 @@ BoxLayout: orientation: 'vertical' - BoxLayout: - id: fields - orientation: 'vertical' + Fields: + entries: app.entries size_hint: (1.0, 6.0) BoxLayout: orientation: 'horizontal' @@ -15,18 +14,19 @@ BoxLayout: font_size: sp(24) on_press: app.remove_field() Label: - id: result - text: f'Smallest Positive: {app.smallest_positive}' + text: f'Smallest Positive: {app.result}' font_size: sp(24) +<Fields>: + orientation: 'vertical' + <Field>: orientation: 'horizontal' Label: - text: root.label_text + text: f'Field #{root.index}: ' font_size: sp(24) TextInput: - id: input multiline: False write_tab: False font_size: sp(24) - on_text: app.update() + on_text: app.set_entry(root.index, self.text) -- GitLab