mirror of
https://github.com/chylex/TweetDuck.git
synced 2025-05-11 20:34:07 +02:00
Add unit tests for Result and a few utility methods & fix edge case in StringUtils
This commit is contained in:
parent
25a8ddffd4
commit
6504dc9184
Core/Utils
lib/TweetTest.Unit
@ -28,10 +28,14 @@ public static string ConvertRot13(string str){
|
||||
}
|
||||
|
||||
public static int CountOccurrences(string source, string substring){
|
||||
int count = 0, index = 0;
|
||||
if (substring.Length == 0){
|
||||
throw new ArgumentOutOfRangeException(nameof(substring), "Searched substring must not be empty.");
|
||||
}
|
||||
|
||||
int count = 0, index = 0, length = substring.Length;
|
||||
|
||||
while((index = source.IndexOf(substring, index)) != -1){
|
||||
index += substring.Length;
|
||||
index += length;
|
||||
++count;
|
||||
}
|
||||
|
||||
|
@ -75,4 +75,44 @@ module ConvertPascalCaseToScreamingSnakeCase =
|
||||
Assert.Equal("I_LIKE_HTML_AND_CSS", StringUtils.ConvertPascalCaseToScreamingSnakeCase("ILikeHTMLAndCSS"))
|
||||
|
||||
|
||||
// TODO add new tests
|
||||
module ConvertRot13 =
|
||||
|
||||
[<Fact>]
|
||||
let ``ignores digits and special characters`` () =
|
||||
Assert.Equal("<123'456.789>", StringUtils.ConvertRot13("<123'456.789>"))
|
||||
|
||||
[<Fact>]
|
||||
let ``converts lowercase letters correctly`` () =
|
||||
Assert.Equal("nopqrstuvwxyzabcdefghijklm", StringUtils.ConvertRot13("abcdefghijklmnopqrstuvwxyz"))
|
||||
|
||||
[<Fact>]
|
||||
let ``converts uppercase letters correctly`` () =
|
||||
Assert.Equal("NOPQRSTUVWXYZABCDEFGHIJKLM", StringUtils.ConvertRot13("ABCDEFGHIJKLMNOPQRSTUVWXYZ"))
|
||||
|
||||
[<Fact>]
|
||||
let ``converts mixed character types correctly`` () =
|
||||
Assert.Equal("Uryyb, jbeyq! :)", StringUtils.ConvertRot13("Hello, world! :)"))
|
||||
|
||||
|
||||
module CountOccurrences =
|
||||
open System
|
||||
|
||||
[<Fact>]
|
||||
let ``empty input string returns zero`` () =
|
||||
Assert.Equal(0, StringUtils.CountOccurrences("", "a"))
|
||||
|
||||
[<Fact>]
|
||||
let ``empty searched string throws`` () =
|
||||
Assert.Throws<ArgumentOutOfRangeException>(fun () -> StringUtils.CountOccurrences("hello", "") |> ignore)
|
||||
|
||||
[<Fact>]
|
||||
let ``counts single letter characters correctly`` () =
|
||||
Assert.Equal(3, StringUtils.CountOccurrences("hello world", "l"))
|
||||
|
||||
[<Fact>]
|
||||
let ``counts longer substrings correctly`` () =
|
||||
Assert.Equal(2, StringUtils.CountOccurrences("hello and welcome in hell", "hell"))
|
||||
|
||||
[<Fact>]
|
||||
let ``does not count overlapping substrings`` () =
|
||||
Assert.Equal(2, StringUtils.CountOccurrences("aaaaa", "aa"))
|
||||
|
@ -97,4 +97,16 @@ module GetMediaLink_Orig =
|
||||
Assert.Equal(domain+"/profile_images/123.jpg:orig", getMediaLinkOrig(domain+"/profile_images/123.jpg:small"))
|
||||
|
||||
|
||||
// TODO add new tests
|
||||
module GetImageFileName =
|
||||
|
||||
[<Fact>]
|
||||
let ``extracts file name from URL w/o quality suffix`` () =
|
||||
Assert.Equal("test.jpg", TwitterUtils.GetImageFileName("http://example.com/test.jpg"))
|
||||
|
||||
[<Fact>]
|
||||
let ``extracts file name from URL with quality suffix`` () =
|
||||
Assert.Equal("test.jpg", TwitterUtils.GetImageFileName("http://example.com/test.jpg:orig"))
|
||||
|
||||
[<Fact>]
|
||||
let ``extracts file name from URL with a port`` () =
|
||||
Assert.Equal("test.jpg", TwitterUtils.GetImageFileName("http://example.com:80/test.jpg"))
|
||||
|
57
lib/TweetTest.Unit/Data/TestResult.fs
Normal file
57
lib/TweetTest.Unit/Data/TestResult.fs
Normal file
@ -0,0 +1,57 @@
|
||||
namespace Unit.Data
|
||||
|
||||
open Xunit
|
||||
open TweetDuck.Data
|
||||
open System
|
||||
|
||||
|
||||
module TestResult_WithValue =
|
||||
let result = Result<int>(10)
|
||||
|
||||
[<Fact>]
|
||||
let ``HasValue returns true`` () =
|
||||
Assert.True(result.HasValue)
|
||||
|
||||
[<Fact>]
|
||||
let ``Accessing Value returns the provided value`` () =
|
||||
Assert.Equal(10, result.Value)
|
||||
|
||||
[<Fact>]
|
||||
let ``Accessing Exception throws`` () =
|
||||
Assert.Throws<InvalidOperationException>(fun () -> result.Exception |> ignore)
|
||||
|
||||
[<Fact>]
|
||||
let ``Handle calls the correct callback`` () =
|
||||
let passTest = fun _ -> ()
|
||||
let failTest = fun _ -> Assert.True(false)
|
||||
result.Handle(Action<_>(passTest), Action<_>(failTest))
|
||||
|
||||
[<Fact>]
|
||||
let ``Select returns another valid Result`` () =
|
||||
Assert.Equal(20, result.Select(fun x -> x * 2).Value)
|
||||
|
||||
|
||||
module TestResult_WithException =
|
||||
let result = Result<int>(IndexOutOfRangeException("bad"))
|
||||
|
||||
[<Fact>]
|
||||
let ``HasValue returns false`` () =
|
||||
Assert.False(result.HasValue)
|
||||
|
||||
[<Fact>]
|
||||
let ``Accessing Value throws`` () =
|
||||
Assert.Throws<InvalidOperationException>(fun () -> result.Value |> ignore)
|
||||
|
||||
[<Fact>]
|
||||
let ``Accessing Exception returns the provided exception`` () =
|
||||
Assert.IsType<IndexOutOfRangeException>(result.Exception)
|
||||
|
||||
[<Fact>]
|
||||
let ``Handle calls the correct callback`` () =
|
||||
let passTest = fun _ -> ()
|
||||
let failTest = fun _ -> Assert.True(false)
|
||||
result.Handle(Action<_>(failTest), Action<_>(passTest))
|
||||
|
||||
[<Fact>]
|
||||
let ``Select returns a Result with the same Exception`` () =
|
||||
Assert.Same(result.Exception, result.Select(fun x -> x * 2).Exception)
|
@ -62,9 +62,10 @@
|
||||
<Import Project="..\..\packages\Microsoft.NET.Test.Sdk.15.7.2\build\net45\Microsoft.Net.Test.Sdk.targets" Condition="Exists('..\..\packages\Microsoft.NET.Test.Sdk.15.7.2\build\net45\Microsoft.Net.Test.Sdk.targets')" />
|
||||
<ItemGroup>
|
||||
<Content Include="packages.config" />
|
||||
<Compile Include="Core\TestStringUtils.fs" />
|
||||
<Compile Include="Core\TestBrowserUtils.fs" />
|
||||
<Compile Include="Core\TestStringUtils.fs" />
|
||||
<Compile Include="Core\TestTwitterUtils.fs" />
|
||||
<Compile Include="Data\TestResult.fs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="Microsoft.VisualStudio.CodeCoverage.Shim">
|
||||
|
Loading…
Reference in New Issue
Block a user