Пример #1
0
        public void ZeroIsParsed()
        {
            // when you send a zero, no point in specifying the units
            var value = DurationParser.Parse("0");

            Assert.That(value, Is.EqualTo(TimeSpan.Zero));
        }
Пример #2
0
        private void AssertSuccess(object durationToParse, object expectedDuration)
        {
            ValueParseResult parseResult = DurationParser.Parse(durationToParse);

            Assert.That(parseResult.Success, Is.True);
            Assert.That(parseResult.Value, Is.EqualTo(expectedDuration));
        }
Пример #3
0
        public void WeeksAreParsed(string input, int expectedWeeks)
        {
            var parsed = DurationParser.Parse(input);

            Assert.That(parsed, Is.Not.Null);
            Assert.That(parsed.Value, Is.EqualTo(TimeSpan.FromDays(expectedWeeks * 7)));
        }
Пример #4
0
        private SettingsContainer MakeSettings()
        {
            var minPackageAge = DurationParser.Parse(MinimumPackageAge);

            if (!minPackageAge.HasValue)
            {
                minPackageAge = TimeSpan.Zero;
                Console.WriteLine($"Min package age '{MinimumPackageAge}' could not be parsed");
            }

            var settings = new SettingsContainer
            {
                ModalSettings = new ModalSettings(),
                UserSettings  = new UserSettings
                {
                    AllowedChange     = AllowedChange,
                    NuGetSources      = NuGetSources,
                    MinimumPackageAge = minPackageAge.Value,
                    PackageIncludes   = SettingsParser.ParseRegex(Include, nameof(Include)),
                    PackageExcludes   = SettingsParser.ParseRegex(Exclude, nameof(Exclude)),
                }
            };

            PopulateSettings(settings);
            return(settings);
        }
Пример #5
0
        protected virtual ValidationResult PopulateSettings(SettingsContainer settings)
        {
            var minPackageAge = DurationParser.Parse(MinimumPackageAge);

            if (!minPackageAge.HasValue)
            {
                return(ValidationResult.Failure($"Min package age '{MinimumPackageAge}' could not be parsed"));
            }

            settings.UserSettings.MinimumPackageAge = minPackageAge.Value;

            var regexIncludeValid = PopulatePackageIncludes(settings, Include);

            if (!regexIncludeValid.IsSuccess)
            {
                return(regexIncludeValid);
            }

            var regexExcludeValid = PopulatePackageExcludes(settings, Exclude);

            if (!regexExcludeValid.IsSuccess)
            {
                return(regexExcludeValid);
            }

            return(ValidationResult.Success);
        }
Пример #6
0
 public void WeekTest()
 {
     Assert.AreEqual(TimeSpan.FromDays(10 * 7), DurationParser.Parse("10w"));
     Assert.AreEqual(TimeSpan.FromDays(1 * 7), DurationParser.Parse("1week"));
     Assert.AreEqual(TimeSpan.FromDays(50 * 7), DurationParser.Parse("50wk"));
     Assert.AreEqual(TimeSpan.FromDays(61 * 7), DurationParser.Parse("61weeks"));
 }
Пример #7
0
 public void SpacingTest()
 {
     Assert.AreEqual(TimeSpan.FromDays(10) + TimeSpan.FromHours(5) + TimeSpan.FromSeconds(3),
                     DurationParser.Parse("3s 5h 10days"));
     Assert.AreEqual(TimeSpan.FromDays(10) + TimeSpan.FromHours(5) + TimeSpan.FromSeconds(3),
                     DurationParser.Parse("3s 5 hr 10   days  "));
 }
Пример #8
0
        private void AssertFail(object durationToParse)
        {
            ValueParseResult parseResult = DurationParser.Parse(durationToParse);

            Assert.That(parseResult.Success, Is.False);
            Assert.That(parseResult.Value, Is.Null);
            Assert.That(parseResult.ErrorDetails, Is.Not.Empty);
        }
