Skip to content

Commit

Permalink
tests: Add invariant contract tests
Browse files Browse the repository at this point in the history
Signed-off-by: Florian Deljarry <[email protected]>
  • Loading branch information
Delja committed Jul 14, 2020
1 parent 8593070 commit 04664bf
Show file tree
Hide file tree
Showing 16 changed files with 283 additions and 0 deletions.
29 changes: 29 additions & 0 deletions tests/contracts_invariant_1.nit
Original file line number Diff line number Diff line change
@@ -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
29 changes: 29 additions & 0 deletions tests/contracts_invariant_attr.nit
Original file line number Diff line number Diff line change
@@ -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
27 changes: 27 additions & 0 deletions tests/contracts_invariant_defaultinit.nit
Original file line number Diff line number Diff line change
@@ -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)
42 changes: 42 additions & 0 deletions tests/contracts_invariant_diamond.nit
Original file line number Diff line number Diff line change
@@ -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)
23 changes: 23 additions & 0 deletions tests/contracts_invariant_in_redef.nit
Original file line number Diff line number Diff line change
@@ -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)
34 changes: 34 additions & 0 deletions tests/contracts_invariant_inheritance.nit
Original file line number Diff line number Diff line change
@@ -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
47 changes: 47 additions & 0 deletions tests/contracts_invariant_inheritance_multi.nit
Original file line number Diff line number Diff line change
@@ -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
35 changes: 35 additions & 0 deletions tests/contracts_invariant_inheritance_multi_2.nit
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions tests/sav/contracts_invariant_1.res
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
10
Empty file.
1 change: 1 addition & 0 deletions tests/sav/contracts_invariant_defaultinit.res
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Runtime error: Assert 'invariant(i > 0)' failed (contracts_invariant_defaultinit.nit:16)
2 changes: 2 additions & 0 deletions tests/sav/contracts_invariant_diamond.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Runtime error: Assert 'invariant(bar > 12)' failed (contracts_invariant_diamond.nit:32)
11
1 change: 1 addition & 0 deletions tests/sav/contracts_invariant_in_redef.res
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Runtime error: Assert 'invariant(false)' failed (contracts_invariant_in_redef.nit:19)
3 changes: 3 additions & 0 deletions tests/sav/contracts_invariant_inheritance.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Runtime error: Assert 'invariant(bar >= 10)' failed (contracts_invariant_inheritance.nit:18)
11
2
5 changes: 5 additions & 0 deletions tests/sav/contracts_invariant_inheritance_multi.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Runtime error: Assert 'invariant(bar >= 10)' failed (contracts_invariant_inheritance_multi.nit:19)
16
1.5
1
3.8
4 changes: 4 additions & 0 deletions tests/sav/contracts_invariant_inheritance_multi_2.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Runtime error: Assert 'invariant(bazz)' failed (contracts_invariant_inheritance_multi_2.nit:19)
10
2.0
false

0 comments on commit 04664bf

Please sign in to comment.