func TestResolver(t *testing.T) { rx := dispatch.NewResolver("/:id") params, _, state := rx.Test("12") if !state { tests.Failed(t, "Should have matched giving path") } tests.Passed(t, "Should have matched giving path") val, ok := params["id"] if !ok { tests.Failed(t, "Should have retrieve parameter :id => %s", val) } tests.Passed(t, "Should have retrieve parameter :id => %s", val) rx.ResolvedPassed(func(px dispatch.Path) { tests.Passed(t, "Should have notified with Path %#v", px) }) rx.ResolvedFailed(func(px dispatch.Path) { tests.Failed(t, "Should have notified with Path %#v", px) }) rx.Resolve(dispatch.UseLocation("/12")) }
func TestResourceRendering(t *testing.T) { trees.SetMode(trees.Testing) defer trees.SetMode(trees.Normal) _ = design.Resource(func() { design.DoOrder(design.Any) design.UseRoute("/home/*") design.DoCSS("../some.css", false) design.DoScript("../some.js", "text/javascript", false) design.DoMarkup(elems.Header1(elems.Text("Speed Dashboard")), "", false) design.DoMarkup(func() *trees.Markup { return elems.Div( elems.Section( elems.Label(elems.Text("Current Speed")), ), ) }, "", false) design.DoView(gu.Static(elems.Div( elems.Section( design.DoRoute("/models/*", "/:speed"), elems.Label(elems.Text("Total Speed")), ), )), "", false) }) master := design.New().Init() testRender := master.Render(dispatch.UseLocation("/home/")) if testRender.HTML() != fullRender { t.Logf("\t\tExpected: %q", fullRender) t.Logf("\t\tReceived: %q", testRender.HTML()) tests.Failed(t, "Should have rendered the expected full markup") } tests.Passed(t, "Should have rendered the expected full markup") testRender2 := master.Render(dispatch.UseLocation("/home/models/340mph")) if testRender2.HTML() != routedRender { t.Logf("\t\tExpected: %q", routedRender) t.Logf("\t\tReceived: %q", testRender2.HTML()) tests.Failed(t, "Should have rendered the routed full markup") } tests.Passed(t, "Should have rendered the routed full markup") testFlatRender := master.Render(dispatch.UseLocation("/root")) if testFlatRender.HTML() != flatRender { t.Logf("\t\tExpected: %q", flatRender) t.Logf("\t\tReceived: %q", testFlatRender.HTML()) tests.Failed(t, "Should have rendered the expected markup") } tests.Passed(t, "Should have rendered the expected markup") }
func TestLinkedCSS(t *testing.T) { expected := "#galatica block {\n Helvetica\n color: Pink;\n}\n#galatica::before {\n content: \"bugger\";\n}\n#galatica div a {\n color: black;\n font-family: Helvetica;\n}\n@media (max-width: 400px) {\n #galatica:hover {\n color: blue;\n font-family: Helvetica;\n }\n}" csr := css.New(` block { font-family: {{ .Font }} color: {{ .Color }} } `) csx := css.New(` ::before { content: "bugger"; } div a { color: black; font-family: {{ .Font }} } @media (max-width: 400px){ :hover { color: blue; font-family: {{ .Font }} } } `, csr) sheet, err := csx.Stylesheet(struct { Font string Color string }{ Font: "Helvetica", Color: "Pink", }, "#galatica") if err != nil { tests.Failed(t, "Should have successfully processed stylesheet for rule") } tests.Passed(t, "Should have successfully processed stylesheet for rule") if val := sheet.String(); val != expected { t.Logf("\t\tRecieved: %q\n", val) t.Logf("\t\tExpected: %q\n", expected) tests.Failed(t, "Should have rendered expected stylesheet") } tests.Passed(t, "Should have rendered expected stylesheet") }
func TestParseMultiSelectors(t *testing.T) { sels := trees.Query.ParseSelector("div.shower.ball a.embeded, h1#header.main-header.color, div[alt*='blocker'], div::nth-child(2n+1)") if sels == nil { tests.Failed(t, "Should have returned lists of selectors") } tests.Passed(t, "Should have returned lists of selectors") if len(sels) != 4 { tests.Failed(t, "Should have returned only 4 elements") } tests.Passed(t, "Should have returned only 4 elements") if len(sels[0].Children) != 1 { tests.Failed(t, "Should have atleast first element with only 1 elements") } tests.Passed(t, "Should have atleast first element with only 1 elements") }
func TestRoute(t *testing.T) { rm := gu.NewRouteManager() home := rm.L("/home/*") if _, _, pass := home.Test("/home/models/12"); !pass { tests.Failed(t, "Should have validated path /home/models/12") } tests.Passed(t, "Should have validated path /home/models/12") index := rm.L("/index/*") if _, _, pass := index.Test("/index"); !pass { tests.Failed(t, "Should have validated path /index") } tests.Passed(t, "Should have validated path /index") getModel := home.N("/models/*") if _, _, pass := getModel.Test("/models"); !pass { tests.Failed(t, "Should have validated path /models") } tests.Passed(t, "Should have validated path /models") if _, _, pass := getModel.Test("/models/12"); !pass { tests.Failed(t, "Should have validated path /models/12") } tests.Passed(t, "Should have validated path /models/12") id := getModel.N("/:id") param, _, pass := id.Test("/12") if !pass { tests.Failed(t, "Should have validated path /12") } tests.Passed(t, "Should have validated path /12: %#v", param) home.ResolvedPassed(func(px dispatch.Path) { tests.Passed(t, "Should have validated path /home/models/12: /home") }).ResolvedFailed(func(px dispatch.Path) { tests.Failed(t, "Should have validated path /home/models/12: /home") }) getModel.ResolvedPassed(func(px dispatch.Path) { tests.Passed(t, "Should have validated path /home/models/12: /models") }).ResolvedFailed(func(px dispatch.Path) { tests.Failed(t, "Should have validated path /home/models/12: /models") }) id.ResolvedPassed(func(px dispatch.Path) { tests.Passed(t, "Should have validated path /home/models/12: /id") }).ResolvedFailed(func(px dispatch.Path) { tests.Failed(t, "Should have validated path /home/models/12: /id") }) home.Resolve(dispatch.UseLocation("/home/models/12")) home.Resolve(dispatch.UseLocationHash("http://thunderhouse.com/#home/models/12")) }
func TestBasicCSS(t *testing.T) { expected := "#galatica:hover {\n color: red;\n}\n#galatica::before {\n content: \"bugger\";\n}\n#galatica div a {\n color: black;\n font-family: Helvetica;\n}\n@media (max-width: 400px) {\n #galatica:hover {\n color: blue;\n font-family: Helvetica;\n }\n}" csr := css.New(` $:hover { color: red; } $::before { content: "bugger"; } $ div a { color: black; font-family: {{ .Font }} } @media (max-width: 400px){ $:hover { color: blue; font-family: {{ .Font }} } } `) sheet, err := csr.Stylesheet(struct { Font string }{Font: "Helvetica"}, "#galatica") if err != nil { tests.Failed(t, "Should have successfully processed stylesheet for rule") } tests.Passed(t, "Should have successfully processed stylesheet for rule") if val := sheet.String(); val != expected { t.Logf("\t\tRecieved: %q\n", val) t.Logf("\t\tExpected: %q\n", expected) tests.Failed(t, "Should have rendered expected stylesheet") } tests.Passed(t, "Should have rendered expected stylesheet") }
func TestResolverFailed(t *testing.T) { rx := dispatch.NewResolver("/:id") rx.ResolvedPassed(func(px dispatch.Path) { tests.Failed(t, "Should have notified with failed Path %#v", px) }) rx.ResolvedFailed(func(px dispatch.Path) { tests.Passed(t, "Should have notified with failed Path %#v", px) }) rx.Resolve(dispatch.UseLocation("/home/12")) }
func TestParseAttr(t *testing.T) { sels := trees.Query.ParseSelector("div[rel|='bull']") if sels == nil { tests.Failed(t, "Should have returned lists of selectors") } tests.Passed(t, "Should have returned lists of selectors") sel := sels[0] if sel.AttrName != "rel" { tests.Failed(t, "Should have selector with attribute name 'rel'") } tests.Passed(t, "Should have selector with attribute name 'rel'") if sel.AttrOp != "|=" { tests.Failed(t, "Should have selector with attribute op '|='") } tests.Passed(t, "Should have selector with attribute op '|='") if sel.AttrValue != "bull" { tests.Failed(t, "Should have selector with attribute value 'bull'") } tests.Passed(t, "Should have selector with attribute value 'bull'") }
func TestResolverLevels(t *testing.T) { home := dispatch.NewResolver("/home/*") rx := dispatch.NewResolver("/:id") home.Register(rx) rx.ResolvedPassed(func(px dispatch.Path) { tests.Passed(t, "Should have notified with Path %#v", px) }) rx.ResolvedFailed(func(px dispatch.Path) { tests.Failed(t, "Should have notified with Path %#v", px) }) home.Resolve(dispatch.UseLocation("home/12")) }
func TestParseSelector(t *testing.T) { sels := trees.Query.ParseSelector("div.shower.ball") if sels == nil { tests.Failed(t, "Should have returned lists of selectors") } tests.Passed(t, "Should have returned lists of selectors") elem := sels[0] if elem.Tag != "div" { tests.Failed(t, "Should have div as selector tag") } tests.Passed(t, "Should have div as selector tag") if elem.AttrName != "" { tests.Failed(t, "Should not have a attribute match required: %q", elem.AttrName) } tests.Passed(t, "Should not have a attribute match required") if len(elem.Classes) != 2 { tests.Failed(t, "Should have 2 classes as part of element selector") } tests.Passed(t, "Should have 2 classes as part of element selector") if elem.Classes[0] != "shower" { tests.Failed(t, "Should contain %q as part of its classes", elem.Classes[0]) } tests.Passed(t, "Should contain %q as part of its class", elem.Classes[0]) if elem.Classes[1] != "ball" { tests.Failed(t, "Should contain %q as part of its classes", elem.Classes[1]) } tests.Passed(t, "Should contain %q as part of its class", elem.Classes[1]) }
func TestQueries(t *testing.T) { tree := trees.ParseAsRoot("section.tree-house#house", ` <div class="wrapper" aria="wrapper-div"> <section id="header" class="section"></section> <section id="menu" class="section"></section> <section id="content" class="section"></section> </div> <div class="links"> <a rel="delay" href="#delay">Delay</a> </div> `) class, err := trees.GetAttr(tree, "class") if err != nil { tests.Failed(t, "Should have root with provided class property") } tests.Passed(t, "Should have root with provided class property") if _, val := class.Render(); val != "tree-house" { tests.Failed(t, "Should have class value matching 'tree-house': %q", val) } tests.Passed(t, "Should have class value matching 'tree-house'") id, err := trees.GetAttr(tree, "id") if err != nil { tests.Failed(t, "Should have root with provided id property") } tests.Passed(t, "Should have root with provided id property") if _, val := id.Render(); val != "house" { tests.Failed(t, "Should have class value matching 'house': %q", val) } tests.Passed(t, "Should have class value matching 'house'") if div := trees.Query.Query(tree, "div.wrapper"); div == nil { tests.Failed(t, "Should have returned a div with provided class 'wrapper'") } tests.Passed(t, "Should have returned a div with provided class 'wrapper'") if item := trees.Query.Query(tree, "section#menu"); item == nil { tests.Failed(t, "Should have returned a section with provided id 'menu'") } tests.Passed(t, "Should have returned a section with provided id 'menu'") if div := trees.Query.Query(tree, "div[aria*=wrapper-div]"); div == nil { tests.Failed(t, "Should have returned a div with provided attr 'div[aria*=wrapper-div]'") } tests.Passed(t, "Should have returned a div with provided attr 'div[aria*=wrapper-div]'") if div := trees.Query.Query(tree, "div[aria=wrapper-div]"); div == nil { tests.Failed(t, "Should have returned a div with provided attr 'div[aria*=wrapper-div]'") } tests.Passed(t, "Should have returned a div with provided attr 'div[aria*=wrapper-div]'") if div := trees.Query.QueryAll(tree, "div[aria*=wrapper-div]"); len(div) != 1 { tests.Failed(t, "Should have returned a div with provided attr 'div[aria*=wrapper-div]': %d", len(div)) } tests.Passed(t, "Should have returned a div with provided attr 'div[aria*=wrapper-div]'") items := trees.Query.QueryAll(tree, "section.section") if len(items) != 3 { tests.Failed(t, "Should have returned 3 elements for selector 'section.section': %d", len(items)) } tests.Passed(t, "Should have returned 3 elements for selector 'section.section'") }
func TestResource(t *testing.T) { trees.SetMode(trees.Testing) defer trees.SetMode(trees.Normal) _ = design.Resource(func() { design.DoOrder(design.Any) design.UseRoute("/home") design.DoCSS("../some.css", false) design.DoScript("../some.js", "text/javascript", false) design.DoMarkup(elems.Header1(elems.Text("Speed Dashboard")), "", false) design.DoMarkup(func() *trees.Markup { return elems.Div( elems.Section( elems.Label(elems.Text("Total Current Speed")), ), ) }, "", false) }) master := design.New() master.Init() root := master.MustCurrentResource() if root.Order != design.Any { tests.Failed(t, "Should have 'Any' RenderingOrder") } tests.Passed(t, "Should have 'Any' RenderingOrder") if len(root.Links) != 2 { tests.Failed(t, "Should have added two links into the resource") } tests.Passed(t, "Should have added two links into the resource") if len(root.Renderables) != 2 { tests.Failed(t, "Should have added two markups into the resource") } tests.Passed(t, "Should have added two markups into the resource") if _, _, state := root.Resolver.Test("/home"); !state { tests.Failed(t, "Should have matched given route: %q", "/home") } tests.Passed(t, "Should have matched given route: %q", "/home") if name := root.Links[0].Content.Name(); name != "link" { tests.Failed(t, "Should have added a style tag static view as the second item: %q", name) } tests.Passed(t, "Should have added a style tag static view as the first item") if name := root.Renderables[0].View.Render().Name(); name != "h1" { tests.Failed(t, "Should have added a h1(header) tag static view as the second item: %q", name) } tests.Passed(t, "Should have added a style tag static view as the second item") if name := root.Renderables[1].View.Render().Name(); name != "div" { tests.Failed(t, "Should have added a div tag static view as the second item: %q", name) } tests.Passed(t, "Should have added a style tag static view as the second item") }