<commit_before>﻿using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
using ReactNative.Bridge.Queue;
using ReactNative.Chakra.Executor;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Windows.Storage;

namespace ReactNative.Tests
{
    static class JavaScriptHelpers
    {
        public static Task Run(Action<ChakraJavaScriptExecutor, IMessageQueueThread> action)
        {
            return Run((executor, jsQueueThread) =>
            {
                action(executor, jsQueueThread);
                return Task.CompletedTask;
            });
        }

        public static async Task Run(Func<ChakraJavaScriptExecutor, IMessageQueueThread, Task> action)
        {
            using (var jsQueueThread = CreateJavaScriptThread())
            {
                var executor = await jsQueueThread.CallOnQueue(() => new ChakraJavaScriptExecutor());
                try
                {
                    await Initialize(executor, jsQueueThread);
                    await action(executor, jsQueueThread);
                }
                finally
                {
                    await jsQueueThread.CallOnQueue(() =>
                    {
                        executor.Dispose();
                        return true;
                    });
                }
            }
        }

        public static async Task Initialize(ChakraJavaScriptExecutor executor, IMessageQueueThread jsQueueThread)
        {
            var scriptUris = new[]
            {
                @"ms-appx:///Resources/test.js",
            };

            var scripts = new KeyValuePair<string, string>[scriptUris.Length];

            for (var i = 0; i < scriptUris.Length; ++i)
            {
                var uri = scriptUris[i];
                var storageFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri(uri));
                using (var stream = await storageFile.OpenStreamForReadAsync())
                using (var reader = new StreamReader(stream))
                {
                    scripts[i] = new KeyValuePair<string, string>(uri, reader.ReadToEnd());
                }
            }

            await jsQueueThread.CallOnQueue(() =>
            {
                foreach (var script in scripts)
                {
                    executor.RunScript(script.Value, script.Key);
                }

                return true;
            });
        }

        private static MessageQueueThread CreateJavaScriptThread()
        {
            return MessageQueueThread.Create(MessageQueueThreadSpec.Create("js", MessageQueueThreadKind.BackgroundSingleThread), ex => { Assert.Fail(); });
        }
    }
}<commit_msg>test(JavaScriptHelpers): fixes broken unit tests from RunScript changes (#658)<commit_after>﻿using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
using ReactNative.Bridge.Queue;
using ReactNative.Chakra.Executor;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Windows.Storage;

namespace ReactNative.Tests
{
    static class JavaScriptHelpers
    {
        public static Task Run(Action<ChakraJavaScriptExecutor, IMessageQueueThread> action)
        {
            return Run((executor, jsQueueThread) =>
            {
                action(executor, jsQueueThread);
                return Task.CompletedTask;
            });
        }

        public static async Task Run(Func<ChakraJavaScriptExecutor, IMessageQueueThread, Task> action)
        {
            using (var jsQueueThread = CreateJavaScriptThread())
            {
                var executor = await jsQueueThread.CallOnQueue(() => new ChakraJavaScriptExecutor());
                try
                {
                    await Initialize(executor, jsQueueThread);
                    await action(executor, jsQueueThread);
                }
                finally
                {
                    await jsQueueThread.CallOnQueue(() =>
                    {
                        executor.Dispose();
                        return true;
                    });
                }
            }
        }

        public static async Task Initialize(ChakraJavaScriptExecutor executor, IMessageQueueThread jsQueueThread)
        {
            var scriptUris = new[]
            {
                @"ms-appx:///Resources/test.js",
            };

            var scripts = new KeyValuePair<string, string>[scriptUris.Length];
            for (var i = 0; i < scriptUris.Length; ++i)
            {
                var uri = scriptUris[i];
                var storageFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri(uri));
                scripts[i] = new KeyValuePair<string, string>(uri, storageFile.Path);
            }

            await jsQueueThread.CallOnQueue(() =>
            {
                foreach (var script in scripts)
                {
                    executor.RunScript(script.Value, script.Key);
                }

                return true;
            });
        }

        private static MessageQueueThread CreateJavaScriptThread()
        {
            return MessageQueueThread.Create(MessageQueueThreadSpec.Create("js", MessageQueueThreadKind.BackgroundSingleThread), ex => { Assert.Fail(); });
        }
    }
}
{'fraction_non_alphanumeric': 0.08512646281615704, 'fraction_numerical': 0.0009437523593808985, 'mean_word_length': 1.6271690629647992, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2766880', 'n_tokens_mistral': 1303, 'n_tokens_neox': 1209, 'n_words': 262}