public void ReplaceSourceCodeReferences_RunsAsExpected(string input, string oldName, string newName, string expectedOutput, IEnumerable <TokenCheckerCall> tokenCheckerCalls) { //Arrange ITokenChecker tokenChecker = Substitute.For <ITokenChecker>(); tokenChecker .CheckIsToken(Arg.Any <string>(), Arg.Any <string>(), Arg.Any <int>()) .Returns(x => x.ArgAt <int>(2) % 2 == 0); CalculationCodeReferenceUpdate calculationCodeReferenceUpdate = new CalculationCodeReferenceUpdate(tokenChecker); //Act string result = calculationCodeReferenceUpdate.ReplaceSourceCodeReferences(input, oldName, newName); //Assert Assert.AreEqual(expectedOutput, result); tokenChecker .Received(tokenCheckerCalls.Count()) .CheckIsToken(Arg.Any <string>(), Arg.Any <string>(), Arg.Any <int>()); foreach (TokenCheckerCall call in tokenCheckerCalls) { tokenChecker .Received(1) .CheckIsToken(call.SourceCode, oldName, call.Position); } }
private void CheckCircularReference(Calculation calculationToPreview, Build compilerOutput) { string sourceCode = calculationToPreview.Current.SourceCode; if (sourceCode.Contains(calculationToPreview.SourceCodeName, StringComparison.InvariantCultureIgnoreCase)) { if (_tokenChecker.CheckIsToken(sourceCode, calculationToPreview.SourceCodeName, sourceCode.IndexOf(calculationToPreview.SourceCodeName))) { compilerOutput.CompilerMessages.Add(new CompilerMessage { Message = $"Circular reference detected - Calculation '{calculationToPreview.SourceCodeName}' calls itself", Severity = Severity.Error }); compilerOutput.Success = false; } } }
public string ReplaceSourceCodeReferences(string sourceCode, string oldCalcSourceCodeName, string newCalcSourceCodeName) { Guard.IsNullOrWhiteSpace(sourceCode, nameof(sourceCode)); Guard.IsNullOrWhiteSpace(oldCalcSourceCodeName, nameof(oldCalcSourceCodeName)); Guard.IsNullOrWhiteSpace(newCalcSourceCodeName, nameof(newCalcSourceCodeName)); //NCrunch can't run the tests for this, but they run fine manually int position = -1; while (sourceCode.Substring(++position).Contains(oldCalcSourceCodeName, StringComparison.CurrentCultureIgnoreCase)) { position = sourceCode.IndexOf(oldCalcSourceCodeName, position, StringComparison.CurrentCultureIgnoreCase); if (_tokenChecker.CheckIsToken(sourceCode, oldCalcSourceCodeName, position)) { string before = sourceCode.Substring(0, position); string after = sourceCode.Substring(position + oldCalcSourceCodeName.Length); sourceCode = $"{before}{newCalcSourceCodeName}{after}"; } } return(sourceCode); }