/// <summary> /// Checks that the enumerable contains only the given expected values and nothing else, in order. /// This check should only be used with IEnumerable that have a consistent iteration order /// (i.e. don't use it with Hashtable, prefer <see cref="IsEquivalentTo" /> in that case). /// </summary> /// <typeparam name="T">Type of the elements to be found.</typeparam> /// <param name="check">The fluent check to be extended.</param> /// <param name="expectedValues">The expected values to be found.</param> /// <returns> /// A check link. /// </returns> /// <exception cref="FluentCheckException"> /// The enumerable does not contains only the exact given values and nothing else, /// in order. /// </exception> public static ICheckLink <ICheck <IEnumerable <T> > > ContainsExactly <T>(this ICheck <IEnumerable <T> > check, IEnumerable <T> expectedValues) { ImplementContainsExactly(ExtensibilityHelper.BeginCheck(check), expectedValues); return(ExtensibilityHelper.BuildCheckLink(check)); }
/// <summary> /// /// </summary> /// <param name="check"></param> /// <typeparam name="T"></typeparam> /// <returns></returns> public static ICheck <long> WhoseSize <T>(this ICheck <IEnumerable <T> > check) { return(ExtensibilityHelper.ExtractChecker(check).ExtractSub(sut => sut.Count(), "size")); }
public static ICheckLink <ICheck <Person> > IsNawouak(this ICheck <Person> check) { var checker = ExtensibilityHelper.ExtractChecker(check); return(checker.BuildChainingObject()); }
public static ICheckLink <IStructCheck <Nationality> > IsOccidental(this IStructCheck <Nationality> check) { var structChecker = ExtensibilityHelper.ExtractStructChecker(check); return(structChecker.BuildChainingObject()); }
/// <summary> /// Checks that the actual actualValue has fields equals to the expected actualValue ones. /// </summary> /// <typeparam name="T"> /// Type of the checked actualValue. /// </typeparam> /// <typeparam name="TU">Type of the expected actualValue.</typeparam> /// <param name="check"> /// The fluent check to be extended. /// </param> /// <param name="expected"> /// The expected actualValue. /// </param> /// <returns> /// A check link. /// </returns> /// <exception cref="FluentCheckException"> /// The actual actualValue doesn't have all fields equal to the expected actualValue ones. /// </exception> /// <remarks> /// The comparison is done field by field. /// </remarks> public static ICheckLink <ICheck <T> > HasFieldsWithSameValues <T, TU>(this ICheck <T> check, TU expected) { check.Considering().All.Fields.IgnoreExtra.IsEqualTo(expected); return(ExtensibilityHelper.BuildCheckLink(check)); }
/// <summary> /// Checks that the checked <see cref="IEnumerable"/> contains items in the expected order. /// </summary> /// <param name="chainedCheckLink"> /// The chained fluent check. /// </param> /// <returns> /// A check link. /// </returns> public static IExtendableCheckLink <IEnumerable> InThatOrder(this IExtendableCheckLink <IEnumerable> chainedCheckLink) { var checker = ExtensibilityHelper.ExtractChecker(chainedCheckLink.And); var orderedList = ConvertToList(chainedCheckLink); var faillingIndex = 0; var scanIndex = 0; Debug.Assert(checker != null, "checker != null"); foreach (var item in checker.Value) { if (item != orderedList[scanIndex]) { var failed = false; // if current item is part of current list, check order var index = orderedList.IndexOf(item, scanIndex); if (index < 0) { // if not found at the end of the list, try the full list index = orderedList.IndexOf(item); if (index >= 0) { failed = true; } } else { var currentReference = orderedList[scanIndex]; // skip all similar entries in the expected list (tolerance: the checked enumerables may not contains as many instances of one item as expected while (currentReference == orderedList[++scanIndex] && scanIndex < orderedList.Count) { } // check if skipped only similar items if (scanIndex < index) { failed = true; } } if (failed) { // check failed. Now we have to refine the issue type. // we assume that Contains was executed (imposed by chaining syntax) // the item violating the order is the previous one! var message = checker.BuildMessage( string.Format( "The {{0}} does not follow to the expected order. Item [{0}] appears too {2} in the list, at index '{1}'.", item.ToStringProperlyFormated().DoubleCurlyBraces(), faillingIndex, index > scanIndex ? "early" : "late")) .ExpectedValues(chainedCheckLink.OriginalComparand); throw new FluentCheckException(message.ToString()); } if (index >= 0) { scanIndex = index; } } faillingIndex++; } return(chainedCheckLink); }
/// <summary> /// Checks that the actual <see cref="IDictionary{K,V}"/> is equivalent to a given dictionary. /// </summary> /// <param name="check">The fluent check to be extended.</param> /// <param name="other">Reference dictionary</param> /// <typeparam name="TK">Type for keys.</typeparam> /// <typeparam name="TU">Type for values.</typeparam> /// <returns>A check link.</returns> public static ICheckLink <ICheck <IEnumerable <KeyValuePair <TK, TU> > > > IsEquivalentTo <TK, TU>( this ICheck <IEnumerable <KeyValuePair <TK, TU> > > check, IEnumerable <KeyValuePair <TK, TU> > other) { EqualityHelper.ImplementEquivalentTo(ExtensibilityHelper.BeginCheck(check), other); return(ExtensibilityHelper.BuildCheckLink(check)); }
public static void HasElement(this ICheck <XElement> check, string name) { var actual = ExtensibilityHelper.ExtractChecker(check).Value; Check.That(actual.HasElement(name)).IsTrue(); }
/// <summary> /// Checks if the sut contains the same element than a given list. /// </summary> /// <param name="context">Context for the check</param> /// <param name="content"></param> /// <returns>A chainable link.</returns> public static ICheckLink <ICheck <IEnumerable> > IsEquivalentTo(this ICheck <IEnumerable> context, params object[] content) { ImplementEquivalentTo(ExtensibilityHelper.BeginCheckAs(context, enumerable => enumerable.Cast <object>()), content); return(ExtensibilityHelper.BuildCheckLink(context)); }
/// <inheritdoc /> public ICheckLinkWhich <ICheck <T>, ICheck <TU> > IsInstanceOf <TU>() { this.IsInstanceOfType(typeof(TU)); return(ExtensibilityHelper.BuildCheckLinkWhich(this, Value is TU ? (TU)(object)Value : default(TU), SutName)); }
public void NewCheckBuilderShouldWhenNegationMessageNotDefined() { var block = ExtensibilityHelper.BeginCheck(Check.That("kamoulox").Not); Check.ThatCode(() => { block.EndCheck(); }).Throws <InvalidOperationException>().WithMessage("Negated error message was not specified. Use 'OnNegate' method to specify one."); }
/// <summary> /// Checks that the actual value is not equal to another expected value. /// </summary> /// <typeparam name="T">Type of the struct or the enum to assert on.</typeparam> /// <param name="check">The fluent check to be extended.</param> /// <param name="expected">The expected value.</param> /// <returns> /// A check link. /// </returns> /// <exception cref="FluentCheckException">The actual value is equal to the expected value.</exception> public static ICheckLink <IStructCheck <T> > IsNotEqualTo <T>(this IStructCheck <T> check, object expected) where T : struct { var runnableStructCheck = ExtensibilityHelper.ExtractStructChecker(check); return(EqualityHelper.PerformEqualCheck(runnableStructCheck, expected, false, true)); }
/// <summary> /// Checks that the actual value is equal to another expected value. /// </summary> /// <typeparam name="T">Type of the struct or the enum to assert on.</typeparam> /// <param name="check">The fluent fluent check.</param> /// <param name="expected">The expected value.</param> /// <returns> /// A check link. /// </returns> /// <exception cref="FluentCheckException">The actual value is not equal to the expected value.</exception> public static ICheckLink <IStructCheck <T> > IsEqualTo <T>(this IStructCheck <T> check, T expected) where T : struct { var checker = ExtensibilityHelper.ExtractStructChecker(check); return(EqualityHelper.PerformEqualCheck(checker, expected, false)); }
/// <summary> /// Checks that the actual expression is in the inheritance hierarchy of the given kind or of the same kind. /// </summary> /// <typeparam name="T">Type of SUT</typeparam> /// <param name="check">The fluent check to be extended.</param> /// <param name="parentType">Expected type that should be^part of hierarchy</param> /// <returns>a check link object</returns> public static ICheckLink <ICheck <T> > InheritsFromType <T>(this ICheck <T> check, Type parentType) { IsInstanceHelper.InheritsFrom(check, parentType); return(ExtensibilityHelper.BuildCheckLink(check)); }
/// <summary> /// Allows to perform checks on the result value. /// </summary> /// <typeparam name="T">Type of the code result. Should be inferred.</typeparam> /// <param name="check">The fluent check to be extended.</param> /// <returns>A check object for the result.</returns> public static ICheck <T> WhichResult <T>(this ICodeCheck <RunTraceResult <T> > check) { var checker = ExtensibilityHelper.ExtractCodeChecker(check); return(new FluentCheck <T>(checker.Value.Result)); }
/// <summary> /// Checks that the nullable sut does not have a value. /// </summary> /// <typeparam name="T">value type</typeparam> /// <param name="check">fluent check</param> /// <returns>A check link</returns> public static ICheckLink <ICheck <T?> > HasNoValue <T>(this ICheck <T?> check) where T : struct { ExtensibilityHelper.BeginCheck(check).FailWhen(sut => sut.HasValue, "The {0} has a value, whereas it must not.") .OnNegate("The {0} has no value, which is unexpected.", MessageOption.NoCheckedBlock).EndCheck(); return(ExtensibilityHelper.BuildCheckLink(check)); }
/// <summary> /// Checks that the actual value is equal to another expected value using operator==. /// </summary> /// <typeparam name="TU">Type of the expected value</typeparam> /// <param name="check"> /// The fluent check to be extended. /// </param> /// <param name="expected"> /// The expected value. /// </param> /// <returns> /// A check link. /// </returns> /// <exception cref="FluentCheckException"> /// The actual value is not equal to the expected value. /// </exception> public static ICheckLink <ICheck <ReflectionWrapper> > HasSameValueAs <TU>(this ICheck <ReflectionWrapper> check, TU expected) { FieldEqualTest(check, expected, true); return(ExtensibilityHelper.BuildCheckLink(check)); }
public static void IsNamed(this ICheck <XElement> check, string name) { var actual = ExtensibilityHelper.ExtractChecker(check).Value; Check.That(actual.Name.LocalName).IsEqualTo(name); }
/// <summary> /// Checks that the actual value is different from another expected value using operator!=. /// </summary> /// <typeparam name="TU">Type of the expected value.</typeparam> /// <param name="check"> /// The fluent check to be extended. /// </param> /// <param name="expected"> /// The expected value. /// </param> /// <returns> /// A check link. /// </returns> /// <exception cref="FluentCheckException"> /// The actual value is equal to the expected value. /// </exception> public static ICheckLink <ICheck <ReflectionWrapper> > HasDifferentValueThan <TU>(this ICheck <ReflectionWrapper> check, TU expected) { FieldEqualTest(check, expected, false); return(ExtensibilityHelper.BuildCheckLink(check)); }
static async Task Main(string[] args) { /* Set interface name. Edit this to suit your needs. */ var interfaceName = "Ethernet1"; // "eth0"; /* Set ESI location. Make sure it contains ESI files! The default path is /home/{user}/.local/share/ESI */ var localAppDataPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); var esiDirectoryPath = Path.Combine(localAppDataPath, "ESI"); Directory.CreateDirectory(esiDirectoryPath); /* Copy native file. NOT required in end user scenarios, where EtherCAT.NET package is installed via NuGet! */ var codeBase = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); Directory.EnumerateFiles(Path.Combine(codeBase, "runtimes"), "*soem_wrapper.*", SearchOption.AllDirectories).ToList().ForEach(filePath => { if (filePath.Contains(RuntimeEnvironment.RuntimeArchitecture)) { File.Copy(filePath, Path.Combine(codeBase, Path.GetFileName(filePath)), true); } }); /* prepare dependency injection */ var services = new ServiceCollection(); ConfigureServices(services); /* create types */ var provider = services.BuildServiceProvider(); var extensionFactory = provider.GetRequiredService <IExtensionFactory>(); var loggerFactory = provider.GetRequiredService <ILoggerFactory>(); var logger = loggerFactory.CreateLogger("EtherCAT Master"); /* create EtherCAT master settings (with 10 Hz cycle frequency) */ var cycleFrequency = 10U; var settings = new EcSettings(cycleFrequency, esiDirectoryPath, interfaceName); /* create root slave info by scanning available slaves */ var rootSlaveInfo = EcUtilities.ScanDevices(settings.InterfaceName); foreach (var current in rootSlaveInfo.Descendants()) { try { ExtensibilityHelper.CreateDynamicData(settings.EsiDirectoryPath, extensionFactory, current); } catch (Exception ex) { logger.LogInformation(ex.Message); Console.ReadKey(true); return; } } /* print list of slaves */ var message = new StringBuilder(); var slaves = rootSlaveInfo.Descendants().ToList(); // Example Slave 0 is EP3174-0002 var EP3174_0002 = slaves[0]; var EP3174_0002_Ch0 = EP3174_0002.DynamicData.PdoSet[0]; var EP3174_0002_Ch0_Variables = EP3174_0002_Ch0.VariableSet; var EP3174_0002_Ch0_Variable0 = EP3174_0002_Ch0_Variables[0]; message.AppendLine($"Found {slaves.Count()} slaves:"); slaves.ForEach(current => { message.AppendLine($"{current.DynamicData.Name} (PDOs: {current.DynamicData.PdoSet.Count} - CSA: { current.Csa })"); }); logger.LogInformation(message.ToString().TrimEnd()); /* create variable references for later use */ var variables = slaves.SelectMany(child => child.GetVariableSet()).ToList(); /* create EC Master */ using (var master = new EcMaster(settings, extensionFactory, logger)) { try { master.Configure(rootSlaveInfo); } catch (Exception ex) { logger.LogError(ex.Message); throw; } /* start master */ var random = new Random(); var cts = new CancellationTokenSource(); var EP3174_0002_Ch0_Value = slaves[0].DynamicData.PdoSet[0].VariableSet.Last(); var task = Task.Run(() => { var sleepTime = 1000 / (int)cycleFrequency; while (!cts.IsCancellationRequested) { try { master.UpdateIO(DateTime.UtcNow); unsafe { if (variables.Any()) { var inputCh0 = new Span <int>(EP3174_0002_Ch0_Value.DataPtr.ToPointer(), 1)[0].ToByteArray(); int inputValue = BitConverter.ToInt16(inputCh0); double value = inputValue / 32767.0 * 10.0; logger.LogDebug($"{EP3174_0002_Ch0_Value.Name}: {value}"); var myVariableSpan = new Span <int>(variables.First().DataPtr.ToPointer(), 1); myVariableSpan[0] = random.Next(0, 100); } } } catch (Exception ex) { logger.LogError(ex.Message); } Thread.Sleep(sleepTime); } }, cts.Token); /* wait for stop signal */ Console.ReadKey(true); cts.Cancel(); await task; } }
public void NewCheckBuilderShouldWhenNegationMessageNotDefined() { var block = ExtensibilityHelper.BeginCheck(Check.That("kamoulox").Not); Check.ThatCode(() => { block.EndCheck(); }).Throws <InvalidOperationException>(); }
public static ICheckLink <IStructCheck <Nationality> > IsOccidental(this IStructCheck <Nationality> check) { var structChecker = ExtensibilityHelper.ExtractStructChecker(check); return(structChecker.ReturnValueForLinkage); }
/// <summary> /// Allows to perform checks on the result value. /// </summary> /// <typeparam name="T">Type of the code result. Should be inferred.</typeparam> /// <param name="check">The fluent check to be extended.</param> /// <returns>A check object for the result.</returns> public static ICheck <T> WhichResult <T>(this ICodeCheck <RunTraceResult <T> > check) { return(ExtensibilityHelper.BuildCheck(check, sut => sut.Result)); }