1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2013-2014 Alexis Laferrière <alexis.laf@xymus.net>
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 # Test the business logic
18 module calculator_test
20 import calculator_logic
23 var context
= new CalculatorContext
24 context
.push_digit
( 1 )
25 context
.push_digit
( 2 )
26 context
.push_op
( "+" )
27 context
.push_digit
( 3 )
28 context
.push_op
( "*" )
29 context
.push_digit
( 2 )
30 context
.push_op
( "=" )
31 var r
= context
.result
32 assert r
== 30 else print r
or else "-"
34 context
= new CalculatorContext
35 context
.push_digit
( 1 )
36 context
.push_digit
( 4 )
37 context
.switch_to_decimals
38 context
.push_digit
( 1 )
39 context
.push_op
( "*" )
40 context
.push_digit
( 3 )
41 context
.push_op
( "=" )
43 assert r
== 42.3 else print r
or else "-"
45 context
.push_op
( "+" )
46 context
.push_digit
( 1 )
47 context
.push_digit
( 1 )
48 context
.push_op
( "=" )
50 assert r
== 53.3 else print r
or else "-"
52 context
= new CalculatorContext
53 context
.push_digit
( 4 )
54 context
.push_digit
( 2 )
55 context
.switch_to_decimals
56 context
.push_digit
( 3 )
57 context
.push_op
( "/" )
58 context
.push_digit
( 3 )
59 context
.push_op
( "=" )
61 assert r
== 14.1 else print r
or else "-"
63 #test multiple decimals
64 context
= new CalculatorContext
65 context
.push_digit
( 5 )
66 context
.push_digit
( 0 )
67 context
.switch_to_decimals
68 context
.push_digit
( 1 )
69 context
.push_digit
( 2 )
70 context
.push_digit
( 3 )
71 context
.push_op
( "+" )
72 context
.push_digit
( 1 )
73 context
.push_op
( "=" )
75 assert r
== 51.123 else print r
or else "-"
78 context
= new CalculatorContext
79 context
.push_digit
( 1 )
80 context
.push_digit
( 0 )
81 context
.push_op
( "+" )
82 context
.push_digit
( 1 )
83 context
.push_digit
( 0 )
84 context
.push_op
( "=" )
85 context
.push_op
( "C" )
87 assert r
== null else print r