Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature request] Natural number sequence Theory #2918

Closed
xp44mm opened this issue Apr 27, 2024 · 3 comments
Closed

[Feature request] Natural number sequence Theory #2918

xp44mm opened this issue Apr 27, 2024 · 3 comments

Comments

@xp44mm
Copy link

xp44mm commented Apr 27, 2024

Natural number sequence Theory is the simplest form of testing our theory with data.

Currently, I use the InlineDataAttribute attribute to represent array indices

type NaturalNumberSeqTest(output: ITestOutputHelper) =
    [<Theory>]
    [<InlineData(0)>]
    [<InlineData(1)>]
    [<InlineData(2)>]
    [<InlineData(3)>]
    member _.``Natural number sequence``(i:int) =
        let expect = [0;1;2;3]
        let actual = [0;1;2;3]
        Should.equal expect.[i] actual.[i]

Feature request: same code using NaturalAttribute attribute

    [<Theory>]
    [<Natural(4)>]
    member _.``Natural number sequence``(i:int) =
        let expect = [0;1;2;3]
        let actual = [0;1;2;3]
        Should.equal expect.[i] actual.[i]
@bartelink
Copy link

bartelink commented Apr 27, 2024

TheoryData is a good general base for stuff like this, rather than having such specific helpers. Recent releases if V2 have a constructor that takes an IEnumerable<T>, which woks well with F# Seq expressions and/or other list comprehensions.

Something like this:

type NaturalNumberSeqTest(output: ITestOutputHelper) =

    let nats = TheoryData(seq { 0..4 })
    [<Theory; MemberData(nameof nats)>]
    let ``Natural number sequence``(i:int) =
        let expect = [0;1;2;3]
        let actual = [0;1;2;3]
        Should.equal expect.[i] actual.[i]

(The fact that your example is a bit contrived gives away the fact that this is not your true requirement, but my general contention that having lots of generation helpers like this becomes very messy remains)

@bradwilson
Copy link
Member

bradwilson commented May 8, 2024

@bartelink's answer is a reasonable solution here.

Additionally, for re-usability you can create your own data sources by creating an attribute that derives from DataAttribute. Sample (utilizing TheoryData) in C#:

public sealed class NaturalAttribute : DataAttribute
{
	private readonly TheoryData<int> values;

	public NaturalAttribute(int count) :
		this(0, count)
	{ }

	public NaturalAttribute(int start, int count) =>
		values = new TheoryData<int>(Enumerable.Range(start, count));

	public override IEnumerable<object[]> GetData(MethodInfo testMethod) =>
		values;
}

@xp44mm
Copy link
Author

xp44mm commented May 9, 2024

Your code has been worked perfectly for me, thank you. @bradwilson

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants