Jens Geyer | 6e16c2b | 2020-06-24 23:51:01 +0200 | [diff] [blame] | 1 | // Licensed to the Apache Software Foundation(ASF) under one |
| 2 | // or more contributor license agreements.See the NOTICE file |
| 3 | // distributed with this work for additional information |
| 4 | // regarding copyright ownership.The ASF licenses this file |
| 5 | // to you under the Apache License, Version 2.0 (the |
| 6 | // "License"); you may not use this file except in compliance |
| 7 | // with the License. You may obtain a copy of the License at |
| 8 | // |
| 9 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | // |
| 11 | // Unless required by applicable law or agreed to in writing, |
| 12 | // software distributed under the License is distributed on an |
| 13 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | // KIND, either express or implied. See the License for the |
| 15 | // specific language governing permissions and limitations |
| 16 | // under the License. |
| 17 | |
| 18 | using System; |
| 19 | using System.Collections.Generic; |
| 20 | using System.Diagnostics; |
| 21 | using System.Linq; |
| 22 | using System.Text; |
| 23 | using Microsoft.VisualStudio.TestPlatform.ObjectModel; |
| 24 | using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 25 | using OptReqDefTest; |
| 26 | using Thrift.Collections; |
| 27 | |
Jens Geyer | 3cac320 | 2022-01-31 18:04:35 +0100 | [diff] [blame^] | 28 | #pragma warning disable IDE0017 // init can be simplified - we don't want that here |
| 29 | |
Jens Geyer | 6e16c2b | 2020-06-24 23:51:01 +0200 | [diff] [blame] | 30 | namespace Thrift.Tests.DataModel |
| 31 | { |
| 32 | // ReSharper disable once InconsistentNaming |
| 33 | [TestClass] |
| 34 | public class Thrift_5238 |
| 35 | { |
Jens Geyer | 3cac320 | 2022-01-31 18:04:35 +0100 | [diff] [blame^] | 36 | private static void CheckInstance(RaceDetails instance) |
Jens Geyer | 6e16c2b | 2020-06-24 23:51:01 +0200 | [diff] [blame] | 37 | { |
| 38 | // object |
| 39 | Assert.IsTrue(instance.__isset.def_nested); |
| 40 | Assert.IsTrue(instance.__isset.opt_nested); |
| 41 | Assert.IsNull(instance.Def_nested); |
| 42 | Assert.IsNull(instance.Opt_nested); |
| 43 | |
| 44 | // string |
| 45 | Assert.IsTrue(instance.__isset.def_four); |
| 46 | Assert.IsTrue(instance.__isset.opt_four); |
Jens Geyer | 3cac320 | 2022-01-31 18:04:35 +0100 | [diff] [blame^] | 47 | Assert.IsTrue(string.IsNullOrEmpty(instance.Req_four)); |
Jens Geyer | 6e16c2b | 2020-06-24 23:51:01 +0200 | [diff] [blame] | 48 | Assert.IsNull(instance.Def_four); |
| 49 | Assert.IsNull(instance.Opt_four); |
| 50 | |
| 51 | // byte[] |
| 52 | Assert.IsTrue(instance.__isset.def_five); |
| 53 | Assert.IsTrue(instance.__isset.opt_five); |
Jens Geyer | 3cac320 | 2022-01-31 18:04:35 +0100 | [diff] [blame^] | 54 | Assert.IsTrue((instance.Req_five == null) || (instance.Req_five.Length == 0)); |
Jens Geyer | 6e16c2b | 2020-06-24 23:51:01 +0200 | [diff] [blame] | 55 | Assert.IsNull(instance.Def_five); |
| 56 | Assert.IsNull(instance.Opt_five); |
| 57 | |
| 58 | // list<> |
| 59 | Assert.IsTrue(instance.__isset.def_six); |
| 60 | Assert.IsTrue(instance.__isset.opt_six); |
| 61 | Assert.IsNull(instance.Req_six); |
| 62 | Assert.IsNull(instance.Opt_six); |
| 63 | Assert.IsNull(instance.Def_six); |
| 64 | } |
| 65 | |
| 66 | [TestMethod] |
| 67 | public void Thrift_5238_ProperNullChecks() |
| 68 | { |
| 69 | var instance = new OptReqDefTest.RaceDetails(); |
| 70 | |
Jens Geyer | 3cac320 | 2022-01-31 18:04:35 +0100 | [diff] [blame^] | 71 | // the following code INTENTIONALLY assigns null to non.nullable reftypes |
| 72 | #pragma warning disable CS8625 |
| 73 | |
Jens Geyer | 6e16c2b | 2020-06-24 23:51:01 +0200 | [diff] [blame] | 74 | // object |
| 75 | instance.Def_nested = null; |
| 76 | instance.Opt_nested = null; |
| 77 | |
| 78 | // string |
| 79 | instance.Req_four = null; |
| 80 | instance.Def_four = null; |
| 81 | instance.Opt_four = null; |
| 82 | |
| 83 | // byte[] |
| 84 | instance.Req_five = null; |
| 85 | instance.Def_five = null; |
| 86 | instance.Opt_five = null; |
| 87 | |
| 88 | // list<> |
| 89 | instance.Req_six = null; |
| 90 | instance.Opt_six = null; |
| 91 | instance.Def_six = null; |
| 92 | |
Jens Geyer | 3cac320 | 2022-01-31 18:04:35 +0100 | [diff] [blame^] | 93 | // back to normal |
| 94 | #pragma warning restore CS8625 |
| 95 | |
Jens Geyer | 6e16c2b | 2020-06-24 23:51:01 +0200 | [diff] [blame] | 96 | // test the setup |
| 97 | CheckInstance(instance); |
| 98 | |
| 99 | // validate proper null checks , any of these throws if not |
| 100 | instance.ToString(); |
| 101 | instance.GetHashCode(); |
| 102 | |
| 103 | // validate proper null checks , any of these throws if not |
| 104 | var copy = instance.DeepCopy(); |
| 105 | CheckInstance(copy); |
| 106 | } |
| 107 | |
| 108 | } |
| 109 | } |