Skip to content

Commit

Permalink
Merge branch 'refactor/useFsUnit' of avp-gitlab.availpro.com:aloisdg/…
Browse files Browse the repository at this point in the history
…cardizer into develop
  • Loading branch information
d-edge-bot committed May 25, 2021
2 parents 92ba876 + cb73a63 commit f040cd6
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 56 deletions.
126 changes: 70 additions & 56 deletions src/Tests/Tests.fs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module Tests

open Xunit
// note: we may want to move to expecto instead
open FsUnit.Xunit
open Dedge

let inline charToInt c = int c - 48
Expand All @@ -16,88 +18,100 @@ let luhn (s: string) =

(g 0 0 s.Length) % 10 = 0

// let TrueWithMessage (message: string) : NHamcrest.IMatcher<obj> =
// let matcher =
// new NHamcrest.Core.IsEqualMatcher<obj>(true)

// matcher.DescribedAs(message)

let LuhnCheck : NHamcrest.IMatcher<obj> =
let matcher =
new NHamcrest.Core.IsEqualMatcher<obj>(true)

matcher.DescribedAs $"Fail the Luhn check."

[<Theory>]
[<InlineData(VisaLengthOptions.Thirteen, 13, 13)>]
[<InlineData(VisaLengthOptions.Sixteen, 16, 16)>]
[<InlineData(VisaLengthOptions.Random, 13, 16)>]
let ``Should generate valid Visa`` length a b =
[<InlineData(VisaLengthOptions.Thirteen, 13)>]
[<InlineData(VisaLengthOptions.Sixteen, 16)>]
// [<InlineData(VisaLengthOptions.Random, 13, 16)>]
let ``Should generate valid Visa`` length expectedLength =
let card = Cardizer.NextVisa length

Assert.StartsWith("4", card)
Assert.True(card.Length = a || card.Length = b, $"The credit card should have a length of 16 but has {card.Length}.")
Assert.True(luhn card, $"The credit card number {card} failed the Luhn Check.")
card |> should startWith "4"
card |> should haveLength expectedLength
card |> luhn |> should be LuhnCheck

[<Theory>]
[<InlineData(VerveLengthOptions.Sixteen, 16, 16)>]
[<InlineData(VerveLengthOptions.Nineteen, 19, 19)>]
[<InlineData(VerveLengthOptions.Random, 16, 19)>]
let ``Should generate valid Verve`` length a b =
[<InlineData(VerveLengthOptions.Sixteen, 16)>]
[<InlineData(VerveLengthOptions.Nineteen, 19)>]
// [<InlineData(VerveLengthOptions.Random, 16, 19)>]
let ``Should generate valid Verve`` length expectedLength =
let card = Cardizer.NextVerve length
let start = card.Substring(0, 6) |> int

let prefixInRange =
start >= 506099 && start <= 506198
|| start >= 650002 && start <= 650027

Assert.True(prefixInRange, $"The credit card should not start with {start}.")

Assert.True(
card.Length = a || card.Length = b,
$"The credit card should have a length of 16 or 19 but has {card.Length}."
)

Assert.True(luhn card, $"The credit card number {card} failed the Luhn Check.")
prefixInRange |> should be True
card |> should haveLength expectedLength // (subsetOf [ 16; 19 ]) // note: is there a better way for a is b or c?
card |> luhn |> should be LuhnCheck

[<Theory>]
[<InlineData(MirLengthOptions.Sixteen, 16, 16)>]
[<InlineData(MirLengthOptions.Seventeen, 17, 17)>]
[<InlineData(MirLengthOptions.Eightteen, 18, 18)>]
[<InlineData(MirLengthOptions.Nineteen, 19, 19)>]
[<InlineData(MirLengthOptions.Random, 16, 19)>]
let ``Should generate valid Mir`` length low high =
[<InlineData(MirLengthOptions.Sixteen, 16)>]
[<InlineData(MirLengthOptions.Seventeen, 17)>]
[<InlineData(MirLengthOptions.Eightteen, 18)>]
[<InlineData(MirLengthOptions.Nineteen, 19)>]
// [<InlineData(MirLengthOptions.Random, 16, 19)>]
let ``Should generate valid Mir`` length expectedLength =
let card = Cardizer.NextMir length
Assert.StartsWith("220", card)
Assert.InRange(card.[3], '0', '4')
Assert.InRange(card.Length, low, high)
Assert.True(luhn card, $"The credit card number {card} failed the Luhn Check.")