Пример #9
0
 public void MinuteTest()
 {
     Assert.AreEqual(TimeSpan.FromMinutes(10), DurationParser.Parse("10m"));
     Assert.AreEqual(TimeSpan.FromMinutes(1), DurationParser.Parse("1min"));
     Assert.AreEqual(TimeSpan.FromMinutes(50), DurationParser.Parse("50mins"));
     Assert.AreEqual(TimeSpan.FromMinutes(898), DurationParser.Parse("898minute"));
     Assert.AreEqual(TimeSpan.FromMinutes(88), DurationParser.Parse("88minutes"));
 }
Пример #10
0
 public void HourTest()
 {
     Assert.AreEqual(TimeSpan.FromHours(10), DurationParser.Parse("10h"));
     Assert.AreEqual(TimeSpan.FromHours(1), DurationParser.Parse("1hr"));
     Assert.AreEqual(TimeSpan.FromHours(50), DurationParser.Parse("50hour"));
     Assert.AreEqual(TimeSpan.FromHours(898), DurationParser.Parse("898hours"));
     Assert.AreEqual(TimeSpan.FromHours(88), DurationParser.Parse("88hrs"));
 }
Пример #11
0
 public void SecondTest()
 {
     Assert.AreEqual(TimeSpan.FromSeconds(10), DurationParser.Parse("10s"));
     Assert.AreEqual(TimeSpan.FromSeconds(1), DurationParser.Parse("1sec"));
     Assert.AreEqual(TimeSpan.FromSeconds(50), DurationParser.Parse("50secs"));
     Assert.AreEqual(TimeSpan.FromSeconds(898), DurationParser.Parse("898second"));
     Assert.AreEqual(TimeSpan.FromSeconds(88), DurationParser.Parse("88seconds"));
 }
Пример #12
0
        private TimeSpan?ReadMinPackageAge()
        {
            const string defaultMinPackageAge = "7d";
            var          settingsFromFile     = FileSettingsCache.Get();
            var          valueWithFallback    = Concat.FirstValue(MinimumPackageAge, settingsFromFile.Age, defaultMinPackageAge);

            return(DurationParser.Parse(valueWithFallback));
        }
Пример #13
0
        public void BanUser(string username, string duration, string reason)
        {
            // Check for valid parameters
            if (!GameData.UserName.IsValid(username))
            {
                User.Send(GameMessage.CommandGeneralInvalidUser, ServerMessageType.GUI, username);
                return;
            }

            if (StringComparer.OrdinalIgnoreCase.Equals(User.Name, username))
            {
                User.Send(GameMessage.SelfBanFailed, ServerMessageType.GUI);
                return;
            }

            var target = World.FindUser(username);

            if (target == null)
            {
                return;
            }

            if (target.Permissions > User.Permissions)
            {
                User.Send(GameMessage.BanInsufficientPermissions, ServerMessageType.GUI, username);
                return;
            }

            if (string.IsNullOrEmpty(duration))
            {
                User.Send(GameMessage.CommandGeneralInvalidParameter, ServerMessageType.GUI, "duration");
                return;
            }

            // Parse the duration
            TimeSpan dur;
            string   parseFailReason;

            if (!DurationParser.TryParse(duration, out dur, out parseFailReason))
            {
                User.Send(GameMessage.CommandGeneralInvalidParameterEx, ServerMessageType.GUI, "duration", parseFailReason);
                return;
            }

            // Perform the ban and notify the issuer if it was successful or not
            BanManagerFailReason banFailReason;

            if (!BanningManager.Instance.TryAddUserBan(username, dur, reason, User.Name, out banFailReason))
            {
                // Ban failed
                User.Send(GameMessage.BanUserFailed, ServerMessageType.GUI, username, banFailReason.GetDetailedString());
            }
            else
            {
                // Ban successful
                User.Send(GameMessage.BanUserSuccessful, ServerMessageType.GUI, username);
            }
        }
Пример #14
0
        public void Should_parse_correctly(string input, TimeSpan expected)
        {
            TimeSpan result;

            var success = DurationParser.TryParse(input, out result);

            success.Should().BeTrue();
            result.Should().Be(expected);
        }
