public void Ensure() { var sut = new Dictionary<string, int>(); var called = 0; Assert.Equal( 1, sut.Ensure( "Key", x => ++called ) ); Assert.Equal( 1, sut.Ensure( "Key", x => ++called ) ); }
public void Ensure() { var dict = new Dictionary<string, int>(); dict.Ensure("a", 1); Assert.AreEqual("([a,1])", dict.Print()); dict.Ensure("a", 2); Assert.AreEqual("([a,2])", dict.Print()); }
internal static RouteToken EnsureTokenCached(string part) { var partHash = part.GetHashCode(); lock (RoutesCache) { return(RoutesCache.Ensure(partHash, () => new RouteToken(part, partHash, RoutesCache.Count))); } }
private static INugetPackage GetLattestNugetPackage(string packageId, string nugetDirectory) { if (nugetDirectory == null) { throw new ArgumentException("Illegal nuget directory"); } var regexMask = new Regex((packageId + ".*").Replace(".", "\\.").Replace("*", ".*"), RegexOptions.IgnoreCase | RegexOptions.CultureInvariant); var regexVersion = new Regex(VersionPattern); var files = new Dictionary <Version, string>(); foreach (var directory in Directory.EnumerateDirectories(nugetDirectory)) { if (!regexMask.IsMatch(directory)) { continue; } var versionMatch = regexVersion.Match(directory); var id = versionMatch.Groups["id"].ToString(); if (!id.EndsWith(packageId, StringComparison.InvariantCultureIgnoreCase)) { continue; } var major = versionMatch.Groups["major"].ToString(); var minor = versionMatch.Groups["minor"].ToString(); var build = versionMatch.Groups["build"].ToString(); var revision = versionMatch.Groups["revision"].ToString(); major = string.IsNullOrEmpty(major) ? "0" : major; minor = string.IsNullOrEmpty(minor) ? "0" : minor; build = string.IsNullOrEmpty(build) ? "0" : build; revision = string.IsNullOrEmpty(revision) ? "0" : revision; var key = new Version($"{major}.{minor}.{build}.{revision}"); if (!files.ContainsKey(key)) { files.Add(key, directory); } files.Ensure(new Version($"{major}.{minor}.{build}.{revision}"), () => directory); } var pair = files.Any() ? files.OrderByDescending(f => f.Key).First() : (KeyValuePair <Version, string>?)null; if (pair.HasValue) { var directory = pair.Value.Value.NormalizePath().EnsureSlash(); var version = pair.Value.Key; return(new NugetPackage(packageId, version, directory)); } return(null); }
public static Dictionary <string, List <Type> > CreateAssociationMap(this IEnumerable <Type> types) { var result = new Dictionary <string, List <Type> >(); foreach (var type in types) { var parts = type.FullName.Split('.'); for (var i = 0; i < parts.Length; i++) { var longForm = string.Join(".", parts.Skip(i)); result.Ensure(longForm, () => new List <Type>()).Add(type); var postfix = "Attribute"; if (longForm.EndsWith(postfix)) { var shortForm = longForm.Substring(0, longForm.Length - postfix.Length); result.Ensure(shortForm, () => new List <Type>()).Add(type); } } } return(result); }
private void EnsureMetadata() { if (_metadataInitialized) { return; } lock (_dependencies) { foreach (var property in typeof(T).GetProperties()) { foreach (var DependOn in property.GetCustomAttributes <DependOnAttribute>(true)) { _dependencies.Ensure(DependOn.PropertyName) .Add(property.Name); } } _metadataInitialized = true; } }
public void Print() { var d = new Dictionary<string, object>(); d.Ensure("foo", 1); d.Ensure("bar", true); // Note - Print should alphabetize the keys Assert.AreEqual("([bar,True],[foo,1])", d.Print()); }
public static Dictionary <Point, Doors> Map(string input) { var map = new Dictionary <Point, Doors>(); var currentLocation = new Point(0, 0); map.Add(currentLocation, Doors.None); var branchPoints = new Stack <Point>(); for (int pointer = 1; pointer < (input.Length - 1); pointer++) { if (input[pointer] == '(') { // We're at a branch point. Push the current location onto the stack to // record where we branched from branchPoints.Push(currentLocation); } else if (input[pointer] == ')') { currentLocation = branchPoints.Pop(); } else if (input[pointer] == '|') { currentLocation = branchPoints.Peek(); } else { Point nextLocation; Doors doorToNextLocation; if (input[pointer] == 'N') { nextLocation = new Point(currentLocation.X, currentLocation.Y - 1); doorToNextLocation = Doors.North; } else if (input[pointer] == 'S') { nextLocation = new Point(currentLocation.X, currentLocation.Y + 1); doorToNextLocation = Doors.South; } else if (input[pointer] == 'E') { nextLocation = new Point(currentLocation.X + 1, currentLocation.Y); doorToNextLocation = Doors.East; } else if (input[pointer] == 'W') { nextLocation = new Point(currentLocation.X - 1, currentLocation.Y); doorToNextLocation = Doors.West; } else { throw new ArgumentException(); } map.Ensure(nextLocation); map[currentLocation] |= doorToNextLocation; map[nextLocation] |= doorToNextLocation.Opposite(); currentLocation = nextLocation; } } return(map); }