A game about forced loneliness, made by TACStudios
1using System;
2using System.Text.RegularExpressions;
3
4namespace Unity.VisualScripting
5{
6 public partial class EnsureThat
7 {
8 public void IsNotNullOrWhiteSpace(string value)
9 {
10 if (!Ensure.IsActive)
11 {
12 return;
13 }
14
15 IsNotNull(value);
16
17 if (StringUtility.IsNullOrWhiteSpace(value))
18 {
19 throw new ArgumentException(ExceptionMessages.Strings_IsNotNullOrWhiteSpace_Failed, paramName);
20 }
21 }
22
23 public void IsNotNullOrEmpty(string value)
24 {
25 if (!Ensure.IsActive)
26 {
27 return;
28 }
29
30 IsNotNull(value);
31
32 if (string.IsNullOrEmpty(value))
33 {
34 throw new ArgumentException(ExceptionMessages.Strings_IsNotNullOrEmpty_Failed, paramName);
35 }
36 }
37
38 public void IsNotNull(string value)
39 {
40 if (!Ensure.IsActive)
41 {
42 return;
43 }
44
45 if (value == null)
46 {
47 throw new ArgumentNullException(paramName, ExceptionMessages.Common_IsNotNull_Failed);
48 }
49 }
50
51 public void IsNotEmpty(string value)
52 {
53 if (!Ensure.IsActive)
54 {
55 return;
56 }
57
58 if (string.Empty.Equals(value))
59 {
60 throw new ArgumentException(ExceptionMessages.Strings_IsNotEmpty_Failed, paramName);
61 }
62 }
63
64 public void HasLengthBetween(string value, int minLength, int maxLength)
65 {
66 if (!Ensure.IsActive)
67 {
68 return;
69 }
70
71 IsNotNull(value);
72
73 var length = value.Length;
74
75 if (length < minLength)
76 {
77 throw new ArgumentException(ExceptionMessages.Strings_HasLengthBetween_Failed_ToShort.Inject(minLength, maxLength, length), paramName);
78 }
79
80 if (length > maxLength)
81 {
82 throw new ArgumentException(ExceptionMessages.Strings_HasLengthBetween_Failed_ToLong.Inject(minLength, maxLength, length), paramName);
83 }
84 }
85
86 public void Matches(string value, string match) => Matches(value, new Regex(match));
87
88 public void Matches(string value, Regex match)
89 {
90 if (!Ensure.IsActive)
91 {
92 return;
93 }
94
95 if (!match.IsMatch(value))
96 {
97 throw new ArgumentException(ExceptionMessages.Strings_Matches_Failed.Inject(value, match), paramName);
98 }
99 }
100
101 public void SizeIs(string value, int expected)
102 {
103 if (!Ensure.IsActive)
104 {
105 return;
106 }
107
108 IsNotNull(value);
109
110 if (value.Length != expected)
111 {
112 throw new ArgumentException(ExceptionMessages.Strings_SizeIs_Failed.Inject(expected, value.Length), paramName);
113 }
114 }
115
116 public void IsEqualTo(string value, string expected)
117 {
118 if (!Ensure.IsActive)
119 {
120 return;
121 }
122
123 if (!StringEquals(value, expected))
124 {
125 throw new ArgumentException(ExceptionMessages.Strings_IsEqualTo_Failed.Inject(value, expected), paramName);
126 }
127 }
128
129 public void IsEqualTo(string value, string expected, StringComparison comparison)
130 {
131 if (!Ensure.IsActive)
132 {
133 return;
134 }
135
136 if (!StringEquals(value, expected, comparison))
137 {
138 throw new ArgumentException(ExceptionMessages.Strings_IsEqualTo_Failed.Inject(value, expected), paramName);
139 }
140 }
141
142 public void IsNotEqualTo(string value, string expected)
143 {
144 if (!Ensure.IsActive)
145 {
146 return;
147 }
148
149 if (StringEquals(value, expected))
150 {
151 throw new ArgumentException(ExceptionMessages.Strings_IsNotEqualTo_Failed.Inject(value, expected), paramName);
152 }
153 }
154
155 public void IsNotEqualTo(string value, string expected, StringComparison comparison)
156 {
157 if (!Ensure.IsActive)
158 {
159 return;
160 }
161
162 if (StringEquals(value, expected, comparison))
163 {
164 throw new ArgumentException(ExceptionMessages.Strings_IsNotEqualTo_Failed.Inject(value, expected), paramName);
165 }
166 }
167
168 public void IsGuid(string value)
169 {
170 if (!Ensure.IsActive)
171 {
172 return;
173 }
174
175 if (!StringUtility.IsGuid(value))
176 {
177 throw new ArgumentException(ExceptionMessages.Strings_IsGuid_Failed.Inject(value), paramName);
178 }
179 }
180
181 private bool StringEquals(string x, string y, StringComparison? comparison = null)
182 {
183 return comparison.HasValue
184 ? string.Equals(x, y, comparison.Value)
185 : string.Equals(x, y);
186 }
187 }
188}