Пример #15
0
        public void MonthTest()
        {
            // "about" 365 days in a year, though let some room slide for minor rounding errors
            const int marginOfErrorPerYears = 2;
            var       a = DurationParser.Parse("24months").TotalDays;
            var       b = TimeSpan.FromDays(365 * 2).TotalDays;

            Assert.IsTrue(Math.Abs(a - b) < (marginOfErrorPerYears * 2));
        }
Пример #16
0
        public void Parse_Failure(string text)
        {
            // arrange
            var parser = new DurationParser(new TokenEnumerator(new StringTokenReader(text)));

            // act
            var builder = new DurationBuilder(Reference);
            var result  = parser.TryMake(builder);

            // assert
            Assert.False(result);
        }
Пример #17
0
        public void DurationParser_ConvertOver24hr_Success()
        {
            //Arrange
            var strDuration = "2456.48";
            var dParser     = new DurationParser();

            //Act
            var result = dParser.Parse(strDuration);

            //Assert
            Assert.AreEqual("1d 16h 56m 28s", result);
        }
Пример #18
0
        public void DurationParser_ConvertUnderMin_Success()
        {
            //Arrange
            var strDuration = "0.56";
            var dParser     = new DurationParser();

            //Act
            var result = dParser.Parse(strDuration);

            //Assert
            Assert.AreEqual("34s", result);
        }
Пример #19
0
        public void DurationParser_ConvertOve60MinUnder24hr_Success()
        {
            //Arrange
            var strDuration = "1134.27";
            var dParser     = new DurationParser();

            //Act
            var result = dParser.Parse(strDuration);

            //Assert
            Assert.AreEqual("18h 54m 16s", result);
        }
Пример #20
0
        public void DurationParser_ConvertOveMinUnder60Mins_Success()
        {
            //Arrange
            var strDuration = "47.32";
            var dParser     = new DurationParser();

            //Act
            var result = dParser.Parse(strDuration);

            //Assert
            Assert.AreEqual("47m 19s", result);
        }
Пример #21
0
        public void Parse_Success(string text, TimeSpan expected)
        {
            // arrange
            var parser = new DurationParser(new TokenEnumerator(new StringTokenReader(text)));

            // act
            var builder = new DurationBuilder(Reference);
            var result  = parser.TryMake(builder);

            // assert
            Assert.True(result);
            Assert.Equal(expected, builder.Build());
        }
Пример #22
0
        public void TestDurations(string str, int?seconds)
        {
            var x = new DurationParser();

            var result = x.Parse(str);

            if (seconds == null)
            {
                Assert.IsNull(result);
            }
            else
            {
                Assert.AreEqual(TimeSpan.FromSeconds((double)seconds !), result);
            }
        }
Пример #23
0
        public static SettingsContainer ParseToSettings(RawConfiguration settings)
        {
            var modalSettings = ReadModalSettings(settings);

            if (modalSettings == null)
            {
                return(null);
            }

            var authSettings = new GithubAuthSettings(
                EnsureTrailingSlash(settings.GithubApiEndpoint),
                settings.GithubToken);

            var minPackageAge = DurationParser.Parse(settings.MinPackageAge);

            if (!minPackageAge.HasValue)
            {
                minPackageAge = TimeSpan.Zero;
                Console.WriteLine($"Min package age '{settings.MinPackageAge}' could not be parsed");
            }

            var userPrefs = new UserSettings
            {
                AllowedChange = settings.AllowedChange,
                ForkMode      = settings.ForkMode,
                ReportMode    = settings.ReportMode,
                MaxPullRequestsPerRepository = settings.MaxPullRequestsPerRepository,
                MaxRepositoriesChanged       = settings.MaxRepositoriesChanged,
                NuGetSources      = ReadNuGetSources(settings),
                PackageIncludes   = ParseRegex(settings.Include, nameof(settings.Include)),
                PackageExcludes   = ParseRegex(settings.Exclude, nameof(settings.Exclude)),
                MinimumPackageAge = minPackageAge.Value,
                Directory         = settings.Dir
            };

            return(new SettingsContainer
            {
                ModalSettings = modalSettings,
                GithubAuthSettings = authSettings,
                UserSettings = userPrefs
            });
        }
