public void Root_NoStates_Null() { DeterministicAutomata<int> automata = new DeterministicAutomata<int>(); Assert.That(automata.Root, Is.Null); }
public void CurrentState_NoElement_Null() { DeterministicAutomata<int> automata = new DeterministicAutomata<int>(); Assert.That(automata.CurrentState, Is.Null); }
public void IsValid_NoStates_Exception() { DeterministicAutomata<int> automata = new DeterministicAutomata<int>(); Assert.Throws<InvalidOperationException> (() => { bool isValid = automata.IsValid; }); }
public void Add_SingleElement_CurrentStateChanged() { DeterministicAutomata<int> automata = new DeterministicAutomata<int>(); AutomataState<int> state = new AutomataState<int>("Root", true); automata.Add(state); Assert.That(automata.CurrentState, Is.SameAs(state)); }
public void Add_FewElements_RootIsFirst() { DeterministicAutomata<int> automata = new DeterministicAutomata<int>(); AutomataState<int> root = new AutomataState<int>("Root", true); AutomataState<int> state1 = new AutomataState<int>("State1", true); AutomataState<int> state2 = new AutomataState<int>("State2", true); automata.Add(root); automata.Add(state1); automata.Add(state2); Assert.That(automata.Root, Is.SameAs(root)); }
/// <summary> /// <see cref="ITypeAutomataBuilder.Build"/> /// </summary> public IDeterministicAutomata<MethodInfo> Build(IDeterministicAutomata<MethodInfo> aggregatedAutomata) { IDictionary<Type, ICollection<MethodInfo>> typeToMethods = GatherTypeToMethods(); ICollection<MethodInfo> alphabet = typeToMethods.Values.SelectMany(x => x) .Distinct(new ToStringComparer<MethodInfo>()) .ToList(); IDictionary<Type, IExtendableDeterministicAutomata<MethodInfo>> typeToAutomata = typeToMethods.ToDictionary (x => x.Key, x => FundamentalAutomatas.OnlySymbolsAreLegal(alphabet, x.Value, new ToStringComparer<MethodInfo>()), new ToStringComparer<Type>()); IExtendableDeterministicAutomata<MethodInfo> result = new DeterministicAutomata<MethodInfo>(new ToStringComparer<MethodInfo>()); result.Alphabet.AddRange(alphabet); foreach (KeyValuePair<Type, ICollection<MethodInfo>> typeToMethod in typeToMethods) { IExtendableDeterministicAutomata<MethodInfo> currentTypeAutomata = typeToAutomata[typeToMethod.Key]; foreach (var method in typeToMethod.Value) { // Yuck cast... ((IExtendableAutomataState<MethodInfo>)currentTypeAutomata.Root) .Add(method, typeToAutomata[method.ReturnType].Root); } result.AddRange(currentTypeAutomata); } return result.Intersect(aggregatedAutomata); }
public void Reset_TwoStatesSingleInput_Root() { DeterministicAutomata<int> automata = new DeterministicAutomata<int>(); AutomataState<int> valid = new AutomataState<int>("Valid", true); AutomataState<int> invalid = new AutomataState<int>("Invalid", false); automata.Add(valid); automata.Add(invalid); valid.Add(3, invalid); valid.Add(4, valid); automata.Read(3); automata.Reset(); Assert.That(automata.CurrentState, Is.SameAs(valid)); }
public void Reset_SingleState_Root() { DeterministicAutomata<int> automata = new DeterministicAutomata<int>(); AutomataState<int> valid = new AutomataState<int>("Valid", true); automata.Add(valid); automata.Reset(); Assert.That(automata.CurrentState, Is.SameAs(valid)); }
public void Reset_NoStates_Exception() { DeterministicAutomata<int> automata = new DeterministicAutomata<int>(); Assert.Throws<InvalidOperationException>(() => automata.Reset()); }
public void Read_TwoStatesTwoInputs_FirstState() { DeterministicAutomata<int> automata = new DeterministicAutomata<int>(); AutomataState<int> valid = new AutomataState<int>("Valid", true); AutomataState<int> invalid = new AutomataState<int>("Invalid", false); automata.Add(valid); automata.Add(invalid); valid.Add(3, invalid); invalid.Add(4, valid); automata.Read(3); automata.Read(4); Assert.That(automata.CurrentState, Is.SameAs(valid)); Assert.That(automata.IsValid, Is.EqualTo(true)); }
public void Read_SingleState_Self() { DeterministicAutomata<int> automata = new DeterministicAutomata<int>(); AutomataState<int> root = new AutomataState<int>("Root", true); automata.Add(root); automata.Read(3); Assert.That(automata.CurrentState, Is.SameAs(root)); Assert.That(automata.IsValid, Is.EqualTo(root.IsValid)); }
public void Read_NoElements_Exception() { DeterministicAutomata<int> automata = new DeterministicAutomata<int>(); Assert.Throws<InvalidOperationException>(() => automata.Read(1)); }
public void GetEnumerator_FewElements_ExpectedEnumeration() { DeterministicAutomata<int> automata = new DeterministicAutomata<int>(); AutomataState<int> root = new AutomataState<int>("Root", true); AutomataState<int> state1 = new AutomataState<int>("State1", true); AutomataState<int> state2 = new AutomataState<int>("State2", true); automata.Add(root); automata.Add(state1); automata.Add(state2); HashSet<IAutomataState<int>> expectedEnumeration = new HashSet<IAutomataState<int>>() { root, state1, state2 }; Assert.IsTrue(expectedEnumeration.SetEquals(automata)); }
public void GetEnumerator_SingleElement_SingleEnumeration() { DeterministicAutomata<int> automata = new DeterministicAutomata<int>(); AutomataState<int> root = new AutomataState<int>("Root", true); automata.Add(root); HashSet<IAutomataState<int>> expectedEnumeration = new HashSet<IAutomataState<int>>() { root, }; Assert.IsTrue(expectedEnumeration.SetEquals(automata)); }
public void GetEnumerator_NoElements_NoEnumeration() { DeterministicAutomata<int> automata = new DeterministicAutomata<int>(); Assert.IsTrue(!automata.Any()); }
/// <summary> /// Inner implementation of <see cref="LinkScopes(System.Collections.Generic.IDictionary{string,Flunet.Automata.Interfaces.IDeterministicAutomata{System.Reflection.MethodInfo}})"/>. /// Gets a dictionary of scope names to their corresponding validation automatas /// and creates an automata in respect to the given type that links them. /// </summary> /// <param name="type">The type to build the automata in respect to.</param> /// <param name="scopesToValidation">A dictionary that /// maps scope names to their validating automatas.</param> /// <returns>An automata that links each scope to its /// corresponding validation automata</returns> private IDeterministicAutomata<MethodInfo> LinkScopes(Type type, IDictionary<string, IDeterministicAutomata<MethodInfo>> scopesToValidation) { IExtendableDeterministicAutomata<MethodInfo> result = FundamentalAutomatas.True("Root", new ToStringComparer<MethodInfo>()); string currentScope = type.GetScopeName(); IDeterministicAutomata<MethodInfo> currentScopeValidation = FundamentalAutomatas.True("Root", new ToStringComparer<MethodInfo>()); if (scopesToValidation.ContainsKey(currentScope)) { currentScopeValidation = scopesToValidation[currentScope]; } var relevantMethods = from method in type.GetMethods() let methodScope = method.ReturnType.GetScopeName() where methodScope != currentScope select method; foreach (MethodInfo method in relevantMethods) { IExtendableDeterministicAutomata<MethodInfo> childValidation = new DeterministicAutomata<MethodInfo>(new ToStringComparer<MethodInfo>()); IExtendableAutomataState<MethodInfo> childValidationRoot = childValidation.AddState("Root", true); IDeterministicAutomata<MethodInfo> childLink = LinkScopes(method.ReturnType, scopesToValidation); childValidationRoot.Add(method, childLink.Root); childValidation.AddRange(childLink); result = result.Intersect(childValidation); } result = result.Intersect(currentScopeValidation); return result; }