ttutisani / Xunit.Gherkin.Quick

BDD in .NET Core - using Xunit and Gherkin (compatible with both .NET Core and .NET)
MIT License
205 stars 29 forks source link

Double are converted to integers #131

Closed jclcbrain closed 2 years ago

jclcbrain commented 2 years ago

Hi,

When we use the framework and create a Scenario Step with a value of type double, the value is converted to an integer.

The scenario step is as follows:

Given field some_field is 4.6

The corresponding definition is

[Given(@"field {word} is (\d+\.\d+)$")]
public void SetNumberField(string name, double value)
    {
    }

The value is parsed as 46 instead of 4.6. We've tried multiple regex expressions.

What are we doing wrong here? thx

jclcbrain commented 2 years ago

Okay, so stepping through the gherkin code in VS debugger, I have come to the conclusion that the function DigestScenarioStepValues() for primitive types and especially the Convert:ChangeType() inside this function is dependent on the CultureInfo.InvariantCulture. And since we are based in Europe, the decimal seperator is comma ( , ) . Does XUnit.Gherkin.Quick set the culture automatically based on the system settings? My other projects use Convert.ChangeType() just fine with dots as decimal seperator.

ttutisani commented 2 years ago

InvariantCulture is the one that does not change, which should work in your case too. You can read more here for example: https://stackoverflow.com/questions/2423377/what-is-the-invariant-culture

If you provided valid information, I'm surprised that the conversion does not work properly. Maybe you don't have the dot but something else that looks like a dot? i.e., if you switch between languages and type dots with a non-English keyboard, the code of that character may be different than the code of the dot on an English keyboard. So, try to delete the dot, set your keyboard to English, and type the dot again. Does that make it work?

jclcbrain commented 2 years ago

Hi, okay so I looked into the issue and it might have been that the regex expression used a period ( . ) from a non-english keyboard. I copy-pasted a period from google and it seems to have fixed the issue. Thank you for your time.

jonathanclausen commented 2 years ago

And I set Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US"); in the feature class constructor.