--- /dev/null
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Test the creation ans the usage on simply contract, in the same time check the no modification of the parameter.
+# See the `foo` method in `Myclass` the change of x in a body method has no effect on the ensure because x is a primitive type and it's given by copy and not by reference.
+
+class MyClass
+ fun foo(x: Int)
+ is
+ expects(x == 1)
+ ensures(x > 0)
+ do
+ x = 0
+ end
+end
+
+class MyClass2
+ fun foo(bool: Bool)
+ is
+ expects(not bool)
+ ensures(not bool)
+ do
+ if bool then print "Error"
+ end
+end
+
+var first = new MyClass
+first.foo(1)
+var second = new MyClass2
+second.foo(true) #Fail
--- /dev/null
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Verification if it's possible to define a contract in different context (abstract class, interface and class) and it's possible to inherit it.
+
+class MyClass
+ fun foo(x: Int, y: Float)is abstract, expects(x != 10)
+end
+
+abstract class AbstractClass
+ fun bar(x: Int, y: Float)is abstract, expects(x >= 1), ensures(y == 10.0)
+end
+
+interface Interface
+ fun baz(x: Int, y: Float)is abstract, ensures(y <= 10.0, y == 42.0)
+end
+
+
+class MySubClass
+ super MyClass
+ super Interface
+ super AbstractClass
+
+ redef fun foo(x: Int, y: Float)
+ do
+ if x == 10 then print "Error"
+ end
+
+ redef fun baz(x: Int, y: Float)
+ do
+
+ end
+
+ redef fun bar(x: Int, y: Float)
+ do
+ if x < 1 then print "Error"
+ end
+end
+
+
+var first = new MySubClass
+first.foo(11,2.0) # Ok
+first.bar(1,10.0) # Ok
+first.baz(9,42.0) # Fail y > 10 (y = 42.0)
--- /dev/null
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Verification if it's possible to add a other contract type on existing method.
+# Only the expect contract display a warning when none expect contract are not defined at the introduction
+
+class MyClass
+ fun foo(x: Int)
+ is
+ expects(x == 1)
+ do
+ x=1
+ end
+
+ fun bar(x: Float): Bool
+ is
+ ensures(result)
+ do
+ return true
+ end
+end
+
+class MyClass2
+ super MyClass
+
+ redef fun foo(x: Int)
+ is
+ ensures(x == 0)
+ do
+ x=0
+ end
+
+ redef fun bar(x: Float)
+ is
+ expects(x == 1)
+ do
+ return true
+ end
+end
+
+var first = new MyClass2
+first.foo(1)
+first.bar(1.0)
+first.foo(0)# Fail because the expect is x == 1
--- /dev/null
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Verification if it's possible to define a contract with a call of an attribute and a method call
+
+class MyClass
+
+ var bar = 10
+
+ fun foo(x: Int)
+ is
+ expects(bar == 10)
+ ensures(x > 0)
+ do
+ if bar != 10 then print "Error"
+ end
+end
+
+class MyClass2
+
+ var my_class: MyClass
+
+ fun baz: Bool
+ do
+ return false
+ end
+
+ fun foo(bool: Bool)
+ is
+ expects(not self.baz)
+ ensures(my_class.bar == 11)
+ do
+ if baz then print "Error"
+ my_class.bar = 11
+ end
+end
+
+var first = new MyClass
+first.foo(1) # Ok
+var second = new MyClass2(first)
+second.foo(false) # Ok
--- /dev/null
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Test the contract on constructor.
+
+class MyClass
+ init construct(test: Int)
+ is
+ expects(test > 10)
+ do
+
+ end
+end
+
+var first = new MyClass.construct(13)
+var second = new MyClass.construct(9)
--- /dev/null
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Verification if it's possible to define a simple ensures contract.
+
+class MyClass
+ fun foo(x: Int)
+ is
+ ensures(x > 0)
+ do
+
+ end
+end
+
+class MyClass2
+ fun foo(bool: Bool)
+ is
+ ensures(not bool)
+ do
+
+ end
+end
+
+var first = new MyClass
+first.foo(1)
+var second = new MyClass2
+second.foo(true) #Fail because the ensure is bool == false
--- /dev/null
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Verification if the contract is inherited
+
+class MyClass
+ fun foo(x: Int)
+ is
+ ensures(x > 0)
+ do
+ print "Good"
+ end
+end
+
+class SubClass
+ super MyClass
+
+ redef fun foo(x: Int) do
+ print "Fail"
+ end
+end
+
+var first = new MyClass
+first.foo(1)
+var sub = new SubClass
+# Check if the contract is inherited
+sub.foo(0) # Fail
\ No newline at end of file
--- /dev/null
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Check if the expects contract is redef with `and`
+
+class MyClass
+ fun foo(x: Int, y: Float)
+ is
+ ensures(x > 0)
+ do
+ print "Good"
+ end
+end
+
+class SubClass
+ super MyClass
+
+ redef fun foo(x: Int, y: Float)
+ is
+ ensures(y == 1.2)
+ do
+ print "Good"
+ end
+end
+
+var first = new MyClass
+first.foo(1,1.2) # OK
+var sub = new SubClass
+sub.foo(1,1.2) # OK
+sub.foo(1,1.0) # Fail
\ No newline at end of file
--- /dev/null
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Check the ensures with a contract on a result (the return result of the method)
+
+class MyClass
+ fun foo(x: Int): Int
+ is
+ ensures(result > 0)
+ do
+ return x
+ end
+end
+
+var first = new MyClass
+first.foo(1) # OK
+first.foo(0) # FAIL
--- /dev/null
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Check the result with the super call
+
+class MyClass
+ fun foo(x: Int): Bool
+ is
+ ensures(x > 0, result)
+ do
+ return true
+ end
+end
+
+class MySubClass
+ super MyClass
+
+ redef fun foo(x: Int)
+ is
+ ensures(not result)
+ do
+ return super
+ end
+end
+
+var first = new MyClass
+first.foo(1)
+var second = new MySubClass
+second.foo(2) #Fail
--- /dev/null
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+class MyClass
+ fun foo(x: Int, y: Float)
+ is
+ ensures(x > 2)
+ do
+
+ end
+end
+
+class MySubClass
+ super MyClass
+
+ redef fun foo(x: Int, y: Float)
+ is
+ ensures(y > 1.0)
+ do
+
+ end
+end
+
+var first = new MyClass
+first.foo(2, 1.1)
+var second = new MySubClass
+second.foo(1, 0.5) #Fail
--- /dev/null
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Test the error if the annotation parameter is not an good expression (ie is not a comparisons or a method calls returning bouleans)
+
+class MyClass
+
+ fun bar_no_return do end
+
+ fun foo(x: Int)
+ is
+ expects(bar_no_return)
+ ensures(assert x == 1)
+ do
+ x = 0
+ end
+end
+
+var first = new MyClass
+first.foo(1)
--- /dev/null
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Verification if it's possible to define a simple expects contract.
+
+class MyClass
+ fun foo(x: Int)
+ is
+ expects(x > 0)
+ do
+ if x <= 0 then print "FAIL"
+ end
+end
+
+class MyClass2
+ fun foo(bool: Bool)
+ is
+ expects(not bool)
+ do
+ if bool then print "FAIL"
+ end
+end
+
+var first = new MyClass
+first.foo(1) # OK
+var second = new MyClass2
+second.foo(false) # OK
--- /dev/null
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Check if the contract is inherited
+
+class MyClass
+ fun foo(x: Int)
+ is
+ expects(x > 0)
+ do
+ if x <= 0 then print "FAIL"
+ end
+end
+
+class SubClass
+ super MyClass
+
+ redef fun foo(x: Int) do
+ if x <= 0 then print "FAIL"
+ end
+end
+
+var first = new MyClass
+first.foo(1) # OK
+var sub = new SubClass
+sub.foo(0) # Fail
--- /dev/null
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Check if the expects contract is redef with `or`
+
+class MyClass
+ fun foo(x: Int)
+ is
+ expects(x > 0)
+ do
+ if x <= 0 then print "FAIL"
+ end
+end
+
+class SubClass
+ super MyClass
+
+ redef fun foo(x: Int)
+ is
+ expects(x == 0)
+ do
+ if x < 0 then print "FAIL"
+ end
+end
+
+var first = new MyClass
+first.foo(1)
+var sub = new SubClass
+sub.foo(3) # OK
+sub.foo(0) # OK
--- /dev/null
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Check if the adding expects contract in a redefinition on an existing method return a warning
+
+class MyClass
+ fun foo(x: Int)
+ do
+ print "Good"
+ end
+end
+
+class SubClass
+ super MyClass
+
+ redef fun foo(x: Int)
+ is
+ expects(x == 0)
+ do
+ if x != 0 then print "Good"
+ end
+end
+
+class SubSubClass
+ super SubClass
+
+ redef fun foo(x: Int)
+ do
+ if x != 0 then print "Good"
+ end
+end
+
+var first = new MyClass
+first.foo(1) # OK
+var sub = new SubClass
+sub.foo(0) # OK
+var subsub = new SubSubClass
+subsub.foo(1) # Ok
--- /dev/null
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Check usage of contract on a diamond inheritance
+
+class ArrayInt
+ super Array[Int]
+
+ fun toto(e: Int)
+ do
+ print "toto ArrayInt"
+ end
+end
+
+
+class MyArrayInt
+ super ArrayInt
+
+ redef fun toto(e)
+ is
+ ensures(e == 12)
+ do
+ print "toto MyArrayInt"
+ super e
+ end
+end
+
+class MyArrayInt2
+ super ArrayInt
+
+ redef fun toto(e)
+ is
+ ensures(e == 11)
+ do
+ print "toto MyArrayInt2"
+ super e
+ print "Good"
+ end
+end
+
+class MySubArray
+ super MyArrayInt
+ super MyArrayInt2
+end
+
+var test = new MySubArray
+test.toto(11)# fail contract on MyArrayInt define e == 12
--- /dev/null
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Test when to same contracts are use in a same method definition
+
+class MyClass
+
+ fun foo(x: Int)
+ is
+ expects(x == 10)
+ expects(x >= 10)
+ do
+ x = 0
+ end
+end
+
+var first = new MyClass
+first.foo(10)
--- /dev/null
+Runtime error: Assert 'expects' failed (contracts.nit:31)
--- /dev/null
+Runtime error: Assert 'ensures' failed (contracts_abstract.nit:26)
--- /dev/null
+contracts_add.nit:46,3--17: Useless contract: No contract defined at the introduction of the method
+Runtime error: Assert 'ensures' failed (contracts_add.nit:39)
--- /dev/null
+Runtime error: Assert 'expects' failed (contracts_constructor.nit:20)
--- /dev/null
+Runtime error: Assert 'ensures' failed (contracts_ensures.nit:29)
--- /dev/null
+Runtime error: Assert 'ensures' failed (contracts_ensures_1.nit:20)
+Good
+Fail
--- /dev/null
+Runtime error: Assert 'ensures' failed (contracts_ensures_2.nit:31)
+Good
+Good
+Good
--- /dev/null
+Runtime error: Assert 'ensures' failed (contracts_ensures_3.nit:20)
--- /dev/null
+Runtime error: Assert 'ensures' failed (contracts_ensures_4.nit:31)
--- /dev/null
+Runtime error: Assert 'ensures' failed (contracts_ensures_5.nit:29)
--- /dev/null
+Runtime error: Assert 'ensures' failed (contracts_ensures_sequence.nit:18)
--- /dev/null
+contracts_error.nit:23,11--23: Error: expected an expression.
+contracts_error.nit:24,11--23: Error: expected an expression.
--- /dev/null
+Runtime error: Assert 'expects' failed (contracts_expects_1.nit:20)
--- /dev/null
+contracts_expects_3.nit:29,3--17: Useless contract: No contract defined at the introduction of the method
+Good
+Good
--- /dev/null
+contracts_inheritance.nit:58,6--9: Warning: conflicting property definitions for property `toto` in `MySubArray`: contracts_inheritance$MyArrayInt$toto contracts_inheritance$MyArrayInt2$toto
+Runtime error: Assert 'ensures' failed (contracts_inheritance.nit:32)
+toto MyArrayInt2
+toto MyArrayInt
+toto ArrayInt
+Good
--- /dev/null
+contracts_same_contract.nit:22,3--18: The method already has a defined `expects` contract at line 21
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
- opts="--warn --warning --quiet --stop-on-first-error --keep-going --no-color --log --log-dir --nit-dir --help --version --set-dummy-tool --verbose --bash-completion --stub-man --option-a --option-b"
+ opts="--warn --warning --quiet --stop-on-first-error --keep-going --no-color --log --log-dir --nit-dir --help --version --set-dummy-tool --verbose --bash-completion --stub-man --no-contract --full-contract --option-a --option-b"
if [[ ${cur} == -* ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
return 0
-h, -?, --help Show Help (This screen)
--version Show version and exit
-v, --verbose Additional messages from the tool
+ --no-contract Disable the contracts usage
+ --full-contract Enable all contracts usage
-a, --option-a option a, do nothing
-b, --option-b option b, do nothing
-c option c, do nothing