TestPreprocessing.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. #region Copyright notice and license
  2. // Protocol Buffers - Google's data interchange format
  3. // Copyright 2008 Google Inc. All rights reserved.
  4. // http://github.com/jskeet/dotnet-protobufs/
  5. // Original C++/Java/Python code:
  6. // http://code.google.com/p/protobuf/
  7. //
  8. // Redistribution and use in source and binary forms, with or without
  9. // modification, are permitted provided that the following conditions are
  10. // met:
  11. //
  12. // * Redistributions of source code must retain the above copyright
  13. // notice, this list of conditions and the following disclaimer.
  14. // * Redistributions in binary form must reproduce the above
  15. // copyright notice, this list of conditions and the following disclaimer
  16. // in the documentation and/or other materials provided with the
  17. // distribution.
  18. // * Neither the name of Google Inc. nor the names of its
  19. // contributors may be used to endorse or promote products derived from
  20. // this software without specific prior written permission.
  21. //
  22. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  25. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  26. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  27. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  28. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  29. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  30. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  32. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. #endregion
  34. using System;
  35. using System.Collections.Generic;
  36. using System.Diagnostics;
  37. using System.IO;
  38. using System.Reflection;
  39. using NUnit.Framework;
  40. namespace Google.ProtocolBuffers.ProtoGen
  41. {
  42. [TestFixture]
  43. [Category("Preprocessor")]
  44. public partial class TestPreprocessing
  45. {
  46. private static readonly string TempPath = Path.Combine(Path.GetTempPath(), "proto-gen-test");
  47. private const string DefaultProto = @"
  48. package nunit.simple;
  49. // Test a very simple message.
  50. message MyMessage {
  51. optional string name = 1;
  52. }";
  53. #region TestFixture SetUp/TearDown
  54. private static readonly string OriginalWorkingDirectory = Environment.CurrentDirectory;
  55. [TestFixtureSetUp]
  56. public virtual void Setup()
  57. {
  58. Teardown();
  59. Directory.CreateDirectory(TempPath);
  60. Environment.CurrentDirectory = TempPath;
  61. }
  62. [TestFixtureTearDown]
  63. public virtual void Teardown()
  64. {
  65. Environment.CurrentDirectory = OriginalWorkingDirectory;
  66. if (Directory.Exists(TempPath))
  67. {
  68. Directory.Delete(TempPath, true);
  69. }
  70. }
  71. #endregion
  72. #region Helper Methods RunProtoGen / RunCsc
  73. void RunProtoGen(int expect, params string[] args)
  74. {
  75. TextWriter tout = Console.Out, terr = Console.Error;
  76. StringWriter temp = new StringWriter();
  77. Console.SetOut(temp);
  78. Console.SetError(temp);
  79. try
  80. {
  81. Assert.AreEqual(expect, ProgramPreprocess.Run(args), "ProtoGen Failed: {0}", temp);
  82. }
  83. finally
  84. {
  85. Console.SetOut(tout);
  86. Console.SetError(terr);
  87. }
  88. }
  89. private Assembly RunCsc(int expect, params string[] sources)
  90. {
  91. using (TempFile tempDll = new TempFile(String.Empty))
  92. {
  93. tempDll.ChangeExtension(".dll");
  94. List<string> args = new List<string>();
  95. args.Add("/nologo");
  96. args.Add("/target:library");
  97. args.Add("/debug-");
  98. args.Add(String.Format(@"""/out:{0}""", tempDll.TempPath));
  99. args.Add("/r:System.dll");
  100. args.Add(String.Format(@"""/r:{0}""", typeof (Google.ProtocolBuffers.DescriptorProtos.DescriptorProto).Assembly.Location));
  101. args.AddRange(sources);
  102. string exe = Path.Combine(System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory(), "csc.exe");
  103. ProcessStartInfo psi = new ProcessStartInfo(exe);
  104. psi.CreateNoWindow = true;
  105. psi.UseShellExecute = false;
  106. psi.RedirectStandardOutput = true;
  107. psi.RedirectStandardError = true;
  108. psi.Arguments = string.Join(" ", args.ToArray());
  109. Process p = Process.Start(psi);
  110. p.WaitForExit();
  111. string errorText = p.StandardOutput.ReadToEnd() + p.StandardError.ReadToEnd();
  112. Assert.AreEqual(expect, p.ExitCode, "CSC.exe Failed: {0}", errorText);
  113. Assembly asm = null;
  114. if (p.ExitCode == 0)
  115. {
  116. byte[] allbytes = File.ReadAllBytes(tempDll.TempPath);
  117. asm = Assembly.Load(allbytes);
  118. foreach (Type t in asm.GetTypes())
  119. {
  120. Debug.WriteLine(t.FullName, asm.FullName);
  121. }
  122. }
  123. return asm;
  124. }
  125. }
  126. #endregion
  127. // *******************************************************************
  128. // The following tests excercise options for protogen.exe
  129. // *******************************************************************
  130. [Test]
  131. public void TestProtoFile()
  132. {
  133. string test = new StackFrame(false).GetMethod().Name;
  134. Setup();
  135. using (TempFile source = TempFile.Attach(test + ".cs"))
  136. using (ProtoFile proto = new ProtoFile(test + ".proto", DefaultProto))
  137. {
  138. RunProtoGen(0, proto.TempPath);
  139. Assembly a = RunCsc(0, source.TempPath);
  140. //assert that the message type is in the expected namespace
  141. Type t = a.GetType("nunit.simple.MyMessage", true, true);
  142. Assert.IsTrue(typeof(IMessage).IsAssignableFrom(t), "Expect an IMessage");
  143. //assert that we can find the static descriptor type
  144. a.GetType("nunit.simple." + test, true, true);
  145. }
  146. }
  147. [Test]
  148. public void TestProtoFileWithConflictingType()
  149. {
  150. string test = new StackFrame(false).GetMethod().Name;
  151. Setup();
  152. using (TempFile source = TempFile.Attach(test + ".cs"))
  153. using (ProtoFile proto = new ProtoFile(test + ".proto", @"
  154. package nunit.simple;
  155. // Test a very simple message.
  156. message " + test + @" {
  157. optional string name = 1;
  158. } "))
  159. {
  160. RunProtoGen(0, proto.TempPath);
  161. Assembly a = RunCsc(0, source.TempPath);
  162. //assert that the message type is in the expected namespace
  163. Type t = a.GetType("nunit.simple." + test, true, true);
  164. Assert.IsTrue(typeof(IMessage).IsAssignableFrom(t), "Expect an IMessage");
  165. //assert that we can find the static descriptor type
  166. a.GetType("nunit.simple.Proto." + test, true, true);
  167. }
  168. }
  169. [Test]
  170. public void TestProtoFileWithNamespace()
  171. {
  172. string test = new StackFrame(false).GetMethod().Name;
  173. Setup();
  174. using (TempFile source = TempFile.Attach(test + ".cs"))
  175. using (ProtoFile proto = new ProtoFile(test + ".proto", DefaultProto))
  176. {
  177. RunProtoGen(0, proto.TempPath, "-namespace:MyNewNamespace");
  178. Assembly a = RunCsc(0, source.TempPath);
  179. //assert that the message type is in the expected namespace
  180. Type t = a.GetType("MyNewNamespace.MyMessage", true, true);
  181. Assert.IsTrue(typeof(IMessage).IsAssignableFrom(t), "Expect an IMessage");
  182. //assert that we can find the static descriptor type
  183. a.GetType("MyNewNamespace." + test, true, true);
  184. }
  185. }
  186. [Test]
  187. public void TestProtoFileWithUmbrellaClassName()
  188. {
  189. string test = new StackFrame(false).GetMethod().Name;
  190. Setup();
  191. using (TempFile source = TempFile.Attach("MyUmbrellaClassname.cs"))
  192. using (ProtoFile proto = new ProtoFile(test + ".proto", DefaultProto))
  193. {
  194. RunProtoGen(0, proto.TempPath, "/umbrella_classname=MyUmbrellaClassname");
  195. Assembly a = RunCsc(0, source.TempPath);
  196. //assert that the message type is in the expected namespace
  197. Type t = a.GetType("nunit.simple.MyMessage", true, true);
  198. Assert.IsTrue(typeof(IMessage).IsAssignableFrom(t), "Expect an IMessage");
  199. //assert that we can find the static descriptor type
  200. a.GetType("nunit.simple.MyUmbrellaClassname", true, true);
  201. }
  202. }
  203. [Test]
  204. public void TestProtoFileWithNestedClass()
  205. {
  206. string test = new StackFrame(false).GetMethod().Name;
  207. Setup();
  208. using (TempFile source = TempFile.Attach(test + ".cs"))
  209. using (ProtoFile proto = new ProtoFile(test + ".proto", DefaultProto))
  210. {
  211. RunProtoGen(0, proto.TempPath, "-nest_classes:true");
  212. Assembly a = RunCsc(0, source.TempPath);
  213. //assert that the message type is in the expected namespace
  214. Type t = a.GetType("nunit.simple." + test + "+MyMessage", true, true);
  215. Assert.IsTrue(typeof(IMessage).IsAssignableFrom(t), "Expect an IMessage");
  216. //assert that we can find the static descriptor type
  217. a.GetType("nunit.simple." + test, true, true);
  218. }
  219. }
  220. [Test]
  221. public void TestProtoFileWithExpandedNsDirectories()
  222. {
  223. string test = new StackFrame(false).GetMethod().Name;
  224. Setup();
  225. using (TempFile source = TempFile.Attach(@"nunit\simple\" + test + ".cs"))
  226. using (ProtoFile proto = new ProtoFile(test + ".proto", DefaultProto))
  227. {
  228. RunProtoGen(0, proto.TempPath, "-expand_namespace_directories:true");
  229. Assembly a = RunCsc(0, source.TempPath);
  230. //assert that the message type is in the expected namespace
  231. Type t = a.GetType("nunit.simple.MyMessage", true, true);
  232. Assert.IsTrue(typeof(IMessage).IsAssignableFrom(t), "Expect an IMessage");
  233. //assert that we can find the static descriptor type
  234. a.GetType("nunit.simple." + test, true, true);
  235. }
  236. }
  237. [Test]
  238. public void TestProtoFileDisablingClsComplianceFlags()
  239. {
  240. string test = new StackFrame(false).GetMethod().Name;
  241. Setup();
  242. using (TempFile source = TempFile.Attach(test + ".cs"))
  243. using (ProtoFile proto = new ProtoFile(test + ".proto", @"
  244. package nunit.simple;
  245. // Test a very simple message.
  246. message MyMessage {
  247. optional uint32 name = 1;
  248. } "))
  249. {
  250. //CS3021: Warning as Error: xx does not need a CLSCompliant attribute because the assembly does not have a CLSCompliant attribute
  251. RunProtoGen(0, proto.TempPath);
  252. RunCsc(1, source.TempPath, "/warnaserror+");
  253. //Now we know it fails, make it pass by turning off cls_compliance generation
  254. RunProtoGen(0, proto.TempPath, "-cls_compliance:false");
  255. Assembly a = RunCsc(0, source.TempPath, "/warnaserror+");
  256. //assert that the message type is in the expected namespace
  257. Type t = a.GetType("nunit.simple.MyMessage", true, true);
  258. Assert.IsTrue(typeof(IMessage).IsAssignableFrom(t), "Expect an IMessage");
  259. //assert that we can find the static descriptor type
  260. a.GetType("nunit.simple." + test, true, true);
  261. }
  262. }
  263. [Test]
  264. public void TestProtoFileWithNewExtension()
  265. {
  266. string test = new StackFrame(false).GetMethod().Name;
  267. Setup();
  268. using (TempFile source = TempFile.Attach(test + ".Generated.cs"))
  269. using (ProtoFile proto = new ProtoFile(test + ".proto", DefaultProto))
  270. {
  271. RunProtoGen(0, proto.TempPath, "-file_extension:.Generated.cs");
  272. Assembly a = RunCsc(0, source.TempPath);
  273. //assert that the message type is in the expected namespace
  274. Type t = a.GetType("nunit.simple.MyMessage", true, true);
  275. Assert.IsTrue(typeof(IMessage).IsAssignableFrom(t), "Expect an IMessage");
  276. //assert that we can find the static descriptor type
  277. a.GetType("nunit.simple." + test, true, true);
  278. }
  279. }
  280. [Test]
  281. public void TestProtoFileWithUmbrellaNamespace()
  282. {
  283. string test = new StackFrame(false).GetMethod().Name;
  284. Setup();
  285. using (TempFile source = TempFile.Attach(test + ".cs"))
  286. using (ProtoFile proto = new ProtoFile(test + ".proto", DefaultProto))
  287. {
  288. RunProtoGen(0, proto.TempPath, "-umbrella_namespace:MyUmbrella.Namespace");
  289. Assembly a = RunCsc(0, source.TempPath);
  290. //assert that the message type is in the expected namespace
  291. Type t = a.GetType("nunit.simple.MyMessage", true, true);
  292. Assert.IsTrue(typeof(IMessage).IsAssignableFrom(t), "Expect an IMessage");
  293. //assert that we can find the static descriptor type
  294. a.GetType("nunit.simple.MyUmbrella.Namespace." + test, true, true);
  295. }
  296. }
  297. [Test]
  298. public void TestProtoFileWithIgnoredUmbrellaNamespaceDueToNesting()
  299. {
  300. string test = new StackFrame(false).GetMethod().Name;
  301. Setup();
  302. using (TempFile source = TempFile.Attach(test + ".cs"))
  303. using (ProtoFile proto = new ProtoFile(test + ".proto", DefaultProto))
  304. {
  305. RunProtoGen(0, proto.TempPath, "-nest_classes:true", "-umbrella_namespace:MyUmbrella.Namespace");
  306. Assembly a = RunCsc(0, source.TempPath);
  307. //assert that the message type is in the expected namespace
  308. Type t = a.GetType("nunit.simple." + test + "+MyMessage", true, true);
  309. Assert.IsTrue(typeof(IMessage).IsAssignableFrom(t), "Expect an IMessage");
  310. //assert that we can find the static descriptor type
  311. a.GetType("nunit.simple." + test, true, true);
  312. }
  313. }
  314. [Test]
  315. public void TestProtoFileWithExplicitEmptyUmbrellaNamespace()
  316. {
  317. string test = new StackFrame(false).GetMethod().Name;
  318. Setup();
  319. using (TempFile source = TempFile.Attach(test + ".cs"))
  320. using (ProtoFile proto = new ProtoFile(test + ".proto", @"
  321. package nunit.simple;
  322. // Test a very simple message.
  323. message " + test + @" {
  324. optional string name = 1;
  325. } "))
  326. {
  327. //Forces the umbrella class to not use a namespace even if a collision with a type is detected.
  328. RunProtoGen(0, proto.TempPath, "-umbrella_namespace:");
  329. //error CS0441: 'nunit.simple.TestProtoFileWithExplicitEmptyUmbrellaNamespace': a class cannot be both static and sealed
  330. RunCsc(1, source.TempPath);
  331. }
  332. }
  333. [Test]
  334. public void TestProtoFileWithNewOutputFolder()
  335. {
  336. string test = new StackFrame(false).GetMethod().Name;
  337. Setup();
  338. using (TempFile source = TempFile.Attach(@"generated-code\" + test + ".cs"))
  339. using (ProtoFile proto = new ProtoFile(test + ".proto", DefaultProto))
  340. {
  341. RunProtoGen(1, proto.TempPath, "-output_directory:generated-code");
  342. Directory.CreateDirectory("generated-code");
  343. RunProtoGen(0, proto.TempPath, "-output_directory:generated-code");
  344. Assembly a = RunCsc(0, source.TempPath);
  345. //assert that the message type is in the expected namespace
  346. Type t = a.GetType("nunit.simple.MyMessage", true, true);
  347. Assert.IsTrue(typeof(IMessage).IsAssignableFrom(t), "Expect an IMessage");
  348. //assert that we can find the static descriptor type
  349. a.GetType("nunit.simple." + test, true, true);
  350. }
  351. }
  352. [Test]
  353. public void TestProtoFileAndIgnoreGoogleProtobuf()
  354. {
  355. string test = new StackFrame(false).GetMethod().Name;
  356. Setup();
  357. using (TempFile source = TempFile.Attach(test + ".cs"))
  358. using (ProtoFile proto = new ProtoFile(test + ".proto", @"
  359. import ""google/protobuf/csharp_options.proto"";
  360. option (google.protobuf.csharp_file_options).namespace = ""MyNewNamespace"";
  361. " + DefaultProto))
  362. {
  363. string google = Path.Combine(TempPath, "google\\protobuf");
  364. Directory.CreateDirectory(google);
  365. foreach (string file in Directory.GetFiles(Path.Combine(OriginalWorkingDirectory, "google\\protobuf")))
  366. {
  367. File.Copy(file, Path.Combine(google, Path.GetFileName(file)));
  368. }
  369. Assert.AreEqual(0, Directory.GetFiles(TempPath, "*.cs").Length);
  370. RunProtoGen(0, proto.TempPath, "-ignore_google_protobuf:true");
  371. Assert.AreEqual(1, Directory.GetFiles(TempPath, "*.cs").Length);
  372. Assembly a = RunCsc(0, source.TempPath);
  373. //assert that the message type is in the expected namespace
  374. Type t = a.GetType("MyNewNamespace.MyMessage", true, true);
  375. Assert.IsTrue(typeof(IMessage).IsAssignableFrom(t), "Expect an IMessage");
  376. //assert that we can find the static descriptor type
  377. a.GetType("MyNewNamespace." + test, true, true);
  378. }
  379. }
  380. [Test]
  381. public void TestProtoFileWithoutIgnoreGoogleProtobuf()
  382. {
  383. string test = new StackFrame(false).GetMethod().Name;
  384. Setup();
  385. using (TempFile source = TempFile.Attach(test + ".cs"))
  386. using (ProtoFile proto = new ProtoFile(test + ".proto", @"
  387. import ""google/protobuf/csharp_options.proto"";
  388. option (google.protobuf.csharp_file_options).namespace = ""MyNewNamespace"";
  389. " + DefaultProto))
  390. {
  391. string google = Path.Combine(TempPath, "google\\protobuf");
  392. Directory.CreateDirectory(google);
  393. foreach (string file in Directory.GetFiles(Path.Combine(OriginalWorkingDirectory, "google\\protobuf")))
  394. {
  395. File.Copy(file, Path.Combine(google, Path.GetFileName(file)));
  396. }
  397. Assert.AreEqual(0, Directory.GetFiles(TempPath, "*.cs").Length);
  398. //Without the option this fails due to being unable to resolve google/protobuf descriptors
  399. RunProtoGen(1, proto.TempPath, "-ignore_google_protobuf:false");
  400. }
  401. }
  402. // *******************************************************************
  403. // The following tests excercise options for protoc.exe
  404. // *******************************************************************
  405. [Test]
  406. public void TestProtoFileWithIncludeImports()
  407. {
  408. string test = new StackFrame(false).GetMethod().Name;
  409. Setup();
  410. using (TempFile source = TempFile.Attach(test + ".cs"))
  411. using (ProtoFile proto = new ProtoFile(test + ".proto", @"
  412. import ""google/protobuf/csharp_options.proto"";
  413. option (google.protobuf.csharp_file_options).namespace = ""MyNewNamespace"";
  414. package nunit.simple;
  415. // Test a very simple message.
  416. message MyMessage {
  417. optional string name = 1;
  418. } "))
  419. {
  420. string google = Path.Combine(TempPath, "google\\protobuf");
  421. Directory.CreateDirectory(google);
  422. foreach (string file in Directory.GetFiles(Path.Combine(OriginalWorkingDirectory, "google\\protobuf")))
  423. {
  424. File.Copy(file, Path.Combine(google, Path.GetFileName(file)));
  425. }
  426. Assert.AreEqual(0, Directory.GetFiles(TempPath, "*.cs").Length);
  427. //if you specify the protoc option --include_imports this should build three source files
  428. RunProtoGen(0, proto.TempPath, "--include_imports");
  429. Assert.AreEqual(3, Directory.GetFiles(TempPath, "*.cs").Length);
  430. //you can (and should) simply omit the inclusion of the extra source files in your project
  431. Assembly a = RunCsc(0, source.TempPath);
  432. //assert that the message type is in the expected namespace
  433. Type t = a.GetType("MyNewNamespace.MyMessage", true, true);
  434. Assert.IsTrue(typeof(IMessage).IsAssignableFrom(t), "Expect an IMessage");
  435. //assert that we can find the static descriptor type
  436. a.GetType("MyNewNamespace." + test, true, true);
  437. }
  438. }
  439. [Test]
  440. public void TestProtoFileWithIncludeImportsAndIgnoreGoogleProtobuf()
  441. {
  442. string test = new StackFrame(false).GetMethod().Name;
  443. Setup();
  444. using (TempFile source = TempFile.Attach(test + ".cs"))
  445. using (ProtoFile proto = new ProtoFile(test + ".proto", @"
  446. import ""google/protobuf/csharp_options.proto"";
  447. option (google.protobuf.csharp_file_options).namespace = ""MyNewNamespace"";
  448. package nunit.simple;
  449. // Test a very simple message.
  450. message MyMessage {
  451. optional string name = 1;
  452. } "))
  453. {
  454. string google = Path.Combine(TempPath, "google\\protobuf");
  455. Directory.CreateDirectory(google);
  456. foreach (string file in Directory.GetFiles(Path.Combine(OriginalWorkingDirectory, "google\\protobuf")))
  457. File.Copy(file, Path.Combine(google, Path.GetFileName(file)));
  458. Assert.AreEqual(0, Directory.GetFiles(TempPath, "*.cs").Length);
  459. //Even with --include_imports, if you provide -ignore_google_protobuf:true you only get the one source file
  460. RunProtoGen(0, proto.TempPath, "-ignore_google_protobuf:true", "--include_imports");
  461. Assert.AreEqual(1, Directory.GetFiles(TempPath, "*.cs").Length);
  462. //you can (and should) simply omit the inclusion of the extra source files in your project
  463. Assembly a = RunCsc(0, source.TempPath);
  464. //assert that the message type is in the expected namespace
  465. Type t = a.GetType("MyNewNamespace.MyMessage", true, true);
  466. Assert.IsTrue(typeof(IMessage).IsAssignableFrom(t), "Expect an IMessage");
  467. //assert that we can find the static descriptor type
  468. a.GetType("MyNewNamespace." + test, true, true);
  469. }
  470. }
  471. [Test]
  472. public void TestProtoFileKeepingTheProtoBuffer()
  473. {
  474. string test = new StackFrame(false).GetMethod().Name;
  475. Setup();
  476. using (TempFile protobuf = TempFile.Attach(test + ".pb"))
  477. using (TempFile source = TempFile.Attach(test + ".cs"))
  478. using (ProtoFile proto = new ProtoFile(test + ".proto", @"
  479. package nunit.simple;
  480. // Test a very simple message.
  481. message MyMessage {
  482. optional string name = 1;
  483. } "))
  484. {
  485. RunProtoGen(0, proto.TempPath, "--descriptor_set_out=" + protobuf.TempPath);
  486. Assert.IsTrue(File.Exists(protobuf.TempPath), "Missing: " + protobuf.TempPath);
  487. Assembly a = RunCsc(0, source.TempPath);
  488. //assert that the message type is in the expected namespace
  489. Type t = a.GetType("nunit.simple.MyMessage", true, true);
  490. Assert.IsTrue(typeof(IMessage).IsAssignableFrom(t), "Expect an IMessage");
  491. //assert that we can find the static descriptor type
  492. a.GetType("nunit.simple." + test, true, true);
  493. }
  494. }
  495. //Seems the --proto_path or -I option is non-functional for me. Maybe others have luck?
  496. [Test, Ignore("http://code.google.com/p/protobuf/issues/detail?id=40")]
  497. public void TestProtoFileInDifferentDirectory()
  498. {
  499. string test = new StackFrame(false).GetMethod().Name;
  500. Setup();
  501. using (TempFile source = TempFile.Attach(test + ".cs"))
  502. using (ProtoFile proto = new ProtoFile(test + ".proto", DefaultProto))
  503. {
  504. Environment.CurrentDirectory = OriginalWorkingDirectory;
  505. RunProtoGen(0, proto.TempPath, "--proto_path=" + TempPath);
  506. Assembly a = RunCsc(0, source.TempPath);
  507. //assert that the message type is in the expected namespace
  508. Type t = a.GetType("nunit.simple.MyMessage", true, true);
  509. Assert.IsTrue(typeof(IMessage).IsAssignableFrom(t), "Expect an IMessage");
  510. //assert that we can find the static descriptor type
  511. a.GetType("nunit.simple." + test, true, true);
  512. }
  513. }
  514. // *******************************************************************
  515. // Handling of mutliple input files
  516. // *******************************************************************
  517. [Test]
  518. public void TestMultipleProtoFiles()
  519. {
  520. Setup();
  521. using (TempFile source1 = TempFile.Attach("MyMessage.cs"))
  522. using (ProtoFile proto1 = new ProtoFile("MyMessage.proto", @"
  523. package nunit.simple;
  524. // Test a very simple message.
  525. message MyMessage {
  526. optional string name = 1;
  527. }"))
  528. using (TempFile source2 = TempFile.Attach("MyMessageList.cs"))
  529. using (ProtoFile proto2 = new ProtoFile("MyMessageList.proto", @"
  530. package nunit.simple;
  531. import ""MyMessage.proto"";
  532. // Test a very simple message.
  533. message MyMessageList {
  534. repeated MyMessage messages = 1;
  535. }"))
  536. {
  537. RunProtoGen(0, proto1.TempPath, proto2.TempPath);
  538. Assembly a = RunCsc(0, source1.TempPath, source2.TempPath);
  539. //assert that the message type is in the expected namespace
  540. Type t1 = a.GetType("nunit.simple.MyMessage", true, true);
  541. Assert.IsTrue(typeof(IMessage).IsAssignableFrom(t1), "Expect an IMessage");
  542. //assert that the message type is in the expected namespace
  543. Type t2 = a.GetType("nunit.simple.MyMessageList", true, true);
  544. Assert.IsTrue(typeof(IMessage).IsAssignableFrom(t2), "Expect an IMessage");
  545. //assert that we can find the static descriptor type
  546. a.GetType("nunit.simple.Proto.MyMessage", true, true);
  547. a.GetType("nunit.simple.Proto.MyMessageList", true, true);
  548. }
  549. }
  550. [Test]
  551. public void TestOneProtoFileWithBufferFile()
  552. {
  553. Setup();
  554. using (TempFile source1 = TempFile.Attach("MyMessage.cs"))
  555. using (TempFile protobuf = TempFile.Attach("MyMessage.pb"))
  556. using (ProtoFile proto1 = new ProtoFile("MyMessage.proto", @"
  557. package nunit.simple;
  558. // Test a very simple message.
  559. message MyMessage {
  560. optional string name = 1;
  561. }"))
  562. using (TempFile source2 = TempFile.Attach("MyMessageList.cs"))
  563. using (ProtoFile proto2 = new ProtoFile("MyMessageList.proto", @"
  564. package nunit.simple;
  565. import ""MyMessage.proto"";
  566. // Test a very simple message.
  567. message MyMessageList {
  568. repeated MyMessage messages = 1;
  569. }"))
  570. {
  571. //build the proto buffer for MyMessage
  572. RunProtoGen(0, proto1.TempPath, "--descriptor_set_out=" + protobuf.TempPath);
  573. //build the MyMessageList proto-buffer and generate code by including MyMessage.pb
  574. RunProtoGen(0, proto2.TempPath, protobuf.TempPath);
  575. Assembly a = RunCsc(0, source1.TempPath, source2.TempPath);
  576. //assert that the message type is in the expected namespace
  577. Type t1 = a.GetType("nunit.simple.MyMessage", true, true);
  578. Assert.IsTrue(typeof(IMessage).IsAssignableFrom(t1), "Expect an IMessage");
  579. //assert that the message type is in the expected namespace
  580. Type t2 = a.GetType("nunit.simple.MyMessageList", true, true);
  581. Assert.IsTrue(typeof(IMessage).IsAssignableFrom(t2), "Expect an IMessage");
  582. //assert that we can find the static descriptor type
  583. a.GetType("nunit.simple.Proto.MyMessage", true, true);
  584. a.GetType("nunit.simple.Proto.MyMessageList", true, true);
  585. }
  586. }
  587. }
  588. }