// Copyright Keysight Technologies 2012-2019
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, you can obtain one at http://mozilla.org/MPL/2.0/.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
namespace OpenTap.Plugins
{
/// Serializer implementation for parameters.
[Display("Parameter Serializer", "Works with parameters in the test plan.")]
public class ExternalParameterSerializer : TapSerializerPlugin
{
///
/// Structure for holding data about
///
public struct ExternalParamData
{
///
/// The object
///
public ITestStep Object;
///
/// The external param property.
///
public IMemberData Property;
///
/// The name of the external test plan parameter.
///
public string Name;
}
/// The order of this serializer.
public override double Order => 50;
List currentNode = new List();
///
/// Stores the data if a test plan was not serialized but the external keyword was used.
///
public readonly List UnusedExternalParamData = new List();
///
/// Pre-Loaded external parameter Name/Value sets.
///
public readonly Dictionary PreloadedValues = new Dictionary();
static readonly XName External = "external";
static readonly XName Scope = "Scope";
static readonly XName Parameter = "Parameter";
bool loadScopeParameter(Guid scope, ITestStep step, IMemberData member, string parameter)
{
ITestStepParent parent;
if (scope == Guid.Empty)
{
parent = step.GetParent();
}
else
{
ITestStep subparent = step.Parent as ITestStep;
while (subparent != null)
{
if (subparent.Id == scope)
break;
subparent = subparent.Parent as ITestStep;
}
parent = subparent;
}
if (parent == null) return false;
var newParameter = member.Parameterize(parent, step, parameter);
if (newParameter.ParameterizedMembers.Skip(1).Any())
{
// merge occured. See similar code in ExternalParameters.
if (ParameterManager.UnmergableListType(newParameter))
{
member.Unparameterize(newParameter, step);
Log.Warning("Unable merge parameters {0}, since their types do not support it.", parameter);
}
}
return true;
}
void loadPreloadedvalues(TestPlan plan)
{
foreach (var value in PreloadedValues)
{
var ext = plan.ExternalParameters.Get(value.Key);
if (ext == null) continue;
try
{
ext.Value = value.Value;
}
catch
{
}
}
// Update all external parameter values.
// This is generally redundant, since they were all serialized with the same values.
// but just to be sure.
foreach (var extParam in plan.ExternalParameters.Entries)
extParam.Value = extParam.Value;
}
XElement rootNode;
/// Deserialization implementation.
public override bool Deserialize(XElement elem, ITypeData t, Action