Пример #24
0
        /// <summary>
        /// Gets the play list songs internal asynchronous.
        /// </summary>
        /// <param name="userEmail">The user email.</param>
        /// <param name="playListId">The play list identifier.</param>
        private async Task GetPlayListSongsInternalAsync(string userEmail, string playListId, List <IYouTubeSong> playListSongs)
        {
            var youtubeService = await this.GetYouTubeService(userEmail);

            var channelsListRequest = youtubeService.Channels.List("contentDetails");

            channelsListRequest.Mine = true;
            var nextPageToken = "";

            while (nextPageToken != null)
            {
                PlaylistItemsResource.ListRequest listRequest = youtubeService.PlaylistItems.List("contentDetails");
                listRequest.MaxResults = 50;
                listRequest.PlaylistId = playListId;
                listRequest.PageToken  = nextPageToken;
                var response = await listRequest.ExecuteAsync();

                if (playListSongs == null)
                {
                    playListSongs = new List <IYouTubeSong>();
                }
                foreach (var playlistItem in response.Items)
                {
                    VideosResource.ListRequest videoR = youtubeService.Videos.List("snippet,contentDetails,status");
                    videoR.Id = playlistItem.ContentDetails.VideoId;
                    var responseV = await videoR.ExecuteAsync();

                    if (responseV.Items.Count > 0)
                    {
                        KeyValuePair <string, string> parsedSong = SongTitleParser.ParseTitle(responseV.Items[0].Snippet.Title);
                        ulong?       duration    = new DurationParser().GetDuration(responseV.Items[0].ContentDetails.Duration);
                        IYouTubeSong currentSong = new YouTubeSong(parsedSong.Key, parsedSong.Value, responseV.Items[0].Snippet.Title, responseV.Items[0].Id, playlistItem.Id, duration);
                        playListSongs.Add(currentSong);
                        Debug.WriteLine(currentSong.Title);
                    }
                }
                nextPageToken = response.NextPageToken;
            }
        }
Пример #25
0
        public void BanUser(string username, string duration, string reason)
        {
            // Check for valid parameters
            if (!GameData.UserName.IsValid(username))
            {
                User.Send(GameMessage.CommandGeneralInvalidUser, ServerMessageType.GUI, username);
                return;
            }

            if (string.IsNullOrEmpty(duration))
            {
                User.Send(GameMessage.CommandGeneralInvalidParameter, ServerMessageType.GUI, "duration");
                return;
            }

            // Parse the duration
            TimeSpan dur;
            string   parseFailReason;

            if (!DurationParser.TryParse(duration, out dur, out parseFailReason))
            {
                User.Send(GameMessage.CommandGeneralInvalidParameterEx, ServerMessageType.GUI, "duration", parseFailReason);
                return;
            }

            // Perform the ban and notify the issuer if it was successful or not
            BanManagerFailReason banFailReason;

            if (!BanningManager.Instance.TryAddUserBan(username, dur, reason, User.Name, out banFailReason))
            {
                // Ban failed
                User.Send(GameMessage.BanUserFailed, ServerMessageType.GUI, username, banFailReason.GetDetailedString());
            }
            else
            {
                // Ban successful
                User.Send(GameMessage.BanUserSuccessful, ServerMessageType.GUI, username);
            }
        }
Пример #26
0
        public void UnknownUnitsIsNotParsed()
        {
            var value = DurationParser.Parse("37x");

            Assert.That(value, Is.Null);
        }
Пример #27
0
        public void Should_fail_if_input_is_not_a_correct_duration(string input, string reason)
        {
            var success = DurationParser.TryParse(input, out _);

            success.Should().BeFalse($"because {reason}. Input: [{input}]");
        }
Пример #28
0
        public void BadStringIsNotParsed()
        {
            var value = DurationParser.Parse("ghoti");

            Assert.That(value, Is.Null);
        }
Пример #29
0
        public void EmptyStringIsNotParsed()
        {
            var value = DurationParser.Parse(string.Empty);

            Assert.That(value, Is.Null);
        }
Пример #30
0
        public void NullStringIsNotParsed()
        {
            var value = DurationParser.Parse(null);

            Assert.That(value, Is.Null);
        }