blob: ebc1717476b24d249fa9ba0c07cd09f3f1dd324e [file] [log] [blame]
Jens Geyer6e16c2b2020-06-24 23:51:01 +02001// 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
18using System;
19using System.Collections.Generic;
20using System.Diagnostics;
21using System.Linq;
22using System.Text;
23using Microsoft.VisualStudio.TestPlatform.ObjectModel;
24using Microsoft.VisualStudio.TestTools.UnitTesting;
25using OptReqDefTest;
26using Thrift.Collections;
27
Jens Geyer3cac3202022-01-31 18:04:35 +010028#pragma warning disable IDE0017 // init can be simplified - we don't want that here
29
Jens Geyer6e16c2b2020-06-24 23:51:01 +020030namespace Thrift.Tests.DataModel
31{
32 // ReSharper disable once InconsistentNaming
33 [TestClass]
34 public class Thrift_5238
35 {
Jens Geyer3cac3202022-01-31 18:04:35 +010036 private static void CheckInstance(RaceDetails instance)
Jens Geyer6e16c2b2020-06-24 23:51:01 +020037 {
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 Geyer3cac3202022-01-31 18:04:35 +010047 Assert.IsTrue(string.IsNullOrEmpty(instance.Req_four));
Jens Geyer6e16c2b2020-06-24 23:51:01 +020048 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 Geyer3cac3202022-01-31 18:04:35 +010054 Assert.IsTrue((instance.Req_five == null) || (instance.Req_five.Length == 0));
Jens Geyer6e16c2b2020-06-24 23:51:01 +020055 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 Geyer3cac3202022-01-31 18:04:35 +010071 // the following code INTENTIONALLY assigns null to non.nullable reftypes
72 #pragma warning disable CS8625
73
Jens Geyer6e16c2b2020-06-24 23:51:01 +020074 // 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 Geyer3cac3202022-01-31 18:04:35 +010093 // back to normal
94 #pragma warning restore CS8625
95
Jens Geyer6e16c2b2020-06-24 23:51:01 +020096 // 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}