contracts: change the contract syntax
[nit.git] / tests / contracts_abstract.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 define a contract in different context (abstract class, interface and class) and it's possible to inherit it.
16
17 class MyClass
18 fun foo(x: Int, y: Float)is abstract, expect(x != 10)
19 end
20
21 abstract class AbstractClass
22 fun bar(x: Int, y: Float)is abstract, expect(x >= 1), ensure(y == 10.0)
23 end
24
25 interface Interface
26 fun baz(x: Int, y: Float)is abstract, ensure(y <= 10.0, y == 42.0)
27 end
28
29
30 class MySubClass
31 super MyClass
32 super Interface
33 super AbstractClass
34
35 redef fun foo(x: Int, y: Float)
36 do
37 if x == 10 then print "Error"
38 end
39
40 redef fun baz(x: Int, y: Float)
41 do
42
43 end
44
45 redef fun bar(x: Int, y: Float)
46 do
47 if x < 1 then print "Error"
48 end
49 end
50
51
52 var first = new MySubClass
53 first.foo(11,2.0) # Ok
54 first.bar(1,10.0) # Ok
55 first.baz(9,42.0) # Fail y > 10 (y = 42.0)