public void TransformJson() { // Arrange var transform = new TransformationCollection() { { "a/y", "2" }, { "a/z/t/w", "3" }, { "b", "5" }, { "c/a", "1" }, { "c/b", "2" }, { "c/b/t", "3" }, { "d", "4" }, { "#e", "" }, { "f/items[]`1", "1" }, { "f/items[]`2", "2" } }; // Act var transformer = new Transformer(); string result = transformer.Transform(new JsonDocument(@"{ 'a': { 'x': '1' }, 'b': '2', 'c': '3', 'e': '4' }"), transform); // Assert Assert.Equal(@"{ ""a"": { ""x"": ""1"", ""y"": ""2"", ""z"": { ""t"": { ""w"": ""3"" } } }, ""b"": ""5"", ""c"": { ""a"": ""1"", ""b"": { ""t"": ""3"" } }, ""d"": ""4"", ""f"": { ""items"": [ ""1"", ""2"" ] } }", result, ignoreCase: true, ignoreLineEndingDifferences: true, ignoreWhiteSpaceDifferences: true); }
/** * Reads data into the tile entity when loaded. */ public override void read(BinaryReader _br, StreamModeRead _eStreamMode) { base.read(_br, _eStreamMode); string collectionString = _br.ReadString(); this.collection = TransformationCollection.Read(collectionString, useHash); string tQueueString = _br.ReadString(); this.tQueue = tQueue.Read(tQueueString, useHash); this.requiresPower = _br.ReadBoolean(); this.powerSources = StringHelpers.WriteStringToList(_br.ReadString()); this.requiresHeat = _br.ReadBoolean(); this.heatSources = StringHelpers.WriteStringToList(_br.ReadString()); this.requiresNearbyBlocks = _br.ReadBoolean(); this.nearbyBlockTags = StringHelpers.WriteStringToList(_br.ReadString()); this.nearbyBlockRequireAllTags = _br.ReadBoolean(); this.nearbyBlockNames = StringHelpers.WriteStringToList(_br.ReadString()); this.nearbyBlocksNeeded = _br.ReadInt32(); this.nearbyBlockRange = StringHelpers.WriteStringToVector3i(_br.ReadString()); // After read: this.hasPower = false; this.hasHeat = false; this.hasNearbyBlocks = false; this.userAccessing = false; this.CalculateLookupCoordinates(); this.random = RandomStatic.Range(0, 20); }
/** * Reads a string into a TransformationCollection object. */ public static TransformationCollection Read(string _s, bool fromHash = false) { MatchCollection collectionCheck = TransformationCollection.readParser["tColParse"].Matches(_s); if (collectionCheck.Count == 0) { throw new Exception("Could not read data string " + _s + " into TransformationCollection."); } MatchCollection tDataGroups = TransformationCollection.readParser["tDataParse"].Matches(_s); if (tDataGroups.Count == 0) { throw new Exception("Could not find groups for data string " + _s + "."); } Dictionary <int, string> tDataStrings = new Dictionary <int, string>(); int i = 0; foreach (Match tDataGroup in tDataGroups) { i += 1; tDataStrings.Add(i, tDataGroup.Groups[(fromHash ? 0 : 1)].ToString()); } TransformationCollection tCollection = new TransformationCollection(); foreach (KeyValuePair <int, string> entry in tDataStrings) { tCollection.Add(TransformationData.Read(entry.Value, fromHash)); } return(tCollection); }
public void TransformXmlWithNamespace() { // Arrange var transform = new TransformationCollection() { { "manifest/@android:versionCode", "10001" } }; // Act var transformer = new Transformer(); string result = transformer.Transform(new XmlDocument(@"<manifest xmlns:android=""http://schemas.android.com/apk/res/android"" package=""com.myapp.name.here"" android:installLocation=""auto"" android:versionCode=""10000"" android:versionName=""1""> <uses-sdk android:minSdkVersion=""18"" android:targetSdkVersion=""23"" /> <uses-permission android:name=""android.permission.CHANGE_WIFI_STATE"" /> <uses-permission android:name=""android.permission.ACCESS_WIFI_STATE"" /> <application android:label=""Scan Plan 2 Test"" android:icon="""" android:theme="""" /> </manifest>"), transform); // Assert Assert.Equal(@"<manifest xmlns:android=""http://schemas.android.com/apk/res/android"" package=""com.myapp.name.here"" android:installLocation=""auto"" android:versionCode=""10001"" android:versionName=""1""> <uses-sdk android:minSdkVersion=""18"" android:targetSdkVersion=""23"" /> <uses-permission android:name=""android.permission.CHANGE_WIFI_STATE"" /> <uses-permission android:name=""android.permission.ACCESS_WIFI_STATE"" /> <application android:label=""Scan Plan 2 Test"" android:icon="""" android:theme="""" /> </manifest>", result, ignoreCase: true, ignoreLineEndingDifferences: true, ignoreWhiteSpaceDifferences: true); }
private TransformationCollection GetTransformations(VisitorResult result, List <RequireCall> flattened) { var trans = new TransformationCollection(); var moduleName = this.CheckForConfigPath(RelativeFileName.ToModuleName()); if (!result.RequireCalls.Any()) { var shim = GetShim(RelativeFileName); var deps = new List <string>(); if (shim != null) { deps = shim.Dependencies.Select(r => r.Dependency).ToList(); } trans.Add(ShimFileTransformation.Create(moduleName, deps)); } else { foreach (var reqCall in result.RequireCalls.Where(r => r.DependencyArrayNode != null)) { trans.Add(NormalizeDepsTransformation.Create(reqCall)); } // if there are no define calls but there is at least one require module call, transform that into a define call if (!result.RequireCalls.Where(r => r.Type == RequireCallType.Define).Any()) { if (result.RequireCalls.Where(r => r.IsModule).Any()) { var call = result.RequireCalls.Where(r => r.IsModule).FirstOrDefault(); trans.Add(ToDefineTransformation.Create(call)); trans.Add(AddIdentifierTransformation.Create(call, moduleName)); } //TODO: Append shimmed dependencies here too } else { var defineCall = result.RequireCalls.Where(r => r.Type == RequireCallType.Define).FirstOrDefault(); if (string.IsNullOrEmpty(defineCall.Id)) { trans.Add(AddIdentifierTransformation.Create(defineCall, moduleName)); } if (defineCall.DependencyArrayNode == null) { trans.Add(AddEmptyDepsArrayTransformation.Create(defineCall)); } var shim = GetShim(RelativeFileName); if (shim != null) { var deps = shim.Dependencies.Select(r => r.Dependency).ToList(); trans.Add(AddDepsTransformation.Create(defineCall, deps)); } } } return(trans); }
public void TransformJsonByIndex() { // Arrange var transform = new TransformationCollection() { { "items[2]/data", "EE" }, { "items[@i='1']/data", "FF" }, { "#items[0]", "" }, { "#items[@i='1']/x", "" }, { "items[@i='3']", "{ y: '20' }" }, { "items[@i='a/b']/data", "2" }, { "items[0]", "{ x: '40' }" }, }; // Act var transformer = new Transformer(); string result = transformer.Transform(new JsonDocument(@"{ 'items': [ { 'i': 0, data: 'AA' }, { 'i': 1, data: 'BB', x: '25' }, { 'i': 2, data: 'CC' }, { 'i': 3, data: 'DD' }, { 'i': 'a/b', data: 'EE' } ], 'b': '2', 'c': '3' }"), transform); // Assert Assert.Equal(@"{ ""items"": [ { ""x"": ""40"" }, { ""i"": 2, ""data"": ""EE"" }, { ""y"": ""20"" }, { ""i"": ""a/b"", ""data"": ""2"" } ], ""b"": ""2"", ""c"": ""3"" }", result, ignoreCase: true, ignoreLineEndingDifferences: true, ignoreWhiteSpaceDifferences: true); }
public void TransformException_WithMappedExceptionAndNullDefault_ReturnsTheStatusCode() { // Arrange var transformationCollection = new TransformationCollection(); transformationCollection.AddMappingFor<ArgumentNullException>(400); var ex = new ArgumentNullException(); // Act var statusCode = transformationCollection.TransformException(ex, null); // Assert Assert.Equal(400, statusCode); }
/** * Reads data into the tile entity when loaded. */ public override void read(BinaryReader _br, StreamModeRead _eStreamMode) { base.read(_br, _eStreamMode); string collectionString = _br.ReadString(); this.collection = TransformationCollection.Read(collectionString, useHash); string tQueueString = _br.ReadString(); this.tQueue = tQueue.Read(tQueueString, useHash); this.requiresPower = _br.ReadBoolean(); this.powerSources = StringHelpers.WriteStringToList(_br.ReadString()); this.requiresHeat = _br.ReadBoolean(); this.heatSources = StringHelpers.WriteStringToList(_br.ReadString()); }
public void AddMappingFor_WithAnUnseenException_AddsANewTransformationIntoTheCollection() { // Arrange var transformationCollection = new TransformationCollection(); // Act transformationCollection.AddMappingFor<ArgumentNullException>(400); // Assert Assert.NotNull(transformationCollection.Transformations); Assert.Equal(1, transformationCollection.Transformations.Count); Assert.True(transformationCollection.Transformations.ContainsKey(typeof(ArgumentNullException))); Assert.Equal(400, transformationCollection.Transformations[typeof(ArgumentNullException)]); }
public bool Execute() { bool result = true; BuildEngine.LogMessageEvent(new BuildMessageEventArgs($"Transforming file: {Path}", string.Empty, nameof(TransformConfig), MessageImportance.Normal)); try { TransformationCollection transforms = new TransformationCollection(); foreach (var item in Trasformations) { if (transforms.ContainsKey(item.ItemSpec) == false) { transforms.Add(item.ItemSpec, item.GetMetadata("Value")); } else { BuildEngine.LogWarningEvent(new BuildWarningEventArgs( null, null, null, 0, 0, 0, 0, $"Transform key duplicate: {item.ItemSpec}. Skipping.", null, nameof(TransformConfig), DateTime.Now)); result = false; } } TransformTask.Transform(Type, Path, Target ?? Path, transforms); BuildEngine.LogMessageEvent(new BuildMessageEventArgs($"File transformed to: {Target ?? Path}", string.Empty, nameof(TransformConfig), MessageImportance.Normal)); } catch (Exception ex) { BuildEngine.LogErrorEvent(new BuildErrorEventArgs( null, null, null, 0, 0, 0, 0, $"File transformation error: {ex.Message}", null, nameof(TransformConfig), DateTime.Now)); result = false; } return(result); }
public void TransformYaml() { // Arrange var transform = new TransformationCollection("e") { { "a/y", "2" }, { "a/z/t/w", "3" }, { "b", "5" }, { "c/a", "1" }, { "c/b", "2" }, { "c/b/t", "3" }, { "d", "4" }, { "f[]`1", "1" }, { "f[]`2", "2" } }; // Act var transformer = new Transformer(); string result = transformer.Transform(new YamlDocument(@"a: x: 1 b: 2 c: 3 e: x: 5"), transform); // Assert Assert.Equal(@"a: x: 1 y: 2 z: t: w: 3 b: 5 c: a: 1 b: t: 3 d: 4 f: - 1 - 2 ", result, ignoreCase: true, ignoreLineEndingDifferences: true, ignoreWhiteSpaceDifferences: true); }
static void Main(string[] args) { var filePath = GetPath(@"Scripts\BForms\Bundles\js\components.js"); var minimalPath = GetPath(@"Scripts\minimal.js"); var initUIPath = GetPath(@"Scripts\BForms\Plugins\UI\bforms.initUI.js"); var text = File.ReadAllText(initUIPath); var parser = new JavaScriptParser(); var program = parser.Parse(text); var visitor = new RequireVisitor(); var result = visitor.Visit(program, initUIPath); var lines = GetScriptLines(text); var flattenedResults = result.GetFlattened(); flattenedResults.ForEach( x => { EnsureHasRange(x.ParentNode.Node, lines); EnsureHasRange(x.DependencyArrayNode, lines); EnsureHasRange(x.ModuleDefinitionNode, lines); EnsureHasRange(x.ModuleIdentifierNode, lines); EnsureHasRange(x.SingleDependencyNode, lines); var arguments = x.ParentNode.Node.As <CallExpression>().Arguments; foreach (var arg in arguments) { EnsureHasRange(arg, lines); } }); var reqLines = flattenedResults.Select(x => GetTextFromFullScript(x.ParentNode.Node.Range, text)).ToList(); var modifiedString = text; var transformCollection = new TransformationCollection(); foreach (var req in flattenedResults) { transformCollection.Add(ToDefineTransformation.Create(req)); if (req.Type != RequireCallType.Define) { transformCollection.Add(AddIdentifierTransformation.Create(req, "gogu")); } } transformCollection.ExecuteAll(ref modifiedString); }
public void ValidateTransformationKey() {// Arrange var transform = new TransformationCollection() { { "", "1" } }; // Act var transformer = new Transformer(); ArgumentException result = Assert.Throws <ArgumentException>(() => transformer.Transform(new JsonDocument(@"{ }"), transform)); // Assert Assert.True(result.Message?.StartsWith("Transformation key is empty.")); }
public void ValidateWrongDocumentType() {// Arrange var transform = new TransformationCollection() { { "x", "1" } }; // Act var transformer = new Transformer(); ArgumentException result = Assert.Throws <ArgumentException>(() => transformer.Transform(@"{ <xml> x:y </xml> }", transform)); // Assert Assert.True(result.Message?.StartsWith("Unknown document type.")); }
public void TransformYamlImplicit() { // Arrange var transform = new TransformationCollection() { { "a/y", "2" }, { "a/z/t/w", "3" }, { "b", "5" }, { "c/a", "1" }, { "c/b", "2" }, { "c/b/t", "3" }, { "d", "4" }, }; // Act var transformer = new Transformer(); string result = transformer.Transform(@"a: x: 1 b: 2 c: 3", transform); // Assert Assert.Equal(@"a: x: 1 y: 2 z: t: w: 3 b: 5 c: a: 1 b: t: 3 d: 4 ", result, ignoreCase: true, ignoreLineEndingDifferences: true, ignoreWhiteSpaceDifferences: true); }
public static bool TransformConfig(this ICakeContext context, string type, string path, string target, TransformationCollection transformations) { var result = true; context.Log.Write(Verbosity.Normal, LogLevel.Information, "Transforming file: {0}", path); try { TransformTask.Transform(type, path, target ?? path, transformations); context.Log.Write(Verbosity.Normal, LogLevel.Information, "File transformed to: {0}", target ?? path); } catch (Exception ex) { context.Log.Write(Verbosity.Normal, LogLevel.Error, "File transformation error: {0}", ex.Message); result = false; } return(result); }
public void TransformXmlByIndex() { // Arrange var transform = new TransformationCollection() { { "info/param[1]/option", "DD" }, }; // Act var transformer = new Transformer(); string result = transformer.Transform(new XmlDocument(@"<info> <param> <option>AA</option> </param> <param> <option>BB</option> <argument>CC</argument> </param> </info>"), transform); // Assert Assert.Equal(@"<info> <param> <option>AA</option> </param> <param> <option>DD</option> <argument>CC</argument> </param> </info>", result, ignoreCase: true, ignoreLineEndingDifferences: true, ignoreWhiteSpaceDifferences: true); }
public void TransformException_WithUnmappedExceptionAndNullDefault_Throws() { // Arrange var transformationCollection = new TransformationCollection(); var ex = new ArgumentNullException(); // Act & Assert var exception = Assert.Throws<ExceptionNotMappedException>(() => transformationCollection.TransformException(ex, null)); Assert.Same(ex, exception.Exception); }
public void WhenInitialized_ShouldHaveAnEmptyTransformationCollection() { // Act var transformationCollection = new TransformationCollection(); // Assert Assert.NotNull(transformationCollection.Transformations); Assert.Equal(0, transformationCollection.Transformations.Count); }
public void TransformException_WithUnmappedException_ReturnsTheDefault() { // Arrange var transformationCollection = new TransformationCollection(); var ex = new ArgumentNullException(); // Act var statusCode = transformationCollection.TransformException(ex, 500); // Assert Assert.Equal(500, statusCode); }
public void TransformXmlImplicit() { // Arrange var transform = new TransformationCollection("xml/g") { { "xml/a/y", "2" }, { "xml/a/@y", "3" }, { "xml/a/z/t/w", "3" }, { "xml/b", "5" }, { "xml/c/a", "1" }, { "xml/c/b", "2" }, { "xml/c/b/t", "3" }, { "xml/e/item[@key = 'item2']", "5" }, { "xml/e/item[@key=\"item3\"]", "6" }, { "xml/f/item[@key = 'item2']/val", "7" }, { "xml/f/item[@key=\"item3\"]/val", "8" }, { "xml/d", "4" }, }; // Act var transformer = new Transformer(); string result = transformer.Transform(@"<xml> <a> <x>1</x> </a> <b>2</b> <c>3</c> <e> <item key=""item1"">1</item> <item key=""item2"">2</item> <item key=""item3"">3</item> </e> <f> <item key=""item1""> <val>1</val> </item> <item key=""item2""> <val>2</val> </item> <item key=""item3""> <val>3</val> </item> </f> <g> <x></x> </g> </xml>", transform); // Assert Assert.Equal(@"<xml> <a y=""3""> <x>1</x> <y>2</y> <z> <t> <w>3</w> </t> </z> </a> <b>5</b> <c> <a>1</a> <b> <t>3</t> </b> </c> <e> <item key=""item1"">1</item> <item key=""item2"">5</item> <item key=""item3"">6</item> </e> <f> <item key=""item1""> <val>1</val> </item> <item key=""item2""> <val>7</val> </item> <item key=""item3""> <val>8</val> </item> </f> <d>4</d> </xml>", result, ignoreCase: true, ignoreLineEndingDifferences: true, ignoreWhiteSpaceDifferences: true); }
public void TransformXml() { // Arrange var transform = new TransformationCollection() { { "xml/a/y", "2" }, { "xml/a/@y", "3" }, { "xml/a/x/@q", "9" }, { "xml/a/z/t/w", "3" }, { "xml/b", "5" }, { "xml/c/a", "1" }, { "xml/c/b", "2" }, { "xml/c/b/t", "3" }, { "xml/e/item[@key = 'item2']", "5" }, { "xml/e/item[@key=\"item3\"]", "6" }, { "xml/f/item[@key = 'item2']/val", "7" }, { "xml/f/item[@key=\"item3\"]/val", "8" }, { "xml/f/item[@key='item3/subsetting']/val", "9" }, { "xml/d", "4" }, { "#xml/g", "" }, { "xml/items[]`1", "<val>1</val>" }, { "xml/items[]`2", "<val>2</val>" }, { "xml/data[]`1", "<x>1</x>" }, { "xml/data[]`2", "<y>2</y>" }, }; // Act var transformer = new Transformer(); string result = transformer.Transform(new XmlDocument(@"<xml> <a> <x>1</x> </a> <b>2</b> <c>3</c> <e> <item key=""item1"">1</item> <item key=""item2"">2</item> <item key=""item3"">3</item> </e> <f> <item key=""item1""> <val>1</val> </item> <item key=""item2""> <val>2</val> </item> <item key=""item3""> <val>3</val> </item> <item key=""item3/subsetting""> <val></val> </item> </f> <g> <x></x> </g> <items /> </xml>"), transform); // Assert Assert.Equal(@"<xml> <a y=""3""> <x q=""9"">1</x> <y>2</y> <z> <t> <w>3</w> </t> </z> </a> <b>5</b> <c> <a>1</a> <b> <t>3</t> </b> </c> <e> <item key=""item1"">1</item> <item key=""item2"">5</item> <item key=""item3"">6</item> </e> <f> <item key=""item1""> <val>1</val> </item> <item key=""item2""> <val>7</val> </item> <item key=""item3""> <val>8</val> </item> <item key=""item3/subsetting""> <val>9</val> </item> </f> <items> <val>1</val> <val>2</val> </items> <d>4</d> <data> <x>1</x> <y>2</y> </data> </xml>", result, ignoreCase: true, ignoreLineEndingDifferences: true, ignoreWhiteSpaceDifferences: true); }
public static bool TransformConfig(this ICakeContext context, string path, string target, TransformationCollection transformations) { return(TransformConfig(context, null, path, target, transformations)); }
/** * Sets the collection variable to hold details of what items transform into what other ones. */ public void SetTransformationCollection(TransformationCollection collection) { this.collection = collection; }
public static void Transform(string type, string sourcePath, string targetPath, TransformationCollection transformation) { if (File.Exists(sourcePath) == false) { throw new ArgumentException($"File \"{sourcePath}\" does not exists.", nameof(sourcePath)); } string target; if (String.IsNullOrWhiteSpace(targetPath) == false) { try { target = Path.GetFullPath(targetPath); } catch (ArgumentException ex) { throw new ArgumentException($"Path \"{targetPath}\" is incorrect.", nameof(targetPath), ex); } catch (IOException ex) { throw new ArgumentException($"Path \"{targetPath}\" is incorrect.", nameof(targetPath), ex); } } else { target = sourcePath; } var source = File.ReadAllText(sourcePath); string result; ITransformer transformer = new Transformer(); if (String.IsNullOrWhiteSpace(type) == false) { var document = transformer.DocumentTypes.FirstOrDefault(x => x.Name == type + "Document"); if (document == null) { throw new ArgumentException($"Wrong document type: {type}", nameof(type)); } result = transformer.Transform(document, source, transformation); } else { result = transformer.Transform(source, transformation); } File.WriteAllText(target, result); }
public static void Transform(string sourcePath, string targetPath, TransformationCollection transformation) { Transform(null, sourcePath, targetPath, transformation); }
public Transformation(IVisibleObject vo) { _vo = vo; Children = new TransformationCollection(); }