"Fossies" - the Fresh Open Source Software Archive 
Member "groovy-4.0.12/subprojects/groovy-contracts/src/test/groovy/org/apache/groovy/contracts/tests/interfaces/AbstractClassTests.groovy" (31 Jan 1980, 2737 Bytes) of package /linux/misc/apache-groovy-src-4.0.12.zip:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Java source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 package org.apache.groovy.contracts.tests.interfaces
20
21 import org.apache.groovy.contracts.PreconditionViolation
22 import org.apache.groovy.contracts.tests.basic.BaseTestClass
23 import org.junit.Test
24
25 class AbstractClassTests extends BaseTestClass {
26
27 def source_stackable = '''
28 @Contracted
29 package tests
30
31 import groovy.contracts.*
32
33 abstract class Stackable {
34
35 @Requires({ item != null })
36 abstract void push(def item)
37
38 @Requires({ item1 != null && item2 != null })
39 abstract void multi_push(def item1, def item2)
40 }
41 '''
42
43 def source_stack = '''
44 @Contracted
45 package tests
46
47 import groovy.contracts.*
48
49 @Invariant({ list != null && anotherName != null })
50 class Stack extends Stackable {
51
52 protected def list
53 def anotherName = ""
54 def protected name = ""
55
56 public Stack() {
57 this.list = []
58 }
59
60 public Stack(def list) {
61 this.list = list
62 }
63
64 @Ensures({ list[-1] == item })
65 void push(def item) {
66 list.add item
67 }
68
69 void multi_push(def item1, def item2) {
70 push item1
71 push item2
72 }
73
74 // @Requires({ list.size() > 0 })
75 // @Ensures({ result != null })
76 // def Object pop() {
77 // list[-1]
78 // }
79
80 @Ensures({ result -> result == list.size() })
81 def int size() {
82 return list.size()
83 }
84
85 @Ensures({ result -> comp1 != null && comp2 != null && result > 0 })
86 def int size(def comp1, comp2) {
87 return comp1 + comp2
88 }
89
90 @Ensures({ result -> result == 'tostring'})
91 @Override
92 def String toString() {
93 return 'tostring'
94 }
95
96 void modifyClassInvariant() {
97 anotherName = null
98 }
99 }
100 '''
101
102 @Test
103 void creation() {
104 add_class_to_classpath(source_stackable)
105 create_instance_of(source_stack)
106 }
107
108 @Test
109 void push_precondition() {
110 add_class_to_classpath(source_stackable)
111 def stack = create_instance_of(source_stack)
112
113 shouldFail PreconditionViolation.class, {
114 stack.push null
115 }
116 }
117 }