tst_simple.qml 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. 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. NoPackageMessage {
  93. id: noPackageMessageUser
  94. testField.testFieldInt: 42
  95. }
  96. function test_1initialization() {
  97. compare(int32Msg.testFieldInt, 2147483647, "SimpleIntMessage initialization")
  98. compare(sint32Msg.testFieldInt, 2147483647, "SimpleSIntMessage initialization")
  99. compare(uint32Msg.testFieldInt, 4294967295, "SimpleUIntMessage initialization")
  100. compare(fixed32Msg.testFieldFixedInt32, 4294967295, "SimpleFixedInt32Message initialization")
  101. compare(sfixed32Msg.testFieldFixedInt32, 2147483647, "SimpleSFixedInt32Message initialization")
  102. }
  103. function test_simpleboolmessage() {
  104. boolMsg.testFieldBool = true;
  105. compare(boolMsg.testFieldBool, true, "SimpleBoolMessage == true")
  106. boolMsg.testFieldBool = false;
  107. compare(boolMsg.testFieldBool, false, "SimpleBoolMessage == false")
  108. }
  109. function test_simpleintmessage() {
  110. int32Msg.testFieldInt = 0;
  111. compare(int32Msg.testFieldInt, 0, "SimpleIntMessage == 0")
  112. int32Msg.testFieldInt = -128;
  113. compare(int32Msg.testFieldInt, -128, "SimpleIntMessage == -128")
  114. int32Msg.testFieldInt = 127;
  115. compare(int32Msg.testFieldInt, 127, "SimpleIntMessage == 127")
  116. int32Msg.testFieldInt = -256;
  117. compare(int32Msg.testFieldInt, -256, "SimpleIntMessage == -256")
  118. int32Msg.testFieldInt = 255;
  119. compare(int32Msg.testFieldInt, 255, "SimpleIntMessage == 255")
  120. int32Msg.testFieldInt = -32768;
  121. compare(int32Msg.testFieldInt, -32768, "SimpleIntMessage == -32768")
  122. int32Msg.testFieldInt = 32767;
  123. compare(int32Msg.testFieldInt, 32767, "SimpleIntMessage == 32767")
  124. int32Msg.testFieldInt = -65536;
  125. compare(int32Msg.testFieldInt, -65536, "SimpleIntMessage == -65536")
  126. int32Msg.testFieldInt = 65535;
  127. compare(int32Msg.testFieldInt, 65535, "SimpleIntMessage == 65535")
  128. int32Msg.testFieldInt = -2147483648;
  129. compare(int32Msg.testFieldInt, -2147483648, "SimpleIntMessage == -2147483648")
  130. int32Msg.testFieldInt = 2147483647;
  131. compare(int32Msg.testFieldInt, 2147483647, "SimpleIntMessage == 2147483647")
  132. }
  133. function test_simplesintmessage() {
  134. sint32Msg.testFieldInt = 0;
  135. compare(sint32Msg.testFieldInt, 0, "SimpleSIntMessage == 0")
  136. sint32Msg.testFieldInt = -128;
  137. compare(sint32Msg.testFieldInt, -128, "SimpleSIntMessage == -128")
  138. sint32Msg.testFieldInt = 127;
  139. compare(sint32Msg.testFieldInt, 127, "SimpleSIntMessage == 127")
  140. sint32Msg.testFieldInt = -256;
  141. compare(sint32Msg.testFieldInt, -256, "SimpleSIntMessage == -256")
  142. sint32Msg.testFieldInt = 255;
  143. compare(sint32Msg.testFieldInt, 255, "SimpleSIntMessage == 255")
  144. sint32Msg.testFieldInt = -32768;
  145. compare(sint32Msg.testFieldInt, -32768, "SimpleSIntMessage == -32768")
  146. sint32Msg.testFieldInt = 32767;
  147. compare(sint32Msg.testFieldInt, 32767, "SimpleSIntMessage == 32767")
  148. sint32Msg.testFieldInt = -65536;
  149. compare(sint32Msg.testFieldInt, -65536, "SimpleSIntMessage == -65536")
  150. sint32Msg.testFieldInt = 65535;
  151. compare(sint32Msg.testFieldInt, 65535, "SimpleSIntMessage == 65535")
  152. sint32Msg.testFieldInt = -2147483648;
  153. compare(sint32Msg.testFieldInt, -2147483648, "SimpleSIntMessage == -2147483648")
  154. sint32Msg.testFieldInt = 2147483647;
  155. compare(sint32Msg.testFieldInt, 2147483647, "SimpleSIntMessage == 2147483647")
  156. }
  157. function test_simpleuintmessage() {
  158. uint32Msg.testFieldInt = 0;
  159. compare(uint32Msg.testFieldInt, 0, "SimpleUIntMessage == 0")
  160. uint32Msg.testFieldInt = 127;
  161. compare(uint32Msg.testFieldInt, 127, "SimpleUIntMessage == 127")
  162. uint32Msg.testFieldInt = 255;
  163. compare(uint32Msg.testFieldInt, 255, "SimpleUIntMessage == 255")
  164. uint32Msg.testFieldInt = 32767;
  165. compare(uint32Msg.testFieldInt, 32767, "SimpleUIntMessage == 32767")
  166. uint32Msg.testFieldInt = 65535;
  167. compare(uint32Msg.testFieldInt, 65535, "SimpleUIntMessage == 65535")
  168. uint32Msg.testFieldInt = 2147483647;
  169. compare(uint32Msg.testFieldInt, 2147483647, "SimpleUIntMessage == 2147483647")
  170. uint32Msg.testFieldInt = 4294967295;
  171. compare(uint32Msg.testFieldInt, 4294967295, "SimpleUIntMessage == 4294967295")
  172. }
  173. function test_simplefixed32message() {
  174. fixed32Msg.testFieldFixedInt32 = 0;
  175. compare(fixed32Msg.testFieldFixedInt32, 0, "SimpleFixedInt32Message == 0")
  176. fixed32Msg.testFieldFixedInt32 = 127;
  177. compare(fixed32Msg.testFieldFixedInt32, 127, "SimpleFixedInt32Message == 127")
  178. fixed32Msg.testFieldFixedInt32 = 255;
  179. compare(fixed32Msg.testFieldFixedInt32, 255, "SimpleFixedInt32Message == 255")
  180. fixed32Msg.testFieldFixedInt32 = 32767;
  181. compare(fixed32Msg.testFieldFixedInt32, 32767, "SimpleFixedInt32Message == 32767")
  182. fixed32Msg.testFieldFixedInt32 = 65535;
  183. compare(fixed32Msg.testFieldFixedInt32, 65535, "SimpleFixedInt32Message == 65535")
  184. fixed32Msg.testFieldFixedInt32 = 2147483647;
  185. compare(fixed32Msg.testFieldFixedInt32, 2147483647, "SimpleFixedInt32Message == 2147483647")
  186. fixed32Msg.testFieldFixedInt32 = 4294967295;
  187. compare(fixed32Msg.testFieldFixedInt32, 4294967295, "SimpleFixedInt32Message == 4294967295")
  188. }
  189. function test_simplesfixed32message() {
  190. sfixed32Msg.testFieldFixedInt32 = 0;
  191. compare(sfixed32Msg.testFieldFixedInt32, 0, "SimpleSFixedInt32Message == 0")
  192. sfixed32Msg.testFieldFixedInt32 = -128;
  193. compare(sfixed32Msg.testFieldFixedInt32, -128, "SimpleSFixedInt32Message == -128")
  194. sfixed32Msg.testFieldFixedInt32 = 127;
  195. compare(sfixed32Msg.testFieldFixedInt32, 127, "SimpleSFixedInt32Message == 127")
  196. sfixed32Msg.testFieldFixedInt32 = -256;
  197. compare(sfixed32Msg.testFieldFixedInt32, -256, "SimpleSFixedInt32Message == -256")
  198. sfixed32Msg.testFieldFixedInt32 = 255;
  199. compare(sfixed32Msg.testFieldFixedInt32, 255, "SimpleSFixedInt32Message == 255")
  200. sfixed32Msg.testFieldFixedInt32 = -32768;
  201. compare(sfixed32Msg.testFieldFixedInt32, -32768, "SimpleSFixedInt32Message == -32768")
  202. sfixed32Msg.testFieldFixedInt32 = 32767;
  203. compare(sfixed32Msg.testFieldFixedInt32, 32767, "SimpleSFixedInt32Message == 32767")
  204. sfixed32Msg.testFieldFixedInt32 = -65536;
  205. compare(sfixed32Msg.testFieldFixedInt32, -65536, "SimpleSFixedInt32Message == -65536")
  206. sfixed32Msg.testFieldFixedInt32 = 65535;
  207. compare(sfixed32Msg.testFieldFixedInt32, 65535, "SimpleSFixedInt32Message == 65535")
  208. sfixed32Msg.testFieldFixedInt32 = -2147483648;
  209. compare(sfixed32Msg.testFieldFixedInt32, -2147483648, "SimpleSFixedInt32Message == -2147483648")
  210. sfixed32Msg.testFieldFixedInt32 = 2147483647;
  211. compare(sfixed32Msg.testFieldFixedInt32, 2147483647, "SimpleSFixedInt32Message == 2147483647")
  212. }
  213. function test_simplesstringmessage() {
  214. compare(stringMsg.testFieldString, "Test string", "SimpleStringMessage")
  215. }
  216. function test_reservedNames() {
  217. reservedMsg.id_proto = 34;
  218. reservedMsg.import_proto = 35;
  219. reservedMsg.property_proto = 36;
  220. compare(reservedMsg.id_proto, 34, "reservedMsg.id_proto == 34")
  221. compare(reservedMsg.import_proto, 35, "reservedMsg.import_proto == 35")
  222. compare(reservedMsg.property_proto, 36, "reservedMsg.property_proto == 36")
  223. }
  224. function test_reservedEnums() {
  225. compare(MessageEnumReserved.Import, 0, "MessageEnumReserved.Import == 0")
  226. compare(MessageEnumReserved.Property, 1, "MessageEnumReserved.Property == 1")
  227. compare(MessageEnumReserved.Id, 2, "MessageEnumReserved.Id == 2")
  228. }
  229. function test_caseSense() {
  230. caseSenseMsg.testField = 34;
  231. compare(caseSenseMsg.testField, 34, "MessageUpperCase == 34")
  232. compare(MessageEnumReserved.EnumValue0 == 0, true, "MessageEnumReserved.EnumValue0 == 0")
  233. compare(MessageEnumReserved.EnumValue1 == 1, true, "MessageEnumReserved.EnumValue1 == 1")
  234. compare(MessageEnumReserved.EnumValue2 == 2, true, "MessageEnumReserved.EnumValue2 == 2")
  235. }
  236. function test_underScoreField() {
  237. underScoreMsg.underScoreMessageField = 123
  238. compare(underScoreMsg.underScoreMessageField, 123, "underScoreMsg.underScoreMessageField == 123")
  239. }
  240. function test_lowerCaseMessage() {
  241. lowerCaseMsg.testField = 34
  242. compare(lowerCaseMsg.testField, 34, "LowerCaseMessageName == 34")
  243. }
  244. function test_int32ImplicitConversion() {
  245. int32Msg.testFieldInt = 0
  246. compare(int32Msg.testFieldInt ? true : false, false, "Invalid implicit conversion: " + int32Msg.testFieldInt + " should be false")
  247. int32Msg.testFieldInt = 1
  248. compare(int32Msg.testFieldInt ? true : false, true, "Invalid implicit conversion: " + int32Msg.testFieldInt + " should be true")
  249. }
  250. function test_int32LocaleStringConversion() {
  251. compare(int32Msg.testFieldInt.toLocaleString(Qt.locale()), Number(int32Msg.testFieldInt).toLocaleString(Qt.locale()),
  252. "Locale number string is not match " + int32Msg.testFieldInt.toLocaleString(Qt.locale()) + " != " + Number(int32Msg.testFieldInt).toLocaleString(Qt.locale()))
  253. }
  254. function test_fixed32ImplicitConversion() {
  255. fixed32Msg.testFieldFixedInt32 = 0
  256. compare(fixed32Msg.testFieldFixedInt32 ? true : false, false, "Invalid implicit conversion: " + fixed32Msg.testFieldInt + " should be false")
  257. fixed32Msg.testFieldFixedInt32 = 1
  258. compare(fixed32Msg.testFieldFixedInt32 ? true : false, true, "Invalid implicit conversion: " + fixed32Msg.testFieldInt + " should be true")
  259. }
  260. function test_fixed32LocaleStringConversion() {
  261. compare(fixed32Msg.testFieldFixedInt32.toLocaleString(Qt.locale()), Number(fixed32Msg.testFieldFixedInt32).toLocaleString(Qt.locale()),
  262. "Locale number string is not match " + fixed32Msg.testFieldFixedInt32.toLocaleString(Qt.locale()) + " != " + Number(fixed32Msg.testFieldFixedInt32).toLocaleString(Qt.locale()))
  263. }
  264. function test_sint32ImplicitConversion() {
  265. sint32Msg.testFieldInt = 0
  266. compare(sint32Msg.testFieldInt ? true : false, false, "Invalid implicit conversion: " + sint32Msg.testFieldInt + " should be false")
  267. sint32Msg.testFieldInt = 1
  268. compare(sint32Msg.testFieldInt ? true : false, true, "Invalid implicit conversion: " + sint32Msg.testFieldInt + " should be true")
  269. }
  270. function test_sint32LocaleStringConversion() {
  271. compare(sint32Msg.testFieldInt.toLocaleString(Qt.locale()), Number(sint32Msg.testFieldInt).toLocaleString(Qt.locale()),
  272. "Locale number string is not match " + sint32Msg.testFieldInt.toLocaleString(Qt.locale()) + " != " + Number(sint32Msg.testFieldInt).toLocaleString(Qt.locale()))
  273. }
  274. function test_sfixed32ImplicitConversion() {
  275. sfixed32Msg.testFieldFixedInt32 = 0
  276. compare(sfixed32Msg.testFieldFixedInt32 ? true : false, false, "Invalid implicit conversion: " + sfixed32Msg.testFieldInt + " should be false")
  277. sfixed32Msg.testFieldFixedInt32 = 1
  278. compare(sfixed32Msg.testFieldFixedInt32 ? true : false, true, "Invalid implicit conversion: " + sfixed32Msg.testFieldInt + " should be true")
  279. }
  280. function test_sfixed32LocaleStringConversion() {
  281. compare(sfixed32Msg.testFieldFixedInt32.toLocaleString(Qt.locale()), Number(sfixed32Msg.testFieldFixedInt32).toLocaleString(Qt.locale()),
  282. "Locale number string is not match " + sfixed32Msg.testFieldFixedInt32.toLocaleString(Qt.locale()) + " != " + Number(sfixed32Msg.testFieldFixedInt32).toLocaleString(Qt.locale()))
  283. }
  284. function test_complexMessage() {
  285. compare(complexMsg.testComplexField, innerMessage, "Invalid object is inside complex message")
  286. compare(complexMsg.testComplexField.testFieldString, "inner", "Invalid value of object inside complex message")
  287. complexMsg.testComplexField = outerMessage
  288. compare(complexMsg.testComplexField, outerMessage, "Invalid object is inside complex message")
  289. compare(complexMsg.testComplexField.testFieldString, "outer", "Invalid value of object inside complex message")
  290. complexMsg.testComplexField = innerMessage
  291. compare(complexMsg.testComplexField, innerMessage, "Invalid object is inside complex message")
  292. compare(complexMsg.testComplexField.testFieldString, "inner", "Invalid value of object inside complex message")
  293. }
  294. function test_nestedMessage() {
  295. compare(nestedMsg.testFieldInt, 100, "Nested message initialization failed");
  296. compare(nestedParent.nested, nestedMsg, "Nested message assignment failed");
  297. }
  298. function test_noPackageMessageUser() {
  299. compare(noPackageMessageUser.testField.testFieldInt, 42, "Package-less message contains invalid value");
  300. noPackageMessageUser.testField.testFieldInt = 43;
  301. compare(noPackageMessageUser.testField.testFieldInt, 43, "Package-less message contains invalid value");
  302. }
  303. }