PageRenderTime 19ms CodeModel.GetById 10ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/ILSpy.BamlDecompiler/Tests/TestRunner.cs

http://github.com/icsharpcode/ILSpy
C# | 146 lines | 125 code | 19 blank | 2 comment | 6 complexity | 90d6321440d212e870123291cfbafa08 MD5 | raw file
  1// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
  2// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
  3
  4using System;
  5using System.Collections;
  6using System.Diagnostics;
  7using System.IO;
  8using System.Linq;
  9using System.Resources;
 10using System.Xml.Linq;
 11using ICSharpCode.Decompiler.Tests.Helpers;
 12using ICSharpCode.ILSpy;
 13using Mono.Cecil;
 14using NUnit.Framework;
 15using Ricciolo.StylesExplorer.MarkupReflection;
 16
 17namespace ILSpy.BamlDecompiler.Tests
 18{
 19	[TestFixture]
 20	public class TestRunner
 21	{
 22		[Test]
 23		public void Simple()
 24		{
 25			RunTest("cases/simple");
 26		}
 27		
 28		[Test]
 29		public void SimpleDictionary()
 30		{
 31			RunTest("cases/simpledictionary");
 32		}
 33		
 34		[Test]
 35		public void Resources()
 36		{
 37			RunTest("cases/resources");
 38		}
 39		
 40		[Test]
 41		[Ignore("Broken on AppVeyor CI (extra x:ConnectionId attribute)")]
 42		public void SimpleNames()
 43		{
 44			RunTest("cases/simplenames");
 45		}
 46		
 47		[Test]
 48		public void AvalonDockBrushes()
 49		{
 50			RunTest("cases/avalondockbrushes");
 51		}
 52		
 53		[Test]
 54		public void AvalonDockCommon()
 55		{
 56			RunTest("cases/avalondockcommon");
 57		}
 58		
 59		[Test]
 60		public void AttachedEvent()
 61		{
 62			RunTest("cases/attachedevent");
 63		}
 64		
 65		[Test]
 66		public void Dictionary1()
 67		{
 68			RunTest("cases/dictionary1");
 69		}
 70		
 71		[Test]
 72		public void MarkupExtension()
 73		{
 74			RunTest("cases/markupextension");
 75		}
 76		
 77		[Test]
 78		public void SimplePropertyElement()
 79		{
 80			RunTest("cases/simplepropertyelement");
 81		}
 82		
 83		[Test]
 84		public void Issue445()
 85		{
 86			RunTest("cases/issue445");
 87		}
 88		
 89		[Test]
 90		public void NamespacePrefix()
 91		{
 92			RunTest("cases/namespaceprefix");
 93		}
 94
 95		[Test]
 96		public void EscapeSequence()
 97		{
 98			RunTest("cases/escapesequence");
 99		}
100		
101		#region RunTest
102		void RunTest(string name)
103		{
104			RunTest(name, typeof(TestRunner).Assembly.Location, Path.Combine("..\\..\\Tests", name + ".xaml"));
105		}
106		
107		void RunTest(string name, string asmPath, string sourcePath)
108		{
109			var resolver = new DefaultAssemblyResolver();
110			var assembly = AssemblyDefinition.ReadAssembly(asmPath, new ReaderParameters { AssemblyResolver = resolver });
111			Resource res = assembly.MainModule.Resources.First();
112			Stream bamlStream = LoadBaml(res, name + ".baml");
113			Assert.IsNotNull(bamlStream);
114			XDocument document = BamlResourceEntryNode.LoadIntoDocument(resolver, assembly, bamlStream);
115
116			CodeAssert.AreEqual(File.ReadAllText(sourcePath), document.ToString());
117		}
118		
119		Stream LoadBaml(Resource res, string name)
120		{
121			EmbeddedResource er = res as EmbeddedResource;
122			if (er != null) {
123				Stream s = er.GetResourceStream();
124				s.Position = 0;
125				ResourceReader reader;
126				try {
127					reader = new ResourceReader(s);
128				}
129				catch (ArgumentException) {
130					return null;
131				}
132				foreach (DictionaryEntry entry in reader.Cast<DictionaryEntry>().OrderBy(e => e.Key.ToString())) {
133					if (entry.Key.ToString() == name) {
134						if (entry.Value is Stream)
135							return (Stream)entry.Value;
136						if (entry.Value is byte[])
137							return new MemoryStream((byte[])entry.Value);
138					}
139				}
140			}
141			
142			return null;
143		}
144		#endregion
145	}
146}