<commit_before>// Copyright (C) Pash Contributors. License: GPL/BSD. See https://github.com/Pash-Project/Pash/
using System;
using NUnit.Framework;
using System.Management.Automation;

namespace ReferenceTests.Language
{
    [TestFixture]
    public class FunctionTests : ReferenceTestBase
    {
        [Test]
        public void FunctionDeclarationWithoutParameterList()
        {
            Assert.DoesNotThrow(
                delegate() { ReferenceHost.Execute("function f() { 'x' }"); }
            );
        }

        [TestCase("function f { param($test=(get-location).path)")]
        [TestCase("function f ($test=(get-location).path) {")]
        public void FunctionParamsDefaultValueCanBeExpression(string funStart)
        {
            var cmd = NewlineJoin(
                funStart,
                "$test",
                "}",
                "f");
            var result = ReferenceHost.Execute(cmd);
            Assert.AreEqual(NewlineJoin(Environment.CurrentDirectory), result);
        }

        [TestCase("function f($a, $b) { ")]
        [TestCase("function f { param($a, $b); ")]
        public void FunctionWithParameters(string funStart)
        {
            ExecuteAndCompareTypedResult(funStart + "$a; $b; }; f 1 2", 1, 2);
        }

        [TestCase("function f($a, $b) { ")]
        [TestCase("function f { param($a, $b); ")]
        public void FunctionWithoutPassedParameters(string funStart)
        {
            ExecuteAndCompareTypedResult(funStart + "$a; $b; }; f", null, null);
        }

        [TestCase("function f($a, $b) { ")]
        [TestCase("function f { param($a, $b); ")]
        public void FunctionWithNamedParameters(string funStart)
        {
            ExecuteAndCompareTypedResult(funStart + "$a; $b; }; f -b 1 -a 2", 2, 1);
        }

        [TestCase("function f($a, $b) { ")]
        [TestCase("function f { param($a, $b); ")]
        public void FunctionWithExplicitlyNamedParameters(string funStart)
        {
            ExecuteAndCompareTypedResult(funStart + "$a; $b; }; f -b: 1 -a: 2", 2, 1);
        }

        [TestCase("function f($a, $b) { ")]
        [TestCase("function f { param($a, $b); ")]
        public void FunctionWithMoreArgsInArgsVar(string funStart)
        {
            ExecuteAndCompareTypedResult(funStart + "$a; $b; $args; }; f -b 1 '-a' -d -e", "-a", 1, "-d", "-e");
        }

        [TestCase("function f($a, $b) { ")]
        [TestCase("function f { param($a, $b); ")]
        public void FunctionsUndefinedNamedArgsAreInArgsVar(string funStart)
        {
            ExecuteAndCompareTypedResult(funStart + "$a; $b; $args; }; f -c 1 -d 'val' -e", null, null, "-c", 1, "-d", "val", "-e");
        }

        [TestCase("function f($a) { ")]
        [TestCase("function f { param($a); ")]
        public void FunctionsUndefinedNamedArgsKeepCorrectOrder(string funStart)
        {
            ExecuteAndCompareTypedResult(funStart + "$a; $args; }; f 1 2 -d 'val' -e 3 4", 1, 2, "-d", "val", "-e", 3, 4);
        }

        [TestCase("function f($a='f') { ")]
        [TestCase("function f { param($a='f'); ")]
        public void FunctionsNamedUsetParameterThrows(string funStart)
        {
            Assert.Throws<ParameterBindingException>(delegate {
               ReferenceHost.Execute(funStart + "}; f -a");
            });
        }

        [TestCase("function f($a='f') { ")]
        [TestCase("function f { param($a='f'); ")]
        public void FunctionsNamedParameterCanTakeUnusedParamName(string funStart)
        {
            ExecuteAndCompareTypedResult(funStart + " $a; }; f -a -b", "-b");
        }

        [Test]
        public void FunctionWithBothParenthesisAndParamBlockThrows()
        {
            Assert.Throws<ParseException>(delegate {
                ReferenceHost.Execute("function f($a, $b) { param($a, $b); };");
            });
        }
    }
}

<commit_msg>More tests describing the param binding machanism of functions<commit_after>// Copyright (C) Pash Contributors. License: GPL/BSD. See https://github.com/Pash-Project/Pash/
using System;
using NUnit.Framework;
using System.Management.Automation;

namespace ReferenceTests.Language
{
    [TestFixture]
    public class FunctionTests : ReferenceTestBase
    {
        [Test]
        public void FunctionDeclarationWithoutParameterList()
        {
            Assert.DoesNotThrow(
                delegate() { ReferenceHost.Execute("function f() { 'x' }"); }
            );
        }

