From 04664bfa4c7f1aed8fecae04bfa74e13b69e7320 Mon Sep 17 00:00:00 2001 From: Florian Deljarry Date: Tue, 14 Jul 2020 11:17:28 -0400 Subject: [PATCH] tests: Add invariant contract tests Signed-off-by: Florian Deljarry --- tests/contracts_invariant_1.nit | 29 ++++++++++++ tests/contracts_invariant_attr.nit | 29 ++++++++++++ tests/contracts_invariant_defaultinit.nit | 27 +++++++++++ tests/contracts_invariant_diamond.nit | 42 +++++++++++++++++ tests/contracts_invariant_in_redef.nit | 23 +++++++++ tests/contracts_invariant_inheritance.nit | 34 ++++++++++++++ .../contracts_invariant_inheritance_multi.nit | 47 +++++++++++++++++++ ...ontracts_invariant_inheritance_multi_2.nit | 35 ++++++++++++++ tests/sav/contracts_invariant_1.res | 1 + tests/sav/contracts_invariant_attr.res | 0 tests/sav/contracts_invariant_defaultinit.res | 1 + tests/sav/contracts_invariant_diamond.res | 2 + tests/sav/contracts_invariant_in_redef.res | 1 + tests/sav/contracts_invariant_inheritance.res | 3 ++ .../contracts_invariant_inheritance_multi.res | 5 ++ ...ontracts_invariant_inheritance_multi_2.res | 4 ++ 16 files changed, 283 insertions(+) create mode 100644 tests/contracts_invariant_1.nit create mode 100644 tests/contracts_invariant_attr.nit create mode 100644 tests/contracts_invariant_defaultinit.nit create mode 100644 tests/contracts_invariant_diamond.nit create mode 100644 tests/contracts_invariant_in_redef.nit create mode 100644 tests/contracts_invariant_inheritance.nit create mode 100644 tests/contracts_invariant_inheritance_multi.nit create mode 100644 tests/contracts_invariant_inheritance_multi_2.nit create mode 100644 tests/sav/contracts_invariant_1.res create mode 100644 tests/sav/contracts_invariant_attr.res create mode 100644 tests/sav/contracts_invariant_defaultinit.res create mode 100644 tests/sav/contracts_invariant_diamond.res create mode 100644 tests/sav/contracts_invariant_in_redef.res create mode 100644 tests/sav/contracts_invariant_inheritance.res create mode 100644 tests/sav/contracts_invariant_inheritance_multi.res create mode 100644 tests/sav/contracts_invariant_inheritance_multi_2.res diff --git a/tests/contracts_invariant_1.nit b/tests/contracts_invariant_1.nit new file mode 100644 index 0000000000..dca5e0421b --- /dev/null +++ b/tests/contracts_invariant_1.nit @@ -0,0 +1,29 @@ +# 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 and usage of simple invariant contract. + +class Test + invariant(bar >= 10) + + var bar: Int + + fun set_bar(x: Int) do + print x + bar = x + end +end + +var test = new Test(10) +test.set_bar(10)# Fail broke the invariant diff --git a/tests/contracts_invariant_attr.nit b/tests/contracts_invariant_attr.nit new file mode 100644 index 0000000000..98945d957c --- /dev/null +++ b/tests/contracts_invariant_attr.nit @@ -0,0 +1,29 @@ +# 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 and usage of simple invariant contract with attribute. + +class Test + invariant(bar >= 10) + + var bar: Int + + fun set_bar(x: Int) do + print x + bar = x + end +end + +var test = new Test(10) +test.bar = 9 # Fail broke the invariant diff --git a/tests/contracts_invariant_defaultinit.nit b/tests/contracts_invariant_defaultinit.nit new file mode 100644 index 0000000000..a546273b2b --- /dev/null +++ b/tests/contracts_invariant_defaultinit.nit @@ -0,0 +1,27 @@ +# 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 A + invariant(i > 0) + + var i: Int + + init + do + + end + +end + +var a = new A(0) diff --git a/tests/contracts_invariant_diamond.nit b/tests/contracts_invariant_diamond.nit new file mode 100644 index 0000000000..c8196fa700 --- /dev/null +++ b/tests/contracts_invariant_diamond.nit @@ -0,0 +1,42 @@ +# 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 A + invariant(bar >= 10) + + var bar: Int +end + +class B + invariant(bar > 10) + super A + + fun set_bar(x: Int) do + print x + bar = x + end +end + +class C + invariant(bar > 12) + super A +end + +class D + super B + super C +end + +var test = new D(13) +test.set_bar(11) diff --git a/tests/contracts_invariant_in_redef.nit b/tests/contracts_invariant_in_redef.nit new file mode 100644 index 0000000000..336e197254 --- /dev/null +++ b/tests/contracts_invariant_in_redef.nit @@ -0,0 +1,23 @@ +# 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. +import contracts_abstract + +# Test if the invariant contract is added to old method definitions. + +redef class MySubClass + invariant(false) +end + +var test = new MySubClass +test.foo(11, 2.5) diff --git a/tests/contracts_invariant_inheritance.nit b/tests/contracts_invariant_inheritance.nit new file mode 100644 index 0000000000..78374464c6 --- /dev/null +++ b/tests/contracts_invariant_inheritance.nit @@ -0,0 +1,34 @@ +# 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 and usage of invariant contracts with inheritance. + +class A + invariant(bar >= 10) + + var bar: Int +end + +class B + super A + + fun set_bar(x: Int) do + print x + bar = x + end +end + +var test = new B(10) +test.set_bar(11) # OK +test.set_bar(2) # Fail broke invariant bar >= 10 diff --git a/tests/contracts_invariant_inheritance_multi.nit b/tests/contracts_invariant_inheritance_multi.nit new file mode 100644 index 0000000000..75a7dadbc9 --- /dev/null +++ b/tests/contracts_invariant_inheritance_multi.nit @@ -0,0 +1,47 @@ +# 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. +module contracts_invariant_inheritance_multi + +# Test the creation and usage of invariant contracts with multiple inheritance. + +class A + invariant(bar >= 10) + + var bar: Int +end + +class B + invariant(baz <= 2.0) + + var baz: Float +end + +class C + super A + super B + + autoinit bar=, baz= + + fun set_bar_baz(x: Int, y: Float) + do + print x + bar = x + print y + baz = y + end +end + +var test = new C(10, 2.0) +test.set_bar_baz(16, 1.5)# Ok +test.set_bar_baz(1, 3.8)# Fail diff --git a/tests/contracts_invariant_inheritance_multi_2.nit b/tests/contracts_invariant_inheritance_multi_2.nit new file mode 100644 index 0000000000..8fb8eb9512 --- /dev/null +++ b/tests/contracts_invariant_inheritance_multi_2.nit @@ -0,0 +1,35 @@ +# 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. +import contracts_invariant_inheritance_multi + +# Test import a class with invariant and add a new one by new inheritance + +class E + invariant(bazz) + + var bazz = true +end + +redef class C + super E + + redef fun set_bar_baz(x: Int, y: Float) do + super + self.bazz = false + print bazz + end +end + +var test = new C(10, 2.0) +test.set_bar_baz(10, 2.0)# The method broke the E invariant with the set bazz = false diff --git a/tests/sav/contracts_invariant_1.res b/tests/sav/contracts_invariant_1.res new file mode 100644 index 0000000000..f599e28b8a --- /dev/null +++ b/tests/sav/contracts_invariant_1.res @@ -0,0 +1 @@ +10 diff --git a/tests/sav/contracts_invariant_attr.res b/tests/sav/contracts_invariant_attr.res new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/sav/contracts_invariant_defaultinit.res b/tests/sav/contracts_invariant_defaultinit.res new file mode 100644 index 0000000000..95ba739301 --- /dev/null +++ b/tests/sav/contracts_invariant_defaultinit.res @@ -0,0 +1 @@ +Runtime error: Assert 'invariant(i > 0)' failed (contracts_invariant_defaultinit.nit:16) diff --git a/tests/sav/contracts_invariant_diamond.res b/tests/sav/contracts_invariant_diamond.res new file mode 100644 index 0000000000..ccd3163337 --- /dev/null +++ b/tests/sav/contracts_invariant_diamond.res @@ -0,0 +1,2 @@ +Runtime error: Assert 'invariant(bar > 12)' failed (contracts_invariant_diamond.nit:32) +11 diff --git a/tests/sav/contracts_invariant_in_redef.res b/tests/sav/contracts_invariant_in_redef.res new file mode 100644 index 0000000000..c49a3b43c4 --- /dev/null +++ b/tests/sav/contracts_invariant_in_redef.res @@ -0,0 +1 @@ +Runtime error: Assert 'invariant(false)' failed (contracts_invariant_in_redef.nit:19) diff --git a/tests/sav/contracts_invariant_inheritance.res b/tests/sav/contracts_invariant_inheritance.res new file mode 100644 index 0000000000..dd226fa9dc --- /dev/null +++ b/tests/sav/contracts_invariant_inheritance.res @@ -0,0 +1,3 @@ +Runtime error: Assert 'invariant(bar >= 10)' failed (contracts_invariant_inheritance.nit:18) +11 +2 diff --git a/tests/sav/contracts_invariant_inheritance_multi.res b/tests/sav/contracts_invariant_inheritance_multi.res new file mode 100644 index 0000000000..4372043fb8 --- /dev/null +++ b/tests/sav/contracts_invariant_inheritance_multi.res @@ -0,0 +1,5 @@ +Runtime error: Assert 'invariant(bar >= 10)' failed (contracts_invariant_inheritance_multi.nit:19) +16 +1.5 +1 +3.8 diff --git a/tests/sav/contracts_invariant_inheritance_multi_2.res b/tests/sav/contracts_invariant_inheritance_multi_2.res new file mode 100644 index 0000000000..81f81265ee --- /dev/null +++ b/tests/sav/contracts_invariant_inheritance_multi_2.res @@ -0,0 +1,4 @@ +Runtime error: Assert 'invariant(bazz)' failed (contracts_invariant_inheritance_multi_2.nit:19) +10 +2.0 +false