tst_simple.qml 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /*
  2. * MIT License
  3. *
  4. * Copyright (c) 2019 Alexey Edelev <semlanik@gmail.com>
  5. *
  6. * This file is part of QtProtobuf project https://git.semlanik.org/semlanik/qtprotobuf
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy of this
  9. * software and associated documentation files (the "Software"), to deal in the Software
  10. * without restriction, including without limitation the rights to use, copy, modify,
  11. * merge, publish, distribute, sublicense, and/or sell copies of the Software, and
  12. * to permit persons to whom the Software is furnished to do so, subject to the following
  13. * conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in all copies
  16. * or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  19. * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  20. * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  21. * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  22. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. */
  25. import QtQuick 2.12
  26. import QtTest 1.0
  27. import QtProtobuf 0.5
  28. import qtprotobufnamespace.tests 1.0
  29. import qtprotobufnamespace.tests.nested 1.0
  30. import qtprotobufnamespace.tests.nested.NestedFieldMessage 1.0 as NestedFieldMessage_
  31. TestCase {
  32. name: "Simple values assignment"
  33. SimpleBoolMessage {
  34. id: boolMsg
  35. testFieldBool: false
  36. }
  37. SimpleIntMessage {
  38. id: int32Msg
  39. testFieldInt: 2147483647
  40. }
  41. SimpleSIntMessage {
  42. id: sint32Msg
  43. testFieldInt: 2147483647
  44. }
  45. SimpleUIntMessage {
  46. id: uint32Msg
  47. testFieldInt: 4294967295
  48. }
  49. SimpleFixedInt32Message {
  50. id: fixed32Msg
  51. testFieldFixedInt32: 4294967295
  52. }
  53. SimpleSFixedInt32Message {
  54. id: sfixed32Msg
  55. testFieldFixedInt32: 2147483647
  56. }
  57. SimpleStringMessage {
  58. id: stringMsg
  59. testFieldString: "Test string"
  60. }
  61. MessageUpperCase {
  62. id: caseSenseMsg
  63. }
  64. MessageReserved {
  65. id: reservedMsg
  66. }
  67. PriorMessageUnderscoreField {
  68. id: underScoreMsg
  69. }
  70. LowerCaseMessageName {
  71. id: lowerCaseMsg
  72. }
  73. ComplexMessage {
  74. id: complexMsg
  75. testComplexField: SimpleStringMessage {
  76. id: innerMessage
  77. testFieldString: "inner"
  78. }
  79. }
  80. SimpleStringMessage {
  81. id: outerMessage
  82. testFieldString: "outer"
  83. }
  84. NestedFieldMessage {
  85. id: nestedParent
  86. nested: nestedMsg
  87. }
  88. NestedFieldMessage_.NestedMessage {
  89. id: nestedMsg
  90. testFieldInt: 100
  91. }
  92. function test_1initialization() {
  93. compare(int32Msg.testFieldInt, 2147483647, "SimpleIntMessage initialization")
  94. compare(sint32Msg.testFieldInt, 2147483647, "SimpleSIntMessage initialization")
  95. compare(uint32Msg.testFieldInt, 4294967295, "SimpleUIntMessage initialization")
  96. compare(fixed32Msg.testFieldFixedInt32, 4294967295, "SimpleFixedInt32Message initialization")
  97. compare(sfixed32Msg.testFieldFixedInt32, 2147483647, "SimpleSFixedInt32Message initialization")
  98. }
  99. function test_simpleboolmessage() {
  100. boolMsg.testFieldBool = true;
  101. compare(boolMsg.testFieldBool, true, "SimpleBoolMessage == true")
  102. boolMsg.testFieldBool = false;
  103. compare(boolMsg.testFieldBool, false, "SimpleBoolMessage == false")
  104. }
  105. function test_simpleintmessage() {
  106. int32Msg.testFieldInt = 0;
  107. compare(int32Msg.testFieldInt, 0, "SimpleIntMessage == 0")
  108. int32Msg.testFieldInt = -128;
  109. compare(int32Msg.testFieldInt, -128, "SimpleIntMessage == -128")
  110. int32Msg.testFieldInt = 127;
  111. compare(int32Msg.testFieldInt, 127, "SimpleIntMessage == 127")
  112. int32Msg.testFieldInt = -256;
  113. compare(int32Msg.testFieldInt, -256, "SimpleIntMessage == -256")
  114. int32Msg.testFieldInt = 255;
  115. compare(int32Msg.testFieldInt, 255, "SimpleIntMessage == 255")
  116. int32Msg.testFieldInt = -32768;
  117. compare(int32Msg.testFieldInt, -32768, "SimpleIntMessage == -32768")
  118. int32Msg.testFieldInt = 32767;
  119. compare(int32Msg.testFieldInt, 32767, "SimpleIntMessage == 32767")
  120. int32Msg.testFieldInt = -65536;
  121. compare(int32Msg.testFieldInt, -65536, "SimpleIntMessage == -65536")
  122. int32Msg.testFieldInt = 65535;
  123. compare(int32Msg.testFieldInt, 65535, "SimpleIntMessage == 65535")
  124. int32Msg.testFieldInt = -2147483648;
  125. compare(int32Msg.testFieldInt, -2147483648, "SimpleIntMessage == -2147483648")
  126. int32Msg.testFieldInt = 2147483647;
  127. compare(int32Msg.testFieldInt, 2147483647, "SimpleIntMessage == 2147483647")
  128. }
  129. function test_simplesintmessage() {
  130. sint32Msg.testFieldInt = 0;
  131. compare(sint32Msg.testFieldInt, 0, "SimpleSIntMessage == 0")
  132. sint32Msg.testFieldInt = -128;
  133. compare(sint32Msg.testFieldInt, -128, "SimpleSIntMessage == -128")
  134. sint32Msg.testFieldInt = 127;
  135. compare(sint32Msg.testFieldInt, 127, "SimpleSIntMessage == 127")
  136. sint32Msg.testFieldInt = -256;
  137. compare(sint32Msg.testFieldInt, -256, "SimpleSIntMessage == -256")
  138. sint32Msg.testFieldInt = 255;
  139. compare(sint32Msg.testFieldInt, 255, "SimpleSIntMessage == 255")
  140. sint32Msg.testFieldInt = -32768;
  141. compare(sint32Msg.testFieldInt, -32768, "SimpleSIntMessage == -32768")
  142. sint32Msg.testFieldInt = 32767;
  143. compare(sint32Msg.testFieldInt, 32767, "SimpleSIntMessage == 32767")
  144. sint32Msg.testFieldInt = -65536;
  145. compare(sint32Msg.testFieldInt, -65536, "SimpleSIntMessage == -65536")
  146. sint32Msg.testFieldInt = 65535;
  147. compare(sint32Msg.testFieldInt, 65535, "SimpleSIntMessage == 65535")
  148. sint32Msg.testFieldInt = -2147483648;
  149. compare(sint32Msg.testFieldInt, -2147483648, "SimpleSIntMessage == -2147483648")
  150. sint32Msg.testFieldInt = 2147483647;
  151. compare(sint32Msg.testFieldInt, 2147483647, "SimpleSIntMessage == 2147483647")
  152. }
  153. function test_simpleuintmessage() {
  154. uint32Msg.testFieldInt = 0;
  155. compare(uint32Msg.testFieldInt, 0, "SimpleUIntMessage == 0")
  156. uint32Msg.testFieldInt = 127;
  157. compare(uint32Msg.testFieldInt, 127, "SimpleUIntMessage == 127")
  158. uint32Msg.testFieldInt = 255;
  159. compare(uint32Msg.testFieldInt, 255, "SimpleUIntMessage == 255")
  160. uint32Msg.testFieldInt = 32767;
  161. compare(uint32Msg.testFieldInt, 32767, "SimpleUIntMessage == 32767")
  162. uint32Msg.testFieldInt = 65535;
  163. compare(uint32Msg.testFieldInt, 65535, "SimpleUIntMessage == 65535")
  164. uint32Msg.testFieldInt = 2147483647;
  165. compare(uint32Msg.testFieldInt, 2147483647, "SimpleUIntMessage == 2147483647")
  166. uint32Msg.testFieldInt = 4294967295;
  167. compare(uint32Msg.testFieldInt, 4294967295, "SimpleUIntMessage == 4294967295")
  168. }
  169. function test_simplefixed32message() {
  170. fixed32Msg.testFieldFixedInt32 = 0;
  171. compare(fixed32Msg.testFieldFixedInt32, 0, "SimpleFixedInt32Message == 0")
  172. fixed32Msg.testFieldFixedInt32 = 127;
  173. compare(fixed32Msg.testFieldFixedInt32, 127, "SimpleFixedInt32Message == 127")
  174. fixed32Msg.testFieldFixedInt32 = 255;
  175. compare(fixed32Msg.testFieldFixedInt32, 255, "SimpleFixedInt32Message == 255")
  176. fixed32Msg.testFieldFixedInt32 = 32767;
  177. compare(fixed32Msg.testFieldFixedInt32, 32767, "SimpleFixedInt32Message == 32767")
  178. fixed32Msg.testFieldFixedInt32 = 65535;
  179. compare(fixed32Msg.testFieldFixedInt32, 65535, "SimpleFixedInt32Message == 65535")
  180. fixed32Msg.testFieldFixedInt32 = 2147483647;
  181. compare(fixed32Msg.testFieldFixedInt32, 2147483647, "SimpleFixedInt32Message == 2147483647")
  182. fixed32Msg.testFieldFixedInt32 = 4294967295;
  183. compare(fixed32Msg.testFieldFixedInt32, 4294967295, "SimpleFixedInt32Message == 4294967295")
  184. }
  185. function test_simplesfixed32message() {
  186. sfixed32Msg.testFieldFixedInt32 = 0;
  187. compare(sfixed32Msg.testFieldFixedInt32, 0, "SimpleSFixedInt32Message == 0")
  188. sfixed32Msg.testFieldFixedInt32 = -128;
  189. compare(sfixed32Msg.testFieldFixedInt32, -128, "SimpleSFixedInt32Message == -128")
  190. sfixed32Msg.testFieldFixedInt32 = 127;
  191. compare(sfixed32Msg.testFieldFixedInt32, 127, "SimpleSFixedInt32Message == 127")
  192. sfixed32Msg.testFieldFixedInt32 = -256;
  193. compare(sfixed32Msg.testFieldFixedInt32, -256, "SimpleSFixedInt32Message == -256")
  194. sfixed32Msg.testFieldFixedInt32 = 255;
  195. compare(sfixed32Msg.testFieldFixedInt32, 255, "SimpleSFixedInt32Message == 255")
  196. sfixed32Msg.testFieldFixedInt32 = -32768;
  197. compare(sfixed32Msg.testFieldFixedInt32, -32768, "SimpleSFixedInt32Message == -32768")
  198. sfixed32Msg.testFieldFixedInt32 = 32767;
  199. compare(sfixed32Msg.testFieldFixedInt32, 32767, "SimpleSFixedInt32Message == 32767")
  200. sfixed32Msg.testFieldFixedInt32 = -65536;
  201. compare(sfixed32Msg.testFieldFixedInt32, -65536, "SimpleSFixedInt32Message == -65536")
  202. sfixed32Msg.testFieldFixedInt32 = 65535;
  203. compare(sfixed32Msg.testFieldFixedInt32, 65535, "SimpleSFixedInt32Message == 65535")
  204. sfixed32Msg.testFieldFixedInt32 = -2147483648;
  205. compare(sfixed32Msg.testFieldFixedInt32, -2147483648, "SimpleSFixedInt32Message == -2147483648")
  206. sfixed32Msg.testFieldFixedInt32 = 2147483647;
  207. compare(sfixed32Msg.testFieldFixedInt32, 2147483647, "SimpleSFixedInt32Message == 2147483647")
  208. }
  209. function test_simplesstringmessage() {
  210. compare(stringMsg.testFieldString, "Test string", "SimpleStringMessage")
  211. }
  212. function test_reservedNames() {
  213. reservedMsg.id_proto = 34;
  214. reservedMsg.import_proto = 35;
  215. reservedMsg.property_proto = 36;
  216. compare(reservedMsg.id_proto, 34, "reservedMsg.id_proto == 34")
  217. compare(reservedMsg.import_proto, 35, "reservedMsg.import_proto == 35")
  218. compare(reservedMsg.property_proto, 36, "reservedMsg.property_proto == 36")
  219. }
  220. function test_reservedEnums() {
  221. compare(MessageEnumReserved.Import, 0, "MessageEnumReserved.Import == 0")
  222. compare(MessageEnumReserved.Property, 1, "MessageEnumReserved.Property == 1")
  223. compare(MessageEnumReserved.Id, 2, "MessageEnumReserved.Id == 2")
  224. }
  225. function test_caseSense() {
  226. caseSenseMsg.testField = 34;
  227. compare(caseSenseMsg.testField, 34, "MessageUpperCase == 34")
  228. compare(MessageEnumReserved.EnumValue0 == 0, true, "MessageEnumReserved.EnumValue0 == 0")
  229. compare(MessageEnumReserved.EnumValue1 == 1, true, "MessageEnumReserved.EnumValue1 == 1")
  230. compare(MessageEnumReserved.EnumValue2 == 2, true, "MessageEnumReserved.EnumValue2 == 2")
  231. }
  232. function test_underScoreField() {
  233. underScoreMsg.underScoreMessageField = 123
  234. compare(underScoreMsg.underScoreMessageField, 123, "underScoreMsg.underScoreMessageField == 123")
  235. }
  236. function test_lowerCaseMessage() {
  237. lowerCaseMsg.testField = 34
  238. compare(lowerCaseMsg.testField, 34, "LowerCaseMessageName == 34")
  239. }
  240. function test_int32ImplicitConversion() {
  241. int32Msg.testFieldInt = 0
  242. compare(int32Msg.testFieldInt ? true : false, false, "Invalid implicit conversion: " + int32Msg.testFieldInt + " should be false")
  243. int32Msg.testFieldInt = 1
  244. compare(int32Msg.testFieldInt ? true : false, true, "Invalid implicit conversion: " + int32Msg.testFieldInt + " should be true")
  245. }
  246. function test_int32LocaleStringConversion() {
  247. compare(int32Msg.testFieldInt.toLocaleString(Qt.locale()), Number(int32Msg.testFieldInt).toLocaleString(Qt.locale()),
  248. "Locale number string is not match " + int32Msg.testFieldInt.toLocaleString(Qt.locale()) + " != " + Number(int32Msg.testFieldInt).toLocaleString(Qt.locale()))
  249. }
  250. function test_fixed32ImplicitConversion() {
  251. fixed32Msg.testFieldFixedInt32 = 0
  252. compare(fixed32Msg.testFieldFixedInt32 ? true : false, false, "Invalid implicit conversion: " + fixed32Msg.testFieldInt + " should be false")
  253. fixed32Msg.testFieldFixedInt32 = 1
  254. compare(fixed32Msg.testFieldFixedInt32 ? true : false, true, "Invalid implicit conversion: " + fixed32Msg.testFieldInt + " should be true")
  255. }
  256. function test_fixed32LocaleStringConversion() {
  257. compare(fixed32Msg.testFieldFixedInt32.toLocaleString(Qt.locale()), Number(fixed32Msg.testFieldFixedInt32).toLocaleString(Qt.locale()),
  258. "Locale number string is not match " + fixed32Msg.testFieldFixedInt32.toLocaleString(Qt.locale()) + " != " + Number(fixed32Msg.testFieldFixedInt32).toLocaleString(Qt.locale()))
  259. }
  260. function test_sint32ImplicitConversion() {
  261. sint32Msg.testFieldInt = 0
  262. compare(sint32Msg.testFieldInt ? true : false, false, "Invalid implicit conversion: " + sint32Msg.testFieldInt + " should be false")
  263. sint32Msg.testFieldInt = 1
  264. compare(sint32Msg.testFieldInt ? true : false, true, "Invalid implicit conversion: " + sint32Msg.testFieldInt + " should be true")
  265. }
  266. function test_sint32LocaleStringConversion() {
  267. compare(sint32Msg.testFieldInt.toLocaleString(Qt.locale()), Number(sint32Msg.testFieldInt).toLocaleString(Qt.locale()),
  268. "Locale number string is not match " + sint32Msg.testFieldInt.toLocaleString(Qt.locale()) + " != " + Number(sint32Msg.testFieldInt).toLocaleString(Qt.locale()))
  269. }
  270. function test_sfixed32ImplicitConversion() {
  271. sfixed32Msg.testFieldFixedInt32 = 0
  272. compare(sfixed32Msg.testFieldFixedInt32 ? true : false, false, "Invalid implicit conversion: " + sfixed32Msg.testFieldInt + " should be false")
  273. sfixed32Msg.testFieldFixedInt32 = 1
  274. compare(sfixed32Msg.testFieldFixedInt32 ? true : false, true, "Invalid implicit conversion: " + sfixed32Msg.testFieldInt + " should be true")
  275. }
  276. function test_sfixed32LocaleStringConversion() {
  277. compare(sfixed32Msg.testFieldFixedInt32.toLocaleString(Qt.locale()), Number(sfixed32Msg.testFieldFixedInt32).toLocaleString(Qt.locale()),
  278. "Locale number string is not match " + sfixed32Msg.testFieldFixedInt32.toLocaleString(Qt.locale()) + " != " + Number(sfixed32Msg.testFieldFixedInt32).toLocaleString(Qt.locale()))
  279. }
  280. function test_complexMessage() {
  281. compare(complexMsg.testComplexField, innerMessage, "Invalid object is inside complex message")
  282. compare(complexMsg.testComplexField.testFieldString, "inner", "Invalid value of object inside complex message")
  283. complexMsg.testComplexField = outerMessage
  284. compare(complexMsg.testComplexField, outerMessage, "Invalid object is inside complex message")
  285. compare(complexMsg.testComplexField.testFieldString, "outer", "Invalid value of object inside complex message")
  286. complexMsg.testComplexField = innerMessage
  287. compare(complexMsg.testComplexField, innerMessage, "Invalid object is inside complex message")
  288. compare(complexMsg.testComplexField.testFieldString, "inner", "Invalid value of object inside complex message")
  289. }
  290. function test_nestedMessage() {
  291. compare(nestedMsg.testFieldInt, 100, "Nested message initialization failed");
  292. compare(nestedParent.nested, nestedMsg, "Nested message assignment failed");
  293. }
  294. }