/
ContentTypeIdentifierTests.cs
89 lines (81 loc) · 2.92 KB
/
ContentTypeIdentifierTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using NUnit.Framework;
namespace DeltaEngine.Content.Xml.Tests
{
public class ContentTypeIdentifierTests
{
[TestCase("Image.png", ContentType.Image)]
[TestCase("Sound.wav", ContentType.Sound)]
[TestCase("Music.wma", ContentType.Music)]
[TestCase("Video.mp4", ContentType.Video)]
[TestCase("Json.json", ContentType.Json)]
[TestCase("Particle.deltaparticle", ContentType.ParticleEmitter)]
[TestCase("Shader.deltashader", ContentType.Shader)]
[TestCase("Material.deltamaterial", ContentType.Material)]
[TestCase("Geometry.deltageometry", ContentType.Geometry)]
[TestCase("Mesh.deltamesh", ContentType.Mesh)]
[TestCase("Model.fbx", ContentType.Model)]
[TestCase("Scene.deltascene", ContentType.Scene)]
[TestCase("Animation.gif", ContentType.JustStore)]
public void UniqueFileExtensionShouldHaveRespectiveContentTypeAssociated(string filename,
ContentType expectedContentType)
{
ContentType contentTypeFromFileExtension = ContentTypeIdentifier.ExtensionToType(filename);
Assert.AreEqual(expectedContentType, contentTypeFromFileExtension);
}
[TestCase("Font", ContentType.Font)]
[TestCase("InputCommands", ContentType.InputCommand)]
[TestCase("Level", ContentType.Level)]
public void ContentTypeOfXmlFileDependsOnRootElement(string tag,
ContentType expectedContentType)
{
string xmlData = CreateXmlDataFromTag(tag);
ContentType contentTypeFromXmlRoot = GetContentTypeOfXmlData(xmlData);
Assert.AreEqual(expectedContentType, contentTypeFromXmlRoot);
}
private static string CreateXmlDataFromTag(string tag)
{
return "<" + tag + "></" + tag + ">";
}
private static ContentType GetContentTypeOfXmlData(string xmlData)
{
using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(xmlData)))
{
XDocument file = XDocument.Load(stream);
return ContentTypeIdentifier.DetermineTypeForXmlFile(file);
}
}
[Test]
public void ContentTypeOfUndefinedRootElementsIsXml()
{
string xmlData = CreateXmlDataFromTag("Test");
ContentType type = GetContentTypeOfXmlData(xmlData);
Assert.AreEqual(ContentType.Xml, type);
}
[Test]
public void ThrowExceptionIfInvalidXmlData()
{
const string InvalidXmlData = "<Opening></Closing>";
Assert.Throws<XmlException>(() => GetContentTypeOfXmlData(InvalidXmlData));
}
#if DEBUG
[Test]
public void UnsupportedTypeLogsAndThrowsExceptionInDebug()
{
Assert.Throws<ContentTypeIdentifier.UnsupportedContentFileFoundCannotParseType>(
() => ContentTypeIdentifier.ExtensionToType(UnsupportedFileName));
}
#else
[Test]
public void UnsupportedTypeReturnsJustStoreInRelease()
{
Assert.AreEqual(ContentType.JustStore,
ContentTypeIdentifier.ExtensionToType(UnsupportedFileName));
}
#endif
private const string UnsupportedFileName = "unsupported";
}
}