realistschuckle / gohaml

An implementation of the popular XHTML Abstraction Markup Language using the Go language.
MIT License
95 stars 13 forks source link

Can't seem to be able to output struct field values #12

Closed jagregory closed 11 years ago

jagregory commented 11 years ago

The following test fails when trying to evaluate %span= v.Val.

type testObject struct{ Val int }

func TestForArrayObjectRangeConstruct(t *testing.T) {
    scope := make(map[string]interface{})
    scope["looper"] = [5]testObject{
        testObject{4},
        testObject{-128},
        testObject{38},
        testObject{99},
        testObject{1},
    }

    expected := "<p>\n" +
        "   <span>0</span><span>4</span>\n" +
        "   <span>1</span><span>-128</span>\n" +
        "   <span>2</span><span>38</span>\n" +
        "   <span>3</span><span>99</span>\n" +
        "   <span>4</span><span>1</span>\n" +
        "</p>"
    input := "%p\n  - for i, v := range looper\n    %span= i<\n    %span= v.Val"
    engine, _ := NewEngine(input)
    output := engine.Render(scope)

    if output != expected {
        t.Errorf("Expected\n%s\nbut got\n%s\n", expected, output)
    }
}
--- FAIL: TestForArrayObjectRangeConstruct (0.00 seconds)
    lang.go:1507: Expected
        <p>
            <span>0</span><span>4</span>
            <span>1</span><span>-128</span>
            <span>2</span><span>38</span>
            <span>3</span><span>99</span>
            <span>4</span><span>1</span>
        </p>
        but got
        <p>
            <span>0</span><span><invalid Value></span>
            <span>1</span><span><invalid Value></span>
            <span>2</span><span><invalid Value></span>
            <span>3</span><span><invalid Value></span>
            <span>4</span><span><invalid Value></span>
        </p>
realistschuckle commented 11 years ago

@jagregory Thanks for finding this issue. I have made a commit to fix it. I also ensured that it works for slices, too.

jagregory commented 11 years ago

Great, thanks for the timely response!