Merge lp:~rpadovani/ubuntu-calculator-app/1179422 into lp:~ubuntu-calculator-dev/ubuntu-calculator-app/old_trunk

Proposed by Riccardo Padovani
Status: Merged
Approved by: Gustavo Pichorim Boiko
Approved revision: 148
Merged at revision: 141
Proposed branch: lp:~rpadovani/ubuntu-calculator-app/1179422
Merge into: lp:~ubuntu-calculator-dev/ubuntu-calculator-app/old_trunk
Diff against target: 208 lines (+77/-34)
3 files modified
formula.js (+62/-33)
tests/autopilot/ubuntu_calculator_app/emulators.py (+6/-1)
tests/autopilot/ubuntu_calculator_app/tests/test_simple_page.py (+9/-0)
To merge this branch: bzr merge lp:~rpadovani/ubuntu-calculator-app/1179422
Reviewer Review Type Date Requested Status
Gustavo Pichorim Boiko (community) Approve
Ubuntu Phone Apps Jenkins Bot continuous-integration Approve
Riccardo Padovani Approve
Review via email: mp+181010@code.launchpad.net

Commit message

Updated formula.js to allow continue calculation and update all functions.
Fixed bug #1179422
Fixed bug #1214069

Partially based on work of f-riccardo87. Thanks!

Thanks also to boiko for his support!

Description of the change

Updated formula.js to allow continue calculation and update all functions.
Fixed bug #1179422
Fixed bug #1214069

Partially based on work of f-riccardo87. Thanks!

To post a comment you must log in.
Revision history for this message
Riccardo Padovani (rpadovani) wrote :

Know bug:
- When change sign to a result, old result is still visible

To-do:
- Add new autopilot test, update old one
- Rewrite changeSign function
- Fix bug #1210082

review: Needs Fixing
Revision history for this message
Riccardo Padovani (rpadovani) :
review: Approve
Revision history for this message
Ubuntu Phone Apps Jenkins Bot (ubuntu-phone-apps-jenkins-bot) wrote :
review: Needs Fixing (continuous-integration)
Revision history for this message
Mihir Soni (mihirsoni) wrote :

Looks good to me.

Great work Riccardo..

Lets wait for Jenkins to successfully pass this.

Revision history for this message
Gustavo Pichorim Boiko (boiko) wrote :

Riccardo, I ended up doing the emulators.py fix in the other branch (to get tests running), so it might generate a conflict on your MR, sorry for that.

51 + // After user press equal the operation we have to forget the math order and restart the operation from the result, but set formula = result is impossible, because we lost a lot of precious information

Can you just break this comment into two lines? it is really long (this will also make sure CI gets re-run for this MR).

review: Needs Fixing
143. By Riccardo Padovani

Modified a long comment

144. By Riccardo Padovani

Update to avoid conflict with tree

Revision history for this message
Ubuntu Phone Apps Jenkins Bot (ubuntu-phone-apps-jenkins-bot) wrote :
review: Needs Fixing (continuous-integration)
145. By Riccardo Padovani

Updated to last tree version

Revision history for this message
Ubuntu Phone Apps Jenkins Bot (ubuntu-phone-apps-jenkins-bot) wrote :
review: Needs Fixing (continuous-integration)
146. By Riccardo Padovani

Maked pep8 happy :)

Revision history for this message
Ubuntu Phone Apps Jenkins Bot (ubuntu-phone-apps-jenkins-bot) wrote :
review: Needs Fixing (continuous-integration)
147. By Riccardo Padovani

Updated to tree

148. By Riccardo Padovani

Merged lp:~boiko/ubuntu-calculator-app/fix_get_result_label
Fix the code to get the result label. It should return None when there is no result.
Thanks to boiko!

Revision history for this message
Ubuntu Phone Apps Jenkins Bot (ubuntu-phone-apps-jenkins-bot) wrote :
review: Approve (continuous-integration)
Revision history for this message
Gustavo Pichorim Boiko (boiko) wrote :

