Fix Tizen.System.SystemSettings unittest (#1519)
[platform/core/csapi/tizenfx.git] / test / Tizen.System.SystemSettings.UnitTest / SystemSettings.UnitTest / test / Assert.cs
1 using System;
2 using System.Collections;
3 using System.Collections.Generic;
4 using System.Runtime.CompilerServices;
5 using Tizen.NUI;
6
7
8 namespace SystemSettingsUnitTest
9 {
10     public class Assert
11     {
12         static public string result;
13         static public string errorMessage;
14
15         public Assert()
16         {
17             result = StrUtils.INIT;
18             errorMessage = null;
19         }
20
21         static internal void SetExceptionMessage(Exception exception)
22         {
23             string errMessage = "Exception: " + exception.Message + Environment.NewLine;
24             errMessage += "  Trace: " + exception.StackTrace + Environment.NewLine;
25             Fail(errMessage);
26         }
27
28         static public void Pass()
29         {
30             // If it was marked Failed before, keep the result fail.
31             if (result != StrUtils.FAIL) result = StrUtils.PASS;
32         }
33
34         static public void Fail(string message)
35         {
36             result = StrUtils.FAIL;
37             // To remember message from first failure
38             if (errorMessage == null) errorMessage = message;
39             LogUtils.Write(LogUtils.INFO, LogUtils.TAG, "  " + message);
40         }
41
42         static public void Init()
43         {
44             result = StrUtils.INIT;
45             errorMessage = null;
46         }
47
48         static public string GetResult()
49         {
50             return result;
51         }
52
53         static public void True(bool condition, string message = "Expects true, but got false.")
54         {
55             if (condition == true) Pass(); else Fail(message);
56         }
57
58         static public void IsTrue(bool condition, string message = "Expects true, but got false.")
59         {
60             if (condition == true) Pass(); else Fail(message);
61         }
62
63         static public void False(bool condition, string message = "Expects false, but got true.")
64         {
65             if (condition == false) Pass(); else Fail(message);
66         }
67
68         static public void IsFalse(bool condition, string message = "Expects false, but got true.")
69         {
70             if (condition == false) Pass(); else Fail(message);
71         }
72
73         static public void AreEqual<T>(T expected, T actual, string message = "Expects given values to be equal, but given values are not.")
74         {
75             if (EqualityComparer<T>.Default.Equals(expected, actual)) Pass(); else Fail(message);
76         }
77
78         static public void AreNotEqual<T>(T expected, T actual, string message = "Expects given values to be unequal, but given values are identical.")
79         {
80             if (!EqualityComparer<T>.Default.Equals(expected, actual)) Pass(); else Fail(message);
81         }
82
83         static public void NotNull(object anObject, string message = "Given object is not null.")
84         {
85             if (anObject != null) Pass(); else Fail(message);
86         }
87
88         static public void IsNull(object anObject, string message = "Given object is not null.")
89         {
90             if (anObject == null) Pass(); else Fail(message);
91         }
92
93         static public void IsNotNull(object anObject, string message = "Given object is null.")
94         {
95             if (anObject != null) Pass(); else Fail(message);
96         }
97
98         static public void IsA<T>(object actual, string message = "Given object is NOT an instance of specified type.")
99         {
100             if (actual is T) Pass(); else Fail(message);
101         }
102
103         static public void IsInstanceOf<T>(object actual, string message = "Given object is NOT an instance of specified type.")
104         {
105             if (actual.GetType() == typeof(T)) Pass(); else Fail(message);
106         }
107
108         static public void Greater<T>(T actual, T expected, string message) where T : IComparable<T>
109         {
110             if (actual.CompareTo(expected) > 0) Pass(); else Fail(message);
111         }
112
113         static public void Smaller<T>(T actual, T expected, string message) where T : IComparable<T>
114         {
115             if (actual.CompareTo(expected) < 0) Pass(); else Fail(message);
116         }
117
118         static public void IsNotEmpty(IEnumerable collection, string message)
119         {
120             foreach (object item in collection)
121             {
122                 Pass();
123                 return;
124             }
125
126             Fail(message);
127         }
128     }
129
130     public class StrUtils
131     {
132         static public string PASS = "PASS";
133         static public string FAIL = "FAIL";
134         static public string INIT = "INIT";
135     }
136
137     public class LogUtils
138     {
139         // write with white font
140         static public string DEBUG = "D";
141
142         // write with blue font
143         static public string INFO = "I";
144
145         // write with red font
146         static public string ERROR = "E";
147
148         static public string TAG = "CS-SYSTEM-SETTINGS";
149
150         static public int ok_cnt = 0;
151         static public int chk_cnt = 0;
152         static public int not_support_cnt = 0;
153
154         static public void Write(string level, string tag, string msg)
155         {
156             foreach (string line in msg.Split('\n'))
157             {
158                 Tizen.Log.Debug(TAG, line);
159             }
160
161             //if (level == INFO)
162             //    Console.ForegroundColor = ConsoleColor.Blue;
163             //else if (level == DEBUG)
164             //    Console.ForegroundColor = ConsoleColor.White;
165             //else if (level == ERROR)
166             //    Console.ForegroundColor = ConsoleColor.Red;
167
168             //Console.WriteLine(tag + " : [" + level + "] | " + msg);
169             //Console.ResetColor();
170         }
171         static public void initWriteResult()
172         {
173             ok_cnt = 0;
174             chk_cnt = 0;
175             not_support_cnt = 0;
176
177         }
178
179         static public void StartTest()
180         {
181             chk_cnt++;
182             int check_progress = (chk_cnt * 100) / 85;
183             if (check_progress < 100)
184             {
185                 Program.testProgress.CurrentValue = check_progress;
186                 string output = "Test progress " + check_progress + "%";
187                 Program.text.Text = output;
188             }
189         }
190         
191         static public void NotSupport()
192         {
193             not_support_cnt++;
194         }
195
196         static public void WriteOK([CallerFilePath] string file = "", [CallerMemberName] string func = "", [CallerLineNumber] int line = 0)
197         {
198             Tizen.Log.Debug(TAG, "ok", file, func, line);
199             ok_cnt++;
200         }
201         static public void WriteResult()
202         {
203             Program.testProgress.CurrentValue = 100;
204             string output = "Result : " + ok_cnt + " Pass / " + not_support_cnt + " Not Support / " + (chk_cnt - (ok_cnt + not_support_cnt)) + " Failed";
205             string output_display = ok_cnt + " Pass / " + not_support_cnt + " NS / " + (chk_cnt - (ok_cnt + not_support_cnt)) + " Failed";
206             Tizen.Log.Debug(TAG, output);
207             Program.text.Text = output_display;
208         }
209
210     }
211 }