contracts: change the contract syntax
[nit.git] / tests / contracts_add.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # Verification if it's possible to add a other contract type on existing method.
16 # Only the expect contract display a warning when none expect contract are not defined at the introduction
17
18 class MyClass
19 fun foo(x: Int)
20 is
21 expect(x == 1)
22 do
23 x=1
24 end
25
26 fun bar(x: Float): Bool
27 is
28 ensure(result)
29 do
30 return true
31 end
32 end
33
34 class MyClass2
35 super MyClass
36
37 redef fun foo(x: Int)
38 is
39 ensure(x == 0)
40 do
41 x=0
42 end
43
44 redef fun bar(x: Float)
45 is
46 expect(x == 1)
47 do
48 return true
49 end
50 end
51
52 var first = new MyClass2
53 first.foo(1)
54 first.bar(1.0)
55 first.foo(0)# Fail because the expect is x == 1