Skip to content

Nothing works in the samples (tests) #16

@paciox

Description

@paciox

Versions:


.NET 8

Describe the bug

indexers, samples given, don't work.
https://github.com/Handlebars-Net/Handlebars.Net.Extension.Json/blob/main/source/Handlebars.Extension.Test/JsonTests.cs

        [Theory]
        [ClassData(typeof(EnvGenerator))]
        public void ArrayCount(IHandlebars handlebars)
        {
	        var model = JsonDocument.Parse("[{\"Key\": \"Key1\", \"Value\": \"Val1\"},{\"Key\": \"Key2\", \"Value\": \"Val2\"}]");

	        var source = "{{this.Count}} = {{this.Length}}";

	        var template = handlebars.Compile(source);

	        var output = template(model);

	        Assert.Equal("2 = 2", output);
        }
        

Count/Lenght don't do anything
Sample:
image


    [Theory]
        [ClassData(typeof(EnvGenerator))]
        public void ArrayIndexProperties(IHandlebars handlebars)
        {
            var model = JsonDocument.Parse("[\"Index0\", \"Index1\"]");

            var source = "{{@root.1}}";

            var template = handlebars.Compile(source);

            var output = template(model);

            Assert.Equal("Index1", output);
        }

        [Theory]
        [ClassData(typeof(EnvGenerator))]
        public void ArrayIndexPropertiesNested(IHandlebars handlebars)
        {
            var model = JsonDocument.Parse("[{}, {\"Items\": [\"Index0\", \"Index1\"]}]");

            var source = "{{@root.1.Items.1}}";

            var template = handlebars.Compile(source);

            var output = template(model);

            Assert.Equal("Index1", output);
        }

Count and indexer don't do anything (using 1 and 0)

image

If I remove 0 it works and shows the whole array.

Expected behavior:

to work?

Test to reproduce

var handlebars = Handlebars.Create();
handlebars.Configuration.UseJson();

//Handlebars testing, uncomment to see results
var handlebarsModel = JsonDocument.Parse(jsonContent);
//Test 1 iterate and test various properties
var testTemplate1 = "{{#each this.body.participants}} COUNT: {{this.Count}} LENGTH: {{this.length}} INDEX: {{@index}} ISFIRST: {{@first}} ISLAST: {{@last}} PARTICIPANT NAME:{{@value.name}} PARTICIPANT ROLE: {{@value.role}};{{/each}}";

//can be done also like
//var source = "{{#each this}}{{@key}}{{@value}}{{/each}}";

var compiledTemplate1 = handlebars.Compile(testTemplate1);

var resultTemplate1 = compiledTemplate1(handlebarsModel);

 //https://github.com/Handlebars-Net/Handlebars.Net.Extension.Json/blob/main/source/Handlebars.Extension.Test/JsonTests.cs
 //In the examples they use @root.0 but it doesn't work..... nor @root.0 and this.0 works to navigate elements
 //@root.body.participants and this.body.participants works, but not the indexer 0,1,2
 //var testTemplate2 = "{{@root.body.participants.0}}";
 //var testTemplate2 = "{{@root.body.participants.0.name}}";
 //var compiledTemplate2 = handlebars.Compile(testTemplate2);

 //var resultTemplate2 = compiledTemplate2(handlebarsModel);

Other related info

Sample json used:

{
  "header": {
    "partyName": "Sweet Treats Fiesta",
    "theme": "Desserts Extravaganza",
    "host": {
      "name": "Jane Doe",
      "contact": "[email protected]"
    },
    "date": "2024-09-28",
    "day": "Saturday",
    "hour": "5:00 PM",
    "address": {
      "street": "123 Party Lane",
      "city": "Funville",
      "state": "CA",
      "zip": "90210"
    }
  },
  "body": {
    "participants": [
      {
        "name": "Alice Smith",
        "role": "Guest",
        "RSVP": true
      },
      {
        "name": "John Johnson",
        "role": "Guest",
        "RSVP": false
      },
      {
        "name": "Emily Davis",
        "role": "Caterer",
        "RSVP": true
      }
    ],
    "menu": {
      "items": [
        {
          "id": "0001",
          "type": "donut",
          "name": "Cake",
          "ppu": 0.55,
          "batters": {
            "batter": [
              {
                "id": "1001",
                "type": "Regular"
              },
              {
                "id": "1002",
                "type": "Chocolate"
              },
              {
                "id": "1003",
                "type": "Blueberry"
              },
              {
                "id": "1004",
                "type": "Devil's Food"
              }
            ]
          },
          "topping": [
            {
              "id": "5001",
              "type": "None"
            },
            {
              "id": "5002",
              "type": "Glazed"
            },
            {
              "id": "5005",
              "type": "Sugar"
            },
            {
              "id": "5007",
              "type": "Powdered Sugar"
            },
            {
              "id": "5006",
              "type": "Chocolate with Sprinkles"
            },
            {
              "id": "5003",
              "type": "Chocolate"
            },
            {
              "id": "5004",
              "type": "Maple"
            }
          ]
        },
        {
          "id": "0002",
          "type": "donut",
          "name": "Raised",
          "ppu": 0.55,
          "batters": {
            "batter": [
              {
                "id": "1001",
                "type": "Regular"
              }
            ]
          },
          "topping": [
            {
              "id": "5001",
              "type": "None"
            },
            {
              "id": "5002",
              "type": "Glazed"
            },
            {
              "id": "5005",
              "type": "Sugar"
            },
            {
              "id": "5003",
              "type": "Chocolate"
            },
            {
              "id": "5004",
              "type": "Maple"
            }
          ]
        },
        {
          "id": "0003",
          "type": "donut",
          "name": "Old Fashioned",
          "ppu": 0.55,
          "batters": {
            "batter": [
              {
                "id": "1001",
                "type": "Regular"
              },
              {
                "id": "1002",
                "type": "Chocolate"
              }
            ]
          },
          "topping": [
            {
              "id": "5001",
              "type": "None"
            },
            {
              "id": "5002",
              "type": "Glazed"
            },
            {
              "id": "5003",
              "type": "Chocolate"
            },
            {
              "id": "5004",
              "type": "Maple"
            }
          ]
        }
      ]
    }
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions