2011-06-18 17:19:24 +00:00
|
|
|
|
using System;
|
2011-11-25 07:56:07 +00:00
|
|
|
|
using System.Collections.Generic;
|
2013-02-18 07:59:43 +00:00
|
|
|
|
using System.Text;
|
2011-06-18 17:19:24 +00:00
|
|
|
|
using FluentAssertions;
|
|
|
|
|
using NUnit.Framework;
|
|
|
|
|
using NzbDrone.Core.Test.Framework;
|
2014-05-04 14:33:25 +00:00
|
|
|
|
using NzbDrone.Test.Common;
|
2011-06-18 17:19:24 +00:00
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Test
|
|
|
|
|
{
|
|
|
|
|
[TestFixture]
|
2013-03-28 22:07:09 +00:00
|
|
|
|
|
2013-02-17 05:44:06 +00:00
|
|
|
|
public class FluentTest : CoreTest
|
2011-06-18 17:19:24 +00:00
|
|
|
|
{
|
|
|
|
|
[TestCase(null, "def", "def")]
|
|
|
|
|
[TestCase("", "def", "def")]
|
|
|
|
|
[TestCase("", 1, "1")]
|
|
|
|
|
[TestCase(null, "", "")]
|
|
|
|
|
[TestCase("actual", "def", "actual")]
|
|
|
|
|
public void WithDefault_success(string actual, object defaultValue, string result)
|
|
|
|
|
{
|
|
|
|
|
actual.WithDefault(defaultValue).Should().Be(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void WithDefault_Fail()
|
|
|
|
|
{
|
2014-05-04 14:33:25 +00:00
|
|
|
|
Assert.Throws<ArgumentNullException>(() => "test".WithDefault(null));
|
|
|
|
|
|
|
|
|
|
ExceptionVerification.IgnoreWarns();
|
2011-06-18 17:19:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-09-28 17:12:53 +00:00
|
|
|
|
[Test]
|
|
|
|
|
public void ToBestDateTime_Yesterday()
|
|
|
|
|
{
|
|
|
|
|
var dateTime = DateTime.Today.AddDays(-1);
|
|
|
|
|
|
|
|
|
|
dateTime.ToBestDateString().Should().Be("Yesterday");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void ToBestDateTime_Today()
|
|
|
|
|
{
|
|
|
|
|
var dateTime = DateTime.Today;
|
|
|
|
|
|
|
|
|
|
dateTime.ToBestDateString().Should().Be("Today");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void ToBestDateTime_Tomorrow()
|
|
|
|
|
{
|
|
|
|
|
var dateTime = DateTime.Today.AddDays(1);
|
|
|
|
|
|
|
|
|
|
dateTime.ToBestDateString().Should().Be("Tomorrow");
|
|
|
|
|
}
|
2011-06-18 17:19:24 +00:00
|
|
|
|
|
2011-09-28 17:12:53 +00:00
|
|
|
|
[Test]
|
|
|
|
|
public void ToBestDateTime_DayOfWeek()
|
|
|
|
|
{
|
|
|
|
|
for (int i = 2; i < 7; i++)
|
|
|
|
|
{
|
|
|
|
|
var dateTime = DateTime.Today.AddDays(i);
|
|
|
|
|
|
|
|
|
|
Console.WriteLine(dateTime.DayOfWeek);
|
|
|
|
|
dateTime.ToBestDateString().Should().Be(dateTime.DayOfWeek.ToString());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void ToBestDateTime_Over_One_Week()
|
|
|
|
|
{
|
|
|
|
|
var dateTime = DateTime.Today.AddDays(8);
|
|
|
|
|
|
|
|
|
|
Console.WriteLine(dateTime.DayOfWeek);
|
|
|
|
|
dateTime.ToBestDateString().Should().Be(dateTime.ToShortDateString());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void ToBestDateTime_Before_Yesterday()
|
|
|
|
|
{
|
|
|
|
|
var dateTime = DateTime.Today.AddDays(-2);
|
|
|
|
|
|
|
|
|
|
Console.WriteLine(dateTime.DayOfWeek);
|
|
|
|
|
dateTime.ToBestDateString().Should().Be(dateTime.ToShortDateString());
|
|
|
|
|
}
|
2011-11-12 20:21:19 +00:00
|
|
|
|
|
2011-11-25 07:56:07 +00:00
|
|
|
|
[Test]
|
|
|
|
|
public void MaxOrDefault_should_return_zero_when_collection_is_empty()
|
|
|
|
|
{
|
2013-03-28 22:07:09 +00:00
|
|
|
|
|
2011-11-25 07:56:07 +00:00
|
|
|
|
|
|
|
|
|
|
2013-03-28 22:07:09 +00:00
|
|
|
|
|
2011-11-25 07:56:07 +00:00
|
|
|
|
var result = (new List<int>()).MaxOrDefault();
|
|
|
|
|
|
|
|
|
|
//Resolve
|
|
|
|
|
result.Should().Be(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void MaxOrDefault_should_return_max_when_collection_is_not_empty()
|
|
|
|
|
{
|
2013-03-28 22:07:09 +00:00
|
|
|
|
|
2013-03-28 19:05:43 +00:00
|
|
|
|
var list = new List<int> { 6, 4, 5, 3, 8, 10 };
|
2011-11-25 07:56:07 +00:00
|
|
|
|
|
2013-03-28 22:07:09 +00:00
|
|
|
|
|
2011-11-25 07:56:07 +00:00
|
|
|
|
var result = list.MaxOrDefault();
|
|
|
|
|
|
|
|
|
|
//Resolve
|
|
|
|
|
result.Should().Be(10);
|
|
|
|
|
}
|
2012-04-12 23:03:01 +00:00
|
|
|
|
|
2012-06-26 18:27:03 +00:00
|
|
|
|
[Test]
|
|
|
|
|
public void MaxOrDefault_should_return_zero_when_collection_is_null()
|
|
|
|
|
{
|
2013-03-28 22:07:09 +00:00
|
|
|
|
|
2012-06-26 18:27:03 +00:00
|
|
|
|
List<int> list = null;
|
|
|
|
|
|
2013-03-28 22:07:09 +00:00
|
|
|
|
|
2012-06-26 18:27:03 +00:00
|
|
|
|
var result = list.MaxOrDefault();
|
|
|
|
|
|
|
|
|
|
//Resolve
|
|
|
|
|
result.Should().Be(0);
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-12 23:03:01 +00:00
|
|
|
|
[Test]
|
|
|
|
|
public void Truncate_should_truncate_strings_to_max_specified_number_of_bytes()
|
|
|
|
|
{
|
2013-03-28 22:07:09 +00:00
|
|
|
|
|
2016-04-04 17:40:51 +00:00
|
|
|
|
var str = ReadAllText("Files/LongOverview.txt");
|
2012-04-12 23:03:01 +00:00
|
|
|
|
|
2013-03-28 22:07:09 +00:00
|
|
|
|
|
2012-04-12 23:03:01 +00:00
|
|
|
|
var resultString = str.Truncate(1000);
|
|
|
|
|
|
|
|
|
|
//Resolve
|
2013-03-28 19:05:43 +00:00
|
|
|
|
var result = new UTF8Encoding().GetBytes(resultString);
|
2012-04-12 23:03:01 +00:00
|
|
|
|
result.Length.Should().BeLessOrEqualTo(1000);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void Truncate_should_not_truncate_string_shorter_than_max_bytes()
|
|
|
|
|
{
|
2013-03-28 22:07:09 +00:00
|
|
|
|
|
2012-04-12 23:03:01 +00:00
|
|
|
|
var str = "Hello World";
|
|
|
|
|
|
2013-03-28 22:07:09 +00:00
|
|
|
|
|
2012-04-12 23:03:01 +00:00
|
|
|
|
var resultString = str.Truncate(1000);
|
|
|
|
|
|
|
|
|
|
//Resolve
|
2013-02-18 07:59:43 +00:00
|
|
|
|
var result = new UTF8Encoding().GetBytes(resultString);
|
2012-04-12 23:03:01 +00:00
|
|
|
|
result.Length.Should().Be(11);
|
|
|
|
|
}
|
2012-05-26 19:15:13 +00:00
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void MinOrDefault_should_return_zero_when_collection_is_empty()
|
|
|
|
|
{
|
2013-03-28 22:07:09 +00:00
|
|
|
|
|
2012-05-26 19:15:13 +00:00
|
|
|
|
|
|
|
|
|
|
2013-03-28 22:07:09 +00:00
|
|
|
|
|
2012-05-26 19:15:13 +00:00
|
|
|
|
var result = (new List<int>()).MinOrDefault();
|
|
|
|
|
|
|
|
|
|
//Resolve
|
|
|
|
|
result.Should().Be(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
2012-06-26 18:27:03 +00:00
|
|
|
|
public void MinOrDefault_should_return_min_when_collection_is_not_empty()
|
2012-05-26 19:15:13 +00:00
|
|
|
|
{
|
2013-03-28 22:07:09 +00:00
|
|
|
|
|
2012-05-26 19:15:13 +00:00
|
|
|
|
var list = new List<int> { 6, 4, 5, 3, 8, 10 };
|
|
|
|
|
|
2013-03-28 22:07:09 +00:00
|
|
|
|
|
2012-05-26 19:15:13 +00:00
|
|
|
|
var result = list.MinOrDefault();
|
|
|
|
|
|
|
|
|
|
//Resolve
|
|
|
|
|
result.Should().Be(3);
|
|
|
|
|
}
|
2012-06-26 18:27:03 +00:00
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void MinOrDefault_should_return_zero_when_collection_is_null()
|
|
|
|
|
{
|
2013-03-28 22:07:09 +00:00
|
|
|
|
|
2012-06-26 18:27:03 +00:00
|
|
|
|
List<int> list = null;
|
|
|
|
|
|
2013-03-28 22:07:09 +00:00
|
|
|
|
|
2012-06-26 18:27:03 +00:00
|
|
|
|
var result = list.MinOrDefault();
|
|
|
|
|
|
|
|
|
|
//Resolve
|
|
|
|
|
result.Should().Be(0);
|
|
|
|
|
}
|
2013-08-07 00:49:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[TestCase(100,100,100)]
|
|
|
|
|
[TestCase(110,100,100)]
|
|
|
|
|
[TestCase(199,100,100)]
|
|
|
|
|
[TestCase(1000,100,1000)]
|
|
|
|
|
[TestCase(0,100,0)]
|
|
|
|
|
public void round_to_level(long number, int level, int result)
|
|
|
|
|
{
|
|
|
|
|
number.Round(level).Should().Be(result);
|
|
|
|
|
}
|
2011-06-18 17:19:24 +00:00
|
|
|
|
}
|
|
|
|
|
}
|