Example #1
0
        public void GenericExtensionMethodOnGenericTypeInstantiatedInJs()
        {
            var options = new Options();

            options.AddExtensionMethods(typeof(ObservableExtensions));

            var engine = new Engine(options);

            engine.SetValue("BaseObservable", typeof(BaseObservable <>));
            engine.SetValue("ObservableFactory", typeof(ObservableFactory));

            var result = engine.Evaluate(@"

                // you can't instantiate generic types in JS (without providing the types as arguments to the constructor) - i.e. not compatible with transpiled typescript
                //const observable = new BaseObservable();
                //const observable = BaseObservable.GetBoolBaseObservable();
                const observable = ObservableFactory.GetBoolBaseObservable();

                var result = observable.Where(function(someBool){
                    return true;
                });
                observable.Update(false);
                observable.BroadcastCompleted();

                return result;
            ");

            var baseObservableResult = result.ToObject() as BaseObservable <bool>;

            System.Console.WriteLine("GenericExtensionMethodOnOpenGenericType: baseObservableResult: " + baseObservableResult);
            Assert.NotNull(baseObservableResult);
            Assert.Equal(false, baseObservableResult.Last);
        }
Example #2
0
        public void GenericExtensionMethodOnClosedGenericType2()
        {
            var options = new Options();

            options.AddExtensionMethods(typeof(ObservableExtensions));

            var engine = new Engine(options);

            NameObservable observable = new NameObservable();

            observable.Where((text) =>
            {
                System.Console.WriteLine("GenericExtensionMethodOnClosedGenericType2: NameObservable: Where: text: " + text);
                return(true);
            });
            engine.SetValue("observable", observable);
            var result = engine.Evaluate(@"
                var result = observable.Where(function(text){
                    return true;
                });

                observable.UpdateName('testing yo');
                observable.CommitName();
                return result;
            ");

            var nameObservableResult = result.ToObject() as NameObservable;

            Assert.NotNull(nameObservableResult);
            Assert.Equal("testing yo", nameObservableResult.Last);
        }
Example #3
0
        public void GenericExtensionMethodOnOpenGenericType()
        {
            var options = new Options();

            options.AddExtensionMethods(typeof(ObservableExtensions));

            var engine = new Engine(options);

            BaseObservable <string> observable = new BaseObservable <string>();

            observable.Where((text) =>
            {
                System.Console.WriteLine("GenericExtensionMethodOnOpenGenericType: BaseObservable: Where: text: " + text);
                return(true);
            });
            engine.SetValue("observable", observable);
            var result = engine.Evaluate(@"
                var result = observable.Where(function(text){
                    return true;
                });

                observable.Update('testing yo');
                observable.BroadcastCompleted();

                return result;
            ");

            System.Console.WriteLine("GenericExtensionMethodOnOpenGenericType: result: " + result + " result.ToString(): " + result.ToString());
            var baseObservableResult = result.ToObject() as BaseObservable <string>;

            System.Console.WriteLine("GenericExtensionMethodOnOpenGenericType: baseObservableResult: " + baseObservableResult);
            Assert.NotNull(baseObservableResult);
            Assert.Equal("testing yo", baseObservableResult.Last);
        }
Example #4
0
        public void GenericTypeExtension()
        {
            var options = new Options();

            options.AddExtensionMethods(typeof(ObservableExtensions));

            var engine = new Engine(options);

            engine.SetValue("log", new System.Action <object>(System.Console.WriteLine));

            NameObservable observable = new NameObservable();

            engine.SetValue("observable", observable);
            engine.Evaluate(@"
                log('before');
                observable.Subscribe((name) =>{
                    log('observable: subscribe: name: ' + name);
                });

                observable.UpdateName('foobar');
                log('after');
            ");

            Assert.Equal("foobar", observable.Last);
        }
Example #5
0
        public void ShouldPrioritizingNonGenericMethod()
        {
            var options = new Options();

            options.AddExtensionMethods(typeof(CustomStringExtensions));

            var engine = new Engine(options);
            var result = engine.Evaluate("\"{'name':'Mickey'}\".DeserializeObject()").ToObject() as dynamic;

            Assert.Equal("Mickey", result.name);
        }
Example #6
0
        public void ShouldInvokeNumberExtensionMethod()
        {
            var options = new Options();

            options.AddExtensionMethods(typeof(DoubleExtensions));

            var engine = new Engine(options);
            var result = engine.Evaluate("let numb = 27; numb.Add(13)").AsInteger();

            Assert.Equal(40, result);
        }
Example #7
0
        public void ShouldInvokeStringExtensionMethod()
        {
            var options = new Options();

            options.AddExtensionMethods(typeof(CustomStringExtensions));

            var engine = new Engine(options);
            var result = engine.Evaluate("\"Hello World!\".Backwards()").AsString();

            Assert.Equal("!dlroW olleH", result);
        }
Example #8
0
        public void ShouldInvokeObjectExtensionMethod()
        {
            var person = new Person();

            person.Name = "Mickey Mouse";
            person.Age  = 35;

            var options = new Options();

            options.AddExtensionMethods(typeof(PersonExtensions));

            var engine = new Engine(options);

            engine.SetValue("person", person);
            var age = engine.Evaluate("person.MultiplyAge(2)").AsInteger();

            Assert.Equal(70, age);
        }
Example #9
0
        public void HasOwnPropertyShouldWorkCorrectlyInPresenceOfExtensionMethods()
        {
            var person = new Person();

            var options = new Options();

            options.AddExtensionMethods(typeof(PersonExtensions));

            var engine = new Engine(options);

            engine.SetValue("person", person);

            var isBogusInPerson = engine.Evaluate("'bogus' in person").AsBoolean();

            Assert.False(isBogusInPerson);

            var propertyValue = engine.Evaluate("person.bogus");

            Assert.Equal(JsValue.Undefined, propertyValue);
        }
Example #10
0
        public void GenericExtensionMethodOnClosedGenericType()
        {
            var options = new Options();

            options.AddExtensionMethods(typeof(ObservableExtensions));

            var engine = new Engine(options);

            engine.SetValue("log", new System.Action <object>(System.Console.WriteLine));

            NameObservable observable = new NameObservable();

            engine.SetValue("observable", observable);
            var result = engine.Evaluate(@"
                log('before calling Select');
                var result = observable.Select('some text');
                log('result: ' + result);
                return result;
            ");

            //System.Console.WriteLine("GenericExtensionMethodOnGenericType: result: " + result + " result.ToString(): " + result.ToString());

            Assert.Equal("some text", result);
        }
Example #11
0
        static async Task Main(string[] args)
        {
            var sc = new ServiceCollection();

            sc.AddScripter(options => options
                           .AddJavaScriptEngine()
                           .AddTypeScriptEngine()
                           .AddScripterModule <ConsoleWriterModule>()
                           .AddScripterModule <CommonModule>()
                           .AddScripterModule <GlobalVariablesModule>()
                           );

            sc.AddSingleton <IVariablesRepository, VariableRepository>();

            sc.AddScoped <Options>(provider =>
            {
                var opts = new Options();
                opts.AddExtensionMethods(typeof(StringExtensions));
                return(opts);
            });
            ServiceProvider = sc.BuildServiceProvider();

            await Execute();
        }