Looks good and works as expected!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'formula.js'
--- formula.js 2013-08-13 18:43:23 +0000
+++ formula.js 2013-08-27 14:02:01 +0000
@@ -23,12 +23,20 @@
23function Formula() {23function Formula() {
24 var previous = new Token(0, T_NUMBER);24 var previous = new Token(0, T_NUMBER);
25 var formula = '';25 var formula = '';
26 var results = [];
27 var lookupTbl = {26 var lookupTbl = {
28 '-': '−',27 '-': '−',
29 '/': '÷',28 '/': '÷',
30 '*': '×'29 '*': '×'
31 };30 };
31 var table = {
32 "65" : "+",
33 "66" : "-",
34 "67" : "*",
35 "68" : "/"
36 }
37
38 var isLastResult = false; // This var becames true if the last button pressed by user is equal
39 var isChangeSign = false; // This var becames true if user want to change the sign of a result
3240
33 var _calculate = function(func){41 var _calculate = function(func){
34 try{42 try{
@@ -48,25 +56,40 @@
48 }56 }
4957
50 this.push = function(digit){58 this.push = function(digit){
51 if(results.length > 0) throw new SyntaxError("cannot push");
52 var last = (new Scanner(previous.value.toString()+digit, context)).tokens.pop();59 var last = (new Scanner(previous.value.toString()+digit, context)).tokens.pop();
60
61 if (!isNaN(digit) && isLastResult) return; // After a calc you have to add a sign before add a number
62
53 switch(last.type){63 switch(last.type){
54 case T_PLUS:64 case T_PLUS:
55 case T_MINUS:65 case T_MINUS:
56 case T_DIV:66 case T_DIV:
57 case T_TIMES:67 case T_TIMES:
58 if(previous.type & T_OPERATOR) return;68 if (previous.type & T_OPERATOR) return; // Not two operator below
59 break;69 break;
60 case T_UNARY_PLUS:70 case T_UNARY_PLUS:
61 case T_UNARY_MINUS:71 case T_UNARY_MINUS:
62 if(previous.type === T_UNARY_MINUS || previous.type === T_UNARY_PLUS ) return;72 if(previous.type === T_UNARY_MINUS || previous.type === T_UNARY_PLUS ) return;
63 break;73 break;
64 }74 }
75
76 formulaView.headerItem.state = "newPush"; // This is to change "AC" to "C" when user continue a calc
77
78 /* After user press equal the operation we have to forget the math order
79 * and restart the operation from the result,
80 * but set formula = result is impossible,
81 * because we lost a lot of precious information
82 */
83 if (isLastResult)
84 formula = "(" + formula + ")";
85
65 formula += digit;86 formula += digit;
87 isLastResult = false;
66 previous = last;88 previous = last;
67 var ret = {value: '', type: last.type};89 var ret = {value: '', type: last.type};
68 //formula Shouldn't start with operators.90 //formula Shouldn't start with operators.
69 if(isNaN(formula.substring(0,1))){ // First letter should be digit91 var firstChar = formula.substring(0,1);
92 if(isNaN(firstChar) && firstChar !== "(" && firstChar !== "-"){ // First letter should be digit or open bracket or minus
70 ret.type = T_OPERATOR // Return93 ret.type = T_OPERATOR // Return
71 formula = ""; // Make formula var empty94 formula = ""; // Make formula var empty
72 }95 }
@@ -79,10 +102,6 @@
79 };102 };
80103
81 this.pop = function(){104 this.pop = function(){
82 if(results.length > 0){
83 results.pop();
84 return;
85 }
86 var scannedFormula = new Scanner(formula, context);105 var scannedFormula = new Scanner(formula, context);
87 var last = {type:T_NUMBER};106 var last = {type:T_NUMBER};
88 var removeSize = 0;107 var removeSize = 0;
@@ -99,42 +118,48 @@
99 var previousSize = previous.value.toString().length;118 var previousSize = previous.value.toString().length;
100 if(formula.length <= previousSize) return;119 if(formula.length <= previousSize) return;
101 var scannedFormula = new Scanner(formula, context);120 var scannedFormula = new Scanner(formula, context);
121
122 // Last and secondLast are one a number and the other an operator. Both can be both, it depends on user input
102 var last = scannedFormula.tokens.pop();123 var last = scannedFormula.tokens.pop();
103 if(results.length > 0){124 var secondLast = scannedFormula.tokens.pop();
104 var newFormula = last.value.toString();125
105 while(last.type !== T_PLUS &&126 // In this case user press equal twice E.G. 2*2=4=8
106 last.type !== T_MINUS &&127 if (isLastResult && !isChangeSign && table[secondLast.type] !== undefined)
107 last.type !== T_DIV &&128 formula = '(' + formula + ')' + table[secondLast.type] + last.value.toString();
108 last.type !== T_TIMES ){129 // In this case user press a number, and operator (or only an operator, if it isn't the first calc), and then equal
109 last = scannedFormula.tokens.pop();130 else if (last.type & T_OPERATOR)
110 newFormula = last.value.toString() + newFormula;131 formula = formula + secondLast.value.toString();
111 }132
112 result = _calculate(results[results.length-1]+newFormula);133 result = _calculate(formula);
113 } else{134
114 // check previous input135 // If user want to change sign to the result
115 if(last.type === T_PLUS ||136 if (isLastResult && isChangeSign) {
116 last.type === T_MINUS ||137 // TO-DO: check if the user has change the sign yet, and remove old *-1
117 last.type === T_DIV ||138 result = -result; // Change the sign
118 last.type === T_TIMES){139 formula = '(' + formula + ')*-1'; // Remember the change of the sign for the future
119 // getting the first value
120 var firstVal = scannedFormula.tokens.pop();
121 // adding the first value to formula
122 formula = formula + firstVal.value.toString()
123 }
124 result = _calculate(formula);
125
126 }140 }
127 results.push(result.toString());141
142 isLastResult = true;
128 return result;143 return result;
129 }144 }
130145
131 this.changeSign = function(){146 this.changeSign = function(){
132 if(results.length > 0) throw new SyntaxError("cannot edit formula");147
148 // If user want to change the sign of the result
149 if (isLastResult) {
150 isChangeSign = true;
151 screenFormula.pop(); // Delete last result from the screen
152 calculate();
153 isChangeSign = false;
154 return;
155 }
156
133 switch(previous.type){157 switch(previous.type){
134 case T_PLUS:158 case T_PLUS:
135 case T_MINUS:159 case T_MINUS:
136 case T_DIV:160 case T_DIV:
137 case T_TIMES:161 case T_TIMES:
162 // After a sign, push minus
138 formulaPush("-");163 formulaPush("-");
139 break;164 break;
140 default:165 default:
@@ -142,6 +167,8 @@
142 var last = {type: undefined};167 var last = {type: undefined};
143 var toAdd = [];168 var toAdd = [];
144 var addMinus = true;169 var addMinus = true;
170
171 // Create a queue with all element to re-add to var formula
145 while(scannedFormula.tokens.length > 0 && last.type !== T_PLUS &&172 while(scannedFormula.tokens.length > 0 && last.type !== T_PLUS &&
146 last.type !== T_MINUS &&173 last.type !== T_MINUS &&
147 last.type !== T_DIV &&174 last.type !== T_DIV &&
@@ -154,7 +181,9 @@
154 addMinus = false;181 addMinus = false;
155 }182 }
156 }183 }
184
157 formulaPop();185 formulaPop();
186
158 while(toAdd.length > 0){187 while(toAdd.length > 0){
159 var el = toAdd.pop()188 var el = toAdd.pop()
160 formulaPush(el.value.toString());189 formulaPush(el.value.toString());
161190
=== modified file 'tests/autopilot/ubuntu_calculator_app/emulators.py'
--- tests/autopilot/ubuntu_calculator_app/emulators.py 2013-08-22 14:51:26 +0000
+++ tests/autopilot/ubuntu_calculator_app/emulators.py 2013-08-27 14:02:01 +0000
@@ -193,7 +193,12 @@
193 return result_label.numbers193 return result_label.numbers
194194
195 def get_result_label(self):195 def get_result_label(self):
196 return self.select_single('CalcLabel', objectName='result')196 results = self.select_many('CalcLabel', objectName='result')
197 count = len(results)
198 if count > 0:
199 return results[count - 1]
200 else:
201 return None
197202
198 def get_operand_by_index(self, index):203 def get_operand_by_index(self, index):
199 """Return an operand of a calculation entered."""204 """Return an operand of a calculation entered."""
200205
=== modified file 'tests/autopilot/ubuntu_calculator_app/tests/test_simple_page.py'
--- tests/autopilot/ubuntu_calculator_app/tests/test_simple_page.py 2013-08-23 17:48:47 +0000
+++ tests/autopilot/ubuntu_calculator_app/tests/test_simple_page.py 2013-08-27 14:02:01 +0000
@@ -216,3 +216,12 @@
216 # self.main_view.calculate_operation("*3")216 # self.main_view.calculate_operation("*3")
217 #217 #
218 # self._assert_result("")218 # self._assert_result("")
219
220 def test_continue_calculation(self):
221 self.main_view.calculate_operation("59+1")
222
223 calc_keyboard = self.main_view.get_calc_keyboard()
224 calc_keyboard.enter_operation("+5")
225 calc_keyboard.click_equals()
226
227 self._assert_result("65")

Subscribers

People subscribed via source and target branches