Esempio n. 1
0
 def test_model_perform_logging(self):
     model = QuadraticEquationViewModel()
     model.set_a(2)
     model.set_b(4)
     model.set_c(2)
     model.perform()
     self.assertEqual('Result = Answer:\n x = -1.0',
                      model.logger.get_last_message())
Esempio n. 2
0
    def test_input_wrong_data(self):
        model = QuadraticEquationViewModel()
        model.set_a('@1')
        model.set_b(4)
        model.set_c(2)
        model.perform()

        wrong_message = 'Incorrect input'
        self.assertEqual(wrong_message, model.logger.get_last_message())
Esempio n. 3
0
    def test_model_perform_all_message_logging(self):
        model = QuadraticEquationViewModel()
        model.set_a(2)
        model.set_b(4)
        model.set_c(2)
        model.perform()

        log_message = [
            'Setting a = 2', 'Setting b = 4', 'Setting c = 2',
            'Button clicked', 'a = 2, b = 4, c = 2',
            'Result = Answer:\n x = -1.0'
        ]
        self.assertEqual(log_message, model.logger.get_log_messages()[-6:])
Esempio n. 4
0
 def test_set_c_logging(self):
     model = QuadraticEquationViewModel()
     model.set_c(2)
     self.assertEqual('Setting c = 2', model.logger.get_last_message())
Esempio n. 5
0
 def test_init_logging(self):
     model = QuadraticEquationViewModel(2, 4, 2)
     self.assertEqual('Welcome!', model.logger.get_last_message())
Esempio n. 6
0
 def test_by_default_button_is_disable(self):
     model = QuadraticEquationViewModel()
     self.assertEqual('disabled', model.is_button_enable())
Esempio n. 7
0
 def test_when_enter_incorrect_values(self):
     model = QuadraticEquationViewModel('-1a', '123', 'q9')
     self.assertEqual('disabled', model.is_button_enable())
Esempio n. 8
0
    def test_correct_set_value(self):
        model = QuadraticEquationViewModel()
        model.set_a(1)
        model.set_b(10)
        model.set_c(-12)

        self.assertEqual(1, model.get_a())
        self.assertEqual(10, model.get_b())
        self.assertEqual(-12, model.get_c())
Esempio n. 9
0
 def test_when_enter_incorrect_values_throw_exception(self):
     model = QuadraticEquationViewModel(2, '@48', 2)
     model.perform()
     answer = 'Incorrect input'
     self.assertEqual(answer, model.get_error_message())
Esempio n. 10
0
 def test_when_enter_values_display_result(self):
     model = QuadraticEquationViewModel(2, 4, 2)
     model.perform()
     answer = 'Answer:\n x = -1.0'
     self.assertEqual(answer, model.get_result())
Esempio n. 11
0
 def test_when_clear_c_button_disabled(self):
     model = QuadraticEquationViewModel(1, -1, 1)
     model.set_c(None)
     self.assertEqual('disabled', model.is_button_enable())
Esempio n. 12
0
 def test_when_enter_a_b_c_button_enabled(self):
     model = QuadraticEquationViewModel(1, 1, 1)
     self.assertTrue(model.is_button_enable())
Esempio n. 13
0
class GUIView(object):
    view_model = QuadraticEquationViewModel()

    def __init__(self):
        self.root = tk.Tk()
        self.root['bg'] = 'maroon1'
        self.root.title('Solve quadratic equation')
        self.root.geometry('600x300')
        self.root.resizable(width=False, height=True)

        self.frame = tk.Frame(self.root, bg='SkyBlue1')
        self.title = tk.Label(self.frame,
                              text='Введите начало и конец промежутка',
                              bg='gray21',
                              font=40)

        self.a_value = tk.Entry(self.frame, bg='white', state='normal')
        self.b_value = tk.Entry(self.frame, bg='white')
        self.c_value = tk.Entry(self.frame, bg='white')

        self.btn = tk.Button(self.frame, text='Solve', bg='gray21')
        self.res = tk.Label(self.frame, text='Result', bg='gray21', font=40)
        self.error_field = tk.Label(self.frame,
                                    text='',
                                    fg='red',
                                    bg='gray21',
                                    font=('', 12, 'bold'))

        self.set_weight_to_grid()
        self.bind_events()

        self.mvvm_bind()
        self.mvvm_back_bind()
        self.root.mainloop()

    def set_weight_to_grid(self):
        self.root.grid_rowconfigure(2, minsize=30)
        self.root.grid_rowconfigure(5, minsize=30, weight=60)
        self.frame.place(relx=0.05, rely=0.05, relwidth=0.9, relheight=0.9)

        self.a_value.grid(row=1, column=0, stick='wens', padx=5, pady=5)
        self.b_value.grid(row=1, column=2, stick='wens', padx=5, pady=5)
        self.c_value.grid(row=1, column=4, stick='wens', padx=5, pady=5)

        self.btn.grid(row=2, column=1, stick='wens')
        self.res.grid(row=4, column=0, columnspan=3, stick='we', padx=5)
        self.error_field.grid(row=5,
                              column=0,
                              columnspan=3,
                              stick='we',
                              padx=5)

    def bind_events(self):
        self.a_value.bind('<KeyRelease>', self.change_a)
        self.b_value.bind('<KeyRelease>', self.change_b)
        self.c_value.bind('<KeyRelease>', self.change_c)
        self.btn.bind('<Button-1>', self.btn_clicked)

    def btn_clicked(self, event):
        self.mvvm_bind()
        self.view_model.perform()
        self.mvvm_back_bind()

    def change_a(self, event):
        self.mvvm_bind()
        self.mvvm_back_bind()

    def change_b(self, event):
        self.mvvm_bind()
        self.mvvm_back_bind()

    def change_c(self, event):
        self.mvvm_bind()
        self.mvvm_back_bind()

    def mvvm_bind(self):
        self.view_model.set_a(self.a_value.get())
        self.view_model.set_b(self.b_value.get())
        self.view_model.set_c(self.c_value.get())

    def mvvm_back_bind(self):
        self.a_value.delete(0, tk.END)
        self.a_value.insert(tk.END, self.view_model.get_a())

        self.b_value.delete(0, tk.END)
        self.b_value.insert(tk.END, self.view_model.get_b())

        self.c_value.delete(0, tk.END)
        self.c_value.insert(tk.END, self.view_model.get_c())

        self.btn.config(state=self.view_model.is_button_enable())

        self.res.config(
            text='{}\n'.format(get_result_text(self.view_model.get_result())))
        self.error_field.config(text='{}\n'.format(
            get_error_text(self.view_model.get_error_message())))