Handlebars-Net / Handlebars.Net.Helpers

Handlebars.Net helpers in the categories: 'Boolean', 'Constants', 'Enumerable', 'Environment', 'Math', 'Regex', 'String', 'DateTime' and 'Url'.
MIT License
38 stars 14 forks source link

Trying to use Xeger.Generate gives different result than using Xeger directly #78

Closed MarcoMartins86 closed 1 year ago

MarcoMartins86 commented 1 year ago

Describe the bug

Calling

var template = Handlebars.Compile("{{Xeger.Generate {[\"]A[\"]:[\"]A[0-9]{3}[1-9][\"]} }}");
var result = template(null);

or calling

var handlebars = Handlebars.Create(new HandlebarsConfiguration(){NoEscape = true});
var render = handlebars.Compile("{{Xeger.Generate {[\"]A[\"]:[\"]A[0-9]{3}[1-9][\"]} }}");
var actual = render(null);

Returns }}

Calling Fare directly

new Xeger("{[\"]A[\"]:[\"]A[0-9]{3}[1-9][\"]}").Generate();

Returns (1 example) {"A":"A6408"}

Expected behavior:

It should return the same value.

Test to reproduce

[Fact]
public void Descriptive_Test_Name_Here()
{
    var handlebars = Handlebars.Create(new HandlebarsConfiguration(){NoEscape = true});
    var render = handlebars.Compile("{{Xeger.Generate {[\"]A[\"]:[\"]A[0-9]{3}[1-9][\"]} }}");
    var actual = render(null);

    var expected = new Xeger("{[\"]A[\"]:[\"]A[0-9]{3}[1-9][\"]}").Generate();

    Assert.Equal(expected.Length, actual.Length);
}

Other related info

I'm using Wiremock.Net and caught this while creating a stub that I would like to do something like this: "{[\"]A[\"]:[\"]A[0-9]{3}[1-9][\"]}|{[\"]B[\"]:[\"]B[0-9]{3}[1-9][\"]}"

StefH commented 1 year ago

@MarcoMartins86 If you want to return something like {"A":"A6408"}, a better approach would be to generate a json with only the value generated.

Like:

var responseBuilder = Response.Create()
            .WithBodyAsJson(new
            {
                A = "{{Xeger.Generate \"[A[0-9]{3}[1-9]\"}}"
            })
            .WithTransformer();
MarcoMartins86 commented 1 year ago

@StefH Thank you for your reply, in reality, I wanted something more similar to this: "{[\"]A[\"]:[\"]A[0-9]{3}[1-9][\"]}|{[\"]B[\"]:[\"]B[0-9]{3}[1-9][\"]}" Which will result in {"A":"A6408"} or {"B":"B6408"}, the JSON property must be the start of the JSON value.

MarcoMartins86 commented 1 year ago

@StefH While debugging I had a better understanding of the parsing rules and noticed what I was doing wrong, I needed to add single quotes for the Xeger argument to be read as a LiteralExpressionToken, otherwise, it would be read as a WordExpressionToken and it would be split into two arguments because of the closing } in {3}.

Conclusion: "{{Xeger.Generate '{[\"]A[\"]:[\"]A[0-9]{3}[1-9][\"]}' }}" works as expected.

I will close this, thank you again.

StefH commented 1 year ago

https://github.com/Handlebars-Net/Handlebars.Net.Helpers/pull/79