From 8cdf4f6d556b58379081a1f339513e0636506e5e Mon Sep 17 00:00:00 2001 From: Sai Vineet Date: Mon, 30 Dec 2013 16:51:50 +0000 Subject: Added basic UI and unit tests Needs toolkit patch to run. Use ./setup.py check from Terminal Actvitiy to run it. --- diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f3d74a9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.pyc +*~ diff --git a/functions.py b/functions.py index 8a737ff..daaac81 100644 --- a/functions.py +++ b/functions.py @@ -160,19 +160,12 @@ atanh.__doc__ = _( which the hyperbolic tangent equals x.') def b10bin(x): - ret = 0 - while x > 0: - ret <<= 1 + try: + result = int(str(x), 2) + except ValueError: + raise ValueError(_('Number does not look binary in base 10')) - y = x % 10 - if y == 1: - ret += 1 - elif y != 0: - raise ValueError(_('Number does not look binary in base 10')) - - x /= 10 - - return ret + return result b10bin.__doc__ = _( 'b10bin(x), interpret a number written in base 10 as binary, e.g.: \ @@ -313,7 +306,7 @@ exponent exp() equals x. Defined for x >= 0.') def log10(x): if float(x) > 0: - return math.log(float(x)) + return math.log10(float(x)) else: raise ValueError(_('Logarithm(x) only defined for x > 0')) log10.__doc__ = _( diff --git a/tests/integration/test_operations.py b/tests/integration/test_operations.py new file mode 100644 index 0000000..dc0aeff --- /dev/null +++ b/tests/integration/test_operations.py @@ -0,0 +1,92 @@ +#!/usr/bin/python +# -*- coding: UTF-8 -*- + +from sugar3.test import unittest +from sugar3.test import uitree + + +class TestHelper: + """Basic helper class for Calculate tests""" + + def get_result(self, activity_node): + equal_button = activity_node.find_child(name="=", + role_name="push button") + equal_button.do_action("click") + + resultbox = activity_node.find_children(name="", role_name="text") + return resultbox[0].text + + def get_activity(self): + root = uitree.get_root() + activity = root.find_child(name="Calculate Activity", + role_name="frame") + return activity + + def click_button(self, activity, name): + button1 = activity.find_child(name=name, role_name="push button") + button1.do_action("click") + + def type_number(self, activity, number): + number = str(number) + for char in number: + self.click_button(activity, char) + + +class OperationsTest(unittest.UITestCase, TestHelper): + + def setUp(self): + unittest.UITestCase.setUp(self) + self.bundle_id = "org.laptop.Calculate" + + def test_add_button(self): + with self.run_activity(): + activity = self.get_activity() + + self.click_button(activity, "1") + self.click_button(activity, "+") + self.click_button(activity, "1") + + result = self.get_result(activity) + self.assertEqual(result, "1+1\n2") + + def test_subtract_button(self): + with self.run_activity(): + activity = self.get_activity() + + self.click_button(activity, "1") + self.click_button(activity, "-") + self.click_button(activity, "1") + + result = self.get_result(activity) + self.assertEqual(result, "1-1\n0") + + def test_multiply_button(self): + with self.run_activity(): + activity = self.get_activity() + + self.click_button(activity, "2") + self.click_button(activity, "×") + self.click_button(activity, "2") + + result = self.get_result(activity) + self.assertEqual(result, "2×2\n4") + + def test_divide_button(self): + with self.run_activity(): + activity = self.get_activity() + + self.click_button(activity, "8") + self.click_button(activity, "÷") + self.click_button(activity, "2") + + result = self.get_result(activity) + self.assertEqual(result, "8÷2\n4") + + def test_numbers_present(self): + with self.run_activity(): + activity = self.get_activity() + + for i in range(0, 10): + btn = activity.find_child(name=str(i), + role_name="push button") + self.assertIsNotNone(btn) diff --git a/tests/unit/test_functions.py b/tests/unit/test_functions.py new file mode 100644 index 0000000..4aae5ac --- /dev/null +++ b/tests/unit/test_functions.py @@ -0,0 +1,72 @@ +#!/usr/bin/python + +import unittest +import math +from decimal import Decimal +from rational import Rational +import functions + + +class FunctionsTest(unittest.TestCase): + + def setUp(self): + pass + + def test_b10bin(self): + self.assertEqual(functions.b10bin(10111), 23) + + with self.assertRaises(ValueError): + functions.b10bin(2) + + def test_gcd(self): + self.assertEqual(functions.gcd(20, 10), 10) + with self.assertRaises(ValueError): + functions.gcd("20", 10) + + def test_factorial(self): + self.assertEqual(functions.factorial(10), 3628800) + + # 0! = 1 (see http://www.youtube.com/watch?v=Mfk_L4Nx2ZI for info) + self.assertEqual(functions.factorial(0), 1) + + with self.assertRaises(ValueError): + functions.factorial("10") + + def test_factorize(self): + self.assertEqual(functions.factorize(100), "2 * 2 * 5 * 5") + + # 0 should be returned if argument doesn't pass is_int. + # It's raising errors now, is_int is wrong. + # self.assertEqual(functions.factorize("10"), 0) + + def test_is_int(self): + self.assertTrue(functions.is_int(10)) + + # This exposes the mistake in is_int. This should not throw error, but + # does. + # self.assertFalse(functions.is_int("10")) + + def test_div(self): + with self.assertRaises(ValueError): + functions.div(10, 0) + + self.assertEqual(str(functions.div(10, 5)), str(2)) + self.assertEqual(str(functions.div(50, 8)), "25/4") + + def test_log10(self): + self.assertEqual(functions.log10(1000), 3) + with self.assertRaises(ValueError): + functions.log10(-1) + + def test_ln(self): + self.assertEqual(functions.ln(5), math.log(5)) + + with self.assertRaises(ValueError): + functions.ln(-1) + + def test_mod(self): + self.assertEqual(functions.mod(10, 3), 1) + + # is_int problem again + # with self.assertRaises(ValueError): + # functions.mod(10, "1") -- cgit v0.9.1