        [TestCase("function f { param($test=(get-location).path)")]
        [TestCase("function f ($test=(get-location).path) {")]
        public void FunctionParamsDefaultValueCanBeExpression(string funStart)
        {
            var cmd = NewlineJoin(
                funStart,
                "$test",
                "}",
                "f");
            var result = ReferenceHost.Execute(cmd);
            Assert.AreEqual(NewlineJoin(Environment.CurrentDirectory), result);
        }

        [TestCase("function f($a, $b) { ")]
        [TestCase("function f { param($a, $b); ")]
        public void FunctionWithParameters(string funStart)
        {
            ExecuteAndCompareTypedResult(funStart + "$a; $b; }; f 1 2", 1, 2);
        }

        [TestCase("function f($a, $b) { ")]
        [TestCase("function f { param($a, $b); ")]
        public void FunctionWithoutPassedParameters(string funStart)
        {
            ExecuteAndCompareTypedResult(funStart + "$a; $b; }; f", null, null);
        }

        [TestCase("function f($a, $b) { ")]
        [TestCase("function f { param($a, $b); ")]
        public void FunctionWithNamedParameters(string funStart)
        {
            ExecuteAndCompareTypedResult(funStart + "$a; $b; }; f -b 1 -a 2", 2, 1);
        }

        [TestCase("function f($a, $b) { ")]
        [TestCase("function f { param($a, $b); ")]
        public void FunctionWithExplicitlyNamedParameters(string funStart)
        {
            ExecuteAndCompareTypedResult(funStart + "$a; $b; }; f -b: 1 -a: 2", 2, 1);
        }

        [TestCase("function f($a, $b) { ")]
        [TestCase("function f { param($a, $b); ")]
        public void FunctionWithMoreArgsInArgsVar(string funStart)
        {
            ExecuteAndCompareTypedResult(funStart + "$a; $b; $args; }; f -b 1 '-a' -d -e", "-a", 1, "-d", "-e");
        }

        [TestCase("function f($a, $b) { ")]
        [TestCase("function f { param($a, $b); ")]
        public void FunctionsUndefinedNamedArgsAreInArgsVar(string funStart)
        {
            ExecuteAndCompareTypedResult(funStart + "$a; $b; $args; }; f -c 1 -d 'val' -e", null, null, "-c", 1, "-d", "val", "-e");
        }

        [TestCase("function f($a) { ")]
        [TestCase("function f { param($a); ")]
        public void FunctionsUndefinedNamedArgsKeepCorrectOrder(string funStart)
        {
            ExecuteAndCompareTypedResult(funStart + "$a; $args; }; f 1 2 -d 'val' -e 3 4", 1, 2, "-d", "val", "-e", 3, 4);
        }

        [TestCase("function f($a='f') { ")]
        [TestCase("function f { param($a='f'); ")]
        public void FunctionsNamedUnsetParameterThrows(string funStart)
        {
            Assert.Throws<ParameterBindingException>(delegate {
               ReferenceHost.Execute(funStart + "}; f -a");
            });
        }

        [TestCase("function f($a='f') { ")]
        [TestCase("function f { param($a='f'); ")]
        public void FunctionsNamedParameterExplicitNull(string funStart)
        {
            Assert.Throws<ParameterBindingException>(delegate
            {
                ReferenceHost.Execute(funStart + "}; f -a");
            });
        }

        [TestCase("function f($a='f') { ")]
        [TestCase("function f { param($a='f'); ")]
        public void FunctionsNamedParameterCanTakeUnusedParamName(string funStart)
        {
            ExecuteAndCompareTypedResult(funStart + " $a; }; f -a -b", "-b");
        }

        [TestCase("function f($a='f') { ", "-a: $null")]
        [TestCase("function f($a='f') { ", "-a $null")]
        [TestCase("function f { param($a='f'); ", "-a: $null")]
        [TestCase("function f { param($a='f'); ", "-a $null")]
        public void FunctionsNamedParameterCanTakeExplicitNull(string funStart, string arg)
        {
            ExecuteAndCompareTypedResult(funStart + " $a; }; f " + arg, null);
        }

        [TestCase("function f($a='f') { ")]
        [TestCase("function f { param($a='f'); ")]
        public void FunctionsNamedParameterCantTakeUnusedButSetParam(string funStart)
        {
            Assert.Throws<ParameterBindingException>(delegate {
                ReferenceHost.Execute(funStart + " $a; }; f -a -b: foo");
            });
        }

        [Test]
        public void FunctionWithBothParenthesisAndParamBlockThrows()
        {
            Assert.Throws<ParseException>(delegate {
                ReferenceHost.Execute("function f($a, $b) { param($a, $b); };");
            });
        }
    }
}


{'fraction_non_alphanumeric': 0.1585583575094845, 'fraction_numerical': 0.005355947333184557, 'mean_word_length': 2.2275837234425637, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 2, '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': '6978891', 'n_tokens_mistral': 2691, 'n_tokens_neox': 2585, 'n_words': 562}