public ContractInjector(Microsoft.CodeAnalysis.Document document, ContractKind kind, string suggestion, BaseMethodDeclarationSyntax oldNode) { this.document = document; this.kind = kind; this.suggestion = suggestion; this.oldNode = oldNode; }
private void StartPropContract(ContractKind kind, Span span) { Contract.Requires(currentModule != null); Contract.Requires(currentModule.CanHaveContract(kind)); Contract.Requires(kind != ContractKind.RequiresSome && kind != ContractKind.RequiresAtLeast && kind != ContractKind.RequiresAtMost); crntContract = new ContractItem(span, kind); switch (currentModule.NodeKind) { case NodeKind.Model: ((Model)currentModule).AddContract(crntContract); break; case NodeKind.Transform: ((Transform)currentModule).AddContract(crntContract); break; case NodeKind.Domain: ((Domain)currentModule).AddConforms(crntContract); break; default: throw new NotImplementedException(); } if (crntSentConf != null) { crntContract.SetConfig(crntSentConf); crntSentConf = null; } }
internal ContractItem(Span span, ContractKind contractKind) : base(span) { ContractKind = contractKind; specification = new LinkedList <Node>(); Specification = new ImmutableCollection <Node>(specification); }
ParameterContractInfo(ContractKind contractKind, [NotNull] IParameterDeclaration declaration, [NotNull] IType type) : base(contractKind, type) { Debug.Assert( contractKind == ContractKind.Requires || contractKind == ContractKind.Ensures || contractKind == ContractKind.RequiresAndEnsures); this.declaration = declaration; }
public async Task Delete(int id) { ContractKind contractKind = await _ctx.ContractKinds.FirstOrDefaultAsync(x => x.Id == id); if (contractKind != null) { _ctx.ContractKinds.Remove(contractKind); } }
PropertyContractInfo(ContractKind contractKind, [NotNull] IAccessorOwnerDeclaration declaration, [NotNull] IType type) : base(contractKind, type) { Debug.Assert( contractKind == ContractKind.Requires || contractKind == ContractKind.Ensures || contractKind == ContractKind.RequiresAndEnsures || contractKind == ContractKind.Invariant); this.declaration = declaration; }
public void AddAvailableContractKind(ContractKind contractKind) { if (CanAddContractKind(contractKind).Any()) { throw new InvalidOperationException(); } _contractKindBindings.Add(new TariffToContractKindBinding(contractKind.Id)); }
public async Task <IActionResult> GetApplication(int id) { ContractKind application = await contractKindRepository.GetContractKind(id); if (application == null) { return(StatusCode(StatusCodes.Status500InternalServerError, $"ContractKind id == {id}. Not found!")); } return(Ok(application.Map())); }
public Contract(string documentNumber, DateTime signDate, DateTime sActionDate, ContractKind contractKind, List <ContractParticipant> contractParticipants) : base(documentNumber, signDate, sActionDate) { if (contractParticipants.Count() != 2) { throw new Exception("В договоре должны быть 2 стороны"); } ContractKind = contractKind; ContractParticipants = contractParticipants; }
protected ContractInfo(ContractKind contractKind, [NotNull] IType type) { Debug.Assert( contractKind == ContractKind.Requires || contractKind == ContractKind.Ensures || contractKind == ContractKind.RequiresAndEnsures || contractKind == ContractKind.Invariant); ContractKind = contractKind; Type = type; }
public static string GetMethodName(this ContractKind kind) { if (kind == ContractKind.Require) { return("Requires"); } else { return("Ensures"); } }
PropertyContractInfo(ContractKind contractKind, [NotNull] IAccessorOwnerDeclaration declaration, [NotNull] IType type) : base( contractKind, type) { Debug.Assert( contractKind == ContractKind.Requires || contractKind == ContractKind.Ensures || contractKind == ContractKind.RequiresAndEnsures || contractKind == ContractKind.Invariant); this.declaration = declaration; }
public async Task <IActionResult> Add([FromBody] ContractKindDto contractKindDto) { if (await contractKindRepository.GetContractKind(contractKindDto.PublicId) != null) { return(StatusCode(StatusCodes.Status500InternalServerError, $"ContractKind with this PublicId == {contractKindDto.PublicId} already exists")); } ContractKind contractKind = new ContractKind(contractKindDto.PublicId, contractKindDto.Name); await contractKindRepository.Add(contractKind); await unitOfWork.SaveEntitiesAsync(); return(Ok()); }
public void CheckContractIsOther(string contract, ContractKind kind) { var root = Syntax.ParseExpression(contract); var req = new CodeContractCollector(kind, Categories.SemanticCategories); req.Visit(root); Assert.IsTrue(req.Labels.Count == 1, "Expected a single contract clause for contract {0}", contract); Assert.IsFalse( req.Labels[0].Labels.Any(), string.Format("Contract '{0}' miscategorized. Label(s): {1}", contract, string.Join(",", req.Labels[0].Labels))); }
public void CheckContract(string contract, ContractKind kind, Category expected) { var root = Syntax.ParseExpression(contract); var collector = new CodeContractCollector(kind, Categories.SemanticCategories); collector.Visit(root); Assert.AreEqual(1, collector.Labels.Count, "Expected a single top-level contract clause"); var labels = collector.Labels[0].Labels.ToList(); Assert.IsTrue(labels.Count > 0, string.Format("Contract {0} not labeled", contract)); Assert.AreEqual(1, labels.Count, "Expected a single label for the contract"); Assert.AreEqual(expected.Name, labels[0], string.Format("Contract {0} miscategorized", contract)); }
public BillPointRule( string description, ContractType contractType, ContractKind contractKind, OrganizationTypeEnum organizationTypeSide1, OrganizationTypeEnum organizationTypeSide2, Sign entrySign) { Description = description; ContractType = contractType; ContractKind = contractKind; OrganizationTypeSide1 = organizationTypeSide1; OrganizationTypeSide2 = organizationTypeSide2; EntrySign = entrySign; }
public AddContractRefactoring( IEnumerable <BlockSyntax> bodies, ContractParameter parameter, IContractProvider provider, ContractKind contractKind) { Contract.Requires(bodies != null); Contract.Requires(provider != null); Contract.Requires(Enum.IsDefined(typeof(ContractKind), contractKind)); _bodies = bodies; _parameter = parameter; _provider = provider; _contractKind = contractKind; }
public static ContractKindDto Map(this ContractKind contractKind) { if (contractKind == null) { return(null); } else { return(new ContractKindDto { Id = contractKind.Id, Name = contractKind.Name, PublicId = contractKind.PublicId, }); } }
public IReadOnlyList <string> CanChangeContractKind(ContractKind contractKind) { List <string> errors = new List <string>(); if (contractKind == null) { errors.Add(ValidationMessage.MustSpecify("product option")); } else { if (contractKind.Id == 0) { errors.Add(ValidationMessage.MustSpecify("product option id")); } } return(errors); }
public bool CanHaveContract(ContractKind kind) { switch (kind) { case ContractKind.ConformsProp: return(NodeKind == NodeKind.Domain); case ContractKind.EnsuresProp: case ContractKind.RequiresProp: return(NodeKind == NodeKind.Transform || NodeKind == NodeKind.Model); case ContractKind.RequiresSome: case ContractKind.RequiresAtLeast: case ContractKind.RequiresAtMost: return(NodeKind == NodeKind.Model); default: throw new NotImplementedException(); } }
public static bool TryParseSuggestion(this string s, out ContractKind kind, out string condition) { if (s == null || !s.ToLower().Contains("suggestion")) { kind = default(ContractKind); condition = null; return(false); } var where = s.LastIndexOf(':') + 1; if (where > 0) { condition = s.Substring(where); if (s.Contains("Requires")) { kind = ContractKind.Precondition; return(true); } else if (s.Contains("Ensures")) { kind = ContractKind.Postcondition; return(true); } else if (s.Contains("Invariant")) { kind = ContractKind.ObjectInvariant; return(true); } else { kind = default(ContractKind); condition = null; return(false); } } kind = default(ContractKind); condition = null; return(false); }
public async Task <Result> VerifyAsFullyAccessed(int agencyId, ContractKind contractKind, string verificationReason) { return(await GetAgency(agencyId) .Ensure(a => a.ParentId is null, "Verification is only available for root agencies") .Ensure(a => a.VerificationState == AgencyVerificationStates.ReadOnly, "Verification as fully accessed is only available for agencies that were verified as read-only earlier") .Ensure(IsContractTypeValid, "Invalid contract type") .Tap(c => SetVerificationState(c, AgencyVerificationStates.FullAccess, verificationReason)) .Tap(SetContractType) .Tap(() => WriteVerificationToAuditLog(agencyId, verificationReason, AgencyVerificationStates.FullAccess))); bool IsContractTypeValid(Agency _) => !contractKind.Equals(default(ContractKind)); async Task SetContractType(Agency agency) { agency.ContractKind = contractKind; await _context.SaveChangesAsync(); } }
public IReadOnlyList <string> CanAddContractKind(ContractKind contractKind) { List <string> errors = new List <string>(); if (contractKind == null) { errors.Add(ValidationMessage.MustSpecify("product option")); } else { if (contractKind.Id == 0) { errors.Add(ValidationMessage.MustSpecify("product option id")); } if (ContractKindBindings.FirstOrDefault(x => x.ContractKindId == contractKind.Id) != null) { errors.Add(ValidationMessage.AlreadyExists("product option")); } } return(errors); }
static ICSharpStatement CreateContractStatement( ContractKind contractKind, [NotNull] IPsiModule psiModule, [NotNull] IExpression contractExpression) { var factory = CSharpElementFactory.GetInstance(contractExpression); var contractType = TypeElementUtil.GetTypeElementByClrName(PredefinedType.CONTRACT_FQN, psiModule); switch (contractKind) { case ContractKind.Requires: return(factory.CreateStatement(string.Format("$0.{0}($1);", nameof(Contract.Requires)), contractType, contractExpression)); case ContractKind.Ensures: return(factory.CreateStatement(string.Format("$0.{0}($1);", nameof(Contract.Ensures)), contractType, contractExpression)); case ContractKind.Invariant: return(factory.CreateStatement(string.Format("$0.{0}($1);", nameof(Contract.Invariant)), contractType, contractExpression)); default: throw new ArgumentOutOfRangeException(nameof(contractKind)); } }
public async Task <IActionResult> Update([FromBody] ContractKindDto contractKindDto) { ContractKind contractKind = await contractKindRepository.GetContractKind(contractKindDto.Id); if (contractKind == null) { return(StatusCode(StatusCodes.Status500InternalServerError, $"ContractKind id == {contractKindDto.Id}. Not found!")); } if (contractKind.PublicId != contractKindDto.PublicId) { if (await contractKindRepository.GetContractKind(contractKindDto.PublicId) != null) { return(StatusCode(StatusCodes.Status500InternalServerError, $"ContractKind with this PublicId == {contractKindDto.PublicId} already exists")); } contractKind.SetPublicId(contractKindDto.PublicId); } contractKind.SetPublicId(contractKindDto.PublicId); contractKind.SetName(contractKindDto.Name); await contractKindRepository.Update(contractKind); await unitOfWork.SaveEntitiesAsync(); return(Ok()); }
public void ChangeContractKind(TariffToContractKindBinding ContractKindBinding, ContractKind contractKind) { if (CanChangeContractKind(contractKind).Any()) { throw new InvalidOperationException(); } var index = _contractKindBindings.FindIndex(x => x.ContractKindId == ContractKindBinding.ContractKindId); _contractKindBindings[index].SetContractKindId(contractKind.Id); }
public async Task <IActionResult> Update([FromBody] TariffDto tariffDto) { Tariff tariff = await tariffRepository.GetTariff(tariffDto.Id); if (tariffDto.TestPeriod.Value != 0) { TariffTestPeriod testPeriod = new TariffTestPeriod(tariffDto.TestPeriod.Value, (TariffTestPeriodUnit)tariffDto.TestPeriod.Unit); tariff.SetTestPeriod(testPeriod); } else { tariff.SetTestPeriod(TariffTestPeriod.Empty()); } tariff.SetName(tariffDto.Name); tariff.SetArchive(tariffDto.IsArchived); tariff.SetAccountingName(tariffDto.AccountingName); tariff.SetPaymentType((PaymentType)tariffDto.PaymentType); tariff.SetAwaitingPaymentStrategy(tariffDto.AwaitingPaymentStrategy); tariff.SetAccountingTariffId(tariffDto.AccountingTariffId); tariff.SetSettingsPresetId(tariffDto.SettingsPresetId); tariff.SetTermsOfUseId(tariffDto.TermsOfUseId); tariff.SetIsAcceptanceRequired(tariffDto.IsAcceptanceRequired); tariff.SetIsGradualFinishAvailable(tariff.IsGradualFinishAvailable); tariff.RemovePriceItems(tariffDto.Prices.ToTariffPrices()); tariff.RemoveAdvancePrices(tariffDto.AdvancePrices.ToTariffAdvancePrices()); tariff.RemoveIncludedProduct(tariffDto.IncludedProducts.Select(x => x.ProductId).ToList()); tariff.RemoveIncludedProductOption(tariffDto.IncludedProductOptions.Select(x => x.ProductOptionId).ToList()); tariff.RemoveContractKindBinding(tariffDto.ContractKindBindings.Select(x => x.ContractKindId).ToList()); //add price foreach (var item in tariffDto.Prices) { Price price = new Price(item.Price.Value, item.Price.Currency); ProlongationPeriod period = new ProlongationPeriod(item.Period.Value, (PeriodUnit)item.Period.periodUnit); if (tariff.Prices.FirstOrDefault(x => x.Period == period && x.Price.Currency == price.Currency) != null) { tariff.ChangePriceItem(price, period); } else { tariff.AddPriceItem(price, period); } } //advancePrice price foreach (var item in tariffDto.AdvancePrices) { Price price = new Price(item.Price.Value, item.Price.Currency); ProlongationPeriod period = new ProlongationPeriod(item.Period.Value, (PeriodUnit)item.Period.periodUnit); if (tariff.AdvancePrices.FirstOrDefault(x => x.Period == period && x.Price.Currency == price.Currency) != null) { tariff.ChangeAdvancePrice(price, period); } else { tariff.AddAdvancePriceItem(price, period); } } //change included product foreach (var item in tariffDto.IncludedProducts) { Product product = await productRepository.GetProduct(item.ProductId); if (tariff.IncludedProducts.Select(x => x.ProductId).Contains(item.ProductId)) { var includedProduct = tariff.IncludedProducts.FirstOrDefault(x => x.ProductId == item.ProductId); tariff.ChangeProduct(includedProduct, product, item.RelativeWeight); } else { tariff.AddProduct(product, item.RelativeWeight); } } //change product option foreach (var item in tariffDto.IncludedProductOptions) { ProductOption productOption = await productOptionRepository.GetProductOption(item.ProductOptionId); if (tariff.IncludedProductOptions.Select(x => x.ProductOptionId).Contains(item.ProductOptionId)) { var includedProductOption = tariff.IncludedProductOptions.FirstOrDefault(x => x.ProductOptionId == item.ProductOptionId); tariff.ChangeProductOption(includedProductOption, productOption, item.Quantity); } else { tariff.AddProductOption(productOption, item.Quantity); } } //change contractKindBindings foreach (var item in tariffDto.ContractKindBindings) { ContractKind contractKind = await contractKindRepository.GetContractKind(item.ContractKindId); if (tariff.ContractKindBindings.Select(x => x.ContractKindId).Contains(item.ContractKindId)) { var contractKindBindings = tariff.ContractKindBindings.FirstOrDefault(x => x.ContractKindId == item.ContractKindId); tariff.ChangeContractKind(contractKindBindings, contractKind); } else { tariff.AddAvailableContractKind(contractKind); } } await tariffRepository.Update(tariff); await unitOfWork.SaveEntitiesAsync(); return(Ok()); }
protected static void AddContract( ContractKind contractKind, [NotNull] IBlock body, [NotNull] IPsiModule psiModule, [NotNull] Func <IExpression> getContractExpression, out ICSharpStatement firstNonContractStatement) { var contractExpression = getContractExpression(); Debug.Assert(contractExpression != null); var statement = CreateContractStatement(contractKind, psiModule, contractExpression); var contractStatements = ContractStatementInfo.CreateContractStatementInfos(body); switch (contractKind) { case ContractKind.Requires: var lastRequiresStatement = (from s in contractStatements where s.ContractKind == ContractKind.Requires select s.Statement) .LastOrDefault(); if (lastRequiresStatement != null) { statement = body.AddStatementAfter(statement, lastRequiresStatement); firstNonContractStatement = null; } else { var firstEnsuresOrEnsuresOnThrowStatement = ( from s in contractStatements where s.ContractKind == ContractKind.Ensures || s.ContractKind == ContractKind.EnsuresOnThrow select s.Statement).FirstOrDefault(); if (firstEnsuresOrEnsuresOnThrowStatement != null) { statement = body.AddStatementBefore(statement, firstEnsuresOrEnsuresOnThrowStatement); firstNonContractStatement = null; } else { firstNonContractStatement = body.StatementsEnumerable.FirstOrDefault(); statement = body.AddStatementBefore(statement, firstNonContractStatement); } } break; case ContractKind.Ensures: var lastEnsuresOrLastRequiresStatement = (from s in contractStatements where s.ContractKind == ContractKind.Ensures select s.Statement).LastOrDefault() ?? (from s in contractStatements where s.ContractKind == ContractKind.Requires select s.Statement).LastOrDefault(); if (lastEnsuresOrLastRequiresStatement != null) { statement = body.AddStatementAfter(statement, lastEnsuresOrLastRequiresStatement); firstNonContractStatement = null; } else { var lastEnsuresOnThrowStatement = (from s in contractStatements where s.ContractKind == ContractKind.EnsuresOnThrow select s.Statement).FirstOrDefault(); if (lastEnsuresOnThrowStatement != null) { statement = body.AddStatementBefore(statement, lastEnsuresOnThrowStatement); firstNonContractStatement = null; } else { firstNonContractStatement = body.StatementsEnumerable.FirstOrDefault(); body.AddStatementBefore(statement, firstNonContractStatement); } } break; case ContractKind.Invariant: var lastInvariantStatement = (from s in contractStatements where s.ContractKind == ContractKind.Invariant select s.Statement) .LastOrDefault(); statement = body.AddStatementAfter(statement, lastInvariantStatement); firstNonContractStatement = null; break; default: throw new ArgumentOutOfRangeException(nameof(contractKind)); } ContextActionUtils.FormatWithDefaultProfile(statement); }
public async Task <IActionResult> Add([FromBody] TariffDto tariffDto) { Tariff tariff = new Tariff(tariffDto.Name, (PaymentType)tariffDto.PaymentType); if (tariffDto.TestPeriod.Value != 0) { TariffTestPeriod testPeriod = new TariffTestPeriod(tariffDto.TestPeriod.Value, (TariffTestPeriodUnit)tariffDto.TestPeriod.Unit); tariff.AddTestPeriod(testPeriod); } tariff.SetArchive(tariffDto.IsArchived); tariff.SetAccountingTariffId(tariffDto.AccountingName); tariff.SetAwaitingPaymentStrategy(tariffDto.AwaitingPaymentStrategy); tariff.SetSettingsPresetId(tariffDto.SettingsPresetId); tariff.SetTermsOfUseId(tariffDto.TermsOfUseId); tariff.SetIsAcceptanceRequired(tariffDto.IsAcceptanceRequired); tariff.SetIsGradualFinishAvailable(tariffDto.IsGradualFinishAvailable); //add price foreach (var tariffPrice in tariffDto.Prices) { Price price = new Price(tariffPrice.Price.Value, tariffPrice.Price.Currency); ProlongationPeriod period = new ProlongationPeriod(tariffPrice.Period.Value, (PeriodUnit)tariffPrice.Period.periodUnit); tariff.AddPriceItem(price, period); } foreach (var advancePrice in tariffDto.AdvancePrices) { Price price = new Price(advancePrice.Price.Value, advancePrice.Price.Currency); ProlongationPeriod period = new ProlongationPeriod(advancePrice.Period.Value, (PeriodUnit)advancePrice.Period.periodUnit); tariff.AddAdvancePriceItem(price, period); } //add included product foreach (var includedProduct in tariffDto.IncludedProducts) { Product product = await productRepository.GetProduct(includedProduct.ProductId); tariff.AddProduct(product, includedProduct.RelativeWeight); } //add product option foreach (var includedProductOption in tariffDto.IncludedProductOptions) { ProductOption productOption = await productOptionRepository.GetProductOption(includedProductOption.ProductOptionId); tariff.AddProductOption(productOption, includedProductOption.Quantity); } //add contractKindBindings foreach (var contractKindBinding in tariffDto.ContractKindBindings) { ContractKind contractkind = await contractKindRepository.GetContractKind(contractKindBinding.ContractKindId); tariff.AddAvailableContractKind(contractkind); } await tariffRepository.Add(tariff); await unitOfWork.SaveEntitiesAsync(); return(Ok()); }
ContractStatementInfo(ContractKind contractKind, [NotNull] ICSharpStatement statement) { ContractKind = contractKind; Statement = statement; }
/// <summary> /// Create a <see cref="CodeContractCollector"/> for the given kinds of contracts and categories /// </summary> /// <param name="kind">The kind(s) of contracts to operate on</param> /// <param name="categories"> /// An ordered array of categories where earlier categories take precedence if /// the <see cref="CATEGORIES_ARE_MUTEX"/> flag is set /// </param> public CodeContractCollector(ContractKind kind, Category[] categories) { this.ContractType = kind; this.Categories = categories; this.Labels = new List <ContractTags>(); }
/// <summary> /// Create a <see cref="CodeContractCollector"/> for the given kinds of contracts and categories /// </summary> /// <param name="kind">The kind(s) of contracts to operate on</param> /// <param name="categories"> /// An ordered array of categories where earlier categories take precedence if /// the <see cref="CATEGORIES_ARE_MUTEX"/> flag is set /// </param> public CodeContractCollector(ContractKind kind, Category[] categories) { this.ContractType = kind; this.Categories = categories; this.Labels = new List<ContractTags>(); }
public ContractTags(ContractKind type, string text) { this.ContractType = type; this.ContractText = text; this.Labels = new HashSet<string>(); }