blob: 09f725140a856b9e5771bf4c31cf0eedfadc73ec [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 Geyer4115e952023-11-21 23:00:01 +010028#pragma warning disable IDE0079 // net20 - unneeded suppression
Jens Geyer3cac3202022-01-31 18:04:35 +010029#pragma warning disable IDE0017 // init can be simplified - we don't want that here
30
Jens Geyer6e16c2b2020-06-24 23:51:01 +020031namespace Thrift.Tests.DataModel
32{
33 // ReSharper disable once InconsistentNaming
34 [TestClass]
35 public class Thrift_5238
36 {
Jens Geyer3cac3202022-01-31 18:04:35 +010037 private static void CheckInstance(RaceDetails instance)
Jens Geyer6e16c2b2020-06-24 23:51:01 +020038 {
39 // object
40 Assert.IsTrue(instance.__isset.def_nested);
41 Assert.IsTrue(instance.__isset.opt_nested);
42 Assert.IsNull(instance.Def_nested);
43 Assert.IsNull(instance.Opt_nested);
44
45 // string
46 Assert.IsTrue(instance.__isset.def_four);
47 Assert.IsTrue(instance.__isset.opt_four);
Jens Geyer3cac3202022-01-31 18:04:35 +010048 Assert.IsTrue(string.IsNullOrEmpty(instance.Req_four));
Jens Geyer6e16c2b2020-06-24 23:51:01 +020049 Assert.IsNull(instance.Def_four);
50 Assert.IsNull(instance.Opt_four);
51
Jens Geyer6e16c2b2020-06-24 23:51:01 +020052 // list<>
53 Assert.IsTrue(instance.__isset.def_six);
54 Assert.IsTrue(instance.__isset.opt_six);
55 Assert.IsNull(instance.Req_six);
56 Assert.IsNull(instance.Opt_six);
57 Assert.IsNull(instance.Def_six);
Jens Geyer62445c12022-06-29 00:00:00 +020058
59 // byte[]
60 Assert.IsTrue(instance.__isset.def_nine);
61 Assert.IsTrue(instance.__isset.opt_nine);
62 Assert.IsTrue((instance.Req_nine == null) || (instance.Req_nine.Length == 0));
63 Assert.IsNull(instance.Def_nine);
64 Assert.IsNull(instance.Opt_nine);
Jens Geyer6e16c2b2020-06-24 23:51:01 +020065 }
66
67 [TestMethod]
68 public void Thrift_5238_ProperNullChecks()
69 {
70 var instance = new OptReqDefTest.RaceDetails();
71
Jens Geyer3cac3202022-01-31 18:04:35 +010072 // the following code INTENTIONALLY assigns null to non.nullable reftypes
73 #pragma warning disable CS8625
74
Jens Geyer6e16c2b2020-06-24 23:51:01 +020075 // object
76 instance.Def_nested = null;
77 instance.Opt_nested = null;
78
79 // string
80 instance.Req_four = null;
81 instance.Def_four = null;
82 instance.Opt_four = null;
83
Jens Geyer6e16c2b2020-06-24 23:51:01 +020084 // list<>
85 instance.Req_six = null;
86 instance.Opt_six = null;
87 instance.Def_six = null;
88
Jens Geyer62445c12022-06-29 00:00:00 +020089 // byte[]
90 instance.Req_nine = null;
91 instance.Def_nine = null;
92 instance.Opt_nine = null;
93
Jens Geyer3cac3202022-01-31 18:04:35 +010094 // back to normal
95 #pragma warning restore CS8625
96
Jens Geyer6e16c2b2020-06-24 23:51:01 +020097 // test the setup
98 CheckInstance(instance);
99
Jens Geyer62445c12022-06-29 00:00:00 +0200100 // validate proper null checks, any of these throws if not
Jens Geyer6e16c2b2020-06-24 23:51:01 +0200101 instance.ToString();
102 instance.GetHashCode();
103
Jens Geyer62445c12022-06-29 00:00:00 +0200104 // validate proper null checks, any of these throws if not
Jens Geyer6e16c2b2020-06-24 23:51:01 +0200105 var copy = instance.DeepCopy();
106 CheckInstance(copy);
107 }
108
109 }
110}