Package: TestSuite

TestSuite

nameinstructionbranchcomplexitylinemethod
TestSuite()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
createTests(Value)
M: 258 C: 0
0%
M: 24 C: 0
0%
M: 13 C: 0
0%
M: 44 C: 0
0%
M: 1 C: 0
0%
getConstructor(ObjectValue, String)
M: 96 C: 0
0%
M: 20 C: 0
0%
M: 11 C: 0
0%
M: 19 C: 0
0%
M: 1 C: 0
0%
getTestConstructor(ObjectValue)
M: 12 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
getTestMethodNamed(Value)
M: 64 C: 0
0%
M: 8 C: 0
0%
M: 5 C: 0
0%
M: 12 C: 0
0%
M: 1 C: 0
0%

Coverage

1: import java.util.List;
2: import java.util.Vector;
3:
4: import org.overture.ast.definitions.AExplicitOperationDefinition;
5: import org.overture.ast.definitions.PDefinition;
6: import org.overture.ast.types.AOperationType;
7: import org.overture.interpreter.runtime.ClassInterpreter;
8: import org.overture.interpreter.runtime.Context;
9: import org.overture.interpreter.runtime.Interpreter;
10: import org.overture.interpreter.runtime.ObjectContext;
11: import org.overture.interpreter.runtime.StateContext;
12: import org.overture.interpreter.values.NameValuePair;
13: import org.overture.interpreter.values.ObjectValue;
14: import org.overture.interpreter.values.OperationValue;
15: import org.overture.interpreter.values.SeqValue;
16: import org.overture.interpreter.values.Value;
17: import org.overture.interpreter.values.ValueList;
18:
19: public class TestSuite
20: {
21:         public static Value getTestMethodNamed(Value test)
22:         {
23:                 List<String> tests = new Vector<String>();
24:                 ObjectValue instance = (ObjectValue) test;
25:
26:•                for (NameValuePair p : instance.members.asList())
27:                 {
28:•                        if (p.value instanceof OperationValue)
29:                         {
30:•                                if (p.name.getName().toLowerCase().startsWith("test"))
31:                                 {
32:                                         tests.add(p.name.getName());
33:                                 }
34:                         }
35:                 }
36:
37:                 ValueList vals = new ValueList();
38:•                for (String value : tests)
39:                 {
40:                         vals.add(new SeqValue(value));
41:                 }
42:
43:                 return new SeqValue(vals);
44:         }
45:
46:         public static Value createTests(Value test) throws Exception
47:         {
48:                 List<String> tests = new Vector<String>();
49:                 ValueList vals = new ValueList();
50:                 ObjectValue instance = (ObjectValue) test;
51:
52:•                for (NameValuePair p : instance.members.asList())
53:                 {
54:•                        if (p.value instanceof OperationValue)
55:                         {
56:                                 OperationValue opVal = (OperationValue) p.value;
57:•                                if (!opVal.isConstructor
58:•                                                && p.name.getName().toLowerCase().startsWith("test"))
59:                                 {
60:                                         tests.add(p.name.getName());
61:
62:                                         Context mainContext = new StateContext(Interpreter.getInstance().getAssistantFactory(), p.name.getLocation(), "reflection scope");
63:
64:                                         mainContext.putAll(ClassInterpreter.getInstance().initialContext);
65:                                         mainContext.setThreadState(ClassInterpreter.getInstance().initialContext.threadState.dbgp, ClassInterpreter.getInstance().initialContext.threadState.CPU);
66:
67:                                         try
68:                                         {
69:                                                 AExplicitOperationDefinition ctor = getTestConstructor(instance);
70:•                                                if (ctor == null
71:•                                                                || !ctor.getName().getModule().equals(instance.type.getName().getLocation().getModule())
72:•                                                                && ctor.getParamDefinitions().isEmpty()
73:•                                                                || !ClassInterpreter.getInstance().getAssistantFactory().createPAccessSpecifierAssistant().isPublic(ctor.getAccess()))
74:                                                 {
75:                                                         throw new Exception("Class "
76:                                                                         + p.name.getModule()
77:                                                                         + " has no public constructor TestCase(String name) or TestCase()");
78:                                                 }
79:
80:                                                 String vdmTestExpression = "";
81:•                                                if (((AOperationType) ctor.getType()).getParameters().size() == 1)
82:                                                 {
83:                                                         vdmTestExpression = "new " + p.name.getModule()
84:                                                                         + "(\"" + p.name.getName() + "\")";
85:                                                         vals.add(ClassInterpreter.getInstance().evaluate(vdmTestExpression, mainContext));
86:                                                 } else
87:                                                 {
88:                                                         boolean foundSetName = false;
89:                                                         // check that we have setName and that it is accesiable
90:•                                                        for (PDefinition def : Interpreter.getInstance().initialContext.assistantFactory.createPDefinitionAssistant().getDefinitions(instance.type.getClassdef()))
91:                                                         {
92:•                                                                if (def.getName().getName().equals("setName"))
93:                                                                 {
94:                                                                         foundSetName = true;
95:                                                                 }
96:                                                         }
97:•                                                        if (!foundSetName)
98:                                                         {
99:                                                                 throw new Exception("Cannot instantiate test case: "
100:                                                                                 + instance.type.getName().getName());
101:                                                         }
102:
103:                                                         vdmTestExpression = "new " + p.name.getModule()
104:                                                                         + "()";
105:                                                         Value testClassInstance = ClassInterpreter.getInstance().evaluate(vdmTestExpression, mainContext);
106:                                                         ObjectValue tmp = (ObjectValue) testClassInstance;
107:                                                         ObjectContext octxt = new ObjectContext(Interpreter.getInstance().getAssistantFactory(), mainContext.location, "TestClassContext", mainContext, tmp);
108:                                                         vdmTestExpression = "setName(\"" + p.name.getName()
109:                                                                         + "\")";
110:
111:                                                         ClassInterpreter.getInstance().evaluate(vdmTestExpression, octxt);
112:                                                         vals.add(testClassInstance);
113:                                                 }
114:
115:                                         } catch (Exception e)
116:                                         {
117:                                                 throw e;
118:                                         }
119:                                 }
120:                         }
121:                 }
122:                 return new SeqValue(vals);
123:         }
124:
125:         private static AExplicitOperationDefinition getTestConstructor(
126:                         ObjectValue instance)
127:         {
128:                 AExplicitOperationDefinition ctor = getConstructor(instance, "seq of (char)");
129:•                if (ctor != null)
130:                 {
131:                         return ctor;
132:                 }
133:                 return getConstructor(instance, "()");
134:         }
135:
136:         private static AExplicitOperationDefinition getConstructor(
137:                         ObjectValue instance, String typeName)
138:         {
139:                 boolean searchForDefaultCtor = typeName.trim().equals("()");
140:                 AExplicitOperationDefinition defaultSuperCtor = null;
141:•                for (NameValuePair pair : instance.members.asList())
142:                 {
143:•                        if (pair.value instanceof OperationValue)
144:                         {
145:                                 OperationValue op = (OperationValue) pair.value;
146:•                                if (op.isConstructor)
147:                                 {
148:•                                        if (searchForDefaultCtor
149:•                                                        && ((AOperationType) op.expldef.getType()).getParameters().isEmpty())
150:                                         {
151:•                                                if (op.expldef.getName().equals(instance.type.getName().getName()))
152:                                                 {
153:                                                         return op.expldef;
154:                                                 } else
155:                                                 {
156:                                                         defaultSuperCtor = op.expldef;
157:                                                 }
158:•                                        } else if (((AOperationType) op.expldef.getType()).getParameters().size() == 1
159:•                                                        && Interpreter.getInstance().getAssistantFactory().createPTypeAssistant().isType(((AOperationType) op.expldef.getType()).getParameters().get(0), typeName) != null
160:•                                                        && op.expldef.getName().equals(instance.type.getName().getName()))
161:                                         {
162:                                                 return op.expldef;
163:                                         }
164:                                 }
165:                         }
166:                 }
167:
168:•                if (searchForDefaultCtor)
169:                 {
170:                         return defaultSuperCtor;
171:                 }
172:                 return null;
173:         }
174: }