0

I am quite new to C#, coming from C++. I am writing a testing framework in C#, and I want writing the test cases to be as easy as possible.

I have two questions for which I was not able to find a conclusive solution. Therefore I am asking for a little bit of help.

Question #1

Is it possible in C# to pass different types as parameters for a function?

The way I want to call the function is as follows:

[Test]
    public void TestCase1() => RunTestCase("test_data.csv",
        // from_m, to_m, parameter_to_test, operator,          expected_values
        (  1000,   1e6,  "vehicle_speed",   Operator.Greater,  50.0           // <-- double), 
        (  0,      1e6,  "vehicle_acc",     Operator.MinMax,   {500.0, 700.0} // <-- double[]),
        (  0,      70,   "vehicle_gear",    Operator.ValueSet, {1, 2, 3, 4}   // <-- int []),
        (  0,      10,   "vehicle_gear",    Operator.Gear,     1              // <-- int)
        (...),
    );

As you can see, the expected_values can either be double, double[], int or int[] How can I write the signature of the RunTestCase function to allow writing the test cases like this?

At the moment the RunTestCase function signature is as follows:

public void RunTestCase(string test_data_file, 
                        params (double start,
                                double end, 
                                string property, 
                                Operator op, 
                                /* what to use instead of double */ double values)[] expected)

My question is what type can values have, in order to achieve this?

Can a template be used, similar to C++?

Question #2

Is creating the arrays like I did in the example even possible? What I want to achieve is avoiding having to write new int[] {1, 2, 3} and just write {1, 2, 3}. As I said, I'm mostly interested in ease of writing.

Thanks!

  • 1
    *I am writing a testing framework in C#*. **Why??** Unless this is an educational exercise, there's no reason to reinvent the wheel here -- there are already plenty of C# testing frameworks. Just use something like NUnit, XUnit, VSTest, etc. – Daniel Mann Jan 08 '22 at 15:49
  • 1
    The only thing that would allow all those types is either `object` or `dynamic` and you'll find that both would likely make your code more difficult to deal with. As for the arrays you can do `new[] {1, 2, 3}` and it will infer that it's an `int` array. – juharr Jan 08 '22 at 15:49
  • @DanielMann it is an educational exercise. – independent_strudel Jan 08 '22 at 15:51
  • Out of interest, how are you going to implement `RunTestCase`, when the last element of the tuple can be so many different types of things? – Sweeper Jan 08 '22 at 15:52
  • @Sweeper Basically, it all depends on the operator. Equals operator expects one value, MinMax two values, and ValueSet multiple values. Having that, I just check if the property respects the criteria in the test_data. The back end is more or less done, all I need is an easy way to write the test cases as I described in the question. – independent_strudel Jan 08 '22 at 16:00
  • You could pass it some type of `Action` instead then you'd pass `x => x.vehicle_speed.Greater(50.0)` or `x => x.vehicle_acc.MinMax(500.0, 700.0)` assuming that the operators are `void`, or maybe `Func` if they return `bool`. – juharr Jan 08 '22 at 16:21
  • Or if `Operator` is just an enum you could get rid of it in favor of `Func` and pass stuff like `x => x.vehicle_speed > 50.0 ` and `x => x.vehicle_acc is >= 500.0 and <= 700.0` and `x => new[] {1, 2, 3, 4}.Contains(x.vehicle_gear)` and `x => x.vehicle_gear == 1` depending on what the operation is suppose to do. Then users are not limited to just the operators you make available. – juharr Jan 08 '22 at 16:29

0 Answers0