public void Test1() { var x = new MyClass1(); Assert.True(x.isTrue()); Assert.Equal(4, 2 + 2); }
static void Main(string[] args) { MyClass1 mc1 = new MyClass1(); Console.WriteLine(mc1[2]); Console.ReadKey(); }
public void ShouldMapNullableValueToNullIfItIsNullOnSource() { var obj1 = new MyClass1(); var obj2 = mapper.Map <MyClass2>(obj1); obj2.NullableValue.ShouldBeNull(); }
public void testEnumAsObject1() { var mc = new MyClass1(); var result = mc.Foo(EnumInt.V2); AssertTrue(result == (int)EnumInt.V2); }
public void Utility_PropertyEquals_1() { object first = new MyClass1 { X = 1, Y = 2 }; object second = new MyClass1 { X = 1, Y = 2 }; Assert.IsTrue(Utility.PropertyEquals(first, second)); Assert.IsTrue(Utility.PropertyEquals(second, first)); Assert.IsTrue(Utility.PropertyEquals(first, first)); Assert.IsTrue(Utility.PropertyEquals(second, second)); second = new MyClass1 { X = 2, Y = 1 }; Assert.IsFalse(Utility.PropertyEquals(first, second)); Assert.IsFalse(Utility.PropertyEquals(second, first)); second = null; Assert.IsFalse(Utility.PropertyEquals(first, second)); Assert.IsFalse(Utility.PropertyEquals(second, first)); second = "Hello"; Assert.IsFalse(Utility.PropertyEquals(first, second)); Assert.IsFalse(Utility.PropertyEquals(second, first)); }
public void ExampleUsage1() { MyClass1 original = new MyClass1() { name = "1", child = new MyClass1() { name = "2" } }; MyClass1 copy = original.DeepCopyViaJson(); Assert.Null(MergeJson.GetDiff(original, copy)); // No diff between original and copy AssertV2.AreEqualJson(original, copy); // WIll use MergeJson.GetDiff internally Assert.Equal(original.child.name, copy.child.name); // Modify the copy, changing the copy will not change the original: copy.child.name = "Some new name.."; // Check that the change was only done in the copy and not the original: Assert.NotEqual(original.child.name, copy.child.name); Assert.NotNull(MergeJson.GetDiff(original, copy)); // Objects that impl. IClonable can also ShallowCopy (will call .Clone internally): MyClass1 shallowCopy = original.ShallowCopyViaClone(); Assert.NotSame(original, shallowCopy); Assert.Same(original.child, shallowCopy.child); }
private static void Main(string[] args) { Vehicle car = new Car { Mark = "BMW", Model = "850i", Color = "Blue", DoorCount = 3, HorsePower = 430, Year = 2019 }; var romPetrol = new GasStation("ROM PETROL"); var driver1 = new Driver("Cristian", 20, car); driver1.Drive(); driver1.Drive(car); Console.WriteLine($"{car.Mark}, {car.Model} petrol volume = {car.PetrolLevel} litres"); romPetrol.Refuel(car, 100); Console.WriteLine($"{car.Mark}, {car.Model} petrol volume = {car.PetrolLevel} litres"); car.TurnLeft(); Console.ReadKey(); var test = new MyClass1(); }
public object testNameOf() { //nameof(T) 会得到"T" var xxx = new MyClass1(); return(nameof(xxx)); }
public void Core_Object_CastOrFill() { var testItem = new MyClass1(); var compareItem = new MyClass3(); Assert.IsTrue(testItem.CastOrFill <MyClass3>().GetType() == compareItem.GetType()); }
public MyClass3(MyClass1 myClass1, MyClass2 myClass2, Ns2.MyClass1 myOtherClass1, Ns2.MyClass2 myOtherClass2) { MyClass1 = myClass1 ?? throw new ArgumentNullException(nameof(myClass1)); MyClass2 = myClass2 ?? throw new ArgumentNullException(nameof(myClass2)); MyOtherClass1 = myOtherClass1 ?? throw new ArgumentNullException(nameof(myOtherClass1)); MyOtherClass2 = myOtherClass2 ?? throw new ArgumentNullException(nameof(myOtherClass2)); }
public void Should_Map_Nullable_Value_To_Null_If_It_Is_Null_On_Source() { var obj1 = new MyClass1(); var obj2 = _mapper.Map <MyClass2>(obj1); obj2.NullableValue.ShouldBe(null); }
public MyClass2() { var myClass1 = new MyClass1(); //this will not work due to protection level myClass1.ModeProperty = MyClass1.Mode. }
public async Task ExampleUsage2() { MyClass1 myState1 = new MyClass1() { myName = "fgh", myChildren = new MyClass1[] { new MyClass1() { // Provoke a change in the state in each test execution: myName = "" + new Random().NextRandomName() } } }; // Deleting old diff entries is possible like this: // await new PersistedRegression().regressionStore.Remove("PersistedRegressionTests_ExampleUsage2_1"); var diffInStateWasAccepted = false; // A filter can be used to remove/filter out acceptable diffs from the report: await new PersistedRegression().AssertEqualToPersisted("PersistedRegressionTests_ExampleUsage2_1", (diff) => { // Ignore differences in the field 'myChildren.0.myName' (has a random name in it): if (diff.Count() == 1 && diff.SelectToken("myChildren.0.myName") != null) { diffInStateWasAccepted = true; return(false); } diffInStateWasAccepted = false; return(true); // For all other diffs confirm that its a non acceptable diff }, myState1); Assert.True(diffInStateWasAccepted); // Make sure the regression entry was really created Assert.True(await new PersistedRegression().regressionStore.ContainsKey("PersistedRegressionTests_ExampleUsage2_1")); }
public void ExampleUsage() { // PlayerPrefsV2.SetBool and PlayerPrefsV2.GetBool example: bool myBool = true; PlayerPrefsV2.SetBool("myBool", myBool); Assert.AreEqual(myBool, PlayerPrefsV2.GetBool("myBool", defaultValue: false)); // PlayerPrefsV2.SetStringEncrypted and PlayerPrefsV2.GetStringDecrypted example: PlayerPrefsV2.SetStringEncrypted("mySecureString", "some text to encrypt", password: "******"); var decryptedAgain = PlayerPrefsV2.GetStringDecrypted("mySecureString", null, password: "******"); Assert.AreEqual("some text to encrypt", decryptedAgain); // PlayerPrefsV2.SetObject and PlayerPrefsV2.GetObject example (uses JSON internally): MyClass1 myObjectToSave = new MyClass1() { myString = "Im a string", myInt = 123 }; PlayerPrefsV2.SetObject("myObject1", myObjectToSave); MyClass1 objLoadedAgain = PlayerPrefsV2.GetObject <MyClass1>("myObject1", defaultValue: null); Assert.AreEqual(myObjectToSave.myInt, objLoadedAgain.myInt); }
public void Map_Null_Tests() { MyClass1 obj1 = null; var obj2 = obj1.MapTo <MyClass2>(); obj2.ShouldBe(null); }
public void TestKeepMissingFieldsViaJsonExtensionData() { // TODO test in WebGL, caused problems there var x1 = new MyClass1() { myString = "s1", myString2 = "s2", myComplexField = new MyClass1() { myString2 = "s11" } }; var x1JsonString = JsonWriter.GetWriter().Write(x1); var x2 = JsonReader.GetReader().Read <MyClass3>(x1JsonString); // myString2 and myComplexChildField are missing x2 as fields/porperties so the count of additionl json fields must be 2: Assert.Equal(2, x2.GetAdditionalJsonFields().Count); Assert.Equal(x1.myString, x2.myString); // The json will still contain the additional fields since they are attached again during serialization: var x2JsonString = JsonWriter.GetWriter().Write(x2); // Now parse it back to a MySubClass1 type: var x3 = JsonReader.GetReader().Read <MyClass1>(x2JsonString); // Ensure that all fields from the original x1 are still there: Assert.Equal(x1.myString, x3.myString); Assert.Equal(x1.myString2, x3.myString2); Assert.Equal(x1.myComplexField.myString2, x3.myComplexField.myString2); }
public void Object_CastSafe() { var testItem = new MyClass1(); var compareItem = new MyClass3(); Assert.IsTrue(testItem.CastSafe <MyClass3>().GetType() == compareItem.GetType()); }
public async Task ExampleUsage2() { var storeDir = EnvironmentV2.instance.GetOrAddTempFolder("KeyValueStoreTests").GetChildDir("ExampleUsage2Dir"); storeDir.DeleteV2(); // Cleanup before tests if the test file exists string myKey1 = "test123"; MyClass1 x1 = new MyClass1() { myString1 = "Abc", myString2 = "Abc2" }; { // Create a fast memory store and combine it with a LiteDB store that is persisted to disk: IKeyValueStore store = new InMemoryKeyValueStore().WithFallbackStore(new FileBasedKeyValueStore(storeDir)); await store.Set(myKey1, x1); MyClass1 x2 = await store.Get <MyClass1>(myKey1, null); Assert.Equal(x1.myString1, x2.myString1); Assert.Equal(x1.myString2, x2.myString2); } { // Create a second store and check that the changes were persisted: IKeyValueStore store2 = new FileBasedKeyValueStore(storeDir); Assert.True(await store2.ContainsKey(myKey1)); MyClass1 x2 = await store2.Get <MyClass1>(myKey1, null); Assert.Equal(x1.myString1, x2.myString1); Assert.Equal(x1.myString2, x2.myString2); Assert.True(await store2.Remove(myKey1)); Assert.False(await store2.ContainsKey(myKey1)); } }
public MainViewModel() { status = new Status(); // The cast is not necessary but added for clarity MyClass1 object1 = new MyClass1((IReadOnlyStatus)status); MyClass3 object3 = new MyClass3((IStatus)status); }
static void Main(string[] args) { int x = MyClass <int> .FactoryMethod(); Console.WriteLine(x); var z = MyClass <double> .FactoryMethod(); Console.WriteLine(z); string y = MyClass <string> .FactoryMethod(); Console.WriteLine(y); Book book = MyClass <Book> .FactoryMethod(); Console.WriteLine(book.ToString()); //тестирование второго варианта var a = MyClass1 <int> .FactoryMethod(); //var b = MyClass1<string>.FactoryMethod();//нескомпилируется var c = MyClass1 <Book> .FactoryMethod(); Console.WriteLine("Тестирование второго варианта решения"); Console.WriteLine(a); Console.WriteLine(c.ToString()); }
public async Task ExampleUsage1() { // First the test produces a state myState1: MyClass1 myState1 = new MyClass1() { myName = "abc", myChildren = new MyClass1[] { new MyClass1() { myName = "def" } } }; // The regression test wants to ensure that this state does never change // Assertions ensure the correctness of the tested component Assert.NotNull(myState1.myName); // In addition the state can be persisted to detect any changes caused e.g. by future refactorings // To create such a persisted snapshot the PersistedRegression class can be used as follows: // The passed myState will be compared to the persisted regression entry stored on disk: await new PersistedRegression().AssertState("PersistedRegressionTests_ExampleUsage1_1", myState1); // If the myState1 ever differs from how it was the first time the test was executed this will fail // This way changes have to be manually approved by the developer }
public async Task ExampleUsage2() { MyClass1 myState1 = new MyClass1() { myName = "abc", myChildren = new MyClass1[] { new MyClass1() { // Provoke a change in the state in each test execution: myName = "" + new Random().NextRandomName() } } }; // Deleting old diff entries is possible like this: //await new PersistedRegression().regressionStore.Remove("RegressionCheckpointTests_2"); // A filter can be used to remove/filter out acceptable diffs from the report: await new PersistedRegression().AssertState("PersistedRegressionTests_ExampleUsage2_1", (diff) => { // Ignore differences in the field 'myChildren.0.myName' (has a random name in it): if (diff.Count() == 1 && diff.SelectToken("myChildren.0.myName") != null) { return(false); } return(true); // For all other diffs confirm that its a non acceptable diff }, myState1); }
static void Main(string[] args) { //里氏转换原则第一条 MyBase m1 = new MyClass1(); //里氏转换原则第二条 MyClass1 m2 = (MyClass1)m1; // MyClass2 m2 = (MyClass2)m2; // is与as的用法 MyClass1 m3; if (m1 is MyClass1) { m3 = (MyClass1)m1; } else { m3 = null; } MyClass1 m4 = m1 as MyClass1; //m3 == m4 Console.WriteLine("OK"); Console.ReadKey(); }
public void Run() { var test1 = 0; if (MyAge.HasValue) { test1 = MyAge.Value; } var test = MyAge.HasValue ? MyAge.Value : 0; var test3 = MyAge ?? 0; MyAge ??= 55; var myClass1 = new MyClass1(); string myclass2test = null; if (myClass1.MyClass2 != null) { myclass2test = myClass1.MyClass2.Test; } string myclass2test2 = myClass1.MyClass2?.Test; }
public void Map_Null_Tests() { MyClass1 obj1 = null; var obj2 = _mapper.Map <MyClass2>(obj1); obj2.ShouldBe(null); }
public void ExampleUsage3_LazyInit() { var IoC_inject = GetInjectorForTest(); { // Setup an injector1 that will answer all requests for class type string: // A string that will lazy initialize when createIfNull=true is called the first time MyClass1 objectThatWillLazyInit = null; var injector1 = new object(); IoC_inject.RegisterInjector <MyClass1>(injector1, (caller, createIfNull) => { // The caller passes itself in IoC.inject.Get(..) so that the injector can // react different for different callers Assert.Equal(this, caller); // If createIfNull was true lazy init the object: if (createIfNull) { objectThatWillLazyInit = new MyClass1(); } return(objectThatWillLazyInit); }); } // Calling IoC.inject.Get(..) with createIfNull false will not cause injector1 to init the string Assert.Null(IoC_inject.Get <MyClass1>(this, createIfNull: false)); // If createIfNull=true is passed the string will be initialized: Assert.NotNull(IoC_inject.Get <MyClass1>(this, createIfNull: true)); // Now the string is initialized, it will not return null anymore even when createIfNull=false is passed Assert.NotNull(IoC_inject.Get <MyClass1>(this, createIfNull: false)); }
public void TestTemporaryContext1() { var IoC_inject = GetInjectorForTest(); Assert.Null(IoC_inject.Get <MyClass1>(this)); for (int i = 0; i < 100; i++) { TaskV2.Run(() => { var myContextInstance1 = new MyClass1(); IoC_inject.DoWithTempContext <MyClass1>(myContextInstance1, () => { Assert.Equal(myContextInstance1, IoC_inject.Get <MyClass1>(this)); }); // when the temporary context is gone requesting an injection returns null again: Assert.Null(IoC_inject.Get <MyClass1>(this)); var myContextInstance2 = new MyClass1(); var testUser = new MyUserClass1(); IoC_inject.DoWithTempContext <MyClass1>(myContextInstance2, () => { IoC_inject.DoWithTempContext <MyUserClass1>(testUser, () => { Assert.Equal(myContextInstance2, IoC_inject.Get <MyClass1>(this)); Assert.Equal(testUser, IoC_inject.Get <MyUserClass1>(this)); }); }); // when the temporary context is gone requesting an injection returns null again: Assert.Null(IoC_inject.Get <MyClass1>(this)); Assert.Null(IoC_inject.Get <MyUserClass1>(this)); }); } }
public void OldUsage2() { /* Its possible to stay closer to the original Unity syntax by using PlayerPrefsV2 where you typically * would write PlayerPrefs but this will couple the code to Unity. Typically Preferences.instance is * the recommended way to interact with persisted preferences. Below are some examples if you want to * stick to PlayerPrefsV2 anyways: */ // PlayerPrefsV2.SetBool and PlayerPrefsV2.GetBool example: bool myBool = true; PlayerPrefsV2.SetBool("myBool", myBool); Assert.AreEqual(myBool, PlayerPrefsV2.GetBool("myBool", defaultValue: false)); // PlayerPrefsV2.SetStringEncrypted and PlayerPrefsV2.GetStringDecrypted example: PlayerPrefsV2.SetStringEncrypted("mySecureString", "some text to encrypt", password: "******"); var decryptedAgain = PlayerPrefsV2.GetStringDecrypted("mySecureString", null, password: "******"); Assert.AreEqual("some text to encrypt", decryptedAgain); // PlayerPrefsV2.SetObject and PlayerPrefsV2.GetObject example (uses JSON internally): MyClass1 myObjectToSave = new MyClass1() { myString = "Im a string", myInt = 123 }; PlayerPrefsV2.SetObject("myObject1", myObjectToSave); MyClass1 objLoadedAgain = PlayerPrefsV2.GetObject <MyClass1>("myObject1", defaultValue: null); Assert.AreEqual(myObjectToSave.myInt, objLoadedAgain.myInt); }
public void Test1() { var myclass = new MyClass1(); var result = myclass.Add(1, 1); Assert.AreEqual(2, result); }
public void TestFromJsonForReadonlyFields() { MyClass1 x = new MyClass1(12, ImmutableList.Create(new string[] { "a", "b", "c" })); { var xAsJson = JsonWriter.GetWriter().Write(x); Assert.NotEmpty(xAsJson); // The fields are immutable so the json logic has to use the constructor to init them from the json: var y = JsonReader.GetReader().Read <MyClass1>(xAsJson); Assert.Equal(x.myNumber, y.myNumber); AssertV2.AreEqualJson(x, y); } // The constructor json logic can also be mixed with normal field json logic: x.myMutableList = new List <string>() { "d", "e" }; { var xAsJson = JsonWriter.GetWriter().Write(x); Assert.NotEmpty(xAsJson); var y = JsonReader.GetReader().Read <MyClass1>(xAsJson); // myMutableList is not required in the constructor but still correctly set by the json logic: Assert.Equal(x.myMutableList, y.myMutableList); AssertV2.AreEqualJson(x, y); } }
public async Task CallsMethodOfTargetObject() { var obj = new MyClass1(); await obj.SendCommandAsync(it => it.M1(5)); Assert.AreEqual(5, obj.X); }
public void ObjectsCanSubscribeToEvents() { var obj = new MyClass1(); EventBus.Send("E1", 5); Thread.Sleep(10); Assert.AreEqual(5, obj.X); }
public static void Main() { MyClass1 mC = new MyClass1(); // Direct access to public members: mC.x = 10; mC.y = 15; Console.WriteLine("x = {0}, y = {1}", mC.x, mC.y); }
public static void Main() { MyClass1 mC = new MyClass1(); //直接访问公开成员 mC.x = 10; mC.y = 15; Console.WriteLine("x = {0}, y = {1}", mC.x, mC.y); Console.ReadLine(); }
public void MapTo_Tests() { var obj1 = new MyClass1 { TestProp = "Test value" }; var obj2 = _mapper.Map<MyClass2>(obj1); obj2.TestProp.ShouldBe("Test value"); var obj3 = _mapper.Map<MyClass3>(obj1); obj3.TestProp.ShouldBe("Test value"); }
public void MapTo_Tests() { var obj1 = new MyClass1 { TestProp = "Test value" }; var obj2 = obj1.MapTo<MyClass2>(); obj2.TestProp.ShouldBe("Test value"); var obj3 = obj1.MapTo<MyClass3>(); obj3.TestProp.ShouldBe("Test value"); }
public void ObjectsCanUnsubscribeFromEvents() { var obj = new MyClass1(); EventBus.Send("E4"); EventBus.Send("NoE4"); Thread.Sleep(10); // give it time for the de-registration to execute EventBus.Send("E4"); Thread.Sleep(10); Assert.AreEqual(1, obj.E4Calls); }
public void MapTo_Existing_Object_Tests() { var obj1 = new MyClass1 { TestProp = "Test value" }; var obj2 = new MyClass2(); obj1.MapTo(obj2); obj2.TestProp.ShouldBe("Test value"); var obj3 = new MyClass3(); obj2.MapTo(obj3); obj3.TestProp.ShouldBe("Test value"); }
public void MethodsAreInvokedAsynchronously() { var obj = new MyClass1(); var sw = new Stopwatch(); sw.Start(); EventBus.Send("E2"); sw.Stop(); Thread.Sleep(10); Assert.IsTrue(sw.ElapsedMilliseconds < 1000); }
public void Should_not_throw_when_servicessource_is_null() { var expectedInstance = new MyClass1(); var expectedInstances = new List<object> { new MyClass1(), new MyClass1() }; Func<Type, object> validSingleSource = t => expectedInstance; Func<Type, IEnumerable<object>> validServicesSource = t => expectedInstances; var serviceLocator = new ExternalServiceLocator(validServicesSource, validSingleSource); Assert.That(serviceLocator.Resolve(typeof(MyClass1)), Is.EqualTo(expectedInstance)); Assert.That(serviceLocator.ResolveAll(typeof(MyClass1)), Is.EqualTo(expectedInstances)); }
/// <summary> /// /// </summary> public static void Main() { // Get the type of MyClass1. Type myType = typeof (MyClass1); // Get the instance of MyClass1. MyClass1 myInstance = new MyClass1(); Console.WriteLine("\nDisplaying the results of using the MyBinder binder.\n"); // Get the method information for MyMethod. MethodInfo myMethod = myType.GetMethod("MyMethod", BindingFlags.Public | BindingFlags.Instance, new MyBinder(), new Type[] {typeof (short), typeof (short)}, null); Console.WriteLine(myMethod); // Invoke MyMethod. myMethod.Invoke(myInstance, BindingFlags.InvokeMethod, new MyBinder(), new Object[] {(int) 32, (int) 32}, CultureInfo.CurrentCulture); }
public static void Main() { object a; a = 1; Console.WriteLine(a); Console.WriteLine(a.GetType()); Console.WriteLine(a.ToString()); Console.WriteLine(); a = new MyClass1 (); MyClass1 ref_MyClass1; ref_MyClass1 = (MyClass1)a; Console.WriteLine(ref_MyClass1.i); Console.WriteLine(a); Console.WriteLine(a.GetType()); Console.WriteLine(a.ToString()); Console.ReadLine(); }
public static void Main() { object[] myObjects = new object[6]; /* test comment 1 */ myObjects[0] = new MyClass1(); myObjects[1] = new MyClass2(); myObjects[2] = "hello"; myObjects[3] = 123; myObjects[4] = 123.4; myObjects[5] = null; for (int i = 0; i < myObjects.Length; ++i) {/* loop it */ string s = myObjects[i] as string; Console.Write("{0}:", i); if (s != null) Console.WriteLine("'" + s + "'"); else Console.WriteLine("not a string"); } }
public void Should_Map_Nullable_Value_To_Null_If_It_Is_Null_On_Source() { var obj1 = new MyClass1(); var obj2 = _mapper.Map<MyClass2>(obj1); obj2.NullableValue.ShouldBeNull(); }
public void Should_match_the_second_case( MyClass1 instance) { Assert.Equal(2, MatchFields(instance)); Assert.Equal(2, MatchProperties(instance)); }
public void TestMyClass1MessagePacket() { int size = MyClass1.Size(); MyClass1 v1 = new MyClass1(); var buffer = ByteBufferAllocator.Instance.Allocate(size); Serializer.Serialize(buffer, v1); Assert.AreEqual(size, buffer.Position); buffer.Rewind(); MyClass1 v2 = Serializer.Deserialize<MyClass1>(buffer); Assert.AreEqual(v1, v2); Assert.AreEqual(size, buffer.Position); }
public async Task ExceptionsThrownByTargetMethodGetRethrownAtCallSite() { var obj = new MyClass1(); await obj.SendCommandAsync(it => it.M2()); }
public MyClass3(MyClass1 obj) { }
public void Map_Should_Set_Null_Existing_Object_Tests() { MyClass1 obj1 = new MyClass1 { TestProp = null }; var obj2 = new MyClass2 { TestProp = "before map" }; obj1.MapTo(obj2); obj2.TestProp.ShouldBe(null); }
public void Should_Map_Nullable_Value_To__Not_Null_If_It_Is__Not_Null_On_Source() { var obj1 = new MyClass1 { NullableValue = 42 }; var obj2 = _mapper.Map<MyClass2>(obj1); obj2.NullableValue.ShouldBe(42); }