You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LuFilterBinderTest.cs 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. using System.Collections.Generic;
  2. using Luticate2.Utils.Dbo.Filter;
  3. using Luticate2.Utils.Dbo.Result;
  4. using Xunit;
  5. namespace TestUtils.Binders
  6. {
  7. public class LuFilterBinderTest
  8. {
  9. [Fact]
  10. public void Test1()
  11. {
  12. var res = LuFilterBinder.FromString("");
  13. Assert.NotNull(res);
  14. Assert.NotNull(res.Data);
  15. Assert.Equal(LuStatus.Success, res.Status);
  16. Assert.Equal("", res.Data.Query);
  17. Assert.Equal(new Dictionary<string, string>(), res.Data.Filters);
  18. }
  19. [Fact]
  20. public void Test2()
  21. {
  22. var res = LuFilterBinder.FromString("test");
  23. Assert.NotNull(res);
  24. Assert.NotNull(res.Data);
  25. Assert.Equal(LuStatus.Success, res.Status);
  26. Assert.Equal("test", res.Data.Query);
  27. Assert.Equal(new Dictionary<string, string>(), res.Data.Filters);
  28. }
  29. [Fact]
  30. public void Test3()
  31. {
  32. var res = LuFilterBinder.FromString(" Test.42 24 ");
  33. Assert.NotNull(res);
  34. Assert.NotNull(res.Data);
  35. Assert.Equal(LuStatus.Success, res.Status);
  36. Assert.Equal("Test.42 24", res.Data.Query);
  37. Assert.Equal(new Dictionary<string, string>(), res.Data.Filters);
  38. }
  39. [Fact]
  40. public void Test4()
  41. {
  42. var res = LuFilterBinder.FromString(" test: 42");
  43. Assert.NotNull(res);
  44. Assert.NotNull(res.Data);
  45. Assert.Equal(LuStatus.Success, res.Status);
  46. Assert.Equal("test: 42", res.Data.Query);
  47. Assert.Equal(new Dictionary<string, string>(), res.Data.Filters);
  48. }
  49. [Fact]
  50. public void Test5()
  51. {
  52. var res = LuFilterBinder.FromString("test: 42");
  53. Assert.NotNull(res);
  54. Assert.NotNull(res.Data);
  55. Assert.Equal(LuStatus.Success, res.Status);
  56. Assert.Equal("", res.Data.Query);
  57. Assert.Equal(new Dictionary<string, string>{{"test", "42"}}, res.Data.Filters);
  58. }
  59. [Fact]
  60. public void Test6()
  61. {
  62. var res = LuFilterBinder.FromString("test: 42 a filter value");
  63. Assert.NotNull(res);
  64. Assert.NotNull(res.Data);
  65. Assert.Equal(LuStatus.Success, res.Status);
  66. Assert.Equal("a filter value", res.Data.Query);
  67. Assert.Equal(new Dictionary<string, string>{{"test", "42"}}, res.Data.Filters);
  68. }
  69. [Fact]
  70. public void Test7()
  71. {
  72. var res = LuFilterBinder.FromString("test: \"42 a filter value\"");
  73. Assert.NotNull(res);
  74. Assert.NotNull(res.Data);
  75. Assert.Equal(LuStatus.Success, res.Status);
  76. Assert.Equal("", res.Data.Query);
  77. Assert.Equal(new Dictionary<string, string>{{"test", "42 a filter value"}}, res.Data.Filters);
  78. }
  79. [Fact]
  80. public void Test8()
  81. {
  82. var res = LuFilterBinder.FromString("test: \'42 a filter value\'");
  83. Assert.NotNull(res);
  84. Assert.NotNull(res.Data);
  85. Assert.Equal(LuStatus.Success, res.Status);
  86. Assert.Equal("", res.Data.Query);
  87. Assert.Equal(new Dictionary<string, string>{{"test", "42 a filter value"}}, res.Data.Filters);
  88. }
  89. [Fact]
  90. public void Test9()
  91. {
  92. var res = LuFilterBinder.FromString("test: \'42 a filter value\' test-2: ' 42 ' test-3: ' 4 ' test-3: ' ' some text");
  93. Assert.NotNull(res);
  94. Assert.NotNull(res.Data);
  95. Assert.Equal(LuStatus.Success, res.Status);
  96. Assert.Equal("some text", res.Data.Query);
  97. Assert.Equal(new Dictionary<string, string>
  98. {
  99. {"test", "42 a filter value"},
  100. {"test-2", "42"},
  101. {"test-3", ""}
  102. }, res.Data.Filters);
  103. }
  104. [Fact]
  105. public void Test10()
  106. {
  107. var res = LuFilterBinder.FromString("test: \'42 a filter value\' test-2 : \'another value\' some text");
  108. Assert.NotNull(res);
  109. Assert.NotNull(res.Data);
  110. Assert.Equal(LuStatus.Success, res.Status);
  111. Assert.Equal("some text", res.Data.Query);
  112. Assert.Equal(new Dictionary<string, string>
  113. {
  114. {"test", "42 a filter value"},
  115. {"test-2", "another value"}
  116. }, res.Data.Filters);
  117. }
  118. [Fact]
  119. public void TestBool()
  120. {
  121. var res = LuFilterBinder.FromString("test: true test-2 : \'false\' fail: 42 some text");
  122. Assert.NotNull(res);
  123. Assert.NotNull(res.Data);
  124. Assert.Equal(LuStatus.Success, res.Status);
  125. Assert.Equal("some text", res.Data.Query);
  126. Assert.Equal(new Dictionary<string, string>
  127. {
  128. {"test", "true"},
  129. {"test-2", "false"},
  130. {"fail", "42"}
  131. }, res.Data.Filters);
  132. Assert.True(res.Data.GetFilterBool("test", false));
  133. Assert.True(res.Data.GetFilterBool("test", true));
  134. Assert.False(res.Data.GetFilterBool("test-2", false));
  135. Assert.False(res.Data.GetFilterBool("test-2", true));
  136. Assert.False(res.Data.GetFilterBool("fail", false));
  137. Assert.True(res.Data.GetFilterBool("fail", true));
  138. Assert.False(res.Data.GetFilterBool("nx", false));
  139. Assert.True(res.Data.GetFilterBool("nx", true));
  140. }
  141. [Fact]
  142. public void TestString()
  143. {
  144. var res = LuFilterBinder.FromString("test: 42 test-2 : \'24\' fail: test some text");
  145. Assert.NotNull(res);
  146. Assert.NotNull(res.Data);
  147. Assert.Equal(LuStatus.Success, res.Status);
  148. Assert.Equal("some text", res.Data.Query);
  149. Assert.Equal(new Dictionary<string, string>
  150. {
  151. {"test", "42"},
  152. {"test-2", "24"},
  153. {"fail", "test"}
  154. }, res.Data.Filters);
  155. Assert.Equal("42", res.Data.GetFilterString("test", ""));
  156. Assert.Equal("24", res.Data.GetFilterString("test-2", ""));
  157. Assert.Equal("test", res.Data.GetFilterString("fail", "azerty"));
  158. Assert.Equal("qwerty", res.Data.GetFilterString("nx", "qwerty"));
  159. }
  160. [Fact]
  161. public void TestInt()
  162. {
  163. var res = LuFilterBinder.FromString("test: 42 test-2 : \'24\' fail: test some text");
  164. Assert.NotNull(res);
  165. Assert.NotNull(res.Data);
  166. Assert.Equal(LuStatus.Success, res.Status);
  167. Assert.Equal("some text", res.Data.Query);
  168. Assert.Equal(new Dictionary<string, string>
  169. {
  170. {"test", "42"},
  171. {"test-2", "24"},
  172. {"fail", "test"}
  173. }, res.Data.Filters);
  174. Assert.Equal(42, res.Data.GetFilterInt("test", 0));
  175. Assert.Equal(24, res.Data.GetFilterInt("test-2", 0));
  176. Assert.Equal(2424, res.Data.GetFilterInt("fail", 2424));
  177. Assert.Equal(4242, res.Data.GetFilterInt("nx", 4242));
  178. }
  179. [Fact]
  180. public void TestFloat()
  181. {
  182. var res = LuFilterBinder.FromString("test: 42.24 test-2 : \'24.42\' fail:test some text");
  183. Assert.NotNull(res);
  184. Assert.NotNull(res.Data);
  185. Assert.Equal(LuStatus.Success, res.Status);
  186. Assert.Equal("some text", res.Data.Query);
  187. Assert.Equal(new Dictionary<string, string>
  188. {
  189. {"test", "42.24"},
  190. {"test-2", "24.42"},
  191. {"fail", "test"}
  192. }, res.Data.Filters);
  193. Assert.Equal(42.24, res.Data.GetFilterFloat("test", 0));
  194. Assert.Equal(24.42, res.Data.GetFilterFloat("test-2", 0));
  195. Assert.Equal(2424.4242, res.Data.GetFilterFloat("fail", 2424.4242));
  196. Assert.Equal(4242.2424, res.Data.GetFilterFloat("nx", 4242.2424));
  197. }
  198. }
  199. }