Files
masonry/lang/parser_page_test.go

140 lines
3.3 KiB
Go

package lang
import (
"testing"
)
func TestParsePageDefinitions(t *testing.T) {
tests := []struct {
name string
input string
want AST
wantErr bool
}{
{
name: "page with container and sections",
input: `page UserManagement at "/admin/users" layout AdminLayout title "User Management" auth
meta description "Manage users"
container main class "grid grid-cols-2"
section sidebar class "col-span-1"
component UserStats for User
data from "/users/stats"`,
want: AST{
Definitions: []Definition{
{
Page: &Page{
Name: "UserManagement",
Path: "/admin/users",
Layout: "AdminLayout",
Title: stringPtr("User Management"),
Auth: true,
Meta: []MetaTag{
{Name: "description", Content: "Manage users"},
},
Containers: []Container{
{
Type: "main",
Class: stringPtr("grid grid-cols-2"),
Sections: []Section{
{
Name: "sidebar",
Class: stringPtr("col-span-1"),
Components: []Component{
{
Type: "UserStats",
Entity: stringPtr("User"),
Elements: []ComponentElement{
{
Config: &ComponentAttr{
DataSource: &ComponentDataSource{
Endpoint: "/users/stats",
},
},
},
},
},
},
},
},
},
},
},
},
},
},
wantErr: false,
},
{
name: "page with panel",
input: `page PageWithPanel at "/panel" layout MainLayout
container main
section content
panel UserEditPanel for User trigger "edit" position "slide-right"
component UserForm for User
field name type text required`,
want: AST{
Definitions: []Definition{
{
Page: &Page{
Name: "PageWithPanel",
Path: "/panel",
Layout: "MainLayout",
Containers: []Container{
{
Type: "main",
Sections: []Section{
{
Name: "content",
Panels: []Panel{
{
Name: "UserEditPanel",
Entity: stringPtr("User"),
Trigger: "edit",
Position: stringPtr("slide-right"),
Components: []Component{
{
Type: "UserForm",
Entity: stringPtr("User"),
Elements: []ComponentElement{
{
Field: &ComponentField{
Name: "name",
Type: "text",
Attributes: []ComponentFieldAttribute{
{Required: true},
},
},
},
},
},
},
},
},
},
},
},
},
},
},
},
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := ParseInput(tt.input)
if (err != nil) != tt.wantErr {
t.Errorf("ParseInput() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !tt.wantErr && !astEqual(got, tt.want) {
t.Errorf("ParseInput() mismatch.\nGot: %+v\nWant: %+v", got, tt.want)
}
})
}
}