card |> should startWith "220"
card.[3] |> should be (inRange '0' '4')
card |> should haveLength expectedLength
card |> luhn |> should be LuhnCheck

[<Theory>]
[<InlineData(JcbLengthOptions.Sixteen, 16, 16)>]
[<InlineData(JcbLengthOptions.Seventeen, 17, 17)>]
[<InlineData(JcbLengthOptions.Eightteen, 18, 18)>]
[<InlineData(JcbLengthOptions.Nineteen, 19, 19)>]
[<InlineData(JcbLengthOptions.Random, 16, 19)>]
let ``Should generate valid Jcb`` length low high =
[<InlineData(JcbLengthOptions.Sixteen, 16)>]
[<InlineData(JcbLengthOptions.Seventeen, 17)>]
[<InlineData(JcbLengthOptions.Eightteen, 18)>]
[<InlineData(JcbLengthOptions.Nineteen, 19)>]
// [<InlineData(JcbLengthOptions.Random, 16, 19)>]
let ``Should generate valid Jcb`` length expectedLength =
let card = Cardizer.NextJcb length
Assert.StartsWith("35", card)
Assert.InRange(charToInt card.[2], 2, 8)
Assert.InRange(charToInt card.[3], 8, 9)
Assert.InRange(card.Length, low, high)
Assert.True(luhn card, $"The credit card number {card} failed the Luhn Check.")

card |> should startWith "35"
card.[2] |> should be (inRange '2' '8')
card.[3] |> should be (inRange '8' '9')
card |> should haveLength expectedLength
card |> luhn |> should be LuhnCheck

[<Fact>]
let ``Should generate valid Amex`` () =
let card = Cardizer.NextAmex()
Assert.StartsWith("3", card)
Assert.Contains(string card.[1], "47")
Assert.Equal(15, card.Length)
Assert.True(luhn card, $"The credit card number {card} failed the Luhn Check.")

card |> should startWith "3"
[ card.[1] ] |> should be (subsetOf [ '4'; '7' ]) // note: is there a better way for a is b or c?
card |> should haveLength 15
card |> luhn |> should be LuhnCheck

[<Theory>]
[<InlineData(DiscoverLengthOptions.Sixteen, 16, 16)>]
[<InlineData(DiscoverLengthOptions.Seventeen, 17, 17)>]
[<InlineData(DiscoverLengthOptions.Eightteen, 18, 18)>]
[<InlineData(DiscoverLengthOptions.Nineteen, 19, 19)>]
[<InlineData(DiscoverLengthOptions.Random, 16, 19)>]
let ``Should generate valid Discover`` length low high =
[<InlineData(DiscoverLengthOptions.Sixteen, 16)>]
[<InlineData(DiscoverLengthOptions.Seventeen, 17)>]
[<InlineData(DiscoverLengthOptions.Eightteen, 18)>]
[<InlineData(DiscoverLengthOptions.Nineteen, 19)>]
// [<InlineData(DiscoverLengthOptions.Random, 16, 19)>]
let ``Should generate valid Discover`` length expectedLength =
let card = Cardizer.NextDiscover length
Assert.StartsWith("6011", card)
Assert.InRange(card.Length, low, high)
Assert.True(luhn card, $"The credit card number {card} failed the Luhn Check.")

card |> should startWith "6011"
card |> should haveLength expectedLength
card |> luhn |> should be LuhnCheck

[<Fact>]
let ``Should generate valid MasterCard`` () =
let card = Cardizer.NextMasterCard()
Assert.StartsWith("5", card)
Assert.Equal(16, card.Length)
Assert.True(luhn card, $"The credit card number {card} failed the Luhn Check.")

card |> should startWith "5"
card |> should haveLength 16
card |> luhn |> should be LuhnCheck
1 change: 1 addition & 0 deletions src/Tests/Tests.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="FsUnit.xUnit" Version="4.0.4" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
Expand Down

0 comments on commit f040cd6

Please sign in to comment.