tst_simple.qml 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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.6
  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. SimpleFileEnumMessage {
  62. id: enumListMessage
  63. globalEnumList: [TestEnum.TEST_ENUM_VALUE0, TestEnum.TEST_ENUM_VALUE1]
  64. }
  65. MessageUpperCase {
  66. id: caseSenseMsg
  67. }
  68. MessageReserved {
  69. id: reservedMsg
  70. }
  71. PriorMessageUnderscoreField {
  72. id: underScoreMsg
  73. }
  74. LowerCaseMessageName {
  75. id: lowerCaseMsg
  76. }
  77. ComplexMessage {
  78. id: complexMsg
  79. testComplexField: SimpleStringMessage {
  80. id: innerMessage
  81. testFieldString: "inner"
  82. }
  83. }
  84. SimpleStringMessage {
  85. id: outerMessage
  86. testFieldString: "outer"
  87. }
  88. NestedFieldMessage {
  89. id: nestedParent
  90. nested: nestedMsg
  91. }
  92. NestedFieldMessage_.NestedMessage {
  93. id: nestedMsg
  94. testFieldInt: 100
  95. }
  96. NoPackageMessage {
  97. id: noPackageMessageUser
  98. testField.testFieldInt: 42
  99. }
  100. function test_1initialization() {
  101. compare(int32Msg.testFieldInt, 2147483647, "SimpleIntMessage initialization")
  102. compare(sint32Msg.testFieldInt, 2147483647, "SimpleSIntMessage initialization")
  103. compare(uint32Msg.testFieldInt, 4294967295, "SimpleUIntMessage initialization")
  104. compare(fixed32Msg.testFieldFixedInt32, 4294967295, "SimpleFixedInt32Message initialization")
  105. compare(sfixed32Msg.testFieldFixedInt32, 2147483647, "SimpleSFixedInt32Message initialization")
  106. }
  107. function test_simpleboolmessage() {
  108. boolMsg.testFieldBool = true;
  109. compare(boolMsg.testFieldBool, true, "SimpleBoolMessage == true")
  110. boolMsg.testFieldBool = false;
  111. compare(boolMsg.testFieldBool, false, "SimpleBoolMessage == false")
  112. }
  113. function test_simpleintmessage() {
  114. int32Msg.testFieldInt = 0;
  115. compare(int32Msg.testFieldInt, 0, "SimpleIntMessage == 0")
  116. int32Msg.testFieldInt = -128;
  117. compare(int32Msg.testFieldInt, -128, "SimpleIntMessage == -128")
  118. int32Msg.testFieldInt = 127;
  119. compare(int32Msg.testFieldInt, 127, "SimpleIntMessage == 127")
  120. int32Msg.testFieldInt = -256;
  121. compare(int32Msg.testFieldInt, -256, "SimpleIntMessage == -256")
  122. int32Msg.testFieldInt = 255;
  123. compare(int32Msg.testFieldInt, 255, "SimpleIntMessage == 255")
  124. int32Msg.testFieldInt = -32768;
  125. compare(int32Msg.testFieldInt, -32768, "SimpleIntMessage == -32768")
  126. int32Msg.testFieldInt = 32767;
  127. compare(int32Msg.testFieldInt, 32767, "SimpleIntMessage == 32767")
  128. int32Msg.testFieldInt = -65536;
  129. compare(int32Msg.testFieldInt, -65536, "SimpleIntMessage == -65536")
  130. int32Msg.testFieldInt = 65535;
  131. compare(int32Msg.testFieldInt, 65535, "SimpleIntMessage == 65535")
  132. int32Msg.testFieldInt = -2147483648;
  133. compare(int32Msg.testFieldInt, -2147483648, "SimpleIntMessage == -2147483648")
  134. int32Msg.testFieldInt = 2147483647;
  135. compare(int32Msg.testFieldInt, 2147483647, "SimpleIntMessage == 2147483647")
  136. }
  137. function test_simplesintmessage() {
  138. sint32Msg.testFieldInt = 0;
  139. compare(sint32Msg.testFieldInt, 0, "SimpleSIntMessage == 0")
  140. sint32Msg.testFieldInt = -128;
  141. compare(sint32Msg.testFieldInt, -128, "SimpleSIntMessage == -128")
  142. sint32Msg.testFieldInt = 127;
  143. compare(sint32Msg.testFieldInt, 127, "SimpleSIntMessage == 127")
  144. sint32Msg.testFieldInt = -256;
  145. compare(sint32Msg.testFieldInt, -256, "SimpleSIntMessage == -256")
  146. sint32Msg.testFieldInt = 255;
  147. compare(sint32Msg.testFieldInt, 255, "SimpleSIntMessage == 255")
  148. sint32Msg.testFieldInt = -32768;
  149. compare(sint32Msg.testFieldInt, -32768, "SimpleSIntMessage == -32768")
  150. sint32Msg.testFieldInt = 32767;
  151. compare(sint32Msg.testFieldInt, 32767, "SimpleSIntMessage == 32767")
  152. sint32Msg.testFieldInt = -65536;
  153. compare(sint32Msg.testFieldInt, -65536, "SimpleSIntMessage == -65536")
  154. sint32Msg.testFieldInt = 65535;
  155. compare(sint32Msg.testFieldInt, 65535, "SimpleSIntMessage == 65535")
  156. sint32Msg.testFieldInt = -2147483648;
  157. compare(sint32Msg.testFieldInt, -2147483648, "SimpleSIntMessage == -2147483648")
  158. sint32Msg.testFieldInt = 2147483647;
  159. compare(sint32Msg.testFieldInt, 2147483647, "SimpleSIntMessage == 2147483647")
  160. }
  161. function test_simpleuintmessage() {
  162. uint32Msg.testFieldInt = 0;
  163. compare(uint32Msg.testFieldInt, 0, "SimpleUIntMessage == 0")
  164. uint32Msg.testFieldInt = 127;
  165. compare(uint32Msg.testFieldInt, 127, "SimpleUIntMessage == 127")
  166. uint32Msg.testFieldInt = 255;
  167. compare(uint32Msg.testFieldInt, 255, "SimpleUIntMessage == 255")
  168. uint32Msg.testFieldInt = 32767;
  169. compare(uint32Msg.testFieldInt, 32767, "SimpleUIntMessage == 32767")
  170. uint32Msg.testFieldInt = 65535;
  171. compare(uint32Msg.testFieldInt, 65535, "SimpleUIntMessage == 65535")
  172. uint32Msg.testFieldInt = 2147483647;
  173. compare(uint32Msg.testFieldInt, 2147483647, "SimpleUIntMessage == 2147483647")
  174. uint32Msg.testFieldInt = 4294967295;
  175. compare(uint32Msg.testFieldInt, 4294967295, "SimpleUIntMessage == 4294967295")
  176. }
  177. function test_simplefixed32message() {
  178. fixed32Msg.testFieldFixedInt32 = 0;
  179. compare(fixed32Msg.testFieldFixedInt32, 0, "SimpleFixedInt32Message == 0")
  180. fixed32Msg.testFieldFixedInt32 = 127;
  181. compare(fixed32Msg.testFieldFixedInt32, 127, "SimpleFixedInt32Message == 127")
  182. fixed32Msg.testFieldFixedInt32 = 255;
  183. compare(fixed32Msg.testFieldFixedInt32, 255, "SimpleFixedInt32Message == 255")
  184. fixed32Msg.testFieldFixedInt32 = 32767;
  185. compare(fixed32Msg.testFieldFixedInt32, 32767, "SimpleFixedInt32Message == 32767")
  186. fixed32Msg.testFieldFixedInt32 = 65535;
  187. compare(fixed32Msg.testFieldFixedInt32, 65535, "SimpleFixedInt32Message == 65535")
  188. fixed32Msg.testFieldFixedInt32 = 2147483647;
  189. compare(fixed32Msg.testFieldFixedInt32, 2147483647, "SimpleFixedInt32Message == 2147483647")
  190. fixed32Msg.testFieldFixedInt32 = 4294967295;
  191. compare(fixed32Msg.testFieldFixedInt32, 4294967295, "SimpleFixedInt32Message == 4294967295")
  192. }
  193. function test_simplesfixed32message() {
  194. sfixed32Msg.testFieldFixedInt32 = 0;
  195. compare(sfixed32Msg.testFieldFixedInt32, 0, "SimpleSFixedInt32Message == 0")
  196. sfixed32Msg.testFieldFixedInt32 = -128;
  197. compare(sfixed32Msg.testFieldFixedInt32, -128, "SimpleSFixedInt32Message == -128")
  198. sfixed32Msg.testFieldFixedInt32 = 127;
  199. compare(sfixed32Msg.testFieldFixedInt32, 127, "SimpleSFixedInt32Message == 127")
  200. sfixed32Msg.testFieldFixedInt32 = -256;
  201. compare(sfixed32Msg.testFieldFixedInt32, -256, "SimpleSFixedInt32Message == -256")
  202. sfixed32Msg.testFieldFixedInt32 = 255;
  203. compare(sfixed32Msg.testFieldFixedInt32, 255, "SimpleSFixedInt32Message == 255")
  204. sfixed32Msg.testFieldFixedInt32 = -32768;
  205. compare(sfixed32Msg.testFieldFixedInt32, -32768, "SimpleSFixedInt32Message == -32768")
  206. sfixed32Msg.testFieldFixedInt32 = 32767;
  207. compare(sfixed32Msg.testFieldFixedInt32, 32767, "SimpleSFixedInt32Message == 32767")
  208. sfixed32Msg.testFieldFixedInt32 = -65536;
  209. compare(sfixed32Msg.testFieldFixedInt32, -65536, "SimpleSFixedInt32Message == -65536")
  210. sfixed32Msg.testFieldFixedInt32 = 65535;
  211. compare(sfixed32Msg.testFieldFixedInt32, 65535, "SimpleSFixedInt32Message == 65535")
  212. sfixed32Msg.testFieldFixedInt32 = -2147483648;
  213. compare(sfixed32Msg.testFieldFixedInt32, -2147483648, "SimpleSFixedInt32Message == -2147483648")
  214. sfixed32Msg.testFieldFixedInt32 = 2147483647;
  215. compare(sfixed32Msg.testFieldFixedInt32, 2147483647, "SimpleSFixedInt32Message == 2147483647")
  216. }
  217. function test_simplesstringmessage() {
  218. compare(stringMsg.testFieldString, "Test string", "SimpleStringMessage")
  219. }
  220. function test_reservedNames() {
  221. reservedMsg.id_proto = 34;
  222. reservedMsg.import_proto = 35;
  223. reservedMsg.property_proto = 36;
  224. compare(reservedMsg.id_proto, 34, "reservedMsg.id_proto == 34")
  225. compare(reservedMsg.import_proto, 35, "reservedMsg.import_proto == 35")
  226. compare(reservedMsg.property_proto, 36, "reservedMsg.property_proto == 36")
  227. }
  228. function test_reservedEnums() {
  229. compare(MessageEnumReserved.Import, 0, "MessageEnumReserved.Import == 0")
  230. compare(MessageEnumReserved.Property, 1, "MessageEnumReserved.Property == 1")
  231. compare(MessageEnumReserved.Id, 2, "MessageEnumReserved.Id == 2")
  232. }
  233. function test_caseSense() {
  234. caseSenseMsg.testField = 34;
  235. compare(caseSenseMsg.testField, 34, "MessageUpperCase == 34")
  236. compare(MessageEnumReserved.EnumValue0 == 0, true, "MessageEnumReserved.EnumValue0 == 0")
  237. compare(MessageEnumReserved.EnumValue1 == 1, true, "MessageEnumReserved.EnumValue1 == 1")
  238. compare(MessageEnumReserved.EnumValue2 == 2, true, "MessageEnumReserved.EnumValue2 == 2")
  239. }
  240. function test_underScoreField() {
  241. underScoreMsg.underScoreMessageField = 123
  242. compare(underScoreMsg.underScoreMessageField, 123, "underScoreMsg.underScoreMessageField == 123")
  243. }
  244. function test_lowerCaseMessage() {
  245. lowerCaseMsg.testField = 34
  246. compare(lowerCaseMsg.testField, 34, "LowerCaseMessageName == 34")
  247. }
  248. function test_int32ImplicitConversion() {
  249. int32Msg.testFieldInt = 0
  250. compare(int32Msg.testFieldInt ? true : false, false, "Invalid implicit conversion: " + int32Msg.testFieldInt + " should be false")
  251. int32Msg.testFieldInt = 1
  252. compare(int32Msg.testFieldInt ? true : false, true, "Invalid implicit conversion: " + int32Msg.testFieldInt + " should be true")
  253. }
  254. function test_int32LocaleStringConversion() {
  255. compare(int32Msg.testFieldInt.toLocaleString(Qt.locale()), Number(int32Msg.testFieldInt).toLocaleString(Qt.locale()),
  256. "Locale number string is not match " + int32Msg.testFieldInt.toLocaleString(Qt.locale()) + " != " + Number(int32Msg.testFieldInt).toLocaleString(Qt.locale()))
  257. }
  258. function test_fixed32ImplicitConversion() {
  259. fixed32Msg.testFieldFixedInt32 = 0
  260. compare(fixed32Msg.testFieldFixedInt32 ? true : false, false, "Invalid implicit conversion: " + fixed32Msg.testFieldInt + " should be false")
  261. fixed32Msg.testFieldFixedInt32 = 1
  262. compare(fixed32Msg.testFieldFixedInt32 ? true : false, true, "Invalid implicit conversion: " + fixed32Msg.testFieldInt + " should be true")
  263. }
  264. function test_fixed32LocaleStringConversion() {
  265. compare(fixed32Msg.testFieldFixedInt32.toLocaleString(Qt.locale()), Number(fixed32Msg.testFieldFixedInt32).toLocaleString(Qt.locale()),
  266. "Locale number string is not match " + fixed32Msg.testFieldFixedInt32.toLocaleString(Qt.locale()) + " != " + Number(fixed32Msg.testFieldFixedInt32).toLocaleString(Qt.locale()))
  267. }
  268. function test_sint32ImplicitConversion() {
  269. sint32Msg.testFieldInt = 0
  270. compare(sint32Msg.testFieldInt ? true : false, false, "Invalid implicit conversion: " + sint32Msg.testFieldInt + " should be false")
  271. sint32Msg.testFieldInt = 1
  272. compare(sint32Msg.testFieldInt ? true : false, true, "Invalid implicit conversion: " + sint32Msg.testFieldInt + " should be true")
  273. }
  274. function test_sint32LocaleStringConversion() {
  275. compare(sint32Msg.testFieldInt.toLocaleString(Qt.locale()), Number(sint32Msg.testFieldInt).toLocaleString(Qt.locale()),
  276. "Locale number string is not match " + sint32Msg.testFieldInt.toLocaleString(Qt.locale()) + " != " + Number(sint32Msg.testFieldInt).toLocaleString(Qt.locale()))
  277. }
  278. function test_sfixed32ImplicitConversion() {
  279. sfixed32Msg.testFieldFixedInt32 = 0
  280. compare(sfixed32Msg.testFieldFixedInt32 ? true : false, false, "Invalid implicit conversion: " + sfixed32Msg.testFieldInt + " should be false")
  281. sfixed32Msg.testFieldFixedInt32 = 1
  282. compare(sfixed32Msg.testFieldFixedInt32 ? true : false, true, "Invalid implicit conversion: " + sfixed32Msg.testFieldInt + " should be true")
  283. }
  284. function test_sfixed32LocaleStringConversion() {
  285. compare(sfixed32Msg.testFieldFixedInt32.toLocaleString(Qt.locale()), Number(sfixed32Msg.testFieldFixedInt32).toLocaleString(Qt.locale()),
  286. "Locale number string is not match " + sfixed32Msg.testFieldFixedInt32.toLocaleString(Qt.locale()) + " != " + Number(sfixed32Msg.testFieldFixedInt32).toLocaleString(Qt.locale()))
  287. }
  288. function test_complexMessage() {
  289. compare(complexMsg.testComplexField, innerMessage, "Invalid object is inside complex message")
  290. compare(complexMsg.testComplexField.testFieldString, "inner", "Invalid value of object inside complex message")
  291. complexMsg.testComplexField = outerMessage
  292. compare(complexMsg.testComplexField, outerMessage, "Invalid object is inside complex message")
  293. compare(complexMsg.testComplexField.testFieldString, "outer", "Invalid value of object inside complex message")
  294. complexMsg.testComplexField = innerMessage
  295. compare(complexMsg.testComplexField, innerMessage, "Invalid object is inside complex message")
  296. compare(complexMsg.testComplexField.testFieldString, "inner", "Invalid value of object inside complex message")
  297. }
  298. function test_nestedMessage() {
  299. compare(nestedMsg.testFieldInt, 100, "Nested message initialization failed");
  300. compare(nestedParent.nested, nestedMsg, "Nested message assignment failed");
  301. }
  302. function test_noPackageMessageUser() {
  303. compare(noPackageMessageUser.testField.testFieldInt, 42, "Package-less message contains invalid value");
  304. noPackageMessageUser.testField.testFieldInt = 43;
  305. compare(noPackageMessageUser.testField.testFieldInt, 43, "Package-less message contains invalid value");
  306. }
  307. function test_enumListMessage() {
  308. if (qVersionMajor > 6 && qVersionMinor > 3) { // This was only fixed in Qt6 (perhaps in the version greater than or equal to 6.3)
  309. compare(enumListMessage.globalEnumList.length, 2, "Global enum list size differs the expected one");
  310. compare(typeof(enumListMessage.globalEnumList[1]), typeof(TestEnum.TEST_ENUM_VALUE1), "Global enum list value type doesn't match the enum type");
  311. compare(enumListMessage.globalEnumList[1], TestEnum.TEST_ENUM_VALUE1, "Global enum list value 1 doesn't match");
  312. } else {
  313. verify(enumListMessage.globalEnumList[0] == TestEnum.TEST_ENUM_VALUE0)
  314. }
  315. }
  316. }