Skip to content
This repository has been archived by the owner on May 21, 2018. It is now read-only.

Refactor the test mocks #57

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 50 additions & 50 deletions T4TS.Tests/CodeTraverserTests.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Linq;
using T4TS.Example.Models;
using T4TS.Tests.Traversal.Models;
using T4TS.Tests.Utils;

namespace T4TS.Tests.Traversal
{
[TestClass]
public class CodeTraverserTests
{
[TestMethod]
public void ShouldBuildInterfacesFromMarkedClassesOnly()
{
var solution = DTETransformer.BuildDteSolution(
typeof(LocalModel), // has the TypeScriptInterface attribute
typeof(ModelFromDifferentProject), // has the TypeScriptInterface attribute
typeof(string) // has no TypeScriptInterface attribute
);

var codeTraverser = new CodeTraverser(solution, new Settings());
Assert.AreEqual(2, codeTraverser.GetAllInterfaces().Count());
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Linq;
using T4TS.Example.Models;
using T4TS.Tests.Mocks;
using T4TS.Tests.Traversal.Models;
namespace T4TS.Tests.Traversal
{
[TestClass]
public class CodeTraverserTests
{
[TestMethod]
public void ShouldBuildInterfacesFromMarkedClassesOnly()
{
var solution = new MockSolution(
typeof(LocalModel), // has the TypeScriptInterface attribute
typeof(ModelFromDifferentProject), // has the TypeScriptInterface attribute
typeof(string) // has no TypeScriptInterface attribute
).Object;
var codeTraverser = new CodeTraverser(solution, new Settings());
Assert.AreEqual(2, codeTraverser.GetAllInterfaces().Count());
}

[TestMethod]
Expand All @@ -34,39 +34,39 @@ public void ShouldWorkIfSolutionContainsPartialClasses() {
//T4TS.Example.Models.Partial
// ...

var solution = DTETransformer.BuildDteSolution(
var solution = new MockSolution(
typeof(T4TS.Tests.Fixtures.Partial.PartialModel),
typeof(T4TS.Tests.Fixtures.Partial.PartialModel),
typeof(T4TS.Tests.Fixtures.Partial.InheritsFromPartialModel)
);
).Object;

var codeTraverser = new CodeTraverser(solution, new Settings());
var allModules = codeTraverser.GetAllInterfaces();
Assert.AreEqual(1, allModules.Count());
Assert.AreEqual(3, allModules.First().Interfaces.Count());
}

[TestMethod]
public void ShouldHandleReservedPropNames()
{
var solution = DTETransformer.BuildDteSolution(typeof(ReservedPropModel));
var codeTraverser = new CodeTraverser(solution, new Settings());

var modules = codeTraverser.GetAllInterfaces();
var interfaces = modules.Single().Interfaces;
var modelInterface = interfaces.Single();

var classProp = modelInterface.Members.SingleOrDefault(m => m.Name == "class");
var readonlyProp = modelInterface.Members.SingleOrDefault(m => m.Name == "readonly");
var publicProp = modelInterface.Members.SingleOrDefault(m => m.Name == "public");

Assert.AreEqual(3, modelInterface.Members.Count);

Assert.IsNotNull(classProp);
Assert.IsNotNull(readonlyProp);
Assert.IsNotNull(publicProp);

Assert.IsTrue(publicProp.Optional);
}
}
}
}
[TestMethod]
public void ShouldHandleReservedPropNames()
{
var solution = new MockSolution(typeof(ReservedPropModel)).Object;
var codeTraverser = new CodeTraverser(solution, new Settings());
var modules = codeTraverser.GetAllInterfaces();
var interfaces = modules.Single().Interfaces;
var modelInterface = interfaces.Single();
var classProp = modelInterface.Members.SingleOrDefault(m => m.Name == "class");
var readonlyProp = modelInterface.Members.SingleOrDefault(m => m.Name == "readonly");
var publicProp = modelInterface.Members.SingleOrDefault(m => m.Name == "public");
Assert.AreEqual(3, modelInterface.Members.Count);
Assert.IsNotNull(classProp);
Assert.IsNotNull(readonlyProp);
Assert.IsNotNull(publicProp);
Assert.IsTrue(publicProp.Optional);
}
}
}
71 changes: 71 additions & 0 deletions T4TS.Tests/Mocks/BaseList.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using System;
using System.Collections;
using System.Collections.Generic;
using EnvDTE;

namespace T4TS.Tests.Mocks
{
internal class BaseList<TItem> : List<TItem>
{
public BaseList()
{
}

public BaseList(IEnumerable<TItem> items)
: base(items)
{
}

public object Parent
{
get { throw new NotImplementedException(); }
}

public DTE DTE
{
get { throw new NotImplementedException(); }
}

public string Kind
{
get { throw new NotImplementedException(); }
}

public TItem Item(object index)
{
return this[(int) index];
}

public new IEnumerator GetEnumerator()
{
return base.GetEnumerator();
}

public void Reserved1(object element)
{
throw new NotImplementedException();
}

public bool CreateUniqueID(string prefix, ref string newName)
{
throw new NotImplementedException();
}
}

internal class CodeElements<TItem> : BaseList<TItem>, CodeElements
{
public CodeElements()
{
}

public CodeElements(IEnumerable<TItem> items)
: base(items)
{
}

public new CodeElement Item(object index)
{
return (CodeElement)this[(int)index - 1];
}
}
}
23 changes: 23 additions & 0 deletions T4TS.Tests/Mocks/MockCodeElements.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using EnvDTE;
using Moq;
using System.Linq;
using System.Collections.Generic;
using T4TS.Tests.Utils;

namespace T4TS.Tests.Mocks
{
internal class MockCodeElements : CodeElements<CodeNamespace>
{
public MockCodeElements(params Type[] types)
{
NamespaceUtil.GroupedByNamespace(types).ToList().ForEach(kv => {
var codeNamespace = new Mock<CodeNamespace>(MockBehavior.Strict);
codeNamespace.Setup(x => x.Members).Returns(new MockCodeTypes(kv.Value));
codeNamespace.Setup(x => x.Name).Returns(kv.Key);

Add(codeNamespace.Object);
});
}
}
}
Loading