public void IsWithValue_ConditionFulfilledAndMessage_DoesNotThrowException() { // Arrange object original = new object(); // Act var returnedValue = Postcondition.Is(original, true, "I will survive :)."); // Arrange Check.That(returnedValue).IsSameReferenceAs(original); }
public void NotNull_ObjectNotNull_DoesNotThrowException() { // Arrange var reference = new object(); // Act var returnedValue = Postcondition.NotNull(reference); // Assert Check.That(returnedValue).IsSameReferenceAs(reference); }
public void Is_ConditionNotFulfilledWithMessage_DoesThrowException() { // Arrange bool exceptionThrown = false; PostConditionViolatedException exception = null; // Act try { Postcondition.Is(false, "Test message"); } catch (Exception ex) { exceptionThrown = true; exception = ex as PostConditionViolatedException; } // Assert Check.That(exceptionThrown).IsTrue(); Check.That(exception).IsInstanceOf <PostConditionViolatedException>(); Check.That(exception.Message).Equals("Test message"); }
public void NotNull_ObjectNull_DoesThrowException() { // Arrange object reference = null; object returnedValue = null; PostConditionViolatedException exception = null; // Act try { returnedValue = Postcondition.NotNull(reference); } catch (PostConditionViolatedException e) { exception = e; } // Assert Check.That(exception).IsNotNull(); Check.That(returnedValue).IsNull(); }
/// <summary> /// A copy constructor that allocates an instance that is the same as the given template, except for its containing block. /// </summary> /// <param name="containingBlock">The containing block of the copied postcondition. This should be different from the containing block of the template postcondition.</param> /// <param name="template">The statement to copy.</param> private Postcondition(BlockStatement containingBlock, Postcondition template) : base(containingBlock, template) { }
public override void OnInspectorGUI() { //Update our list GetTarget.Update(); EditorGUILayout.Space(); EditorGUILayout.Space(); EditorGUILayout.PropertyField(actionName); EditorGUILayout.Space(); EditorGUILayout.Space(); //preconditions lists EditorGUILayout.LabelField("Preconditions", EditorStyles.boldLabel); EditorGUILayout.LabelField("Define the Preconditions of this action"); EditorGUILayout.BeginHorizontal(); precondChoice = EditorGUILayout.Popup(precondChoice, t.choices); if (GUILayout.Button("Add New")) { ConditionList.Condition x = t.globalConditionList.conditionList[precondChoice]; Precondition newCond = new Precondition(ref x); //newCond.refrencedCondition = t.globalConditionList.conditionList[precondChoice]; if (t.preconditions == null) { Debug.Log("No t"); } t.preconditions.Add(newCond); } EditorGUILayout.EndHorizontal(); //Display our list to the inspector window for (int i = 0; i < PrecondList.arraySize; i++) { EditorGUILayout.Space(); EditorGUILayout.Space(); SerializedProperty MyListRef = PrecondList.GetArrayElementAtIndex(i); SerializedProperty referencedCond = MyListRef.FindPropertyRelative("refrencedCondition"); SerializedProperty condName = referencedCond.FindPropertyRelative("conditionName"); SerializedProperty condValue = MyListRef.FindPropertyRelative("value"); // Display the property fields EditorGUILayout.BeginHorizontal("box"); EditorGUILayout.LabelField(condName.stringValue); t.preconditions[i].boolValue = EditorGUILayout.Toggle(condValue.boolValue); if (GUILayout.Button("Remove")) { PrecondList.DeleteArrayElementAtIndex(i); } EditorGUILayout.EndHorizontal(); EditorGUILayout.Space(); EditorGUILayout.Space(); } EditorGUILayout.Space(); EditorGUILayout.Space(); EditorGUILayout.Space(); //post conditions list EditorGUILayout.LabelField("Postconditions", EditorStyles.boldLabel); EditorGUILayout.LabelField("Define the Postconditions of this action"); EditorGUILayout.BeginHorizontal(); postcondChoice = EditorGUILayout.Popup(postcondChoice, t.choices); if (GUILayout.Button("Add New")) { ConditionList.Condition x = t.globalConditionList.conditionList[postcondChoice]; Postcondition newCond = new Postcondition(ref x, t.globalConditionList); //newCond.refrencedCondition = t.globalConditionList.conditionList[postcondChoice]; t.postConditions.Add(newCond); } EditorGUILayout.EndHorizontal(); //Display our list to the inspector window for (int i = 0; i < PostcondList.arraySize; i++) { EditorGUILayout.Space(); EditorGUILayout.Space(); SerializedProperty MyListRef = PostcondList.GetArrayElementAtIndex(i); SerializedProperty referencedCond = MyListRef.FindPropertyRelative("refrencedCondition"); SerializedProperty condName = referencedCond.FindPropertyRelative("conditionName"); SerializedProperty condValue = MyListRef.FindPropertyRelative("value"); // Display the property fields EditorGUILayout.BeginHorizontal("box"); EditorGUILayout.LabelField(condName.stringValue); t.postConditions[i].boolValue = EditorGUILayout.Toggle(condValue.boolValue); if (GUILayout.Button("Remove")) { PostcondList.DeleteArrayElementAtIndex(i); } EditorGUILayout.EndHorizontal(); EditorGUILayout.Space(); EditorGUILayout.Space(); } EditorGUILayout.PropertyField(Dialogue); //Apply the changes to our list GetTarget.ApplyModifiedProperties(); }
/// <summary> /// Visits the specified post condition. /// </summary> /// <param name="postCondition">The post condition.</param> protected virtual IPostcondition DeepCopy(Postcondition postCondition) { postCondition.Condition = this.Substitute(postCondition.Condition); if (postCondition.Description != null) postCondition.Description = this.Substitute(postCondition.Description); return postCondition; }
/// <summary> /// Get the mutable copy of a postcondition. /// </summary> /// <param name="postcondition"></param> /// <returns></returns> public virtual Postcondition GetMutableCopy(IPostcondition postcondition) { object cachedValue; if (this.cache.TryGetValue(postcondition, out cachedValue)) return (Postcondition)cachedValue; var result = new Postcondition(postcondition); // Probably not necessary, no two postconditions are shared. this.cache.Add(postcondition, result); this.cache.Add(result, result); return result; }
protected bool IsPostconditionMet() { return(Postcondition == null || Postcondition.IsTrue()); }
private MethodContract CreateQueryContract(State state, State target) { var contracts = new MethodContract(); // Source state invariant as a precondition var stateInv = Helper.GenerateStateInvariant(host, state); var preconditions = from condition in stateInv select new Precondition { Condition = condition, OriginalSource = new CciExpressionPrettyPrinter().PrintExpression(condition), Description = new CompileTimeConstant { Value = "Source state invariant", Type = host.PlatformType.SystemString } }; contracts.Preconditions.AddRange(preconditions); // Add a redundant postcondition for only those conditions that predicate ONLY about parameters and not the instance. // These postconditions will be translated as assumes in the ContractRewriter.cs var contractDependencyAnalyzer = new CciContractDependenciesAnalyzer(new ContractProvider(new ContractMethods(host), null)); foreach (var action in target.EnabledActions.Union(target.DisabledActions)) { if (action.Contract != null) { foreach (var pre in action.Contract.Preconditions) { if (contractDependencyAnalyzer.PredicatesAboutInstance(pre) || !contractDependencyAnalyzer.PredicatesAboutParameter(pre)) { continue; } var post = new Postcondition { Condition = pre.Condition, OriginalSource = new CciExpressionPrettyPrinter().PrintExpression(pre.Condition), Description = new CompileTimeConstant { Value = "Conditions over parameters are assumed satisfiable", Type = host.PlatformType.SystemString } }; contracts.Postconditions.Add(post); } } } // Negated target state invariant as a postcondition var targetInv = Helper.GenerateStateInvariant(host, target); IExpression joinedTargetInv = new LogicalNot { Type = host.PlatformType.SystemBoolean, Operand = Helper.JoinWithLogicalAnd(host, targetInv, true) }; var postcondition = new Postcondition { Condition = joinedTargetInv, OriginalSource = new CciExpressionPrettyPrinter().PrintExpression(joinedTargetInv), Description = new CompileTimeConstant { Value = "Negated target state invariant", Type = host.PlatformType.SystemString } }; contracts.Postconditions.Add(postcondition); return(contracts); }
/// <summary> /// Allocates an exception that can be thrown by the associated method, along with a possibly empty list of postconditions that are true when that happens. /// </summary> /// <param name="exceptionType">The exception that can be thrown by the associated method.</param> /// <param name="postcondition">The postcondition that holds if the associated method throws this exception.</param> /// <param name="sourceLocation">The source location corresponding to the newly allocated source item.</param> public ThrownException(TypeExpression exceptionType, Postcondition postcondition, ISourceLocation sourceLocation) : base(sourceLocation) { this.exceptionType = exceptionType; this.postcondition = postcondition; }
/// <summary> /// Visits the specified post condition. /// </summary> /// <param name="postCondition">The post condition.</param> public virtual IPostcondition Visit(IPostcondition postCondition) { Postcondition mutablePostCondition = postCondition as Postcondition; if (!this.copyOnlyIfNotAlreadyMutable || mutablePostCondition == null) mutablePostCondition = new Postcondition(postCondition); return this.Visit(mutablePostCondition); }
public void Is_ConditionNotFulfilled_DoesThrowException() { // Arrange & Act Postcondition.Is(false); }
/// <summary> /// Visits the specified post condition. /// </summary> /// <param name="postCondition">The post condition.</param> public virtual IPostcondition Visit(Postcondition postCondition) { postCondition.Condition = this.Visit(postCondition.Condition); if (postCondition.Description != null) postCondition.Description = this.Visit(postCondition.Description); return postCondition; }
public void Is_ConditionFulfilled_DoesNotThrowException() { // Arrange & Act Postcondition.Is(true); }
public override void OnInspectorGUI() { //Update our list GetTarget.Update(); EditorGUILayout.Space(); EditorGUILayout.Space(); EditorGUILayout.PropertyField(actionName); EditorGUILayout.Space(); EditorGUILayout.Space(); //preconditions lists EditorGUILayout.LabelField("Preconditions", EditorStyles.boldLabel); EditorGUILayout.LabelField("Define the Preconditions of this action"); EditorGUILayout.BeginHorizontal(); precondChoice = EditorGUILayout.Popup(precondChoice, t.choices); if (GUILayout.Button("Add New")) { ConditionList.Condition x = t.globalConditionList.conditionList[precondChoice]; Precondition newCond = new Precondition(ref x); //newCond.refrencedCondition = t.globalConditionList.conditionList[precondChoice]; if (t.preconditions == null) { Debug.Log("No t"); } t.preconditions.Add(newCond); } EditorGUILayout.EndHorizontal(); //Display our list to the inspector window for (int i = 0; i < PrecondList.arraySize; i++) { EditorGUILayout.Space(); EditorGUILayout.Space(); SerializedProperty MyListRef = PrecondList.GetArrayElementAtIndex(i); SerializedProperty referencedCond = MyListRef.FindPropertyRelative("refrencedCondition"); SerializedProperty condName = referencedCond.FindPropertyRelative("conditionName"); SerializedProperty condBoolValue = MyListRef.FindPropertyRelative("boolValue"); SerializedProperty condintValue = MyListRef.FindPropertyRelative("integerValue"); SerializedProperty compType = MyListRef.FindPropertyRelative("comparisonType"); // Display the property fields EditorGUILayout.BeginHorizontal("box"); EditorGUILayout.LabelField(condName.stringValue); //change what values can be changed depending on type if (t.preconditions [i].refrencedCondition.conditonType == global::ConditionList.ConditionType.Boolean) { //if its a bool t.preconditions [i].boolValue = EditorGUILayout.Toggle(condBoolValue.boolValue); } else { //its an int GUILayout.Label("Integer Value"); t.preconditions [i].integerValue = EditorGUILayout.IntField(condintValue.intValue); EditorGUILayout.PropertyField(compType); } if (GUILayout.Button("Remove")) { PrecondList.DeleteArrayElementAtIndex(i); } EditorGUILayout.EndHorizontal(); EditorGUILayout.Space(); EditorGUILayout.Space(); } EditorGUILayout.Space(); EditorGUILayout.Space(); EditorGUILayout.Space(); //POST CONDITIONS EditorGUILayout.LabelField("Postconditions", EditorStyles.boldLabel); EditorGUILayout.LabelField("Define the Postconditions of this action"); EditorGUILayout.BeginHorizontal(); postcondChoice = EditorGUILayout.Popup(postcondChoice, t.choices); if (GUILayout.Button("Add New")) { ConditionList.Condition x = t.globalConditionList.conditionList[postcondChoice]; Postcondition newCond = new Postcondition(ref x, t.globalConditionList); //newCond.refrencedCondition = t.globalConditionList.conditionList[postcondChoice]; t.postConditions.Add(newCond); } EditorGUILayout.EndHorizontal(); //Display our list to the inspector window for (int i = 0; i < PostcondList.arraySize; i++) { EditorGUILayout.Space(); EditorGUILayout.Space(); SerializedProperty MyListRef = PostcondList.GetArrayElementAtIndex(i); SerializedProperty referencedCond = MyListRef.FindPropertyRelative("refrencedCondition"); SerializedProperty condName = referencedCond.FindPropertyRelative("conditionName"); SerializedProperty boolCondValue = MyListRef.FindPropertyRelative("boolValue"); SerializedProperty intCondValue = MyListRef.FindPropertyRelative("integerValue"); SerializedProperty asngType = MyListRef.FindPropertyRelative("assignmentType"); // Display the property fields EditorGUILayout.BeginHorizontal("box"); EditorGUILayout.LabelField(condName.stringValue); //change what values can be changed depending on type if (t.postConditions [i].refrencedCondition.conditonType == global::ConditionList.ConditionType.Boolean) { //if its a bool t.postConditions[i].boolValue = EditorGUILayout.Toggle(boolCondValue.boolValue); } else { //its an int EditorGUILayout.BeginVertical(); EditorGUILayout.PropertyField(asngType); if (t.postConditions [i].assignmentType == AssignmentType.SET) { t.postConditions[i].integerValue = EditorGUILayout.IntField(intCondValue.intValue); } EditorGUILayout.EndVertical(); } if (GUILayout.Button("Remove")) { PostcondList.DeleteArrayElementAtIndex(i); } EditorGUILayout.EndHorizontal(); EditorGUILayout.Space(); EditorGUILayout.Space(); } //Apply the changes to our list GetTarget.ApplyModifiedProperties(); }
private void AppendPostcondition(ContractStateEnum postconditionState, string methodCallResult, Postcondition postcondition, StringBuilder code) { if (postconditionState == ContractStateEnum.Ok) { code.Append(PostconditionComment); var expression = ComputePostconditionExpression(postcondition.Expression, methodCallResult); code.Append(Assertion(expression)); } if (postconditionState == ContractStateEnum.Invalid) { code.Append(InvalidPostconditionComment); } }
/// <summary> /// A copy constructor that allocates an instance that is the same as the given template, except for its containing block. /// </summary> /// <param name="containingBlock">The containing block of the copied postcondition. This should be different from the containing block of the template postcondition.</param> /// <param name="template">The statement to copy.</param> private ThrownException(BlockStatement containingBlock, ThrownException template) : base(template.SourceLocation) { this.exceptionType = (TypeExpression)template.ExceptionType.MakeCopyFor(containingBlock); this.postcondition = template.Postcondition.MakeCopyFor(containingBlock); }
protected override IMethodContract CreateQueryContract(State state, Action target) { var queryContract = new MethodContract(); var targetContract = target.Contract; // Add preconditions of enabled actions foreach (var a in state.EnabledActions) { var actionContract = a.Contract; if (actionContract == null) { continue; } var preconditions = from p in actionContract.Preconditions select new Precondition { Condition = p.Condition, Description = new CompileTimeConstant { Value = string.Format("Enabled action ({0})", a.Name), Type = host.PlatformType.SystemString }, OriginalSource = new CciExpressionPrettyPrinter().PrintExpression(p.Condition) }; queryContract.Preconditions.AddRange(preconditions); } // Add negated preconditions of disabled actions foreach (var a in state.DisabledActions) { var actionContract = a.Contract; if (actionContract == null || !actionContract.Preconditions.Any()) { continue; } var preconditions = from p in actionContract.Preconditions select p.Condition; var joinedPreconditions = new LogicalNot { Type = host.PlatformType.SystemBoolean, Operand = Helper.JoinWithLogicalAnd(host, preconditions.ToList(), true) }; var compactPrecondition = new Precondition { Condition = joinedPreconditions, // Add the user message to identify easily each precondition Description = new CompileTimeConstant { Value = string.Format("Disabled action ({0})", a.Name), Type = host.PlatformType.SystemString }, // Add the string-ified version of the condition to help debugging OriginalSource = new CciExpressionPrettyPrinter().PrintExpression(joinedPreconditions) }; queryContract.Preconditions.Add(compactPrecondition); } // Now the postconditions // Having no preconditions is the same as having the 'true' precondition if (targetContract == null || !targetContract.Preconditions.Any()) { var post = new Postcondition { Condition = new CompileTimeConstant { Type = host.PlatformType.SystemBoolean, Value = false }, OriginalSource = "false", Description = new CompileTimeConstant { Value = "Target negated precondition", Type = host.PlatformType.SystemString } }; queryContract.Postconditions.Add(post); } else { var exprs = (from pre in targetContract.Preconditions select pre.Condition).ToList(); var post = new Postcondition { Condition = new LogicalNot { Type = host.PlatformType.SystemBoolean, Operand = Helper.JoinWithLogicalAnd(host, exprs, true) }, OriginalSource = new CciExpressionPrettyPrinter().PrintExpression(Helper.JoinWithLogicalAnd(host, exprs, true)), Description = new CompileTimeConstant { Value = "Target negated precondition", Type = host.PlatformType.SystemString } }; queryContract.Postconditions.Add(post); } return(queryContract); }