public void Get_File() { const string testSnippet = @"this is just a test {0}"; var virtualPathId = CodeDelegateVirtualPath.GetOrCreateVirtualPathId("MyKey"); CodeDelegatesCollection.TryAdd(virtualPathId, (dp, vpi) => string.Format(testSnippet, dp)); var path = CodeDelegateVirtualPath.CreateFullPath(virtualPathId, "cs").TrimStart("~"); var provider = new CodeDelegateVirtualPathProvider(); var file = provider.GetFile(path); Assert.AreEqual(typeof(CodeDelegateVirtualFile), file.GetType()); }
public void Open_File() { const string testSnippet = @"this is just a test {0}"; var virtualPathId = CodeDelegateVirtualPath.GetOrCreateVirtualPathId("MyKey"); CodeDelegatesCollection.TryAdd(virtualPathId, (dp, vpi) => string.Format(testSnippet, dp)); var def = CodeDelegateVirtualPath.TryGetVirtualPathDefinition(virtualPathId); var path = CodeDelegateVirtualPath.CreateFullPath(virtualPathId, "cs"); var file = new CodeDelegateVirtualFile(virtualPathId, def.Result.Value.Parameter, path); using (var sr = new StreamReader(file.Open())) { Assert.AreEqual(string.Format(testSnippet, "MyKey"), sr.ReadToEnd()); } }
/// <summary> /// Ensures that the code delegate for the filter is in the CodeDelegatesCollection /// </summary> /// <param name="backOfficeRequestContext"></param> /// <param name="dataTypeId"></param> /// <param name="blockType"></param> /// <param name="codedelegate"> </param> /// <returns></returns> private static string SetupCodeDelegate( IBackOfficeRequestContext backOfficeRequestContext, HiveId dataTypeId, CSharpCodeBlockType blockType, Func <object, string, string> codedelegate) { var delegateParameter = new Tuple <HiveId, CSharpCodeBlockType>(dataTypeId, blockType); //first, check if we have this registered (multiple ids may exist for the same delegate, but in thsi case it will never be true) var ids = CodeDelegateVirtualPath.GetVirtualPathIdsForDelegate(delegateParameter).ToArray(); if (ids.Any()) { return(ids.First()); } //need to lookup the data type to get its alias as we'll use this for the path using (var uow = backOfficeRequestContext.Application.Hive.OpenReader <IContentStore>()) { var dt = uow.Repositories.Schemas.Get <AttributeType>(true, dataTypeId).SingleOrDefault(); if (dt == null) { throw new InvalidOperationException("Could not find AttributeType with id " + dataTypeId); } //create a unique path for our object which will be used in the virtual path creation, if we don't set this //then the ToString of the object key will be used which is much harder to debug. var path = dt.Alias + "_" + delegateParameter.Item2 + "_" + delegateParameter.GetHashCode().ToString().Replace("-", "0"); //only alphanumeric chars allowed var virtualPathId = CodeDelegateVirtualPath.GetOrCreateVirtualPathId(delegateParameter, path); //add the delegate to the collection CodeDelegatesCollection.TryAdd(virtualPathId, codedelegate); return(virtualPathId); } }
public void SetupTest() { CodeDelegatesCollection.Clear(); CodeDelegateVirtualPath.ClearPathIds(); }