public void ThrowExceptionIfMethodNotFound() { // # Arrange. var pr = new PseudoRandom(nameof(ThrowExceptionIfMethodNotFound)); var obj = new MyBaseClass(); dynamic sut = new ReachIn(obj); // # Act and Assert. // The method does not exist. Assert.Throws <Exception>( () => { sut.ThisMethodIsNotFound(pr.Int()); }); // There is a property by the same name. Assert.Throws <RuntimeBinderException>( () => { sut.MyPrivateProperty(pr.Int()); }); }
public void ThrowExceptionIfStaticMethodNotFound() { // # Arrange. var pr = new PseudoRandom(nameof(ThrowExceptionIfStaticMethodNotFound)); dynamic sut = new ReachIn(typeof(MyBaseClass)); // # Act and Assert. // The method does not exist. Assert.Throws <Exception>( () => { sut.ThisMethodIsNotFound(pr.Int()); }); // There is a property by the same name. // Trying to call it fails. Assert.Throws <RuntimeBinderException>( () => { sut.MyStaticPrivateProperty(pr.Int()); }); }
public void ReachAllInnerClasses() { // # Arrange. var pr = new PseudoRandom(nameof(ReachAllInnerClasses)); var obj = new MyPublicClass(); dynamic sut = new ReachIn(obj); int res; int val; // # Act and Assert. val = pr.Int(); // We don't support finding privates in inherited classes // so here we look for a field overloaded in the child class // just as a sanity check. sut._myInnerPrivateClass._myPrivateField = val; res = sut._myInnerPrivateClass._myPrivateField; res.Should().Be(val); // Keep looking for a visible (e.g. internal) field that comes // from the base class. sut._myInnerPrivateClass._myInternalField = val; res = sut._myInnerPrivateClass._myInternalField; res.Should().Be(val); sut._myInnerInternalClass._myInternalField = val; res = sut._myInnerInternalClass._myInternalField; res.Should().Be(val); sut._myInnerProtectedClass._myInternalField = val; res = sut._myInnerProtectedClass._myInternalField; res.Should().Be(val); sut._myInnerPublicClass._myInternalField = val; res = sut._myInnerPublicClass._myInternalField; res.Should().Be(val); }
public void ReachAllFieldsOfAllTypes() { // # Arrange. var pr = new PseudoRandom(nameof(ReachAllFieldsOfAllTypes)); dynamic sut = new ReachIn(typeof(MyStaticBaseClassWithTypes)); // # Act and Assert. var anyIntValue = pr.Int(); sut._myPrivateStaticIntField = anyIntValue; // We have a problem here with FluentAssertions where it, runtime, throws an exception even though the parameters are equal. //sut._myPrivateStaticIntField.Should().be(anyIntValue); Assert.Equal(anyIntValue, sut._myPrivateStaticIntField); var anyStringValue = pr.String(); sut._myPrivateStaticStringField = anyStringValue; // We have a problem here with FluentAssertions where it, runtime, throws an exception even though the parameters are equal. //sut._myPrivateStaticStringField.Should().Be(anyStringValue); Assert.Equal(anyStringValue, sut._myPrivateStaticStringField); var anyLongValue = pr.PositiveLong(); sut._myPrivateStaticLongField = anyLongValue; // We have a problem here with FluentAssertions where it, runtime, throws an exception even though the parameters are equal. //sut._myPrivateStaticLongField.Should().be(anyLongValue); Assert.Equal(anyLongValue, sut._myPrivateStaticLongField); var anyObject = new { FieldOne = pr.Int(), }; sut._myPrivateStaticObjectField = anyObject; // We have a problem here with FluentAssertions where it, runtime, throws an exception even though the parameters are equal. //sut._myPrivateStaticObjectField.FieldOne.Should().Be(anyObject.FieldOne); Assert.Equal(anyObject.FieldOne, sut._myPrivateStaticObjectField.FieldOne); // If there is any other type to test, this is the place to add. }
public void ThrowExceptionIfStaticFieldOrPropetyNotFound() { // # Arrange. var pr = new PseudoRandom(nameof(ThrowExceptionIfStaticFieldOrPropetyNotFound)); dynamic sut = new ReachIn(typeof(MyBaseClass)); // # Act and Assert. // A setter does not exist. Assert.Throws <Exception>( () => { sut.ThisPropertyOrFieldIsNotFound = pr.Int(); }); // A getter does not exist. Assert.Throws <Exception>( () => { var res = sut.ThisPropertyOrFieldIsNotFound; }); }
public void ReachAllStaticFieldsAndProperties() { // # Arrange. var pr = new PseudoRandom(nameof(ReachAllStaticFieldsAndProperties)); dynamic sut = new ReachIn(typeof(MyBaseClass)); int value, res; // # Act and Assert. // ## Fields. value = pr.Int(); sut._myStaticPrivateField = value; res = sut._myStaticPrivateField; res.Should().Be(value); value = pr.Int(); sut._myStaticInternalField = value; res = sut._myStaticInternalField; res.Should().Be(value); value = pr.Int(); sut._myStaticProtectedField = value; res = sut._myStaticProtectedField; res.Should().Be(value); value = pr.Int(); sut._myStaticPublicField = value; res = sut._myStaticPublicField; res.Should().Be(value); // ## Properties. value = pr.Int(); sut.MyStaticPrivateProperty = value; res = sut.MyStaticPrivateProperty; res.Should().Be(value); value = pr.Int(); sut.MyStaticInternalProperty = value; res = sut.MyStaticInternalProperty; res.Should().Be(value); value = pr.Int(); sut.MyStaticProtectedProperty = value; res = sut.MyStaticProtectedProperty; res.Should().Be(value); value = pr.Int(); sut.MyStaticPublicProperty = value; res = sut.MyStaticPublicProperty; res.Should().Be(value); }
public void ReachAllStaticMethods() { // # Arrange. var pr = new PseudoRandom(nameof(ReachAllStaticMethods)); dynamic sut = new ReachIn(typeof(MyBaseClass)); var val = pr.Int(); // # Act and Assert. // We have a problem here with FluentAssertions where it, runtime, throws an exception even though the parameters are equal. //sut.MyStaticPrivateMethod(val).Should().Be(val + 1); //sut.MyStaticInternalMethod(val).Should().Be(val+1); //sut.MyStaticProtectedMethod(val).Should().Be(val+1); //sut.MyStaticPublicMethod(val).Should().Be(val+1); Assert.Equal(val + 1, sut.MyStaticPrivateMethod(val)); Assert.Equal(val + 1, sut.MyStaticInternalMethod(val)); Assert.Equal(val + 1, sut.MyStaticProtectedMethod(val)); Assert.Equal(val + 1, sut.MyStaticPublicMethod(val)); }
public void RandomizerTest2() { // Test needs to run in parallel to RandomizerTest // Checks that Parallel test runs don't share randomizer seeds for (int i = 0; i < 10; i++) { var randomizedObject = new { String = PseudoRandom.String(), Int = PseudoRandom.Int(), DateTime = PseudoRandom.DateTime() }; DiskAssert.Matches(randomizedObject, $"RandomizedObj_{i}"); Thread.Sleep(1000); } }
public void ObjAndSutUseTheSameInstance() { // # Arrange. var pr = new PseudoRandom(nameof(ObjAndSutUseTheSameInstance)); var obj = new MyBaseClass(); dynamic sut = new ReachIn(obj); dynamic sut2 = new ReachIn(obj); var value = pr.Int(); value.Should().NotBe(default(int), "Sanity check we haven't randomised the default 0 value since all tests then would be moot."); // # Act. sut.MyPublicProperty = value; sut2.MyPublicProperty = value; // # Assert. value.Should().Be(obj.MyPublicProperty); // We have a problem here with FluentAssertions where it, runtime, throws an exception even though the parameters are equal. //sut2.MyPublicProperty.Should().Be(sut.MyPublicProperty); Assert.Equal(sut.MyPublicProperty, sut2.MyPublicProperty); }
internal static Dto CreateRandomised(PseudoRandom pr) { return(CreateSetAllProperties(pr.String(), pr.String(), pr.String(), pr.String(), pr.Int())); }
internal static Dto Randomise(PseudoRandom pr) { return(new Dto(pr.Int(), null)); }