PageRenderTime 31ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/Test/UnitTest1.cs

#
C# | 3295 lines | 3172 code | 122 blank | 1 comment | 3 complexity | 762ed6f3efb63e16530ca66794c78f63 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. using Cs2hx;
  5. using Microsoft.VisualStudio.TestTools.UnitTesting;
  6. using Test;
  7. namespace UnitTestProject1
  8. {
  9. [TestClass]
  10. public class UnitTest1
  11. {
  12. [TestMethod]
  13. public void HelloWorld()
  14. {
  15. TestFramework.TestCode(MethodInfo.GetCurrentMethod().Name, @"
  16. using System;
  17. namespace Blargh
  18. {
  19. public static class Utilities
  20. {
  21. public static void SomeFunction()
  22. {
  23. Console.WriteLine(""Hello, World!"");
  24. }
  25. }
  26. }", @"
  27. package blargh;
  28. " + WriteImports.StandardImports + @"
  29. class Utilities
  30. {
  31. public static function SomeFunction():Void
  32. {
  33. system.Console.WriteLine(""Hello, World!"");
  34. }
  35. public function new()
  36. {
  37. }
  38. }");
  39. }
  40. [TestMethod]
  41. public void IfStatement()
  42. {
  43. TestFramework.TestCode(MethodInfo.GetCurrentMethod().Name, @"
  44. using System;
  45. namespace Blargh
  46. {
  47. public static class Utilities
  48. {
  49. public static void SomeFunction()
  50. {
  51. string notInitialized;
  52. int myNum = 0;
  53. notInitialized = ""InitMe!"";
  54. if (myNum > 4)
  55. myNum = 2;
  56. else if (notInitialized == ""asdf"")
  57. myNum = 1;
  58. else
  59. myNum = 999;
  60. Console.WriteLine(myNum == 999 ? ""One"" : ""Two"");
  61. }
  62. }
  63. }", @"
  64. package blargh;
  65. " + WriteImports.StandardImports + @"
  66. class Utilities
  67. {
  68. public static function SomeFunction():Void
  69. {
  70. var notInitialized:String;
  71. var myNum:Int = 0;
  72. notInitialized = ""InitMe!"";
  73. if (myNum > 4)
  74. {
  75. myNum = 2;
  76. }
  77. else if (notInitialized == ""asdf"")
  78. {
  79. myNum = 1;
  80. }
  81. else
  82. {
  83. myNum = 999;
  84. }
  85. system.Console.WriteLine(myNum == 999 ? ""One"" : ""Two"");
  86. }
  87. public function new()
  88. {
  89. }
  90. }");
  91. }
  92. [TestMethod]
  93. public void Loops()
  94. {
  95. TestFramework.TestCode(MethodInfo.GetCurrentMethod().Name, @"
  96. using System;
  97. namespace Blargh
  98. {
  99. public static class Utilities
  100. {
  101. public static void SomeFunction()
  102. {
  103. while (true)
  104. {
  105. Console.WriteLine(""hi"");
  106. break;
  107. }
  108. while (true)
  109. Console.WriteLine(""nobreak"");
  110. for (int i=0;i<50;i++)
  111. Console.WriteLine(i);
  112. do
  113. {
  114. Console.WriteLine(""Dowhile"");
  115. }
  116. while (false);
  117. }
  118. }
  119. }", @"
  120. package blargh;
  121. " + WriteImports.StandardImports + @"
  122. class Utilities
  123. {
  124. public static function SomeFunction():Void
  125. {
  126. while (true)
  127. {
  128. system.Console.WriteLine(""hi"");
  129. break;
  130. }
  131. while (true)
  132. {
  133. system.Console.WriteLine(""nobreak"");
  134. }
  135. { //for
  136. var i:Int = 0;
  137. while (i < 50)
  138. {
  139. system.Console.WriteLine_Int32(i);
  140. i++;
  141. }
  142. } //end for
  143. do
  144. {
  145. system.Console.WriteLine(""Dowhile"");
  146. }
  147. while (false);
  148. }
  149. public function new()
  150. {
  151. }
  152. }");
  153. }
  154. [TestMethod]
  155. public void EnumerateOnString()
  156. {
  157. TestFramework.TestCode(MethodInfo.GetCurrentMethod().Name, @"
  158. using System;
  159. using System.Linq;
  160. namespace Blargh
  161. {
  162. public class Foo
  163. {
  164. public Foo()
  165. {
  166. var s = ""hello"";
  167. var chars = s.ToCharArray();
  168. foreach(var c in s)
  169. {
  170. }
  171. s.Select(o => o);
  172. }
  173. }
  174. }", @"
  175. package blargh;
  176. " + WriteImports.StandardImports + @"
  177. class Foo
  178. {
  179. public function new()
  180. {
  181. var s:String = ""hello"";
  182. var chars:Array<Int> = system.Cs2Hx.ToCharArray(s);
  183. for (c in Cs2Hx.ToCharArray(s))
  184. {
  185. }
  186. system.linq.Enumerable.Select(Cs2Hx.ToCharArray(s), function (o:Int):Int { return o; } );
  187. }
  188. }");
  189. }
  190. [TestMethod]
  191. public void Indexing()
  192. {
  193. TestFramework.TestCode(MethodInfo.GetCurrentMethod().Name, @"
  194. using System;
  195. using System.Collections.Generic;
  196. namespace Blargh
  197. {
  198. public class Foo
  199. {
  200. public Foo()
  201. {
  202. var dict = new Dictionary<int, int>();
  203. dict[3] = 4;
  204. var i = dict[3];
  205. var array = new int[3];
  206. array[0] = 1;
  207. array[1]++;
  208. var str = ""hello"";
  209. var c = str[2];
  210. var list = new List<int>();
  211. i = list[0];
  212. }
  213. }
  214. }", @"
  215. package blargh;
  216. " + WriteImports.StandardImports + @"
  217. class Foo
  218. {
  219. public function new()
  220. {
  221. var dict:system.collections.generic.Dictionary<Int, Int> = new system.collections.generic.Dictionary<Int, Int>();
  222. dict.SetValue(3, 4);
  223. var i:Int = dict.GetValue_TKey(3);
  224. var array:Array<Int> = [ ];
  225. array[0] = 1;
  226. array[1]++;
  227. var str:String = ""hello"";
  228. var c:Int = str.charCodeAt(2);
  229. var list:Array<Int> = new Array<Int>();
  230. i = list[0];
  231. }
  232. }");
  233. }
  234. [TestMethod]
  235. [ExpectedException(typeof(AggregateException), "Events are not supported")]
  236. public void Events()
  237. {
  238. TestFramework.TestCode(MethodInfo.GetCurrentMethod().Name, @"
  239. using System;
  240. using System.Text;
  241. namespace Blargh
  242. {
  243. public class Foo
  244. {
  245. public static event Action Evt;
  246. public static event Action<int> EvtArg;
  247. public event Action NonStatic;
  248. public Foo()
  249. {
  250. Evt += Program_Evt;
  251. Evt -= Program_Evt;
  252. Evt();
  253. EvtArg += Program_EvtArg;
  254. EvtArg -= Program_EvtArg;
  255. EvtArg(3);
  256. }
  257. static void Program_Evt()
  258. {
  259. }
  260. static void Program_EvtArg(int i)
  261. {
  262. }
  263. }
  264. }", @"
  265. package blargh;
  266. " + WriteImports.StandardImports + @"
  267. class Foo
  268. {
  269. public static var Evt:CsEvent<(Void -> Void)>;
  270. public static var EvtArg:CsEvent<(Int -> Void)>;
  271. public var NonStatic:CsEvent<(Void -> Void)>;
  272. public function new()
  273. {
  274. NonStatic = new CsEvent<(Void -> Void)>();
  275. Evt.Add(Program_Evt);
  276. Evt.Remove(Program_Evt);
  277. Evt.Invoke0();
  278. EvtArg.Add(Program_EvtArg);
  279. EvtArg.Remove(Program_EvtArg);
  280. EvtArg.Invoke1(3);
  281. }
  282. static function Program_Evt():Void
  283. {
  284. }
  285. static function Program_EvtArg(i:Int):Void
  286. {
  287. }
  288. public static function cctor():Void
  289. {
  290. Evt = new CsEvent<(Void -> Void)>();
  291. EvtArg = new CsEvent<(Int -> Void)>();
  292. }
  293. }");
  294. }
  295. [TestMethod]
  296. public void NamedParameters()
  297. {
  298. TestFramework.TestCode(MethodInfo.GetCurrentMethod().Name, @"
  299. using System;
  300. using System.Text;
  301. namespace Blargh
  302. {
  303. public class Foo
  304. {
  305. public void Bar(int a, int b, int c, int d = 3)
  306. {
  307. }
  308. public void Bar2(int a, int b = 1, int c = 2, int d = 4)
  309. {
  310. }
  311. public Foo()
  312. {
  313. Bar(1,2,3,4);
  314. Bar(1,2,3);
  315. Bar(a: 1, b: 2, c: 3, d: 4);
  316. Bar(a: 1, b: 2, c: 3);
  317. Bar(a: 1, c: 3, b: 2);
  318. Bar(1, c: 3, b: 2);
  319. Bar(1, 2, c: 3, d: 4);
  320. Bar2(1, d: 5);
  321. }
  322. }
  323. }", @"
  324. package blargh;
  325. " + WriteImports.StandardImports + @"
  326. class Foo
  327. {
  328. public function Bar(a:Int, b:Int, c:Int, d:Int = 3):Void
  329. {
  330. }
  331. public function Bar2(a:Int, b:Int = 1, c:Int = 2, d:Int = 4):Void
  332. {
  333. }
  334. public function new()
  335. {
  336. Bar(1, 2, 3, 4);
  337. Bar(1, 2, 3);
  338. Bar(1, 2, 3, 4);
  339. Bar(1, 2, 3);
  340. Bar(1, 2, 3);
  341. Bar(1, 2, 3);
  342. Bar(1, 2, 3, 4);
  343. Bar2(1, 1, 2, 5);
  344. }
  345. }");
  346. }
  347. [TestMethod]
  348. public void NestedClasses()
  349. {
  350. TestFramework.TestCode(MethodInfo.GetCurrentMethod().Name, @"
  351. using System;
  352. using System.Text;
  353. namespace Blargh
  354. {
  355. public class Outer
  356. {
  357. public class Inner
  358. {
  359. public int InnerField;
  360. public Inner()
  361. {
  362. InnerField = 0;
  363. }
  364. }
  365. public Outer()
  366. {
  367. var i = new Inner();
  368. i.InnerField = 4;
  369. }
  370. }
  371. }", new[] { @"
  372. package blargh;
  373. " + WriteImports.StandardImports + @"
  374. class Outer
  375. {
  376. public function new()
  377. {
  378. var i:blargh.Outer_Inner = new blargh.Outer_Inner();
  379. i.InnerField = 4;
  380. }
  381. }",
  382. @"
  383. package blargh;
  384. " + WriteImports.StandardImports + @"
  385. class Outer_Inner
  386. {
  387. public var InnerField:Int;
  388. public function new()
  389. {
  390. InnerField = 0;
  391. InnerField = 0;
  392. }
  393. }"
  394. });
  395. //We have to live with generating two InnerField = 0 lines. One is directly copied verbatim from the source, and the other is generated by our code that ensures that all fields are initialized.
  396. }
  397. [TestMethod]
  398. public void AnonymousTypes()
  399. {
  400. TestFramework.TestCode(MethodInfo.GetCurrentMethod().Name, @"
  401. using System;
  402. using System.Text;
  403. namespace Blargh
  404. {
  405. public class Foo
  406. {
  407. public Foo()
  408. {
  409. var i = new { Field1 = 3, Field2 = new StringBuilder() };
  410. Console.WriteLine(i.Field1);
  411. }
  412. }
  413. }", new[] { @"
  414. package blargh;
  415. " + WriteImports.StandardImports + @"
  416. class Foo
  417. {
  418. public function new()
  419. {
  420. var i:Anon_Field1_Int__Field2_system_text_StringBuilder = new Anon_Field1_Int__Field2_system_text_StringBuilder(3, new system.text.StringBuilder());
  421. system.Console.WriteLine_Int32(i.Field1);
  422. }
  423. }",
  424. @"
  425. package anonymoustypes;
  426. " + WriteImports.StandardImports + @"
  427. class Anon_Field1_Int__Field2_system_text_StringBuilder
  428. {
  429. public var Field1:Int;
  430. public var Field2:system.text.StringBuilder;
  431. public function new(Field1:Int, Field2:system.text.StringBuilder)
  432. {
  433. this.Field1 = Field1;
  434. this.Field2 = Field2;
  435. }
  436. }"
  437. });
  438. }
  439. [TestMethod]
  440. public void AttributesAreIgnored()
  441. {
  442. TestFramework.TestCode(MethodInfo.GetCurrentMethod().Name, @"
  443. using System;
  444. using Shared;
  445. #if !CS2HX
  446. namespace Shared
  447. {
  448. public class TestingAttribute : Attribute
  449. {
  450. }
  451. }
  452. #endif
  453. namespace Blargh
  454. {
  455. public class Foo
  456. {
  457. [Testing]
  458. public string Str;
  459. }
  460. }", @"
  461. package blargh;
  462. " + WriteImports.StandardImports + @"
  463. class Foo
  464. {
  465. public var Str:String;
  466. public function new()
  467. {
  468. }
  469. }");
  470. }
  471. [TestMethod]
  472. public void PreprocessorDirectives()
  473. {
  474. TestFramework.TestCode(MethodInfo.GetCurrentMethod().Name, @"
  475. using System;
  476. namespace Blargh
  477. {
  478. public class SomeClass
  479. {
  480. #if CS2HX
  481. public static var Variable:Int;
  482. #endif
  483. public SomeClass()
  484. {
  485. #if CS2HX
  486. Console.WriteLine(""cs2hx1"");
  487. #else
  488. Console.WriteLine(""not1"");
  489. #endif
  490. #if CS2HX //comment
  491. Console.WriteLine(""cs2hx2"");
  492. #else
  493. Console.WriteLine(""not2"");
  494. #if nope
  495. Console.WriteLine(""not3"");
  496. #endif
  497. #endif
  498. Console.WriteLine(""outside"");
  499. #if CS2HX
  500. Console.WriteLine(""cs2hx3"");
  501. #endif
  502. }
  503. }
  504. }", @"
  505. package blargh;
  506. " + WriteImports.StandardImports + @"
  507. class SomeClass
  508. {
  509. public static var Variable:Int;
  510. public function new()
  511. {
  512. Console.WriteLine(""cs2hx1"");
  513. Console.WriteLine(""cs2hx2"");
  514. system.Console.WriteLine(""outside"");
  515. Console.WriteLine(""cs2hx3"");
  516. }
  517. }");
  518. }
  519. [TestMethod]
  520. public void OfType()
  521. {
  522. TestFramework.TestCode(MethodInfo.GetCurrentMethod().Name, @"
  523. using System.Text;
  524. using System.Linq;
  525. namespace Blargh
  526. {
  527. public class SomeClass
  528. {
  529. public SomeClass()
  530. {
  531. var a = new[] { 1,2,3 };
  532. var b = a.OfType<StringBuilder>().ToList();
  533. }
  534. }
  535. }", @"
  536. package blargh;
  537. " + WriteImports.StandardImports + @"
  538. class SomeClass
  539. {
  540. public function new()
  541. {
  542. var a:Array<Int> = [ 1, 2, 3 ];
  543. var b:Array<system.text.StringBuilder> = system.linq.Enumerable.ToList(system.linq.Enumerable.OfType(a, system.text.StringBuilder));
  544. }
  545. }");
  546. }
  547. [TestMethod]
  548. public void GlobalKeyword()
  549. {
  550. TestFramework.TestCode(MethodInfo.GetCurrentMethod().Name, @"
  551. using System;
  552. namespace Blargh
  553. {
  554. public class SomeClass
  555. {
  556. public SomeClass()
  557. {
  558. global::Blargh.SomeClass c = null;
  559. }
  560. }
  561. }", @"
  562. package blargh;
  563. " + WriteImports.StandardImports + @"
  564. class SomeClass
  565. {
  566. public function new()
  567. {
  568. var c:blargh.SomeClass = null;
  569. }
  570. }");
  571. }
  572. [TestMethod]
  573. public void DefaultParameter()
  574. {
  575. TestFramework.TestCode(MethodInfo.GetCurrentMethod().Name, @"
  576. using System;
  577. namespace Blargh
  578. {
  579. public class SomeClass
  580. {
  581. public void Foo(int i1, int i2 = 4, string s1 = ""hi"")
  582. {
  583. }
  584. public SomeClass(int i3 = 9)
  585. {
  586. Foo(4);
  587. Foo(5, 6);
  588. Foo(6, 7, ""eight"");
  589. }
  590. }
  591. }", @"
  592. package blargh;
  593. " + WriteImports.StandardImports + @"
  594. class SomeClass
  595. {
  596. public function Foo(i1:Int, i2:Int = 4, s1:String = ""hi""):Void
  597. {
  598. }
  599. public function new(i3:Int = 9)
  600. {
  601. Foo(4);
  602. Foo(5, 6);
  603. Foo(6, 7, ""eight"");
  604. }
  605. }");
  606. }
  607. [TestMethod]
  608. public void ForStatementWithNoCondition()
  609. {
  610. TestFramework.TestCode(MethodInfo.GetCurrentMethod().Name, @"
  611. using System;
  612. namespace Blargh
  613. {
  614. public static class Utilities
  615. {
  616. public static void SomeFunction()
  617. {
  618. for(;;)
  619. {
  620. Console.WriteLine(""Hello, World!"");
  621. }
  622. }
  623. }
  624. }", @"
  625. package blargh;
  626. " + WriteImports.StandardImports + @"
  627. class Utilities
  628. {
  629. public static function SomeFunction():Void
  630. {
  631. { //for
  632. while (true)
  633. {
  634. system.Console.WriteLine(""Hello, World!"");
  635. }
  636. } //end for
  637. }
  638. public function new()
  639. {
  640. }
  641. }");
  642. }
  643. [TestMethod]
  644. public void AutomaticProperties()
  645. {
  646. TestFramework.TestCode(MethodInfo.GetCurrentMethod().Name, @"
  647. using System;
  648. namespace Blargh
  649. {
  650. class Box
  651. {
  652. public float Width
  653. {
  654. get;
  655. set;
  656. }
  657. }
  658. }", @"
  659. package blargh;
  660. " + WriteImports.StandardImports + @"
  661. class Box
  662. {
  663. public var Width:Float;
  664. public function new()
  665. {
  666. }
  667. }");
  668. }
  669. [TestMethod]
  670. public void GenericClass()
  671. {
  672. TestFramework.TestCode(MethodInfo.GetCurrentMethod().Name, @"
  673. using System;
  674. using System.Collections.Generic;
  675. namespace Blargh
  676. {
  677. public class KeyValueList<K, V> : IEquatable<K>
  678. {
  679. private List<KeyValuePair<K, V>> _list = new List<KeyValuePair<K, V>>();
  680. public void Add(K key, V value)
  681. {
  682. this._list.Add(new KeyValuePair<K, V>(key, value));
  683. }
  684. public void Insert(int index, K key, V value)
  685. {
  686. _list.Insert(index, new KeyValuePair<K, V>(key, value));
  687. }
  688. public void Clear()
  689. {
  690. _list.Clear();
  691. var castTest = (K)MemberwiseClone();
  692. }
  693. public void RemoveAt(int index)
  694. {
  695. _list.RemoveAt(index);
  696. }
  697. public bool Equals(K other)
  698. {
  699. throw new NotImplementedException();
  700. }
  701. }
  702. }", @"
  703. package blargh;
  704. " + WriteImports.StandardImports + @"
  705. class KeyValueList<K, V> implements system.IEquatable<K>
  706. {
  707. private var _list:Array<system.collections.generic.KeyValuePair<K, V>>;
  708. public function Add(key:K, value:V):Void
  709. {
  710. this._list.push(new system.collections.generic.KeyValuePair<K, V>(key, value));
  711. }
  712. public function Insert(index:Int, key:K, value:V):Void
  713. {
  714. _list.insert(index, new system.collections.generic.KeyValuePair<K, V>(key, value));
  715. }
  716. public function Clear():Void
  717. {
  718. system.Cs2Hx.Clear(_list);
  719. var castTest:K = MemberwiseClone();
  720. }
  721. public function RemoveAt(index:Int):Void
  722. {
  723. _list.splice(index, 1);
  724. }
  725. public function Equals(other:K):Bool
  726. {
  727. return throw new system.NotImplementedException();
  728. }
  729. public function new()
  730. {
  731. _list = new Array<system.collections.generic.KeyValuePair<K, V>>();
  732. }
  733. }");
  734. }
  735. [TestMethod]
  736. public void ByteArrays()
  737. {
  738. TestFramework.TestCode(MethodInfo.GetCurrentMethod().Name, @"
  739. using System;
  740. namespace Blargh
  741. {
  742. public static class Utilities
  743. {
  744. public static void SomeFunction()
  745. {
  746. byte[] b1 = new byte[4];
  747. byte[] b2 = new byte[Foo()];
  748. }
  749. static int Foo() { return 4; }
  750. }
  751. }", @"
  752. package blargh;
  753. " + WriteImports.StandardImports + @"
  754. class Utilities
  755. {
  756. public static function SomeFunction():Void
  757. {
  758. var b1:haxe.io.Bytes = haxe.io.Bytes.alloc(4);
  759. var b2:haxe.io.Bytes = haxe.io.Bytes.alloc(Foo());
  760. }
  761. static function Foo():Int
  762. {
  763. return 4;
  764. }
  765. public function new()
  766. {
  767. }
  768. }");
  769. }
  770. [TestMethod]
  771. [ExpectedException(typeof(AggregateException), "Cannot use \"continue\" in a \"for\" loop")]
  772. public void CannotUseContinueInForLoop()
  773. {
  774. TestFramework.TestCode(MethodInfo.GetCurrentMethod().Name, @"
  775. using System;
  776. namespace Blargh
  777. {
  778. public class SomeClass
  779. {
  780. public void SomeMethod()
  781. {
  782. for(int i=0;i<40;i++)
  783. {
  784. if (i % 3 == 0)
  785. continue;
  786. Console.WriteLine(i);
  787. }
  788. }
  789. }
  790. }", "");
  791. }
  792. [TestMethod]
  793. public void ConstructorCallsBaseConstructor()
  794. {
  795. var cs = @"
  796. using System;
  797. namespace Blargh
  798. {
  799. public class Top
  800. {
  801. public Top(int i) { }
  802. }
  803. public class Derived : Top
  804. {
  805. public Derived() : base(4) { }
  806. }
  807. }";
  808. var haxe1 = @"
  809. package blargh;
  810. " + WriteImports.StandardImports + @"
  811. class Top
  812. {
  813. public function new(i:Int)
  814. {
  815. }
  816. }";
  817. var haxe2 = @"
  818. package blargh;
  819. " + WriteImports.StandardImports + @"
  820. class Derived extends blargh.Top
  821. {
  822. public function new()
  823. {
  824. super(4);
  825. }
  826. }";
  827. TestFramework.TestCode(MethodInfo.GetCurrentMethod().Name, cs, new string[] { haxe1, haxe2 });
  828. }
  829. [TestMethod]
  830. public void ImportStatements()
  831. {
  832. var cSharp = @"
  833. using System;
  834. namespace SomeClassNamespace
  835. {
  836. using SomeInterfaceNamespace;
  837. public class SomeClass : ISomeInterface, IDisposable
  838. {
  839. public void SomeClassMethod() { }
  840. public void Dispose() { }
  841. }
  842. }
  843. namespace SomeInterfaceNamespace
  844. {
  845. public interface ISomeInterface
  846. {
  847. }
  848. public class UnusedClass
  849. {
  850. }
  851. }";
  852. var haxe1 = @"
  853. package someclassnamespace;
  854. " + WriteImports.StandardImports + @"
  855. class SomeClass implements someinterfacenamespace.ISomeInterface implements system.IDisposable
  856. {
  857. public function SomeClassMethod():Void
  858. {
  859. }
  860. public function Dispose():Void
  861. {
  862. }
  863. public function new()
  864. {
  865. }
  866. }";
  867. var haxe2 = @"
  868. package someinterfacenamespace;
  869. " + WriteImports.StandardImports + @"
  870. interface ISomeInterface
  871. {
  872. }";
  873. var haxe3 = @"
  874. package someinterfacenamespace;
  875. " + WriteImports.StandardImports + @"
  876. class UnusedClass
  877. {
  878. public function new()
  879. {
  880. }
  881. }";
  882. TestFramework.TestCode(MethodInfo.GetCurrentMethod().Name, cSharp, new string[] { haxe1, haxe2, haxe3 });
  883. }
  884. [TestMethod]
  885. public void TypeInference()
  886. {
  887. TestFramework.TestCode(MethodInfo.GetCurrentMethod().Name, @"
  888. using System;
  889. using System.Text;
  890. namespace Blargh
  891. {
  892. public class Box
  893. {
  894. public static void Main()
  895. {
  896. SomeFunction((_, o) => o + 1);
  897. }
  898. public static int SomeFunction(Func<StringBuilder, int, int> doWork)
  899. {
  900. var value = doWork(null, 3);
  901. return value;
  902. }
  903. public Box(Action<DateTime> doWork)
  904. {
  905. }
  906. }
  907. }", @"
  908. package blargh;
  909. " + WriteImports.StandardImports + @"
  910. class Box
  911. {
  912. public static function Main():Void
  913. {
  914. SomeFunction(function (_:system.text.StringBuilder, o:Int):Int { return o + 1; } );
  915. }
  916. public static function SomeFunction(doWork:(system.text.StringBuilder -> Int -> Int)):Int
  917. {
  918. var value:Int = doWork(null, 3);
  919. return value;
  920. }
  921. public function new(doWork:(system.DateTime -> Void))
  922. {
  923. }
  924. }
  925. ");
  926. }
  927. [TestMethod]
  928. [ExpectedException(typeof(AggregateException), "C# 3.5 object initialization syntax is not supported")]
  929. public void ObjectInitilization()
  930. {
  931. TestFramework.TestCode(MethodInfo.GetCurrentMethod().Name, @"
  932. namespace Blargh
  933. {
  934. public static class Utilities
  935. {
  936. public static void SomeFunction()
  937. {
  938. var usingMe = new SomeUsingType()
  939. {
  940. Init1 = 0
  941. };
  942. }
  943. }
  944. }", "");
  945. }
  946. [TestMethod]
  947. [ExpectedException(typeof(AggregateException), "You cannot return from within a using block")]
  948. public void CannotReturnFromUsing()
  949. {
  950. TestFramework.TestCode(MethodInfo.GetCurrentMethod().Name, @"
  951. namespace Blargh
  952. {
  953. public static class Utilities
  954. {
  955. public static int SomeFunction()
  956. {
  957. var usingMe = new SomeUsingType();
  958. using (usingMe)
  959. {
  960. return 4;
  961. }
  962. }
  963. }
  964. }", "");
  965. }
  966. [TestMethod]
  967. public void UsingStatement()
  968. {
  969. TestFramework.TestCode(MethodInfo.GetCurrentMethod().Name, @"
  970. using System;
  971. using System.IO;
  972. namespace Blargh
  973. {
  974. public static class Utilities
  975. {
  976. public static void SomeFunction()
  977. {
  978. var usingMe = new MemoryStream();
  979. using (usingMe)
  980. {
  981. Console.WriteLine(""In using"");
  982. }
  983. }
  984. }
  985. }", @"
  986. package blargh;
  987. " + WriteImports.StandardImports + @"
  988. class Utilities
  989. {
  990. public static function SomeFunction():Void
  991. {
  992. var usingMe:system.io.MemoryStream = new system.io.MemoryStream();
  993. var __disposed_usingMe:Bool = false;
  994. try
  995. {
  996. system.Console.WriteLine(""In using"");
  997. __disposed_usingMe = true;
  998. usingMe.Dispose();
  999. }
  1000. catch (__catch_usingMe:Dynamic)
  1001. {
  1002. if (!__disposed_usingMe)
  1003. usingMe.Dispose();
  1004. throw __catch_usingMe;
  1005. }
  1006. }
  1007. public function new()
  1008. {
  1009. }
  1010. }
  1011. ");
  1012. }
  1013. [TestMethod]
  1014. public void Math()
  1015. {
  1016. TestFramework.TestCode(MethodInfo.GetCurrentMethod().Name, @"
  1017. using System;
  1018. namespace Blargh
  1019. {
  1020. public static class Utilities
  1021. {
  1022. public static void SomeFunction()
  1023. {
  1024. int i = 3;
  1025. i += 4;
  1026. i -= 3;
  1027. ++i;
  1028. i++;
  1029. i--;
  1030. --i;
  1031. i *= 4;
  1032. i %= 3;
  1033. i = i + 1;
  1034. i = i % 3;
  1035. i = i - 4;
  1036. i = i * 100;
  1037. double f = i / 3f;
  1038. int hex = 0x00ff;
  1039. i = (int)f;
  1040. var z = (i & hex) == 5;
  1041. var x = (int)(i / 3);
  1042. }
  1043. }
  1044. }", @"
  1045. package blargh;
  1046. " + WriteImports.StandardImports + @"
  1047. class Utilities
  1048. {
  1049. public static function SomeFunction():Void
  1050. {
  1051. var i:Int = 3;
  1052. i += 4;
  1053. i -= 3;
  1054. ++i;
  1055. i++;
  1056. i--;
  1057. --i;
  1058. i *= 4;
  1059. i %= 3;
  1060. i = i + 1;
  1061. i = i % 3;
  1062. i = i - 4;
  1063. i = i * 100;
  1064. var f:Float = i / 3;
  1065. var hex:Int = 0x00ff;
  1066. i = Std.int(f);
  1067. var z:Bool = (i & hex) == 5;
  1068. var x:Int = Std.int(i / 3);
  1069. }
  1070. public function new()
  1071. {
  1072. }
  1073. }");
  1074. }
  1075. [TestMethod]
  1076. public void Delegates()
  1077. {
  1078. TestFramework.TestCode(MethodInfo.GetCurrentMethod().Name, @"
  1079. using System;
  1080. namespace Blargh
  1081. {
  1082. public delegate int NamespaceDlg();
  1083. public delegate T TemplatedDelegate<T>(T arg, int arg2);
  1084. public static class Utilities
  1085. {
  1086. public static Action StaticAction;
  1087. public delegate int GetMahNumber(int arg);
  1088. public static void SomeFunction(GetMahNumber getit, NamespaceDlg getitnow, TemplatedDelegate<float> unused)
  1089. {
  1090. Console.WriteLine(getit(getitnow()));
  1091. var a = new[] { getitnow };
  1092. a[0]();
  1093. StaticAction();
  1094. Utilities.StaticAction();
  1095. Blargh.Utilities.StaticAction();
  1096. }
  1097. }
  1098. }", @"
  1099. package blargh;
  1100. " + WriteImports.StandardImports + @"
  1101. class Utilities
  1102. {
  1103. public static var StaticAction:(Void -> Void);
  1104. public static function SomeFunction(getit:(Int -> Int), getitnow:(Void -> Int), unused:(Float -> Int -> Float)):Void
  1105. {
  1106. system.Console.WriteLine_Int32(getit(getitnow()));
  1107. var a:Array<(Void -> Int)> = [ getitnow ];
  1108. a[0]();
  1109. StaticAction();
  1110. blargh.Utilities.StaticAction();
  1111. blargh.Utilities.StaticAction();
  1112. }
  1113. public function new()
  1114. {
  1115. }
  1116. }");
  1117. }
  1118. [TestMethod]
  1119. public void TypeStatics()
  1120. {
  1121. TestFramework.TestCode(MethodInfo.GetCurrentMethod().Name, @"
  1122. using System;
  1123. namespace Blargh
  1124. {
  1125. public static class Utilities
  1126. {
  1127. static int Foo;
  1128. public static void SomeFunction()
  1129. {
  1130. Blargh.Utilities.Foo = 4;
  1131. Console.WriteLine(int.MaxValue);
  1132. Console.WriteLine(int.MinValue);
  1133. string s = ""123"";
  1134. Console.WriteLine(int.Parse(s) + 1);
  1135. float.Parse(s);
  1136. double.Parse(s);
  1137. }
  1138. }
  1139. }", @"
  1140. package blargh;
  1141. " + WriteImports.StandardImports + @"
  1142. class Utilities
  1143. {
  1144. static var Foo:Int;
  1145. public static function SomeFunction():Void
  1146. {
  1147. blargh.Utilities.Foo = 4;
  1148. system.Console.WriteLine_Int32(2147483647);
  1149. system.Console.WriteLine_Int32(-2147483648);
  1150. var s:String = ""123"";
  1151. system.Console.WriteLine_Int32(Std.parseInt(s) + 1);
  1152. Std.parseFloat(s);
  1153. Std.parseFloat(s);
  1154. }
  1155. public static function cctor():Void
  1156. {
  1157. Foo = 0;
  1158. }
  1159. public function new()
  1160. {
  1161. }
  1162. }");
  1163. }
  1164. [TestMethod]
  1165. public void DictionaryAndHashSet()
  1166. {
  1167. TestFramework.TestCode(MethodInfo.GetCurrentMethod().Name, @"
  1168. using System;
  1169. using System.Linq;
  1170. using System.Collections.Generic;
  1171. namespace Blargh
  1172. {
  1173. public static class Utilities
  1174. {
  1175. public static void SomeFunction()
  1176. {
  1177. Dictionary<int, int> dict = new Dictionary<int, int>();
  1178. dict.Add(4, 3);
  1179. Console.WriteLine(dict[4]);
  1180. Console.WriteLine(dict.ContainsKey(8));
  1181. dict.Remove(4);
  1182. foreach(int key in dict.Keys)
  1183. Console.WriteLine(key);
  1184. foreach(int val in dict.Values)
  1185. Console.WriteLine(val);
  1186. foreach(var kv in dict)
  1187. Console.WriteLine(kv.Key + "" "" + kv.Value);
  1188. var dict2 = dict.ToDictionary(o => o.Key, o => o.Value);
  1189. var vals = dict.Values;
  1190. HashSet<int> hash = new HashSet<int>();
  1191. hash.Add(999);
  1192. Console.WriteLine(hash.Contains(999));
  1193. hash.Remove(999);
  1194. Console.WriteLine(hash.Contains(999));
  1195. foreach(int hashItem in hash)
  1196. Console.WriteLine(hashItem);
  1197. var z = hash.Select(o => 3).ToArray();
  1198. var g = hash.GroupBy(o => o).Select(o => o.Count()).Min();
  1199. }
  1200. }
  1201. }", @"
  1202. package blargh;
  1203. " + WriteImports.StandardImports + @"
  1204. class Utilities
  1205. {
  1206. public static function SomeFunction():Void
  1207. {
  1208. var dict:system.collections.generic.Dictionary<Int, Int> = new system.collections.generic.Dictionary<Int, Int>();
  1209. dict.Add(4, 3);
  1210. system.Console.WriteLine_Int32(dict.GetValue_TKey(4));
  1211. system.Console.WriteLine_Boolean(dict.ContainsKey(8));
  1212. dict.Remove(4);
  1213. for (key in dict.Keys)
  1214. {
  1215. system.Console.WriteLine_Int32(key);
  1216. }
  1217. for (val in dict.Values)
  1218. {
  1219. system.Console.WriteLine_Int32(val);
  1220. }
  1221. for (kv in dict.GetEnumerator())
  1222. {
  1223. system.Console.WriteLine(kv.Key + "" "" + kv.Value);
  1224. }
  1225. var dict2:system.collections.generic.Dictionary<Int, Int> = system.linq.Enumerable.ToDictionary(Cs2Hx.GetEnumeratorNullCheck(dict), function (o:system.collections.generic.KeyValuePair<Int, Int>):Int { return o.Key; } , function (o:system.collections.generic.KeyValuePair<Int, Int>):Int { return o.Value; } );
  1226. var vals:Array<Int> = dict.Values;
  1227. var hash:system.collections.generic.HashSet<Int> = new system.collections.generic.HashSet<Int>();
  1228. hash.Add(999);
  1229. system.Console.WriteLine_Boolean(hash.Contains(999));
  1230. hash.Remove(999);
  1231. system.Console.WriteLine_Boolean(hash.Contains(999));
  1232. for (hashItem in hash.GetEnumerator())
  1233. {
  1234. system.Console.WriteLine_Int32(hashItem);
  1235. }
  1236. var z:Array<Int> = system.linq.Enumerable.ToArray(system.linq.Enumerable.Select(Cs2Hx.GetEnumeratorNullCheck(hash), function (o:Int):Int { return 3; } ));
  1237. var g:Int = system.linq.Enumerable.Min(system.linq.Enumerable.Select(system.linq.Enumerable.GroupBy(Cs2Hx.GetEnumeratorNullCheck(hash), function (o:Int):Int { return o; } ), function (o:system.linq.IGrouping<Int, Int>):Int { return system.linq.Enumerable.Count(Cs2Hx.GetEnumeratorNullCheck(o)); } ));
  1238. }
  1239. public function new()
  1240. {
  1241. }
  1242. }");
  1243. }
  1244. [TestMethod]
  1245. public void NullableTypes()
  1246. {
  1247. TestFramework.TestCode(MethodInfo.GetCurrentMethod().Name, @"
  1248. using System;
  1249. namespace Blargh
  1250. {
  1251. public static class Utilities
  1252. {
  1253. public static void SomeFunction()
  1254. {
  1255. int? nullableInt = new Nullable<int>();
  1256. double d = 3;
  1257. var cond = nullableInt.HasValue ? (float?)null : ((float)d);
  1258. Console.WriteLine(nullableInt.HasValue);
  1259. int? withValue = new Nullable<int>(8);
  1260. Console.WriteLine(withValue.Value);
  1261. int? implicitNull = null;
  1262. implicitNull = null;
  1263. int? implicitValue = 5;
  1264. implicitValue = 8;
  1265. Foo(3);
  1266. int? n = (int?)null;
  1267. }
  1268. public static int? Foo(int? i)
  1269. {
  1270. return 4;
  1271. }
  1272. }
  1273. }", @"
  1274. package blargh;
  1275. " + WriteImports.StandardImports + @"
  1276. class Utilities
  1277. {
  1278. public static function SomeFunction():Void
  1279. {
  1280. var nullableInt:Nullable_Int = new Nullable_Int();
  1281. var d:Float = 3;
  1282. var cond:Nullable_Float = nullableInt.HasValue ? new Nullable_Float() : (new Nullable_Float(d));
  1283. system.Console.WriteLine_Boolean(nullableInt.HasValue);
  1284. var withValue:Nullable_Int = new Nullable_Int(8);
  1285. system.Console.WriteLine_Int32(withValue.Value);
  1286. var implicitNull:Nullable_Int = new Nullable_Int();
  1287. implicitNull = new Nullable_Int();
  1288. var implicitValue:Nullable_Int = new Nullable_Int(5);
  1289. implicitValue = new Nullable_Int(8);
  1290. Foo(new Nullable_Int(3));
  1291. var n:Nullable_Int = new Nullable_Int();
  1292. }
  1293. public static function Foo(i:Nullable_Int):Nullable_Int
  1294. {
  1295. return new Nullable_Int(4);
  1296. }
  1297. public function new()
  1298. {
  1299. }
  1300. }");
  1301. }
  1302. [TestMethod]
  1303. public void NullableDefaults()
  1304. {
  1305. TestFramework.TestCode(MethodInfo.GetCurrentMethod().Name, @"
  1306. using System;
  1307. namespace Blargh
  1308. {
  1309. public static class Utilities
  1310. {
  1311. public static void SomeFunction(int? f = 3, float? s = null)
  1312. {
  1313. Console.WriteLine(""Hello, World!"");
  1314. }
  1315. }
  1316. }", @"
  1317. package blargh;
  1318. " + WriteImports.StandardImports + @"
  1319. class Utilities
  1320. {
  1321. public static function SomeFunction(f:Nullable_Int = null, s:Nullable_Float = null):Void
  1322. {
  1323. if (f == null)
  1324. f = new Nullable_Int(3);
  1325. if (s == null)
  1326. s = new Nullable_Float();
  1327. system.Console.WriteLine(""Hello, World!"");
  1328. }
  1329. public function new()
  1330. {
  1331. }
  1332. }");
  1333. }
  1334. [TestMethod]
  1335. [ExpectedException(typeof(AggregateException), "When using nullable types, you must use the .Value and .HasValue properties instead of the object directly")]
  1336. public void MustUseNullableProperties()
  1337. {
  1338. TestFramework.TestCode(MethodInfo.GetCurrentMethod().Name, @"
  1339. namespace Blargh
  1340. {
  1341. public static class Utilities
  1342. {
  1343. public static void SomeFunction()
  1344. {
  1345. int? i = 5;
  1346. if (i == null)
  1347. {
  1348. }
  1349. }
  1350. }
  1351. }", "");
  1352. }
  1353. [TestMethod]
  1354. public void Enums()
  1355. {
  1356. TestFramework.TestCode(MethodInfo.GetCurrentMethod().Name, new string[] { @"
  1357. using System;
  1358. namespace Blargh
  1359. {
  1360. public enum MostlyNumbered
  1361. {
  1362. One = 1,
  1363. Two = 2,
  1364. Three = 3,
  1365. Unnumbered,
  1366. SomethingElse = 50
  1367. }
  1368. public enum UnNumbered
  1369. {
  1370. One, Two, Three
  1371. }
  1372. class Clazz
  1373. {
  1374. public static void Methodz()
  1375. {
  1376. var f = MostlyNumbered.One;
  1377. var arr = new UnNumbered[] { UnNumbered.One, UnNumbered.Two, UnNumbered.Three };
  1378. var i = (int)f;
  1379. var e = (MostlyNumbered)Enum.Parse(typeof(MostlyNumbered), ""One"");
  1380. var s = e.ToString();
  1381. s = e + ""asdf"";
  1382. s = ""asdf"" + e;
  1383. var vals = Enum.GetValues(typeof(MostlyNumbered));
  1384. }
  1385. }
  1386. }" }, new string[] { @"
  1387. package blargh;
  1388. " + WriteImports.StandardImports + @"
  1389. class MostlyNumbered
  1390. {
  1391. public static inline var One:Int = 1;
  1392. public static inline var Two:Int = 2;
  1393. public static inline var Three:Int = 3;
  1394. public static inline var Unnumbered:Int = 4;
  1395. public static inline var SomethingElse:Int = 50;
  1396. public static function ToString(e:Int):String
  1397. {
  1398. switch (e)
  1399. {
  1400. case 1: return ""One"";
  1401. case 2: return ""Two"";
  1402. case 3: return ""Three"";
  1403. case 4: return ""Unnumbered"";
  1404. case 50: return ""SomethingElse"";
  1405. default: return Std.string(e);
  1406. }
  1407. }
  1408. public static function Parse(s:String):Int
  1409. {
  1410. switch (s)
  1411. {
  1412. case ""One"": return 1;
  1413. case ""Two"": return 2;
  1414. case ""Three"": return 3;
  1415. case ""Unnumbered"": return 4;
  1416. case ""SomethingElse"": return 50;
  1417. default: throw new InvalidOperationException(s);
  1418. }
  1419. }
  1420. public static function Values():Array<Int>
  1421. {
  1422. return [1, 2, 3, 4, 50];
  1423. }
  1424. }", @"
  1425. package blargh;
  1426. " + WriteImports.StandardImports + @"
  1427. class UnNumbered
  1428. {
  1429. public static inline var One:Int = 0;
  1430. public static inline var Two:Int = 1;
  1431. public static inline var Three:Int = 2;
  1432. public static function ToString(e:Int):String
  1433. {
  1434. switch (e)
  1435. {
  1436. case 0: return ""One"";
  1437. case 1: return ""Two"";
  1438. case 2: return ""Three"";
  1439. default: return Std.string(e);
  1440. }
  1441. }
  1442. public static function Parse(s:String):Int
  1443. {
  1444. switch (s)
  1445. {
  1446. case ""One"": return 0;
  1447. case ""Two"": return 1;
  1448. case ""Three"": return 2;
  1449. default: throw new InvalidOperationException(s);
  1450. }
  1451. }
  1452. public static function Values():Array<Int>
  1453. {
  1454. return [0, 1, 2];
  1455. }
  1456. }", @"
  1457. package blargh;
  1458. " + WriteImports.StandardImports + @"
  1459. class Clazz
  1460. {
  1461. public static function Methodz():Void
  1462. {
  1463. var f:Int = blargh.MostlyNumbered.One;
  1464. var arr:Array<Int> = [ blargh.UnNumbered.One, blargh.UnNumbered.Two, blargh.UnNumbered.Three ];
  1465. var i:Int = f;
  1466. var e:Int = blargh.MostlyNumbered.Parse(""One"");
  1467. var s:String = blargh.MostlyNumbered.ToString(e);
  1468. s = blargh.MostlyNumbered.ToString(e) + ""asdf"";
  1469. s = ""asdf"" + blargh.MostlyNumbered.ToString(e);
  1470. var vals = blargh.MostlyNumbered.Values();
  1471. }
  1472. public function new()
  1473. {
  1474. }
  1475. }"});
  1476. }
  1477. [TestMethod]
  1478. public void NestedEnum()
  1479. {
  1480. TestFramework.TestCode(MethodInfo.GetCurrentMethod().Name, new string[] { @"
  1481. namespace Blargh
  1482. {
  1483. class Foo
  1484. {
  1485. public enum TestEnum
  1486. {
  1487. One, Two, Three
  1488. }
  1489. public Foo()
  1490. {
  1491. var i = TestEnum.One;
  1492. i.ToString();
  1493. }
  1494. }
  1495. }" }, new string[] { @"
  1496. package blargh;
  1497. " + WriteImports.StandardImports + @"
  1498. class Foo
  1499. {
  1500. public function new()
  1501. {
  1502. var i:Int = blargh.Foo_TestEnum.One;
  1503. blargh.Foo_TestEnum.ToString(i);
  1504. }
  1505. }", @"
  1506. package blargh;
  1507. " + WriteImports.StandardImports + @"
  1508. class Foo_TestEnum
  1509. {
  1510. public static inline var One:Int = 0;
  1511. public static inline var Two:Int = 1;
  1512. public static inline var Three:Int = 2;
  1513. public static function ToString(e:Int):String
  1514. {
  1515. switch (e)
  1516. {
  1517. case 0: return ""One"";
  1518. case 1: return ""Two"";
  1519. case 2: return ""Three"";
  1520. default: return Std.string(e);
  1521. }
  1522. }
  1523. public static function Parse(s:String):Int
  1524. {
  1525. switch (s)
  1526. {
  1527. case ""One"": return 0;
  1528. case ""Two"": return 1;
  1529. case ""Three"": return 2;
  1530. default: throw new InvalidOperationException(s);
  1531. }
  1532. }
  1533. public static function Values():Array<Int>
  1534. {
  1535. return [0, 1, 2];
  1536. }
  1537. }" });
  1538. }
  1539. [TestMethod]
  1540. public void SwitchStatement()
  1541. {
  1542. TestFramework.TestCode(MethodInfo.GetCurrentMethod().Name, @"
  1543. using System;
  1544. namespace Blargh
  1545. {
  1546. public static class Utilities
  1547. {
  1548. public static void SomeFunction()
  1549. {
  1550. string s = ""Blah"";
  1551. switch (s)
  1552. {
  1553. case ""NotMe"": Console.WriteLine(5); break;
  1554. case ""Box"": Console.WriteLine(4); break;
  1555. case ""Blah"":
  1556. case ""Blah2"": Console.WriteLine(3); break;
  1557. default: throw new InvalidOperationException();
  1558. }
  1559. }
  1560. }
  1561. }", @"
  1562. package blargh;
  1563. " + WriteImports.StandardImports + @"
  1564. class Utilities
  1565. {
  1566. public static function SomeFunction():Void
  1567. {
  1568. var s:String = ""Blah"";
  1569. switch (s)
  1570. {
  1571. case ""NotMe"":
  1572. system.Console.WriteLine_Int32(5);
  1573. case ""Box"":
  1574. system.Console.WriteLine_Int32(4);
  1575. case ""Blah"", ""Blah2"":
  1576. system.Console.WriteLine_Int32(3);
  1577. default:
  1578. throw new system.InvalidOperationException();
  1579. }
  1580. }
  1581. public function new()
  1582. {
  1583. }
  1584. }");
  1585. }
  1586. [TestMethod]
  1587. public void Linq()
  1588. {
  1589. TestFramework.TestCode(MethodInfo.GetCurrentMethod().Name, @"
  1590. using System;
  1591. using System.Linq;
  1592. namespace Blargh
  1593. {
  1594. public static class Utilities
  1595. {
  1596. public static void SomeFunction()
  1597. {
  1598. int[] e = new int[] { 0, 1, 2, 3 };
  1599. Console.WriteLine(e.First());
  1600. Console.WriteLine(e.First(o => o == 1));
  1601. Console.WriteLine(e.ElementAt(2));
  1602. Console.WriteLine(e.Last());
  1603. Console.WriteLine(e.Select(o => o).Count());
  1604. Console.WriteLine(e.Where(o => o > 0).Count() + 2);
  1605. Console.WriteLine(e.Count(o => true) + 2);
  1606. var dict = e.ToDictionary(o => o, o => 555);
  1607. e.OfType<int>();
  1608. e.OrderBy(o => 4);
  1609. e.OrderBy(o => ""z"");
  1610. }
  1611. }
  1612. }", @"
  1613. package blargh;
  1614. " + WriteImports.StandardImports + @"
  1615. class Utilities
  1616. {
  1617. public static function SomeFunction():Void
  1618. {
  1619. var e:Array<Int> = [ 0, 1, 2, 3 ];
  1620. system.Console.WriteLine_Int32(system.linq.Enumerable.First(e));
  1621. system.Console.WriteLine_Int32(system.linq.Enumerable.First_IEnumerable_FuncBoolean(e, function (o:Int):Bool
  1622. {
  1623. return o == 1;
  1624. } ));
  1625. system.Console.WriteLine_Int32(system.linq.Enumerable.ElementAt(e, 2));
  1626. system.Console.WriteLine_Int32(system.linq.Enumerable.Last(e));
  1627. system.Console.WriteLine_Int32(system.linq.Enumerable.Count(system.linq.Enumerable.Select(e, function (o:Int):Int { return o; } )));
  1628. system.Console.WriteLine_Int32(system.linq.Enumerable.Count(system.linq.Enumerable.Where(e, function (o:Int):Bool
  1629. {
  1630. return o > 0;
  1631. } )) + 2);
  1632. system.Console.WriteLine_Int32(system.linq.Enumerable.Count_IEnumerable_FuncBoolean(e, function (o:Int):Bool
  1633. {
  1634. return true;
  1635. } ) + 2);
  1636. var dict:system.collections.generic.Dictionary<Int, Int> = system.linq.Enumerable.ToDictionary(e, function (o:Int):Int
  1637. {
  1638. return o;
  1639. } , function (o:Int):Int
  1640. {
  1641. return 555;
  1642. } );
  1643. system.linq.Enumerable.OfType(e, Int);
  1644. system.linq.Enumerable.OrderBy_Int(e, function (o:Int):Int { return 4; } );
  1645. system.linq.Enumerable.OrderBy_String(e, function (o:Int):String { return ""z""; } );
  1646. }
  1647. public function new()
  1648. {
  1649. }
  1650. }");
  1651. }
  1652. [TestMethod]
  1653. public void OverloadedMethods()
  1654. {
  1655. TestFramework.TestCode(MethodInfo.GetCurrentMethod().Name, @"
  1656. using System;
  1657. namespace Blargh
  1658. {
  1659. public static class Utilities
  1660. {
  1661. public static void OverOne()
  1662. {
  1663. OverOne(3);
  1664. Math.Max(3, 3);
  1665. Math.Max(4.0, 4.0);
  1666. Math.Max(5f, 5f);
  1667. }
  1668. public static void OverOne(int param)
  1669. {
  1670. OverOne(param, ""Blah"");
  1671. }
  1672. public static void OverOne(int param, string prm)
  1673. {
  1674. Console.WriteLine(param + prm);
  1675. }
  1676. public static int OverTwo()
  1677. {
  1678. return OverTwo(18);
  1679. }
  1680. public static int OverTwo(int prm)
  1681. {
  1682. return prm;
  1683. }
  1684. }
  1685. }", @"
  1686. package blargh;
  1687. " + WriteImports.StandardImports + @"
  1688. class Utilities
  1689. {
  1690. public static function OverOne():Void
  1691. {
  1692. OverOne_Int32(3);
  1693. system.MathCS.Max_Int32_Int32(3, 3);
  1694. system.MathCS.Max_Double_Double(4.0, 4.0);
  1695. system.MathCS.Max_Single_Single(5, 5);
  1696. }
  1697. public static function OverOne_Int32(param:Int):Void
  1698. {
  1699. OverOne_Int32_String(param, ""Blah"");
  1700. }
  1701. public static function OverOne_Int32_String(param:Int, prm:String):Void
  1702. {
  1703. system.Console.WriteLine(param + prm);
  1704. }
  1705. public static function OverTwo():Int
  1706. {
  1707. return OverTwo_Int32(18);
  1708. }
  1709. public static function OverTwo_Int32(prm:Int):Int
  1710. {
  1711. return prm;
  1712. }
  1713. public function new()
  1714. {
  1715. }
  1716. }");
  1717. }
  1718. [TestMethod]
  1719. public void IsAndAs()
  1720. {
  1721. TestFramework.TestCode(MethodInfo.GetCurrentMethod().Name, @"
  1722. using System;
  1723. using System.Collections.Generic;
  1724. namespace Blargh
  1725. {
  1726. public static class Utilities
  1727. {
  1728. public static void SomeFunction()
  1729. {
  1730. string s = ""Blah"";
  1731. var list = new List<int>();
  1732. if (s is string)
  1733. Console.WriteLine(""Yes"");
  1734. if (list is List<int>)
  1735. Console.WriteLine(""Yes"");
  1736. // object o = s;
  1737. // string sss = o as string;
  1738. // Console.WriteLine(sss);
  1739. }
  1740. }
  1741. }", @"
  1742. package blargh;
  1743. " + WriteImports.StandardImports + @"
  1744. class Utilities
  1745. {
  1746. public static function SomeFunction():Void
  1747. {
  1748. var s:String = ""Blah"";
  1749. var list:Array<Int> = new Array<Int>();
  1750. if (Std.is(s, String))
  1751. {
  1752. system.Console.WriteLine(""Yes"");
  1753. }
  1754. if (Std.is(list, Array))
  1755. {
  1756. system.Console.WriteLine(""Yes"");
  1757. }
  1758. }
  1759. public function new()
  1760. {
  1761. }
  1762. }");
  1763. }
  1764. [TestMethod]
  1765. public void AbstractAndOverrides()
  1766. {
  1767. TestFramework.TestCode(MethodInfo.GetCurrentMethod().Name, @"
  1768. using System;
  1769. namespace Blargh
  1770. {
  1771. abstract class TopLevel
  1772. {
  1773. public abstract void AbstractMethod();
  1774. public abstract string AbstractProperty { get; }
  1775. public virtual void VirtualMethod()
  1776. {
  1777. Console.WriteLine(""TopLevel::VirtualMethod"");
  1778. }
  1779. public virtual string VirtualProperty
  1780. {
  1781. get
  1782. {
  1783. return ""TopLevel::VirtualProperty"";
  1784. }
  1785. }
  1786. public override string ToString()
  1787. {
  1788. return string.Empty;
  1789. }
  1790. }
  1791. class Derived : TopLevel
  1792. {
  1793. public override void AbstractMethod()
  1794. {
  1795. Console.WriteLine(""Derived::AbstractMethod"");
  1796. }
  1797. public override string AbstractProperty
  1798. {
  1799. get { return ""Derived::AbstractProperty""; }
  1800. }
  1801. public override void VirtualMethod()
  1802. {
  1803. base.VirtualMethod();
  1804. Console.WriteLine(""Derived::VirtualMethod"");
  1805. }
  1806. public override string VirtualProperty
  1807. {
  1808. get
  1809. {
  1810. return base.VirtualProperty + ""Derived:VirtualProperty"";
  1811. }
  1812. }
  1813. public override string ToString()
  1814. {
  1815. return ""DerivedToString"";
  1816. }
  1817. }
  1818. }", new string[] {
  1819. @"
  1820. package blargh;
  1821. " + WriteImports.StandardImports + @"
  1822. class TopLevel
  1823. {
  1824. public function AbstractMethod():Void
  1825. {
  1826. throw new Exception(""Abstract item called"");
  1827. }
  1828. public var AbstractProperty(get_AbstractProperty, never):String;
  1829. public function get_AbstractProperty():String
  1830. {
  1831. return throw new Exception(""Abstract item called"");
  1832. }
  1833. public function VirtualMethod():Void
  1834. {
  1835. system.Console.WriteLine(""TopLevel::VirtualMethod"");
  1836. }
  1837. public var VirtualProperty(get_VirtualProperty, never):String;
  1838. public function get_VirtualProperty():String
  1839. {
  1840. return ""TopLevel::VirtualProperty"";
  1841. }
  1842. public function toString():String
  1843. {
  1844. return """";
  1845. }
  1846. public function new()
  1847. {
  1848. }
  1849. }",
  1850. @"
  1851. package blargh;
  1852. " + WriteImports.StandardImports + @"
  1853. class Derived extends blargh.TopLevel
  1854. {
  1855. override public function AbstractMethod():Void
  1856. {
  1857. system.Console.WriteLine(""Derived::AbstractMethod"");
  1858. }
  1859. override public function get_AbstractProperty():String
  1860. {
  1861. return ""Derived::AbstractProperty"";
  1862. }
  1863. override public function VirtualMethod():Void
  1864. {
  1865. super.VirtualMethod();
  1866. system.Console.WriteLine(""Derived::VirtualMethod"");
  1867. }
  1868. override public function get_VirtualProperty():String
  1869. {
  1870. return super.VirtualProperty + ""Derived:VirtualProperty"";
  1871. }
  1872. override public function toString():String
  1873. {
  1874. return ""DerivedToString"";
  1875. }
  1876. public function new()
  1877. {
  1878. super();
  1879. }
  1880. }" });
  1881. }
  1882. [TestMethod]
  1883. public void FieldsAndProperties()
  1884. {
  1885. TestFramework.TestCode(MethodInfo.GetCurrentMethod().Name, @"
  1886. using System;
  1887. using System.Text;
  1888. namespace Blargh
  1889. {
  1890. class Box
  1891. {
  1892. private float _width;
  1893. public float Width
  1894. {
  1895. get { return _width; }
  1896. set { _width = value; }
  1897. }
  1898. public float SetOnly
  1899. {
  1900. set { Console.WriteLine(value); }
  1901. }
  1902. public int GetOnly
  1903. {
  1904. get { return 4; }
  1905. }
  1906. public bool IsRectangular = true;
  1907. public char[] Characters = new char[] { 'a', 'b' };
  1908. public static StringBuilder StaticField = new StringBuilder();
  1909. public const int ConstInt = 24;
  1910. public static readonly int StaticReadonlyInt = 5;
  1911. public const string WithQuoteMiddle = @""before""""after"";
  1912. public const string WithQuoteStart = @""""""after"";
  1913. public int MultipleOne, MultipleTwo;
  1914. public readonly int ReadonlyInt = 3;
  1915. public DateTime UninitializedDate;
  1916. public int? UnitializedNullableInt;
  1917. public TimeSpan UninitializedTimeSpan;
  1918. public static DateTime StaticUninitializedDate;
  1919. public static int? StaticUnitializedNullableInt;
  1920. public static TimeSpan StaticUninitializedTimeSpan;
  1921. static Box()
  1922. {
  1923. Console.WriteLine(""cctor"");
  1924. }
  1925. public Box()
  1926. {
  1927. Console.WriteLine(""ctor"");
  1928. }
  1929. }
  1930. }", @"
  1931. package blargh;
  1932. " + WriteImports.StandardImports + @"
  1933. class Box
  1934. {
  1935. private var _width:Float;
  1936. public var Width(get_Width, set_Width):Float;
  1937. public function get_Width():Float
  1938. {
  1939. return _width;
  1940. }
  1941. public function set_Width(value:Float):Float
  1942. {
  1943. _width = value;
  1944. return 0;
  1945. }
  1946. public var SetOnly(never, set_SetOnly):Float;
  1947. public function set_SetOnly(value:Float):Float
  1948. {
  1949. system.Console.WriteLine_Single(value);
  1950. return 0;
  1951. }
  1952. public var GetOnly(get_GetOnly, never):Int;
  1953. public function get_GetOnly():Int
  1954. {
  1955. return 4;
  1956. }
  1957. public var IsRectangular:Bool;
  1958. public var Characters:Array<Int>;
  1959. public static var StaticField:system.text.StringBuilder;
  1960. public static inline var ConstInt:Int = 24;
  1961. public static inline var StaticReadonlyInt:Int = 5;
  1962. public static inline var WithQuoteMiddle:String = ""before\""after"";
  1963. public static inline var WithQuoteStart:String = ""\""after"";
  1964. public var MultipleOne:Int;
  1965. public var MultipleTwo:Int;
  1966. public var ReadonlyInt:Int;
  1967. public var UninitializedDate:system.DateTime;
  1968. public var UnitializedNullableInt:Nullable_Int;
  1969. public var UninitializedTimeSpan:system.TimeSpan;
  1970. public static var StaticUninitializedDate:system.DateTime;
  1971. public static var StaticUnitializedNullableInt:Nullable_Int;
  1972. public static var StaticUninitializedTimeSpan:system.TimeSpan;
  1973. public static function cctor():Void
  1974. {
  1975. StaticField = new system.text.StringBuilder();
  1976. StaticUninitializedDate = new system.DateTime();
  1977. StaticUnitializedNullableInt = new Nullable_Int();
  1978. StaticUninitializedTimeSpan = new system.TimeSpan();
  1979. system.Console.WriteLine(""cctor"");
  1980. }
  1981. public function new()
  1982. {
  1983. _width = 0;
  1984. IsRectangular = true;
  1985. Characters = [ 97, 98 ];
  1986. MultipleOne = 0;
  1987. MultipleTwo = 0;
  1988. ReadonlyInt = 3;
  1989. UninitializedDate = new system.DateTime();
  1990. UnitializedNullableInt = new Nullable_Int();
  1991. UninitializedTimeSpan = new system.TimeSpan();
  1992. system.Console.WriteLine(""ctor"");
  1993. }
  1994. }");
  1995. }
  1996. [TestMethod]
  1997. public void

Large files files are truncated, but you can click here to view the full file