private void TestStartingWithMacroOperationSingle(Type t, int rounds) { for (int i = 0; i < rounds; i++) { MacroOpBase raw = (MacroOpBase)RandomPropertyGenerator.Create(t); ProtocolVersion minimumVersion = MacroOpManager.FindForType(raw.Id).Item1; // TODO - what about newer versions of the op? ICommand cmd = raw.ToCommand(minimumVersion); bool nullCommand = cmd == null; bool shouldBeNull = expectedNullCommand.Contains(t); //Assert.Equal(shouldBeNull, nullCommand); // TODO - reenable this once possible if (shouldBeNull || nullCommand) { continue; } var serCmd = cmd as SerializableCommandBase; // TODO - this shouldnt be needed once all Commands have appropriate macro stuff set Assert.NotNull(serCmd); MacroOpBase entry = serCmd.ToMacroOps(minimumVersion).Single(); if (entry == null) { throw new Exception("Deserialized not implemented"); } if (!t.GetTypeInfo().IsAssignableFrom(entry.GetType())) { throw new Exception("Deserialized operation of wrong type"); } RandomPropertyGenerator.AssertAreTheSame(raw, entry); } }
private static MacroOpBase DeserializeSingle(byte[] arr) { Assert.True(arr.Length >= 4); int cmdLength = arr[0]; Assert.False(arr.Length != cmdLength || cmdLength == 0); return(MacroOpManager.CreateFromData(arr, false)); }
private void RunForFile(string byteFilename, string xmlFilename) { XmlState xmlSpec = XmlStatePersistor.LoadState(xmlFilename); Assert.NotNull(xmlSpec); bool failed = false; using (StreamReader byteFile = new StreamReader(byteFilename)) { while (!byteFile.EndOfStream) { string[] parts = byteFile.ReadLine().Split(": "); Assert.Equal(2, parts.Length); int index = int.Parse(parts[0]); int count = int.Parse(parts[1]); Macro macroXml = xmlSpec.MacroPool.FirstOrDefault(m => m.Index == index); Assert.NotNull(macroXml); List <byte[]> data = Enumerable.Range(0, count).Select(x => byteFile.ReadLine().HexToByteArray()).ToList(); Assert.Equal(macroXml.Operations.Count, data.Count); for (var i = 0; i < count; i++) { try { MacroOpBase converted = MacroOpManager.CreateFromData(data[i], false); MacroOpBase op = macroXml.Operations[i].ToMacroOp(); if (!Equals(converted, op)) { output.WriteLine("Line {2}\nGot:\n {0}Expected:\n {1}", ToString(converted), ToString(op), i); failed = true; } } catch (Exception e) { output.WriteLine(e.Message + "\n"); failed = true; } } } } Assert.False(failed); }
private static Tuple <List <Operation>, List <Field> > FakeSpec() { var operations = new List <Operation>(); var fields = new List <Field>(); IReadOnlyDictionary <MacroOperationType, Type> types = MacroOpManager.FindAll(); foreach (var t in types) { MacroOperationType id = t.Key; Type type = t.Value; IEnumerable <PropertyInfo> props = type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).Where(prop => prop.GetCustomAttribute <NoSerializeAttribute>() == null); var opFields = new List <OperationField>(); foreach (PropertyInfo prop in props) { var fieldAttr = prop.GetCustomAttribute <MacroFieldAttribute>(); if (fieldAttr == null) { continue; } Tuple <string, bool> mappedType = TypeMappings.MapTypeFull(prop.PropertyType); fields.Add(new Field(fieldAttr.Id, fieldAttr.Name, mappedType.Item1, mappedType.Item2, prop.PropertyType.GetCustomAttributes <FlagsAttribute>().Any() || prop.PropertyType.GetCustomAttributes <XmlAsStringAttribute>().Any())); opFields.Add(new OperationField(fieldAttr.Id, fieldAttr.Name, prop.Name, prop.PropertyType.ToString())); } operations.Add(new Operation(id.ToString(), type.FullName, opFields, type.GetCustomAttributes <NoMacroFieldsAttribute>().Any())); } fields = fields.Distinct().OrderBy(f => f.Id).ToList(); operations = operations.OrderBy(o => o.Id).ToList(); var res = new List <Field>(); foreach (IGrouping <string, Field> grp in fields.GroupBy(f => f.Id)) { FieldEntry[] newTypes = grp.SelectMany(g => g.Entries).ToArray(); Field f = grp.First(); res.Add(new Field(f.Id, newTypes, f.EnumAsString, f.IsEnum)); } return(Tuple.Create(operations, res)); }
static AtemConnection() { AutoSerializeBase.CompilePropertySpecForTypes(CommandManager.GetAllTypes().SelectMany(g => g.Value.Select(t => t.Item2))); AutoSerializeBase.CompilePropertySpecForTypes(MacroOpManager.FindAll().Select(m => m.Value.Item2)); }
static AtemConnection() { AutoSerializeBase.CompilePropertySpecForTypes(CommandManager.GetAllTypes().Values); AutoSerializeBase.CompilePropertySpecForTypes(MacroOpManager.FindAll().Values); }
public static MacroSpec CompileData(DeviceProfile profile) { var res = new MacroSpec(); foreach (var op in MacroOpManager.FindAll()) { var xmlOp = new MacroOperationSpec() { Id = op.Key.ToString() }; res.Operations.Add(xmlOp); IEnumerable <PropertyInfo> props = op.Value.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) .Where(prop => prop.GetCustomAttribute <NoSerializeAttribute>() == null) .OrderBy(prop => prop.GetCustomAttribute <SerializeAttribute>()?.StartByte ?? 999); foreach (PropertyInfo prop in props) { var fieldAttr = prop.GetCustomAttribute <MacroFieldAttribute>(); if (fieldAttr == null) { continue; } var xmlField = new MacroFieldSpec() { Id = fieldAttr.Id, Name = fieldAttr.Name, IsId = prop.GetCustomAttribute <CommandIdAttribute>() != null }; xmlOp.Fields.Add(xmlField); if (prop.GetCustomAttribute <BoolAttribute>() != null) { xmlField.Type = MacroFieldType.Bool; } else if (prop.GetCustomAttribute <Enum8Attribute>() != null || prop.GetCustomAttribute <Enum16Attribute>() != null || prop.GetCustomAttribute <Enum32Attribute>() != null) { xmlField.Type = prop.PropertyType.GetCustomAttribute <FlagsAttribute>() != null ? MacroFieldType.Flags : MacroFieldType.Enum; string mappedTypeName = TypeMappings.MapType(prop.PropertyType.FullName); Type mappedType = prop.PropertyType; if (mappedTypeName != mappedType.FullName && mappedTypeName.IndexOf("System.") != 0) { mappedType = GetType(mappedTypeName); } foreach (object val in Enum.GetValues(mappedType)) { string id = val.ToString(); var xmlAttr = mappedType.GetMember(val.ToString())[0].GetCustomAttribute <XmlEnumAttribute>(); if (xmlAttr != null) { id = xmlAttr.Name; } if (!AvailabilityChecker.IsAvailable(profile, val)) { continue; } // TODO check value is available for usage location xmlField.Values.Add(new MacroFieldValueSpec() { Id = id, Name = val.ToString(), }); } } else { SetNumericProps(profile, op.Key, xmlField, prop); } } } return(res); }
public void AutoTestMacroOps() { using (var helper = new AtemComparisonHelper(Client, Output)) { IBMDSwitcherMacroControl ctrl = GetMacroControl(); var failures = new List <string>(); Assembly assembly = typeof(ICommand).GetTypeInfo().Assembly; IEnumerable <Type> types = assembly.GetTypes().Where(t => typeof(SerializableCommandBase).GetTypeInfo().IsAssignableFrom(t)); foreach (Type type in types) { if (type == typeof(SerializableCommandBase)) { continue; } /* * if (type != typeof(AuxSourceSetCommand)) * continue;*/ try { Output.WriteLine("Testing: {0}", type.Name); for (int i = 0; i < 10; i++) { SerializableCommandBase raw = (SerializableCommandBase)RandomPropertyGenerator.Create(type, (o) => AvailabilityChecker.IsAvailable(helper.Profile, o)); // TODO - wants to be ICommand IEnumerable <MacroOpBase> expectedOps = raw.ToMacroOps(ProtocolVersion.Latest); if (expectedOps == null) { Output.WriteLine("Skipping"); break; } using (new StopMacroRecord(ctrl)) // Hopefully this will stop recording if it exceptions { ctrl.Record(0, string.Format("record-{0}-{1}", type.Name, i), ""); helper.SendCommand(raw); helper.Sleep(20); } helper.Sleep(40); byte[] r = DownloadMacro(0); if (r.Length == 0) { throw new Exception("Macro has no operations"); } MacroOpBase decoded = MacroOpManager.CreateFromData(r, false); // This is assuming that there is a single macro op RandomPropertyGenerator.AssertAreTheSame(expectedOps.Single(), decoded); } } catch (Exception e) { var msg = string.Format("{0}: {1}", type.Name, e.Message); Output.WriteLine(msg); failures.Add(msg); } } Assert.Empty(failures); } }
private static IReadOnlyList <MacroOpBase> Deserialize(IEnumerable <byte[]> data) { return(data?.Select(d => MacroOpManager.CreateFromData(d, true)).ToList()); }