Browse Source

string to guid extension method; tests

tags/v0.2.5
Robin Thoni 8 years ago
parent
commit
adf2fec8a5

+ 9
- 0
Luticate2.Utils/Utils/LuCoreUtilsExtensions.cs View File

@@ -8,9 +8,18 @@ namespace Luticate2.Utils.Utils
8 8
     {
9 9
         public static string ToSnakeCase(this string str)
10 10
         {
11
+            if (str == null)
12
+            {
13
+                throw new ArgumentNullException(nameof(str));
14
+            }
11 15
             return string.Concat(str.Select((x, i) => i > 0 && char.IsUpper(x) ? "_" + x.ToString() : x.ToString())).ToLower();
12 16
         }
13 17
 
18
+        public static Guid? ToGuid(this string str)
19
+        {
20
+            return str == null ? (Guid?)null : new Guid(str);
21
+        }
22
+
14 23
         public static bool HasProperty(this Type type, string name)
15 24
         {
16 25
             return type.GetProperty(name) != null;

+ 65
- 0
TestUtils/Utils/LuCoreUtilsExtensionsTest.cs View File

@@ -0,0 +1,65 @@
1
+using System;
2
+using Luticate2.Utils.Utils;
3
+using Xunit;
4
+
5
+namespace TestUtils.Utils
6
+{
7
+    public class LuCoreUtilsExtensionsTest
8
+    {
9
+        [Fact]
10
+        public void TestStringToGuidNull()
11
+        {
12
+            string str = null;
13
+            Assert.Null(str.ToGuid());
14
+        }
15
+
16
+        [Fact]
17
+        public void TestStringToGuidValid()
18
+        {
19
+            var str = "365989fc-cbd6-11e6-99ca-3085a902d67c";
20
+            Assert.Equal(str, str.ToGuid().ToString());
21
+        }
22
+
23
+        [Fact]
24
+        public void TestStringToGuidInvalid()
25
+        {
26
+            var str = "42";
27
+            Assert.Throws<FormatException>(() => str.ToGuid());
28
+        }
29
+
30
+        [Fact]
31
+        public void TestStringToSnakeCaseNull()
32
+        {
33
+            string str = null;
34
+            Assert.Throws<ArgumentNullException>(() => str.ToSnakeCase());
35
+        }
36
+
37
+        [Fact]
38
+        public void TestStringToSnakeCaseEmpty()
39
+        {
40
+            var str = "";
41
+            Assert.Equal("", str.ToSnakeCase());
42
+        }
43
+
44
+        [Fact]
45
+        public void TestStringToSnakeCase1()
46
+        {
47
+            var str = "TestNotEmpty";
48
+            Assert.Equal("test_not_empty", str.ToSnakeCase());
49
+        }
50
+
51
+        [Fact]
52
+        public void TestStringToSnakeCase2()
53
+        {
54
+            var str = "testNotEmpty";
55
+            Assert.Equal("test_not_empty", str.ToSnakeCase());
56
+        }
57
+
58
+        [Fact]
59
+        public void TestStringToSnakeCase3()
60
+        {
61
+            var str = "DBContext";
62
+            Assert.Equal("d_b_context", str.ToSnakeCase());
63
+        }
64
+    }
65
+}

Loading…
Cancel
Save