blob: 80eacc258964bdb7e38bf84534ddc5a70322a7d2 [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
Jens Geyer6e16c2b2020-06-24 23:51:01 +020051 // list<>
52 Assert.IsTrue(instance.__isset.def_six);
53 Assert.IsTrue(instance.__isset.opt_six);
54 Assert.IsNull(instance.Req_six);
55 Assert.IsNull(instance.Opt_six);
56 Assert.IsNull(instance.Def_six);
Jens Geyer62445c12022-06-29 00:00:00 +020057
58 // byte[]
59 Assert.IsTrue(instance.__isset.def_nine);
60 Assert.IsTrue(instance.__isset.opt_nine);
61 Assert.IsTrue((instance.Req_nine == null) || (instance.Req_nine.Length == 0));
62 Assert.IsNull(instance.Def_nine);
63 Assert.IsNull(instance.Opt_nine);
Jens Geyer6e16c2b2020-06-24 23:51:01 +020064 }
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
Jens Geyer6e16c2b2020-06-24 23:51:01 +020083 // list<>
84 instance.Req_six = null;
85 instance.Opt_six = null;
86 instance.Def_six = null;
87
Jens Geyer62445c12022-06-29 00:00:00 +020088 // byte[]
89 instance.Req_nine = null;
90 instance.Def_nine = null;
91 instance.Opt_nine = 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
Jens Geyer62445c12022-06-29 00:00:00 +020099 // validate proper null checks, any of these throws if not
Jens Geyer6e16c2b2020-06-24 23:51:01 +0200100 instance.ToString();
101 instance.GetHashCode();
102
Jens Geyer62445c12022-06-29 00:00:00 +0200103 // validate proper null checks, any of these throws if not
Jens Geyer6e16c2b2020-06-24 23:51:01 +0200104 var copy = instance.DeepCopy();
105 CheckInstance(copy);
106 }
107
108 }
109}