using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
namespace PdbLib
{
public static class AssertExtensions
{
public static void AssertVec3Equal(this Assert assert, Vec3 expected, Vec3 actual, double tolerance, string message = "")
{
Assert.IsTrue(expected.DistanceTo(actual) <= tolerance, message);
}
public static void AssertVec3NotEqual(this Assert assert, Vec3 expected, Vec3 actual, double tolerance, string message = "")
{
Assert.IsTrue(expected.DistanceTo(actual) > tolerance, message);
}
public static void AssertVec3Equal2(this Assert assert, Vec3 expected, Vec3 actual, double tolerance, string message = "")
{
if (Math.Abs(expected.X - actual.X) >= tolerance)
{
throw new AssertFailedException($"{message} X-component is incorrect. Expected: {expected.X}, Actual: {actual.X}");
}
if (Math.Abs(expected.Y - actual.Y) >= tolerance)
{
throw new AssertFailedException($"{message} Y-component is incorrect. Expected: {expected.Y}, Actual: {actual.Y}");
}
if (Math.Abs(expected.Z - actual.Z) >= tolerance)
{
throw new AssertFailedException($"{message} Z-component is incorrect. Expected: {expected.Z}, Actual: {actual.Z}");
}
}
}
}
I have kept this class in a unit-test project and named it unit_test_library
.
Then, I added unit_test_library
library as a reference to the actual unit test projects.
However, those projects are not finding these functions as members of the Assert
class.
What is the issue?
I am using:
Example usage:
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PdbLib;
namespace PdbLibTests
{
[TestClass]
public class HingeMoversTests
{
[TestMethod]
public void MakePredefinedMove()
{
MakePredefinedMoveN(1);
//MakePredefinedHingeMoveN(7);
//MakePredefinedHingeMoveN(8);
}
private void MakePredefinedMoveN(int hingeMoveSize)
{
int residue_count = 3;
// Setup random generator
Random random = new Random(42);
PdbLib.System model = PdbLib.System.MakeRandom(new int[] { residue_count }, 100.0, random);
// Initialize internal coordinates
double[] r = new double[residue_count];
double[] planar = new double[residue_count];
double[] dihedral = new double[residue_count];
Vec3[] coords = new Vec3[residue_count];
for (int i = 0; i < residue_count; i++)
{
r[i] = 1.0;
planar[i] = (90.0).ToRadian();
dihedral[i] = (180.0).ToRadian();
coords[i] = new Vec3();
}
NeRF.RestoreLinearChain(r, planar, dihedral, ref coords);//(0,0,0), (1,0,0), (1,1,0)
for (int i = 0; i < residue_count; i++)
{
model.Vec3ToCa(i, coords[i]);//(0,0,0), (42949672, 0, 0), (42949672, 42949672, 0)
}
AtomMover mover = new AtomMover(hingeMoveSize, Math.PI / 2.0, Math.PI / 2.0);
MoveProposal mp = new MoveProposal(hingeMoveSize);//(0,0,0)
Vec3 v0Before = model.CaToVec3(0);//0,0,0
Vec3 v1Before = model.CaToVec3(1);//1,0,0
Vec3 lastBefore = model.CaToVec3(hingeMoveSize + 1);//1,1,0
mover.ComputeMove(model, 1, Math.PI / 2, ref mp);//mp = (42949672, 0, -30370005)
mp.Apply(ref model);//model = (0,0,0), (42949672, 0, -30370005), (42949672,42949672,0)
Vec3 v0After = model.CaToVec3(0);//0,0,0
Vec3 v1After = model.CaToVec3(1);//1,0,0
Vec3 lastAfter = model.CaToVec3(hingeMoveSize + 1);//1,1,0
Assert.AssertVec3Equal(v0Before, v0After, 1e-5, "First CA should remain unchanged.");
Assert.AssertVec3Equal(lastBefore, lastAfter, 1e-5, "Last CA should remain unchanged.");
double dihedralMovedDegrees = Vec3Helper.DihedralAngle4(v1Before, v0Before, lastBefore, v1After).ToDegrees();
Assert.AreEqual(90.0, Math.Abs(dihedralMovedDegrees), 0.01, "Dihedral angle should change by 90 degrees.");
}
}
}
If you define extension methods for the Assert
class of MsTest, you should use them via the That
property of this class. In your case, this is
Assert.That.AssertVec3NotEqual(vector1, vector2, tolerance);
If you omit the Assert
from the name of the method, you will even get a phrase that reads like an English sentnece: Assert that vec3 are not equal.
See also the documentation: https://github.com/Microsoft/testfx-docs/blob/main/RFCs/002-Framework-Extensibility-Custom-Assertions.md