PageRenderTime 42ms CodeModel.GetById 1ms 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
  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 Interfaces()
  1998. {
  1999. TestFramework.TestCode(MethodInfo.GetCurrentMethod().Name, @"
  2000. using System;
  2001. namespace Blargh
  2002. {
  2003. public interface ITesting
  2004. {
  2005. void Poke();
  2006. }
  2007. class Pokable : ITesting
  2008. {
  2009. public void Poke()
  2010. {
  2011. Console.WriteLine(""Implementation"");
  2012. }
  2013. }
  2014. }",
  2015. new string[] { @"
  2016. package blargh;
  2017. " + WriteImports.StandardImports + @"
  2018. interface ITesting
  2019. {
  2020. function Poke():Void;
  2021. }",
  2022. @"
  2023. package blargh;
  2024. " + WriteImports.StandardImports + @"
  2025. class Pokable implements blargh.ITesting
  2026. {
  2027. public function Poke():Void
  2028. {
  2029. system.Console.WriteLine(""Implementation"");
  2030. }
  2031. public function new()
  2032. {
  2033. }
  2034. }"});
  2035. }
  2036. [TestMethod]
  2037. public void TryCatchThrow()
  2038. {
  2039. TestFramework.TestCode(MethodInfo.GetCurrentMethod().Name, @"
  2040. using System;
  2041. using System.IO;
  2042. namespace Blargh
  2043. {
  2044. public static class Utilities
  2045. {
  2046. public static void SomeFunction()
  2047. {
  2048. Console.WriteLine(""Before try"");
  2049. try
  2050. {
  2051. Console.WriteLine(""In try"");
  2052. }
  2053. catch (Exception ex)
  2054. {
  2055. Console.WriteLine(""In catch "" + ex + "" "" + ex.ToString());
  2056. TakesObject(ex);
  2057. }
  2058. try
  2059. {
  2060. Console.WriteLine(""Try without finally"");
  2061. }
  2062. catch (IOException ex)
  2063. {
  2064. Console.WriteLine(""In second catch"");
  2065. }
  2066. try
  2067. {
  2068. Console.WriteLine(""Try in parameterless catch"");
  2069. }
  2070. catch
  2071. {
  2072. Console.WriteLine(""In parameterless catch"");
  2073. }
  2074. throw new InvalidOperationException(""err"");
  2075. }
  2076. public static void TakesObject(object o) { }
  2077. public static string ReturnsSomething()
  2078. {
  2079. throw new Exception();
  2080. }
  2081. }
  2082. }", @"
  2083. package blargh;
  2084. " + WriteImports.StandardImports + @"
  2085. class Utilities
  2086. {
  2087. public static function SomeFunction():Void
  2088. {
  2089. system.Console.WriteLine(""Before try"");
  2090. try
  2091. {
  2092. system.Console.WriteLine(""In try"");
  2093. }
  2094. catch (ex:Dynamic)
  2095. {
  2096. system.Console.WriteLine(""In catch "" + ex + "" "" + ex.toString());
  2097. TakesObject(ex);
  2098. }
  2099. try
  2100. {
  2101. system.Console.WriteLine(""Try without finally"");
  2102. }
  2103. catch (ex:system.io.IOException)
  2104. {
  2105. system.Console.WriteLine(""In second catch"");
  2106. }
  2107. try
  2108. {
  2109. system.Console.WriteLine(""Try in parameterless catch"");
  2110. }
  2111. catch (__ex:Dynamic)
  2112. {
  2113. system.Console.WriteLine(""In parameterless catch"");
  2114. }
  2115. throw new system.InvalidOperationException(""err"");
  2116. }
  2117. public static function TakesObject(o:Dynamic):Void
  2118. {
  2119. }
  2120. public static function ReturnsSomething():String
  2121. {
  2122. return throw new system.Exception();
  2123. }
  2124. public function new()
  2125. {
  2126. }
  2127. }");
  2128. }
  2129. [ExpectedException(typeof(AggregateException), "When catching an Exception, you cannot use the object as an Exception object.")]
  2130. [TestMethod]
  2131. public void CatchException()
  2132. {
  2133. TestFramework.TestCode(MethodInfo.GetCurrentMethod().Name, @"
  2134. using System;
  2135. using System.IO;
  2136. namespace Blargh
  2137. {
  2138. public static class Utilities
  2139. {
  2140. public static void SomeFunction()
  2141. {
  2142. try
  2143. {
  2144. }
  2145. catch (Exception ex)
  2146. {
  2147. throw new Exception("""", ex);
  2148. }
  2149. }
  2150. }
  2151. }", @"");
  2152. }
  2153. [ExpectedException(typeof(AggregateException), "When catching an Exception, you cannot use the object as an Exception object.")]
  2154. [TestMethod]
  2155. public void CatchException2()
  2156. {
  2157. TestFramework.TestCode(MethodInfo.GetCurrentMethod().Name, @"
  2158. using System;
  2159. using System.IO;
  2160. namespace Blargh
  2161. {
  2162. public static class Utilities
  2163. {
  2164. public static void SomeFunction()
  2165. {
  2166. try
  2167. {
  2168. }
  2169. catch (Exception ex)
  2170. {
  2171. TakesEx(ex);
  2172. }
  2173. }
  2174. public static void TakesEx(Exception ex)
  2175. {
  2176. }
  2177. }
  2178. }", @"");
  2179. }
  2180. [TestMethod]
  2181. public void Generics()
  2182. {
  2183. TestFramework.TestCode(MethodInfo.GetCurrentMethod().Name, @"
  2184. using System;
  2185. using System.Collections.Generic;
  2186. namespace Blargh
  2187. {
  2188. public static class Utilities
  2189. {
  2190. public static Queue<T> ToQueue<T>(this IEnumerable<T> array)
  2191. {
  2192. var queue = new Queue<T>();
  2193. foreach (T a in array)
  2194. queue.Enqueue(a);
  2195. queue.Dequeue();
  2196. return queue;
  2197. }
  2198. public static IEnumerable<T> SideEffect<T>(this IEnumerable<T> array, Action<T> effect)
  2199. {
  2200. foreach(var i in array)
  2201. effect(i);
  2202. return array;
  2203. }
  2204. }
  2205. }", @"
  2206. package blargh;
  2207. " + WriteImports.StandardImports + @"
  2208. class Utilities
  2209. {
  2210. public static function ToQueue<T>(array:Array<T>):Array<T>
  2211. {
  2212. var queue:Array<T> = new Array<T>();
  2213. for (a in array)
  2214. {
  2215. queue.push(a);
  2216. }
  2217. queue.shift();
  2218. return queue;
  2219. }
  2220. public static function SideEffect<T>(array:Array<T>, effect:(T -> Void)):Array<T>
  2221. {
  2222. for (i in array)
  2223. {
  2224. effect(i);
  2225. }
  2226. return array;
  2227. }
  2228. public function new()
  2229. {
  2230. }
  2231. }");
  2232. }
  2233. [TestMethod]
  2234. public void Objects()
  2235. {
  2236. TestFramework.TestCode(MethodInfo.GetCurrentMethod().Name, @"
  2237. using System;
  2238. using System.Collections.Generic;
  2239. namespace Blargh
  2240. {
  2241. public static class Utilities
  2242. {
  2243. public static void SomeFunction()
  2244. {
  2245. var queue = new Queue<int>(10);
  2246. queue.Enqueue(4);
  2247. queue.Enqueue(2);
  2248. Console.WriteLine(queue.Dequeue());
  2249. queue.Clear();
  2250. var list = new List<string>(3);
  2251. list.Add(""Three"");
  2252. list.RemoveAt(0);
  2253. list.Insert(4, ""Seven"");
  2254. list.Sort();
  2255. }
  2256. }
  2257. }", @"
  2258. package blargh;
  2259. " + WriteImports.StandardImports + @"
  2260. class Utilities
  2261. {
  2262. public static function SomeFunction():Void
  2263. {
  2264. var queue:Array<Int> = new Array<Int>();
  2265. queue.push(4);
  2266. queue.push(2);
  2267. system.Console.WriteLine_Int32(queue.shift());
  2268. system.Cs2Hx.Clear(queue);
  2269. var list:Array<String> = new Array<String>();
  2270. list.push(""Three"");
  2271. list.splice(0, 1);
  2272. list.insert(4, ""Seven"");
  2273. list.sort(Cs2Hx.SortStrings);
  2274. }
  2275. public function new()
  2276. {
  2277. }
  2278. }");
  2279. }
  2280. [TestMethod]
  2281. public void Lambda()
  2282. {
  2283. TestFramework.TestCode(MethodInfo.GetCurrentMethod().Name, @"
  2284. using System;
  2285. using System.Collections.Generic;
  2286. namespace Blargh
  2287. {
  2288. public static class Utilities
  2289. {
  2290. public static void SomeFunction()
  2291. {
  2292. Func<int, int> f1 = x => x + 5;
  2293. Console.WriteLine(f1(3));
  2294. Func<int, int> f2 = x => { return x + 6; };
  2295. Console.WriteLine(f2(3));
  2296. List<Action> actions = new List<Action>();
  2297. }
  2298. }
  2299. }", @"
  2300. package blargh;
  2301. " + WriteImports.StandardImports + @"
  2302. class Utilities
  2303. {
  2304. public static function SomeFunction():Void
  2305. {
  2306. var f1:(Int -> Int) = function (x:Int):Int
  2307. {
  2308. return x + 5;
  2309. } ;
  2310. system.Console.WriteLine_Int32(f1(3));
  2311. var f2:(Int -> Int) = function (x:Int):Int
  2312. {
  2313. return x + 6;
  2314. } ;
  2315. system.Console.WriteLine_Int32(f2(3));
  2316. var actions:Array<(Void -> Void)> = new Array<(Void -> Void)>();
  2317. }
  2318. public function new()
  2319. {
  2320. }
  2321. }");
  2322. }
  2323. [TestMethod]
  2324. public void LambdaNoReturn()
  2325. {
  2326. TestFramework.TestCode(MethodInfo.GetCurrentMethod().Name, @"
  2327. using System;
  2328. namespace Blargh
  2329. {
  2330. public static class Utilities
  2331. {
  2332. public static void SomeFunction()
  2333. {
  2334. int i = 3;
  2335. Action a = () => i = 4;
  2336. Func<int> b = () => i = 5;
  2337. Foo(() => i = 6);
  2338. }
  2339. public static void Foo(Action a)
  2340. {
  2341. }
  2342. }
  2343. }", @"
  2344. package blargh;
  2345. " + WriteImports.StandardImports + @"
  2346. class Utilities
  2347. {
  2348. public static function SomeFunction():Void
  2349. {
  2350. var i:Int = 3;
  2351. var a:(Void -> Void) = function ():Void
  2352. {
  2353. i = 4;
  2354. } ;
  2355. var b:(Void -> Int) = function ():Int
  2356. {
  2357. return i = 5;
  2358. } ;
  2359. Foo(function ():Void
  2360. {
  2361. i = 6;
  2362. } );
  2363. }
  2364. public static function Foo(a:(Void -> Void)):Void
  2365. {
  2366. }
  2367. public function new()
  2368. {
  2369. }
  2370. }");
  2371. }
  2372. [TestMethod]
  2373. public void Casts()
  2374. {
  2375. var cs = @"
  2376. using System;
  2377. namespace Blargh
  2378. {
  2379. #if !CS2HX
  2380. public static class Utilities
  2381. {
  2382. public static T As<T>(this object o)
  2383. {
  2384. return (T)o;
  2385. }
  2386. }
  2387. #endif
  2388. public static class Test
  2389. {
  2390. public static void SomeFunction()
  2391. {
  2392. var a = DateTime.Now.As<String>();
  2393. object o = 4;
  2394. var b = (byte)(short)o;
  2395. }
  2396. }
  2397. }";
  2398. var haxe = @"
  2399. package blargh;
  2400. " + WriteImports.StandardImports + @"
  2401. class Test
  2402. {
  2403. public static function SomeFunction():Void
  2404. {
  2405. var a:String = cast(system.DateTime.Now, String);
  2406. var o:Dynamic = 4;
  2407. var b:Int = o;
  2408. }
  2409. public function new()
  2410. {
  2411. }
  2412. }";
  2413. var transform = @"<?xml version=""1.0"" encoding=""utf-8""?>
  2414. <Translations>
  2415. <Method SourceObject=""*"" Match=""As"">
  2416. <ReplaceWith>
  2417. <String>cast(</String>
  2418. <Expression />
  2419. <String>, {genericType})</String>
  2420. </ReplaceWith>
  2421. </Method>
  2422. </Translations>";
  2423. TestFramework.TestCode(MethodInfo.GetCurrentMethod().Name, new[] { cs }, new[] { haxe }, transform);
  2424. }
  2425. [TestMethod]
  2426. public void ArrayAndForEach()
  2427. {
  2428. TestFramework.TestCode(MethodInfo.GetCurrentMethod().Name, @"
  2429. using System;
  2430. using System.Collections.Generic;
  2431. namespace Blargh
  2432. {
  2433. public static class Utilities
  2434. {
  2435. public static void SomeFunction()
  2436. {
  2437. var ar = new int[] { 1, 2, 3 };
  2438. foreach(var i in ar)
  2439. Console.WriteLine(i);
  2440. Console.WriteLine(ar[1]);
  2441. Console.WriteLine(ar.Length);
  2442. Console.WriteLine(new List<string>().Count);
  2443. }
  2444. }
  2445. }", @"
  2446. package blargh;
  2447. " + WriteImports.StandardImports + @"
  2448. class Utilities
  2449. {
  2450. public static function SomeFunction():Void
  2451. {
  2452. var ar:Array<Int> = [ 1, 2, 3 ];
  2453. for (i in ar)
  2454. {
  2455. system.Console.WriteLine_Int32(i);
  2456. }
  2457. system.Console.WriteLine_Int32(ar[1]);
  2458. system.Console.WriteLine_Int32(ar.length);
  2459. system.Console.WriteLine_Int32(new Array<String>().length);
  2460. }
  2461. public function new()
  2462. {
  2463. }
  2464. }");
  2465. }
  2466. [TestMethod]
  2467. public void PartialClasses()
  2468. {
  2469. TestFramework.TestCode(MethodInfo.GetCurrentMethod().Name,
  2470. new string[] { @"
  2471. using System;
  2472. namespace Blargh
  2473. {
  2474. public partial class Utilities
  2475. {
  2476. public void FunFromOne()
  2477. {
  2478. Console.WriteLine(""I'm in one!"");
  2479. }
  2480. }
  2481. }",
  2482. @"
  2483. using System;
  2484. namespace Blargh
  2485. {
  2486. public partial class Utilities
  2487. {
  2488. public void FunFromTwo()
  2489. {
  2490. Console.WriteLine(""I'm in Two!"");
  2491. }
  2492. }
  2493. }"
  2494. }, @"
  2495. package blargh;
  2496. " + WriteImports.StandardImports + @"
  2497. class Utilities
  2498. {
  2499. public function FunFromOne():Void
  2500. {
  2501. system.Console.WriteLine(""I'm in one!"");
  2502. }
  2503. public function FunFromTwo():Void
  2504. {
  2505. system.Console.WriteLine(""I'm in Two!"");
  2506. }
  2507. public function new()
  2508. {
  2509. }
  2510. }");
  2511. }
  2512. [TestMethod]
  2513. public void StringMethods()
  2514. {
  2515. TestFramework.TestCode(MethodInfo.GetCurrentMethod().Name, @"
  2516. using System;
  2517. namespace Blargh
  2518. {
  2519. public static class Utilities
  2520. {
  2521. public static void SomeFunction(string s2)
  2522. {
  2523. string s = @""50\0"";
  2524. Console.WriteLine(s.IndexOf(""0""));
  2525. Console.WriteLine(s2.IndexOf(""0""));
  2526. foreach(string s3 in new string[] { ""Hello"" })
  2527. s3.Substring(4, 5);
  2528. int i = 4;
  2529. string si = i.ToString();
  2530. if (si.StartsWith(""asdf""))
  2531. Console.WriteLine(4);
  2532. char c = 'A';
  2533. s = c.ToString();
  2534. s = c + """";
  2535. s = """" + c;
  2536. var c2 = c + 3;
  2537. }
  2538. }
  2539. }", @"
  2540. package blargh;
  2541. " + WriteImports.StandardImports + @"
  2542. class Utilities
  2543. {
  2544. public static function SomeFunction(s2:String):Void
  2545. {
  2546. var s:String = ""50\\0"";
  2547. system.Console.WriteLine_Int32(s.indexOf(""0""));
  2548. system.Console.WriteLine_Int32(s2.indexOf(""0""));
  2549. for (s3 in [ ""Hello"" ])
  2550. {
  2551. s3.substr(4, 5);
  2552. }
  2553. var i:Int = 4;
  2554. var si:String = Std.string(i);
  2555. if (system.Cs2Hx.StartsWith(si, ""asdf""))
  2556. {
  2557. system.Console.WriteLine_Int32(4);
  2558. }
  2559. var c:Int = 65;
  2560. s = Cs2Hx.CharToString(c);
  2561. s = Cs2Hx.CharToString(c) + """";
  2562. s = """" + Cs2Hx.CharToString(c);
  2563. var c2:Int = c + 3;
  2564. }
  2565. public function new()
  2566. {
  2567. }
  2568. }");
  2569. }
  2570. [TestMethod]
  2571. public void ExtensionMethod()
  2572. {
  2573. TestFramework.TestCode(MethodInfo.GetCurrentMethod().Name, @"
  2574. using System;
  2575. namespace Blargh
  2576. {
  2577. public static class Utilities
  2578. {
  2579. public static void SomeFunction()
  2580. {
  2581. int i = -3;
  2582. Console.WriteLine(""false "" + i.IsFour());
  2583. i++;
  2584. i += 6;
  2585. var b = i.IsFour();
  2586. Console.WriteLine(""true "" + b);
  2587. Utilities.IsFour(5);
  2588. }
  2589. public static bool IsFour(this int i)
  2590. {
  2591. return i == 4;
  2592. }
  2593. }
  2594. }", @"
  2595. package blargh;
  2596. " + WriteImports.StandardImports + @"
  2597. class Utilities
  2598. {
  2599. public static function SomeFunction():Void
  2600. {
  2601. var i:Int = -3;
  2602. system.Console.WriteLine(""false "" + blargh.Utilities.IsFour(i));
  2603. i++;
  2604. i += 6;
  2605. var b:Bool = blargh.Utilities.IsFour(i);
  2606. system.Console.WriteLine(""true "" + b);
  2607. blargh.Utilities.IsFour(5);
  2608. }
  2609. public static function IsFour(i:Int):Bool
  2610. {
  2611. return i == 4;
  2612. }
  2613. public function new()
  2614. {
  2615. }
  2616. }");
  2617. }
  2618. [TestMethod]
  2619. public void StringJoin()
  2620. {
  2621. TestFramework.TestCode(MethodInfo.GetCurrentMethod().Name, @"
  2622. using System;
  2623. namespace Blargh
  2624. {
  2625. public class Foo
  2626. {
  2627. public Foo()
  2628. {
  2629. var s = string.Join("";"", new[] { ""one"", ""two"" });
  2630. }
  2631. }
  2632. }", @"
  2633. package blargh;
  2634. " + WriteImports.StandardImports + @"
  2635. class Foo
  2636. {
  2637. public function new()
  2638. {
  2639. var s:String = system.Cs2Hx.Join("";"", [ ""one"", ""two"" ]);
  2640. }
  2641. }");
  2642. }
  2643. [TestMethod]
  2644. public void RefAndOut()
  2645. {
  2646. TestFramework.TestCode(MethodInfo.GetCurrentMethod().Name, @"
  2647. using System;
  2648. using System.Text;
  2649. namespace Blargh
  2650. {
  2651. public class Foo
  2652. {
  2653. public Foo()
  2654. {
  2655. int x;
  2656. TestOut(out x);
  2657. x = 3;
  2658. var s = x.ToString();
  2659. int i = 1;
  2660. TestRef(ref i);
  2661. i = 5;
  2662. new StringBuilder(i);
  2663. Func<int> fun = () => x;
  2664. }
  2665. public void TestRef(ref int i)
  2666. {
  2667. var sb = new StringBuilder(i);
  2668. i = 4;
  2669. }
  2670. public void TestOut(out int i)
  2671. {
  2672. i = 4;
  2673. var sb = new StringBuilder(i);
  2674. }
  2675. }
  2676. }", @"
  2677. package blargh;
  2678. " + WriteImports.StandardImports + @"
  2679. class Foo
  2680. {
  2681. public function new()
  2682. {
  2683. var x:CsRef<Int> = new CsRef<Int>(0);
  2684. TestOut(x);
  2685. x.Value = 3;
  2686. var s:String = Std.string(x.Value);
  2687. var i:CsRef<Int> = new CsRef<Int>(1);
  2688. TestRef(i);
  2689. i.Value = 5;
  2690. new system.text.StringBuilder(i.Value);
  2691. var fun:(Void -> Int) = function ():Int { return x.Value; } ;
  2692. }
  2693. public function TestRef(i:CsRef<Int>):Void
  2694. {
  2695. var sb:system.text.StringBuilder = new system.text.StringBuilder(i.Value);
  2696. i.Value = 4;
  2697. }
  2698. public function TestOut(i:CsRef<Int>):Void
  2699. {
  2700. i.Value = 4;
  2701. var sb:system.text.StringBuilder = new system.text.StringBuilder(i.Value);
  2702. }
  2703. }");
  2704. }
  2705. [TestMethod]
  2706. public void MemoryStream()
  2707. {
  2708. TestFramework.TestCode(MethodInfo.GetCurrentMethod().Name, @"
  2709. using System;
  2710. using System.IO;
  2711. namespace Blargh
  2712. {
  2713. public class Foo
  2714. {
  2715. public Foo()
  2716. {
  2717. var s = new MemoryStream(5);
  2718. var b = new byte[4];
  2719. s = new MemoryStream(b);
  2720. s = new MemoryStream(b.Length);
  2721. }
  2722. }
  2723. }", @"
  2724. package blargh;
  2725. " + WriteImports.StandardImports + @"
  2726. class Foo
  2727. {
  2728. public function new()
  2729. {
  2730. var s:system.io.MemoryStream = new system.io.MemoryStream();
  2731. var b:haxe.io.Bytes = haxe.io.Bytes.alloc(4);
  2732. s = new system.io.MemoryStream(b);
  2733. s = new system.io.MemoryStream();
  2734. }
  2735. }");
  2736. }
  2737. [TestMethod]
  2738. public void PartialMethods()
  2739. {
  2740. TestFramework.TestCode(MethodInfo.GetCurrentMethod().Name, @"
  2741. using System;
  2742. namespace Blargh
  2743. {
  2744. public partial class Foo
  2745. {
  2746. partial void NoOther();
  2747. partial void Other();
  2748. }
  2749. partial class Foo
  2750. {
  2751. partial void Other()
  2752. {
  2753. Console.WriteLine();
  2754. }
  2755. }
  2756. }", @"
  2757. package blargh;
  2758. " + WriteImports.StandardImports + @"
  2759. class Foo
  2760. {
  2761. function NoOther():Void
  2762. {
  2763. }
  2764. function Other():Void
  2765. {
  2766. system.Console.WriteLine();
  2767. }
  2768. public function new()
  2769. {
  2770. }
  2771. }");
  2772. }
  2773. [TestMethod]
  2774. public void TypeConstraints()
  2775. {
  2776. TestFramework.TestCode(MethodInfo.GetCurrentMethod().Name, @"
  2777. using System;
  2778. namespace Blargh
  2779. {
  2780. public static class Utilities
  2781. {
  2782. public static void SomeFunction<T>() where T : class, IComparable<T>
  2783. {
  2784. }
  2785. }
  2786. }", @"
  2787. package blargh;
  2788. " + WriteImports.StandardImports + @"
  2789. class Utilities
  2790. {
  2791. public static function SomeFunction<T: (system.IComparable<T>)>():Void
  2792. {
  2793. }
  2794. public function new()
  2795. {
  2796. }
  2797. }");
  2798. }
  2799. [TestMethod]
  2800. public void ExplicitCastOperators()
  2801. {
  2802. TestFramework.TestCode(MethodInfo.GetCurrentMethod().Name, @"
  2803. using System;
  2804. namespace Foo
  2805. {
  2806. public class Bar
  2807. {
  2808. public static explicit operator string(Bar value)
  2809. {
  2810. return ""blah"";
  2811. }
  2812. public static void Foo()
  2813. {
  2814. var b = new Bar();
  2815. var s = (string)b;
  2816. }
  2817. }
  2818. }", @"
  2819. package foo;
  2820. " + WriteImports.StandardImports + @"
  2821. class Bar
  2822. {
  2823. public static function op_Explicit_String(value:foo.Bar):String
  2824. {
  2825. return ""blah"";
  2826. }
  2827. public static function Foo():Void
  2828. {
  2829. var b:foo.Bar = new foo.Bar();
  2830. var s:String = foo.Bar.op_Explicit_String(b);
  2831. }
  2832. public function new()
  2833. {
  2834. }
  2835. }");
  2836. }
  2837. [TestMethod]
  2838. public void ParamsArguments()
  2839. {
  2840. TestFramework.TestCode(MethodInfo.GetCurrentMethod().Name, @"
  2841. using System;
  2842. namespace Foo
  2843. {
  2844. public class Bar
  2845. {
  2846. public static void Method1(params int[] p)
  2847. {
  2848. }
  2849. public static void Method2(int i, params int[] p)
  2850. {
  2851. }
  2852. public static void Method3(int i, int z, params int[] p)
  2853. {
  2854. }
  2855. public static void Foo()
  2856. {
  2857. Method1(1);
  2858. Method1(1, 2);
  2859. Method1(1, 2, 3);
  2860. Method1(1, 2, 3, 4);
  2861. Method2(1);
  2862. Method2(1, 2);
  2863. Method2(1, 2, 3);
  2864. Method2(1, 2, 3, 4);
  2865. Method3(1, 2);
  2866. Method3(1, 2, 3);
  2867. Method3(1, 2, 3, 4);
  2868. }
  2869. }
  2870. }", @"
  2871. package foo;
  2872. " + WriteImports.StandardImports + @"
  2873. class Bar
  2874. {
  2875. public static function Method1(p:Array<Int>):Void
  2876. {
  2877. }
  2878. public static function Method2(i:Int, p:Array<Int>):Void
  2879. {
  2880. }
  2881. public static function Method3(i:Int, z:Int, p:Array<Int>):Void
  2882. {
  2883. }
  2884. public static function Foo():Void
  2885. {
  2886. Method1([ 1 ]);
  2887. Method1([ 1, 2 ]);
  2888. Method1([ 1, 2, 3 ]);
  2889. Method1([ 1, 2, 3, 4 ]);
  2890. Method2(1);
  2891. Method2(1, [ 2 ]);
  2892. Method2(1, [ 2, 3 ]);
  2893. Method2(1, [ 2, 3, 4 ]);
  2894. Method3(1, 2);
  2895. Method3(1, 2, [ 3 ]);
  2896. Method3(1, 2, [ 3, 4 ]);
  2897. }
  2898. public function new()
  2899. {
  2900. }
  2901. }");
  2902. }
  2903. [TestMethod]
  2904. public void PassNullToEnumerableExtensionMethod()
  2905. {
  2906. TestFramework.TestCode(MethodInfo.GetCurrentMethod().Name, @"
  2907. using System;
  2908. using System.Collections.Generic;
  2909. namespace NS
  2910. {
  2911. public static class C
  2912. {
  2913. public static void Foo<T>(this IEnumerable<T> a)
  2914. {
  2915. }
  2916. public static void Bar()
  2917. {
  2918. Dictionary<int, string> dict = null;
  2919. dict.Foo();
  2920. }
  2921. }
  2922. }", @"
  2923. package ns;
  2924. " + WriteImports.StandardImports + @"
  2925. class C
  2926. {
  2927. public static function Foo<T>(a:Array<T>):Void
  2928. {
  2929. }
  2930. public static function Bar():Void
  2931. {
  2932. var dict:system.collections.generic.Dictionary<Int, String> = null;
  2933. ns.C.Foo(Cs2Hx.GetEnumeratorNullCheck(dict));
  2934. }
  2935. public function new() { }
  2936. }");
  2937. }
  2938. }
  2939. }