public void TestClosure() { var yearFrom = DateTime.Today.Year - 18; string result = Js <User> .Convert(u => DateTime.Today.Year - u.Age > yearFrom); Assert.AreEqual("function (u) { return ((2018 - u.Age) > 2000); }", result); }
public void TestLinqWhere() { string result = Js <User[], int> .Convert((x, age) => x.Where(t => t.Age > age)); Assert.AreEqual("function (x, age) { var r = []; for(var i=0;i<x.length;i++) { if (x[i].Age > age) r.push(x[i]); } return r; }", result); }
public void TestLinqSelect() { string result = Js <User[]> .Convert((x) => x.Select(t => t.Name)); Assert.AreEqual("function (x) { var r = []; for(var i=0;i<x.length;i++) { r.push(x[i].Name); } return r; }", result); }
public void TestEnums() { string result = Js <User> .Convert(u => u.Gender == Gender.Female); Assert.AreEqual("function (u) { return (u.Gender == 1); }", result); }
public void TestFuncName() { string result = Js <int> .Convert(x => x, "SomeName"); Assert.AreEqual("function SomeName(x) { return x; }", result); }
public void TestSimpleConstant() { string result = Js.Convert(() => 2 * 2); Assert.AreEqual("function () { return 4; }", result); }
public void TestArrayIndexerObject() { string result = Js <User> .Convert(u => u.Name[5]); Assert.AreEqual("function (u) { return u.Name[5]; }", result); }
public void TestAdvancedOperators() { string result = Js <int, int> .Convert((x, y) => (x % y / 2) >> 5); Assert.AreEqual("function (x, y) { return (((x % y) / 2) >> 5); }", result); }
public void TestMemberAccess() { string result = Js <User> .Convert(u => u.Age); Assert.AreEqual("function (u) { return u.Age; }", result); }
public void TestArrayIndexer() { string result = Js <int[]> .Convert(x => x[5]); Assert.AreEqual("function (x) { return x[5]; }", result); }
public void TestNull() { string result = Js <User> .Convert(u => u.Name == null); Assert.AreEqual("function (u) { return (u.Name == null); }", result); }
public void TestConditionalOperator() { string result = Js <int> .Convert(x => x > 2? 3 : 1); Assert.AreEqual("function (x) { return ((x > 2) ? 3 : 1); }", result); }
public void TestCoalesceOperator() { string result = Js <User> .Convert(u => u.Balance ?? 0); Assert.AreEqual("function (u) { return ((u.Balance == null || u.Balance == undefined) ? 0 : u.Balance); }", result); }
public void TestNotLinqWhere() { Js.Convert(() => new User().Where()); }
public void TestObjectInitializer() { string result = Js <User> .Convert(u => new { NormalizedAge = u.Age - 18, UpperName = u.Name.ToUpper() }); Assert.AreEqual("function (u) { return { NormalizedAge: (u.Age - 18), UpperName: u.Name.toUpperCase() }; }", result); }
public void TestLinqCombinedX2() { string result = Js <User[], int> .Convert((x, age) => from t in x where t.Age > age where t.Gender == Gender.Male select t.Name); Assert.AreEqual("function (x, age) { var r = []; for(var i=0;i<x.length;i++) { if ((x[i].Gender == 0) && (x[i].Age > age)) r.push(x[i].Name); } return r; }", result); }
public void TestSimpleOperators() { string result = Js <int> .Convert(x => x + 1 *x); Assert.AreEqual("function (x) { return (x + (1 * x)); }", result); }