<commit_before>﻿using IronVelocity;
using IronVelocity.Compilation;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace Tests
{
    public static class Utility
    {
        private static bool DefaultToGlobals = false;

        public static VelocityAsyncTemplateMethod CompileAsyncTemplate(string input, string fileName = "", IDictionary<string, object> globals = null)
        {
            var runtime = new VelocityRuntime(null, globals);
            return runtime.CompileAsyncTemplate(input, "TestExpression", fileName, true);
        }

        public static VelocityTemplateMethod CompileTemplate(string input, string fileName = "", IDictionary<string, object> globals = null)
        {
            var runtime = new VelocityRuntime(null, globals);
            return runtime.CompileTemplate(input, "TestExpression", fileName, true);
        }

        public static IDictionary<string, object> Evaluate(string input, IDictionary<string, object> environment, string fileName = "", IDictionary<string, object> globals = null)
        {
            if (DefaultToGlobals && globals == null && environment != null)
                globals = environment.Where(x => x.Value != null).ToDictionary(x => x.Key, x => x.Value);

            var action = CompileTemplate(input, fileName, globals);

            var builder = new StringBuilder();
            var ctx = environment as VelocityContext;
            if (ctx == null)
                ctx = new VelocityContext(environment);

            action(ctx, builder);

            return ctx;
        }
        public static String GetNormalisedOutput(string input, IDictionary<string, object> environment, string fileName = "")
        {
            IDictionary<string,object> globals = null;
            if (DefaultToGlobals)
                globals = environment;

            return GetNormalisedOutput(input, environment, globals, fileName);

        }

        public static String GetNormalisedOutput(string input, IDictionary<string, object> environment, IDictionary<string, object> globals, string fileName = "")
        {
            var action = CompileTemplate(input, fileName, globals);

            var builder = new StringBuilder();
            var ctx = environment as VelocityContext;
            if (ctx == null)
                ctx = new VelocityContext(environment);

            action(ctx,builder);<commit_msg>Fixed tests that failed using static globals when a context item was null<commit_after>﻿using IronVelocity;
using IronVelocity.Compilation;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace Tests
{
    public static class Utility
    {
        private static bool DefaultToGlobals = true;

        public static VelocityAsyncTemplateMethod CompileAsyncTemplate(string input, string fileName = "", IDictionary<string, object> globals = null)
        {
            var runtime = new VelocityRuntime(null, globals);
            return runtime.CompileAsyncTemplate(input, "TestExpression", fileName, true);
        }

        public static VelocityTemplateMethod CompileTemplate(string input, string fileName = "", IDictionary<string, object> globals = null)
        {
            var runtime = new VelocityRuntime(null, globals);
            return runtime.CompileTemplate(input, "TestExpression", fileName, true);
        }

        public static IDictionary<string, object> Evaluate(string input, IDictionary<string, object> environment, string fileName = "", IDictionary<string, object> globals = null)
        {
            if (DefaultToGlobals && globals == null && environment != null)
                globals = environment.Where(x => x.Value != null).ToDictionary(x => x.Key, x => x.Value);

            var action = CompileTemplate(input, fileName, globals);

            var builder = new StringBuilder();
            var ctx = environment as VelocityContext;
            if (ctx == null)
                ctx = new VelocityContext(environment);

            action(ctx, builder);

            return ctx;
        }
        public static String GetNormalisedOutput(string input, IDictionary<string, object> environment, string fileName = "")
        {
            IDictionary<string,object> globals = null;
            if (DefaultToGlobals)
            {
                globals = environment
                    .Where(x => x.Value != null)
                    .ToDictionary(x => x.Key, x => x.Value);
            }

            return GetNormalisedOutput(input, environment, globals, fileName);

        }

        public static String GetNormalisedOutput(string input, IDictionary<string, object> environment, IDictionary<string, object> globals, string fileName = "")
        {
            var action = CompileTemplate(input, fileName, globals);

            var builder = new StringBuilder();
            var ctx = environment as VelocityContext;
            if (ctx == null)
                ctx = new VelocityContext(environment);

            action(ctx,builder);
{'fraction_non_alphanumeric': 0.0887797506611258, 'fraction_numerical': 0.0, 'mean_word_length': 2.6391752577319587, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 30, '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': '3718073', 'n_tokens_mistral': 1286, 'n_tokens_neox': 1271, 'n_words': 380}