2017-03-06 20:53:26 -08:00
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using FluentAssertions ;
2017-03-09 09:14:55 -08:00
using System.Linq ;
2017-03-06 20:53:26 -08:00
using Microsoft.DotNet.Cli ;
using Microsoft.DotNet.Cli.CommandLine ;
using Xunit ;
using static Microsoft . DotNet . Cli . CommandLine . Accept ;
using static Microsoft . DotNet . Cli . CommandLine . Create ;
namespace Microsoft.DotNet.Tests.ParserTests
{
public class ArgumentForwardingExtensionsTests
{
[Fact]
2017-03-09 09:14:55 -08:00
public void AnOutgoingCommandLineCanBeGeneratedBasedOnAParseResult ( )
2017-03-06 20:53:26 -08:00
{
var command = Command ( "the-command" , "" ,
2017-03-09 09:14:55 -08:00
Option ( "-o|--one" , "" ,
2017-03-10 17:11:19 -08:00
ZeroOrOneArgument ( )
2017-03-16 13:22:08 -07:00
. ForwardAsSingle ( o = > $"/i:{o.Arguments.Single()}" ) ) ,
2017-03-09 09:14:55 -08:00
Option ( "-t|--two" , "" ,
2017-03-10 17:11:19 -08:00
NoArguments ( )
2017-03-09 09:14:55 -08:00
. ForwardAs ( "/s:true" ) ) ) ;
2017-03-06 20:53:26 -08:00
2017-03-09 09:14:55 -08:00
var result = command . Parse ( "the-command -t -o 123" ) ;
2017-03-06 20:53:26 -08:00
result [ "the-command" ]
2017-03-09 07:35:06 -08:00
. OptionValuesToBeForwarded ( )
2017-03-06 20:53:26 -08:00
. Should ( )
2017-03-09 09:14:55 -08:00
. BeEquivalentTo ( "/i:123" , "/s:true" ) ;
2017-03-06 20:53:26 -08:00
}
[Fact]
public void MultipleArgumentsCanBeJoinedWhenForwarding ( )
{
var command = Command ( "the-command" , "" ,
2017-03-09 09:14:55 -08:00
Option ( "-x" , "" ,
2017-03-10 17:11:19 -08:00
ZeroOrMoreArguments ( )
2017-03-16 13:22:08 -07:00
. ForwardAsSingle ( o = > $"/x:{string.Join(" & ", o.Arguments)}" ) ) ) ;
2017-03-06 20:53:26 -08:00
var result = command . Parse ( "the-command -x one -x two" ) ;
result [ "the-command" ]
2017-03-09 07:35:06 -08:00
. OptionValuesToBeForwarded ( )
2017-03-06 20:53:26 -08:00
. Should ( )
. BeEquivalentTo ( "/x:one&two" ) ;
}
2017-03-09 09:14:55 -08:00
[Fact]
public void AnArgumentCanBeForwardedAsIs ( )
{
var command = Command ( "the-command" , "" ,
Option ( "-x" , "" ,
2017-03-10 17:11:19 -08:00
ZeroOrMoreArguments ( )
2017-03-09 09:14:55 -08:00
. Forward ( ) ) ) ;
var result = command . Parse ( "the-command -x one" ) ;
result [ "the-command" ]
. OptionValuesToBeForwarded ( )
. Should ( )
. BeEquivalentTo ( "one" ) ;
}
2017-03-06 20:53:26 -08:00
}
2017-03-10 10:13:11 -08:00
}