123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464 |
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Reflection;
- using Jint.Native;
- using Jint.Native.Object;
- using Jint.Tests.Runtime.Converters;
- using Jint.Tests.Runtime.Domain;
- using Shapes;
- using Xunit;
- namespace Jint.Tests.Runtime
- {
- public class InteropTests : IDisposable
- {
- private readonly Engine _engine;
- public InteropTests()
- {
- _engine = new Engine(cfg => cfg.AllowClr(
- typeof(Shape).GetTypeInfo().Assembly,
- typeof(System.IO.File).GetTypeInfo().Assembly))
- .SetValue("log", new Action<object>(Console.WriteLine))
- .SetValue("assert", new Action<bool>(Assert.True))
- .SetValue("equal", new Action<object, object>(Assert.Equal))
- ;
- }
- void IDisposable.Dispose()
- {
- }
- private void RunTest(string source)
- {
- _engine.Execute(source);
- }
- [Fact]
- public void PrimitiveTypesCanBeSet()
- {
- _engine.SetValue("x", 10);
- _engine.SetValue("y", true);
- _engine.SetValue("z", "foo");
- RunTest(@"
- assert(x === 10);
- assert(y === true);
- assert(z === 'foo');
- ");
- }
- [Fact]
- public void DelegatesCanBeSet()
- {
- _engine.SetValue("square", new Func<double, double>(x => x * x));
- RunTest(@"
- assert(square(10) === 100);
- ");
- }
- [Fact]
- public void DelegateWithNullableParameterCanBePassedANull()
- {
- _engine.SetValue("isnull", new Func<double?, bool>(x => x == null));
- RunTest(@"
- assert(isnull(null) === true);
- ");
- }
- [Fact]
- public void DelegateWithObjectParameterCanBePassedANull()
- {
- _engine.SetValue("isnull", new Func<object, bool>(x => x == null));
- RunTest(@"
- assert(isnull(null) === true);
- ");
- }
- [Fact]
- public void DelegateWithNullableParameterCanBePassedAnUndefined()
- {
- _engine.SetValue("isnull", new Func<double?, bool>(x => x == null));
- RunTest(@"
- assert(isnull(undefined) === true);
- ");
- }
- [Fact]
- public void DelegateWithObjectParameterCanBePassedAnUndefined()
- {
- _engine.SetValue("isnull", new Func<object, bool>(x => x == null));
- RunTest(@"
- assert(isnull(undefined) === true);
- ");
- }
- [Fact]
- public void DelegateWithNullableParameterCanBeExcluded()
- {
- _engine.SetValue("isnull", new Func<double?, bool>(x => x == null));
- RunTest(@"
- assert(isnull() === true);
- ");
- }
- [Fact]
- public void DelegateWithObjectParameterCanBeExcluded()
- {
- _engine.SetValue("isnull", new Func<object, bool>(x => x == null));
- RunTest(@"
- assert(isnull() === true);
- ");
- }
- [Fact]
- public void ExtraParametersAreIgnored()
- {
- _engine.SetValue("passNumber", new Func<int, int>(x => x));
- RunTest(@"
- assert(passNumber(123,'test',{},[],null) === 123);
- ");
- }
- private delegate string callParams(params object[] values);
- private delegate string callArgumentAndParams(string firstParam, params object[] values);
- [Fact]
- public void DelegatesWithParamsParameterCanBeInvoked()
- {
- var a = new A();
- _engine.SetValue("callParams", new callParams(a.Call13));
- _engine.SetValue("callArgumentAndParams", new callArgumentAndParams(a.Call14));
- RunTest(@"
- assert(callParams('1','2','3') === '1,2,3');
- assert(callParams('1') === '1');
- assert(callParams() === '');
- assert(callArgumentAndParams('a','1','2','3') === 'a:1,2,3');
- assert(callArgumentAndParams('a','1') === 'a:1');
- assert(callArgumentAndParams('a') === 'a:');
- assert(callArgumentAndParams() === ':');
- ");
- }
- [Fact]
- public void CanGetObjectProperties()
- {
- var p = new Person
- {
- Name = "Mickey Mouse"
- };
- _engine.SetValue("p", p);
- RunTest(@"
- assert(p.Name === 'Mickey Mouse');
- ");
- }
- [Fact]
- public void CanInvokeObjectMethods()
- {
- var p = new Person
- {
- Name = "Mickey Mouse"
- };
- _engine.SetValue("p", p);
- RunTest(@"
- assert(p.ToString() === 'Mickey Mouse');
- ");
- }
- [Fact]
- public void CanInvokeObjectMethodsWithPascalCase()
- {
- var p = new Person
- {
- Name = "Mickey Mouse"
- };
- _engine.SetValue("p", p);
- RunTest(@"
- assert(p.toString() === 'Mickey Mouse');
- ");
- }
- [Fact]
- public void CanSetObjectProperties()
- {
- var p = new Person
- {
- Name = "Mickey Mouse"
- };
- _engine.SetValue("p", p);
- RunTest(@"
- p.Name = 'Donald Duck';
- assert(p.Name === 'Donald Duck');
- ");
- Assert.Equal("Donald Duck", p.Name);
- }
- [Fact]
- public void CanGetIndexUsingStringKey()
- {
- var dictionary = new Dictionary<string, Person>();
- dictionary.Add("person1", new Person { Name = "Mickey Mouse" });
- dictionary.Add("person2", new Person { Name = "Goofy" });
- _engine.SetValue("dictionary", dictionary);
- RunTest(@"
- assert(dictionary['person1'].Name === 'Mickey Mouse');
- assert(dictionary['person2'].Name === 'Goofy');
- ");
- }
- [Fact]
- public void CanSetIndexUsingStringKey()
- {
- var dictionary = new Dictionary<string, Person>();
- dictionary.Add("person1", new Person { Name = "Mickey Mouse" });
- dictionary.Add("person2", new Person { Name = "Goofy" });
- _engine.SetValue("dictionary", dictionary);
- RunTest(@"
- dictionary['person2'].Name = 'Donald Duck';
- assert(dictionary['person2'].Name === 'Donald Duck');
- ");
- Assert.Equal("Donald Duck", dictionary["person2"].Name);
- }
- [Fact]
- public void CanGetIndexUsingIntegerKey()
- {
- var dictionary = new Dictionary<int, string>();
- dictionary.Add(1, "Mickey Mouse");
- dictionary.Add(2, "Goofy");
- _engine.SetValue("dictionary", dictionary);
- RunTest(@"
- assert(dictionary[1] === 'Mickey Mouse');
- assert(dictionary[2] === 'Goofy');
- ");
- }
- [Fact]
- public void CanSetIndexUsingIntegerKey()
- {
- var dictionary = new Dictionary<int, string>();
- dictionary.Add(1, "Mickey Mouse");
- dictionary.Add(2, "Goofy");
- _engine.SetValue("dictionary", dictionary);
- RunTest(@"
- dictionary[2] = 'Donald Duck';
- assert(dictionary[2] === 'Donald Duck');
- ");
- Assert.Equal("Mickey Mouse", dictionary[1]);
- Assert.Equal("Donald Duck", dictionary[2]);
- }
- [Fact]
- public void CanUseGenericMethods()
- {
- var dictionary = new Dictionary<int, string>();
- dictionary.Add(1, "Mickey Mouse");
- _engine.SetValue("dictionary", dictionary);
- RunTest(@"
- dictionary.Add(2, 'Goofy');
- assert(dictionary[2] === 'Goofy');
- ");
- Assert.Equal("Mickey Mouse", dictionary[1]);
- Assert.Equal("Goofy", dictionary[2]);
- }
- [Fact]
- public void CanUseMultiGenericTypes()
- {
- RunTest(@"
- var type = System.Collections.Generic.Dictionary(System.Int32, System.String);
- var dictionary = new type();
- dictionary.Add(1, 'Mickey Mouse');
- dictionary.Add(2, 'Goofy');
- assert(dictionary[2] === 'Goofy');
- ");
- }
- [Fact]
- public void CanUseIndexOnCollection()
- {
- var collection = new System.Collections.ObjectModel.Collection<string>();
- collection.Add("Mickey Mouse");
- collection.Add("Goofy");
- _engine.SetValue("dictionary", collection);
- RunTest(@"
- dictionary[1] = 'Donald Duck';
- assert(dictionary[1] === 'Donald Duck');
- ");
- Assert.Equal("Mickey Mouse", collection[0]);
- Assert.Equal("Donald Duck", collection[1]);
- }
- [Fact]
- public void CanUseIndexOnList()
- {
- var list = new List<object>(2);
- list.Add("Mickey Mouse");
- list.Add("Goofy");
- _engine.SetValue("list", list);
- RunTest(@"
- list[1] = 'Donald Duck';
- assert(list[1] === 'Donald Duck');
- ");
- Assert.Equal("Mickey Mouse", list[0]);
- Assert.Equal("Donald Duck", list[1]);
- }
- [Fact]
- public void CanAccessAnonymousObject()
- {
- var p = new
- {
- Name = "Mickey Mouse",
- };
- _engine.SetValue("p", p);
- RunTest(@"
- assert(p.Name === 'Mickey Mouse');
- ");
- }
- [Fact]
- public void CanAccessAnonymousObjectProperties()
- {
- var p = new
- {
- Address = new
- {
- City = "Mouseton"
- }
- };
- _engine.SetValue("p", p);
- RunTest(@"
- assert(p.Address.City === 'Mouseton');
- ");
- }
- [Fact]
- public void PocosCanReturnJsValueDirectly()
- {
- var o = new
- {
- x = new JsValue(1),
- y = new JsValue("string"),
- };
- _engine.SetValue("o", o);
- RunTest(@"
- assert(o.x === 1);
- assert(o.y === 'string');
- ");
- }
- [Fact]
- public void PocosCanReturnObjectInstanceDirectly()
- {
- var x = new ObjectInstance(_engine) { Extensible = true};
- x.Put("foo", new JsValue("bar"), false);
- var o = new
- {
- x
- };
- _engine.SetValue("o", o);
- RunTest(@"
- assert(o.x.foo === 'bar');
- ");
- }
- [Fact]
- public void DateTimeIsConvertedToDate()
- {
- var o = new
- {
- z = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)
- };
- _engine.SetValue("o", o);
- RunTest(@"
- assert(o.z.valueOf() === 0);
- ");
- }
- [Fact]
- public void DateTimeOffsetIsConvertedToDate()
- {
- var o = new
- {
- z = new DateTimeOffset(1970, 1, 1, 0, 0, 0, new TimeSpan())
- };
- _engine.SetValue("o", o);
- RunTest(@"
- assert(o.z.valueOf() === 0);
- ");
- }
- [Fact]
- public void EcmaValuesAreAutomaticallyConvertedWhenSetInPoco()
- {
- var p = new Person
- {
- Name = "foo",
- };
- _engine.SetValue("p", p);
- RunTest(@"
- assert(p.Name === 'foo');
- assert(p.Age === 0);
- p.Name = 'bar';
- p.Age = 10;
- ");
- Assert.Equal("bar", p.Name);
- Assert.Equal(10, p.Age);
- }
- [Fact]
- public void EcmaValuesAreAutomaticallyConvertedToBestMatchWhenSetInPoco()
- {
- var p = new Person
- {
- Name = "foo",
- };
- _engine.SetValue("p", p);
- RunTest(@"
- p.Name = 10;
- p.Age = '20';
- ");
- Assert.Equal("10", p.Name);
- Assert.Equal(20, p.Age);
- }
- [Fact]
- public void ShouldCallInstanceMethodWithoutArgument()
- {
- _engine.SetValue("a", new A());
- RunTest(@"
- assert(a.Call1() === 0);
- ");
- }
- [Fact]
- public void ShouldCallInstanceMethodOverloadArgument()
- {
- _engine.SetValue("a", new A());
- RunTest(@"
- assert(a.Call1(1) === 1);
- ");
- }
- [Fact]
- public void ShouldCallInstanceMethodWithString()
- {
- var p = new Person();
- _engine.SetValue("a", new A());
- _engine.SetValue("p", p);
- RunTest(@"
- p.Name = a.Call2('foo');
- assert(p.Name === 'foo');
- ");
- Assert.Equal("foo", p.Name);
- }
- [Fact]
- public void CanUseTrim()
- {
- var p = new Person { Name = "Mickey Mouse "};
- _engine.SetValue("p", p);
- RunTest(@"
- assert(p.Name === 'Mickey Mouse ');
- p.Name = p.Name.trim();
- assert(p.Name === 'Mickey Mouse');
- ");
- Assert.Equal("Mickey Mouse", p.Name);
- }
- [Fact]
- public void CanUseMathFloor()
- {
- var p = new Person();
- _engine.SetValue("p", p);
- RunTest(@"
- p.Age = Math.floor(1.6);p
- assert(p.Age === 1);
- ");
- Assert.Equal(1, p.Age);
- }
- [Fact]
- public void CanUseDelegateAsFunction()
- {
- var even = new Func<int, bool>(x => x % 2 == 0);
- _engine.SetValue("even", even);
- RunTest(@"
- assert(even(2) === true);
- ");
- }
- private class TestClass
- {
- public int? NullableInt { get; set; }
- public DateTime? NullableDate { get; set; }
- public bool? NullableBool { get; set; }
- }
- [Fact]
- public void CanSetNullablePropertiesOnPocos()
- {
- var instance = new TestClass();
- _engine.SetValue("instance", instance);
- RunTest(@"
- instance.NullableInt = 2;
- instance.NullableDate = new Date();
- instance.NullableBool = true;
- assert(instance.NullableInt===2);
- assert(instance.NullableDate!=null);
- assert(instance.NullableBool===true);
- ");
- }
- [Fact]
- public void ShouldConvertArrayToArrayInstance()
- {
- var result = _engine
- .SetValue("values", new[] { 1, 2, 3, 4, 5, 6 })
- .Execute("values.filter(function(x){ return x % 2 == 0; })");
- var parts = result.GetCompletionValue().ToObject();
- Assert.True(parts.GetType().IsArray);
- Assert.Equal(3, ((object[])parts).Length);
- Assert.Equal(2d, ((object[])parts)[0]);
- Assert.Equal(4d, ((object[])parts)[1]);
- Assert.Equal(6d, ((object[])parts)[2]);
- }
- [Fact]
- public void ShouldConvertListsToArrayInstance()
- {
- var result = _engine
- .SetValue("values", new List<object> { 1, 2, 3, 4, 5, 6 })
- .Execute("new Array(values).filter(function(x){ return x % 2 == 0; })");
- var parts = result.GetCompletionValue().ToObject();
- Assert.True(parts.GetType().IsArray);
- Assert.Equal(3, ((object[])parts).Length);
- Assert.Equal(2d, ((object[])parts)[0]);
- Assert.Equal(4d, ((object[])parts)[1]);
- Assert.Equal(6d, ((object[])parts)[2]);
- }
- [Fact]
- public void ShouldConvertArrayInstanceToArray()
- {
- var result = _engine.Execute("'[email protected]'.split('@');");
- var parts = result.GetCompletionValue().ToObject();
- Assert.True(parts.GetType().IsArray);
- Assert.Equal(2, ((object[])parts).Length);
- Assert.Equal("foo", ((object[])parts)[0]);
- Assert.Equal("bar.com", ((object[])parts)[1]);
- }
- [Fact]
- public void ShouldConvertBooleanInstanceToBool()
- {
- var result = _engine.Execute("new Boolean(true)");
- var value = result.GetCompletionValue().ToObject();
- Assert.Equal(typeof(bool), value.GetType());
- Assert.Equal(true, value);
- }
- [Fact]
- public void ShouldConvertDateInstanceToDateTime()
- {
- var result = _engine.Execute("new Date(0)");
- var value = result.GetCompletionValue().ToObject();
- Assert.Equal(typeof(DateTime), value.GetType());
- Assert.Equal(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc), value);
- }
- [Fact]
- public void ShouldConvertNumberInstanceToDouble()
- {
- var result = _engine.Execute("new Number(10)");
- var value = result.GetCompletionValue().ToObject();
- Assert.Equal(typeof(double), value.GetType());
- Assert.Equal(10d, value);
- }
- [Fact]
- public void ShouldConvertStringInstanceToString()
- {
- var result = _engine.Execute("new String('foo')");
- var value = result.GetCompletionValue().ToObject();
- Assert.Equal(typeof(string), value.GetType());
- Assert.Equal("foo", value);
- }
- [Fact]
- public void ShouldConvertObjectInstanceToExpando()
- {
- _engine.Execute("var o = {a: 1, b: 'foo'}");
- var result = _engine.GetValue("o");
- dynamic value = result.ToObject();
- Assert.Equal(1, value.a);
- Assert.Equal("foo", value.b);
- var dic = (IDictionary<string, object>)result.ToObject();
- Assert.Equal(1d, dic["a"]);
- Assert.Equal("foo", dic["b"]);
- }
- [Fact]
- public void ShouldNotTryToConvertCompatibleTypes()
- {
- _engine.SetValue("a", new A());
- RunTest(@"
- assert(a.Call3('foo') === 'foo');
- assert(a.Call3(1) === '1');
- ");
- }
- [Fact]
- public void ShouldNotTryToConvertDerivedTypes()
- {
- _engine.SetValue("a", new A());
- _engine.SetValue("p", new Person { Name = "Mickey" });
- RunTest(@"
- assert(a.Call4(p) === 'Mickey');
- ");
- }
- [Fact]
- public void ShouldExecuteFunctionCallBackAsDelegate()
- {
- _engine.SetValue("a", new A());
- RunTest(@"
- assert(a.Call5(function(a,b){ return a+b }) === '1foo');
- ");
- }
- [Fact]
- public void ShouldExecuteFunctionCallBackAsFuncAndThisCanBeAssigned()
- {
- _engine.SetValue("a", new A());
- RunTest(@"
- assert(a.Call6(function(a,b){ return this+a+b }) === 'bar1foo');
- ");
- }
- [Fact]
- public void ShouldExecuteFunctionCallBackAsPredicate()
- {
- _engine.SetValue("a", new A());
- // Func<>
- RunTest(@"
- assert(a.Call8(function(){ return 'foo'; }) === 'foo');
- ");
- }
- [Fact]
- public void ShouldExecuteFunctionWithParameterCallBackAsPredicate()
- {
- _engine.SetValue("a", new A());
- // Func<,>
- RunTest(@"
- assert(a.Call7('foo', function(a){ return a === 'foo'; }) === true);
- ");
- }
- [Fact]
- public void ShouldExecuteActionCallBackAsPredicate()
- {
- _engine.SetValue("a", new A());
- // Action
- RunTest(@"
- var value;
- a.Call9(function(){ value = 'foo'; });
- assert(value === 'foo');
- ");
- }
- [Fact]
- public void ShouldExecuteActionWithParameterCallBackAsPredicate()
- {
- _engine.SetValue("a", new A());
- // Action<>
- RunTest(@"
- var value;
- a.Call10('foo', function(b){ value = b; });
- assert(value === 'foo');
- ");
- }
- [Fact]
- public void ShouldExecuteActionWithMultipleParametersCallBackAsPredicate()
- {
- _engine.SetValue("a", new A());
- // Action<,>
- RunTest(@"
- var value;
- a.Call11('foo', 'bar', function(a,b){ value = a + b; });
- assert(value === 'foobar');
- ");
- }
- [Fact]
- public void ShouldExecuteFunc()
- {
- _engine.SetValue("a", new A());
- // Func<int, int>
- RunTest(@"
- var result = a.Call12(42, function(a){ return a + a; });
- assert(result === 84);
- ");
- }
- [Fact]
- public void ShouldExecuteActionCallbackOnEventChanged()
- {
- var collection = new System.Collections.ObjectModel.ObservableCollection<string>();
- Assert.True(collection.Count == 0);
- _engine.SetValue("collection", collection);
- RunTest(@"
- var eventAction;
- collection.add_CollectionChanged(function(sender, eventArgs) { eventAction = eventArgs.Action; } );
- collection.Add('test');
- ");
- var eventAction = _engine.GetValue("eventAction").AsNumber();
- Assert.True(eventAction == 0);
- Assert.True(collection.Count == 1);
- }
- [Fact]
- public void ShouldUseSystemIO()
- {
- RunTest(@"
- var filename = System.IO.Path.GetTempFileName();
- var sw = System.IO.File.CreateText(filename);
- sw.Write('Hello World');
- sw.Dispose();
- var content = System.IO.File.ReadAllText(filename);
- System.Console.WriteLine(content);
- assert(content === 'Hello World');
- ");
- }
- [Fact]
- public void ShouldImportNamespace()
- {
- RunTest(@"
- var Shapes = importNamespace('Shapes');
- var circle = new Shapes.Circle();
- assert(circle.Radius === 0);
- assert(circle.Perimeter() === 0);
- ");
- }
- [Fact]
- public void ShouldConstructReferenceTypeWithParameters()
- {
- RunTest(@"
- var Shapes = importNamespace('Shapes');
- var circle = new Shapes.Circle(1);
- assert(circle.Radius === 1);
- assert(circle.Perimeter() === Math.PI);
- ");
- }
- [Fact]
- public void ShouldConstructValueTypeWithoutParameters()
- {
- RunTest(@"
- var guid = new System.Guid();
- assert('00000000-0000-0000-0000-000000000000' === guid.ToString());
- ");
- }
- [Fact]
- public void ShouldInvokeAFunctionByName()
- {
- RunTest(@"
- function add(x, y) { return x + y; }
- ");
- Assert.Equal(3, _engine.Invoke("add", 1, 2));
- }
- [Fact]
- public void ShouldNotInvokeNonFunctionValue()
- {
- RunTest(@"
- var x= 10;
- ");
- Assert.Throws<ArgumentException>(() => _engine.Invoke("x", 1, 2));
- }
- [Fact]
- public void CanGetField()
- {
- var o = new ClassWithField
- {
- Field = "Mickey Mouse"
- };
- _engine.SetValue("o", o);
- RunTest(@"
- assert(o.Field === 'Mickey Mouse');
- ");
- }
- [Fact]
- public void CanSetField()
- {
- var o = new ClassWithField();
- _engine.SetValue("o", o);
- RunTest(@"
- o.Field = 'Mickey Mouse';
- assert(o.Field === 'Mickey Mouse');
- ");
- Assert.Equal("Mickey Mouse", o.Field);
- }
- [Fact]
- public void CanGetStaticField()
- {
- RunTest(@"
- var domain = importNamespace('Jint.Tests.Runtime.Domain');
- var statics = domain.ClassWithStaticFields;
- assert(statics.Get == 'Get');
- ");
- }
- [Fact]
- public void CanSetStaticField()
- {
- RunTest(@"
- var domain = importNamespace('Jint.Tests.Runtime.Domain');
- var statics = domain.ClassWithStaticFields;
- statics.Set = 'hello';
- assert(statics.Set == 'hello');
- ");
- Assert.Equal(ClassWithStaticFields.Set, "hello");
- }
- [Fact]
- public void CanGetStaticAccessor()
- {
- RunTest(@"
- var domain = importNamespace('Jint.Tests.Runtime.Domain');
- var statics = domain.ClassWithStaticFields;
- assert(statics.Getter == 'Getter');
- ");
- }
- [Fact]
- public void CanSetStaticAccessor()
- {
- RunTest(@"
- var domain = importNamespace('Jint.Tests.Runtime.Domain');
- var statics = domain.ClassWithStaticFields;
- statics.Setter = 'hello';
- assert(statics.Setter == 'hello');
- ");
- Assert.Equal(ClassWithStaticFields.Setter, "hello");
- }
- [Fact]
- public void CantSetStaticReadonly()
- {
- RunTest(@"
- var domain = importNamespace('Jint.Tests.Runtime.Domain');
- var statics = domain.ClassWithStaticFields;
- statics.Readonly = 'hello';
- assert(statics.Readonly == 'Readonly');
- ");
- Assert.Equal(ClassWithStaticFields.Readonly, "Readonly");
- }
- [Fact]
- public void CanSetCustomConverters()
- {
- var engine1 = new Engine();
- engine1.SetValue("p", new { Test = true });
- engine1.Execute("var result = p.Test;");
- Assert.True((bool)engine1.GetValue("result").ToObject());
- var engine2 = new Engine(o => o.AddObjectConverter(new NegateBoolConverter()));
- engine2.SetValue("p", new { Test = true });
- engine2.Execute("var result = p.Test;");
- Assert.False((bool)engine2.GetValue("result").ToObject());
- }
- [Fact]
- public void CanConvertEnumsToString()
- {
- var engine1 = new Engine(o => o.AddObjectConverter(new EnumsToStringConverter()))
- .SetValue("assert", new Action<bool>(Assert.True));
- engine1.SetValue("p", new { Comparison = StringComparison.CurrentCulture });
- engine1.Execute("assert(p.Comparison === 'CurrentCulture');");
- engine1.Execute("var result = p.Comparison;");
- Assert.Equal("CurrentCulture", (string)engine1.GetValue("result").ToObject());
- }
- [Fact]
- public void CanUserIncrementOperator()
- {
- var p = new Person
- {
- Age = 1,
- };
- _engine.SetValue("p", p);
- RunTest(@"
- assert(++p.Age === 2);
- ");
- Assert.Equal(2, p.Age);
- }
- [Fact]
- public void CanOverwriteValues()
- {
- _engine.SetValue("x", 3);
- _engine.SetValue("x", 4);
- RunTest(@"
- assert(x === 4);
- ");
- }
- [Fact]
- public void ShouldCreateGenericType()
- {
- RunTest(@"
- var ListOfString = System.Collections.Generic.List(System.String);
- var list = new ListOfString();
- list.Add('foo');
- list.Add(1);
- assert(2 === list.Count);
- ");
- }
- [Fact]
- public void EnumComparesByName()
- {
- var o = new
- {
- r = Colors.Red,
- b = Colors.Blue,
- g = Colors.Green,
- b2 = Colors.Red
- };
- _engine.SetValue("o", o);
- _engine.SetValue("assertFalse", new Action<bool>(Assert.False));
- RunTest(@"
- var domain = importNamespace('Jint.Tests.Runtime.Domain');
- var colors = domain.Colors;
- assert(o.r === colors.Red);
- assert(o.g === colors.Green);
- assert(o.b === colors.Blue);
- assertFalse(o.b2 === colors.Blue);
- ");
- }
- [Fact]
- public void ShouldSetEnumProperty()
- {
- var s = new Circle
- {
- Color = Colors.Red,
- };
- _engine.SetValue("s", s);
- RunTest(@"
- var domain = importNamespace('Jint.Tests.Runtime.Domain');
- var colors = domain.Colors;
- s.Color = colors.Blue;
- assert(s.Color === colors.Blue);
- ");
- _engine.SetValue("s", s);
- RunTest(@"
- s.Color = colors.Blue | colors.Green;
- assert(s.Color === colors.Blue | colors.Green);
- ");
- Assert.Equal(Colors.Blue | Colors.Green, s.Color);
- }
- [Fact]
- public void EnumIsConvertedToNumber()
- {
- var o = new
- {
- r = Colors.Red,
- b = Colors.Blue,
- g = Colors.Green
- };
- _engine.SetValue("o", o);
- RunTest(@"
- assert(o.r === 0);
- assert(o.g === 1);
- assert(o.b === 10);
- ");
- }
- [Fact]
- public void ShouldConvertToEnum()
- {
- var s = new Circle
- {
- Color = Colors.Red,
- };
- _engine.SetValue("s", s);
- RunTest(@"
- assert(s.Color === 0);
- s.Color = 10;
- assert(s.Color === 10);
- ");
- _engine.SetValue("s", s);
- RunTest(@"
- s.Color = 11;
- assert(s.Color === 11);
- ");
- Assert.Equal(Colors.Blue | Colors.Green, s.Color);
- }
- [Fact]
- public void ShouldUseExplicitPropertyGetter()
- {
- _engine.SetValue("c", new Company("ACME"));
- RunTest(@"
- assert(c.Name === 'ACME');
- ");
- }
- [Fact]
- public void ShouldUseExplicitIndexerPropertyGetter()
- {
- var company = new Company("ACME");
- ((ICompany)company)["Foo"] = "Bar";
- _engine.SetValue("c", company);
- RunTest(@"
- assert(c.Foo === 'Bar');
- ");
- }
- [Fact]
- public void ShouldUseExplicitPropertySetter()
- {
- _engine.SetValue("c", new Company("ACME"));
- RunTest(@"
- c.Name = 'Foo';
- assert(c.Name === 'Foo');
- ");
- }
- [Fact]
- public void ShouldUseExplicitIndexerPropertySetter()
- {
- var company = new Company("ACME");
- ((ICompany)company)["Foo"] = "Bar";
- _engine.SetValue("c", company);
- RunTest(@"
- c.Foo = 'Baz';
- assert(c.Foo === 'Baz');
- ");
- }
- [Fact]
- public void ShouldUseExplicitMethod()
- {
- _engine.SetValue("c", new Company("ACME"));
- RunTest(@"
- assert(0 === c.CompareTo(c));
- ");
- }
- [Fact]
- public void ShouldCallInstanceMethodWithParams()
- {
- _engine.SetValue("a", new A());
- RunTest(@"
- assert(a.Call13('1','2','3') === '1,2,3');
- assert(a.Call13('1') === '1');
- assert(a.Call13(1) === '1');
- assert(a.Call13() === '');
- assert(a.Call14('a','1','2','3') === 'a:1,2,3');
- assert(a.Call14('a','1') === 'a:1');
- assert(a.Call14('a') === 'a:');
- function call13wrapper(){ return a.Call13.apply(a, Array.prototype.slice.call(arguments)); }
- assert(call13wrapper('1','2','3') === '1,2,3');
- assert(a.Call13('1','2','3') === a.Call13(['1','2','3']));
- ");
- }
- [Fact]
- public void ShouldCallInstanceMethodWithJsValueParams()
- {
- _engine.SetValue("a", new A());
- RunTest(@"
- assert(a.Call16('1','2','3') === '1,2,3');
- assert(a.Call16('1') === '1');
- assert(a.Call16(1) === '1');
- assert(a.Call16() === '');
- assert(a.Call16('1','2','3') === a.Call16(['1','2','3']));
- ");
- }
- [Fact]
- public void NullValueAsArgumentShouldWork()
- {
- _engine.SetValue("a", new A());
- RunTest(@"
- var x = a.Call2(null);
- assert(x === null);
- ");
- }
- [Fact]
- public void ShouldSetPropertyToNull()
- {
- var p = new Person { Name = "Mickey" };
- _engine.SetValue("p", p);
- RunTest(@"
- assert(p.Name != null);
- p.Name = null;
- assert(p.Name == null);
- ");
- Assert.True(p.Name == null);
- }
- [Fact]
- public void ShouldCallMethodWithNull()
- {
- _engine.SetValue("a", new A());
- RunTest(@"
- a.Call15(null);
- var result = a.Call2(null);
- assert(result == null);
- ");
- }
- [Fact]
- public void ShouldReturnUndefinedProperty()
- {
- _engine.SetValue("uo", new { foo = "bar" });
- _engine.SetValue("ud", new Dictionary<string, object>() { {"foo", "bar"} });
- _engine.SetValue("ul", new List<string>() { "foo", "bar" });
- RunTest(@"
- assert(!uo.undefinedProperty);
- assert(!ul[5]);
- assert(!ud.undefinedProperty);
- ");
- }
- [Fact]
- public void ShouldAutomaticallyConvertArraysToFindBestInteropResulution()
- {
- _engine.SetValue("a", new ArrayConverterTestClass());
- _engine.SetValue("item1", new ArrayConverterItem(1));
- _engine.SetValue("item2", new ArrayConverterItem(2));
- RunTest(@"
- assert(a.MethodAcceptsArrayOfInt([false, '1', 2]) === a.MethodAcceptsArrayOfInt([0, 1, 2]));
- assert(a.MethodAcceptsArrayOfStrings(['1', 2]) === a.MethodAcceptsArrayOfStrings([1, 2]));
- assert(a.MethodAcceptsArrayOfBool(['1', 0]) === a.MethodAcceptsArrayOfBool([true, false]));
- assert(a.MethodAcceptsArrayOfStrings([item1, item2]) === a.MethodAcceptsArrayOfStrings(['1', '2']));
- assert(a.MethodAcceptsArrayOfInt([item1, item2]) === a.MethodAcceptsArrayOfInt([1, 2]));
- ");
- }
- [Fact]
- public void ShouldImportNamespaceNestedType()
- {
- RunTest(@"
- var shapes = importNamespace('Shapes.Circle');
- var kinds = shapes.Kind;
- assert(kinds.Unit === 0);
- assert(kinds.Ellipse === 1);
- assert(kinds.Round === 5);
- ");
- }
- [Fact]
- public void ShouldImportNamespaceNestedNestedType()
- {
- RunTest(@"
- var meta = importNamespace('Shapes.Circle.Meta');
- var usages = meta.Usage;
- assert(usages.Public === 0);
- assert(usages.Private === 1);
- assert(usages.Internal === 11);
- ");
- }
- [Fact]
- public void ShouldGetNestedNestedProp()
- {
- RunTest(@"
- var meta = importNamespace('Shapes.Circle');
- var m = new meta.Meta();
- assert(m.Description === 'descp');
- ");
- }
- [Fact]
- public void ShouldSetNestedNestedProp()
- {
- RunTest(@"
- var meta = importNamespace('Shapes.Circle');
- var m = new meta.Meta();
- m.Description = 'hello';
- assert(m.Description === 'hello');
- ");
- }
- [Fact]
- public void CanGetStaticNestedField()
- {
- RunTest(@"
- var domain = importNamespace('Jint.Tests.Runtime.Domain.Nested');
- var statics = domain.ClassWithStaticFields;
- assert(statics.Get == 'Get');
- ");
- }
- [Fact]
- public void CanSetStaticNestedField()
- {
- RunTest(@"
- var domain = importNamespace('Jint.Tests.Runtime.Domain.Nested');
- var statics = domain.ClassWithStaticFields;
- statics.Set = 'hello';
- assert(statics.Set == 'hello');
- ");
- Assert.Equal(Nested.ClassWithStaticFields.Set, "hello");
- }
- [Fact]
- public void CanGetStaticNestedAccessor()
- {
- RunTest(@"
- var domain = importNamespace('Jint.Tests.Runtime.Domain.Nested');
- var statics = domain.ClassWithStaticFields;
- assert(statics.Getter == 'Getter');
- ");
- }
- [Fact]
- public void CanSetStaticNestedAccessor()
- {
- RunTest(@"
- var domain = importNamespace('Jint.Tests.Runtime.Domain.Nested');
- var statics = domain.ClassWithStaticFields;
- statics.Setter = 'hello';
- assert(statics.Setter == 'hello');
- ");
- Assert.Equal(Nested.ClassWithStaticFields.Setter, "hello");
- }
- [Fact]
- public void CantSetStaticNestedReadonly()
- {
- RunTest(@"
- var domain = importNamespace('Jint.Tests.Runtime.Domain.Nested');
- var statics = domain.ClassWithStaticFields;
- statics.Readonly = 'hello';
- assert(statics.Readonly == 'Readonly');
- ");
- Assert.Equal(Nested.ClassWithStaticFields.Readonly, "Readonly");
- }
- [Fact]
- public void ShouldExecuteFunctionWithValueTypeParameterCorrectly()
- {
- _engine.SetValue("a", new A());
- // Func<int, int>
- RunTest(@"
- assert(a.Call17(function(value){ return value; }) === 17);
- ");
- }
- [Fact]
- public void ShouldExecuteActionWithValueTypeParameterCorrectly()
- {
- _engine.SetValue("a", new A());
- // Action<int>
- RunTest(@"
- a.Call18(function(value){ assert(value === 18); });
- ");
- }
- [Fact]
- public void ShouldConvertToJsValue()
- {
- RunTest(@"
- var now = System.DateTime.Now;
- assert(new String(now) == now.toString());
- var zero = System.Int32.MaxValue;
- assert(new String(zero) == zero.toString());
- ");
- }
- [Fact]
- public void ShouldCatchClrExceptions()
- {
- string exceptionMessage = "myExceptionMessage";
- _engine.SetValue("throwMyException", new Action(() => { throw new Exception(exceptionMessage); }));
-
- RunTest(@"
- function throwException(){
- try {
- throwMyException();
- return '';
- }
- catch(e) {
- return e.message;
- }
- }
- ");
- var result = _engine.Invoke("throwException");
- Assert.Equal(result.AsString(), exceptionMessage);
- }
- }
- }
|