//forName expects pageName to be in all-lower case (it's an optimization)
targetPageName = targetPageName.toLowerCase();
final PageBook pageBook = createMock(PageBook.class);
final PageBook.Page page = createMock(PageBook.Page.class);
final Respond respond = RespondersForTesting.newRespond();
final MvelEvaluator evaluator = new MvelEvaluator();
final WidgetChain widget = new ProceedingWidgetChain();
final WidgetChain targetWidgetChain = new ProceedingWidgetChain();
//noinspection unchecked
targetWidgetChain.addWidget(new XmlWidget(new ProceedingWidgetChain()
.addWidget(new IncludeWidget(new TerminalWidgetChain(), "'me'", evaluator)),
"p", new MvelEvaluatorCompiler(Object.class), new LinkedHashMap<String, String>() {{
put("class", "pretty");
put("id", "a-p-tag");
}}));
widget.addWidget(new ShowIfWidget(targetWidgetChain, "true", evaluator));
Renderable bodyWrapper = new XmlWidget(widget, "body", createMock(EvaluatorCompiler.class), Collections.<String, String>emptyMap());
expect(pageBook.forName(targetPageName))
.andReturn(page);
//mypage does?
final MyEmbeddedPage myEmbeddedPage = new MyEmbeddedPage();
expect(page.instantiate())
.andReturn(myEmbeddedPage);
expect(page.doMethod(isA(String.class), anyObject(), isA(String.class),
isA(Request.class)))
.andReturn(null);
expect(page.widget())
.andReturn(bodyWrapper);
replay(pageBook, page);
//create embedding arguments
final String includeExpr = "me";
Map<String, ArgumentWidget> inners = new HashMap<String, ArgumentWidget>();
inners.put(includeExpr, new ArgumentWidget(new ProceedingWidgetChain().addWidget(new TextWidget(HELLO_FROM_INCLUDE, new MvelEvaluatorCompiler(Object.class))),
includeExpr, evaluator));
final EmbedWidget embedWidget = new EmbedWidget(inners, expression, evaluator, pageBook, targetPageName);
embedWidget.init(new EmbeddedRespondFactory(), HtmlTemplateCompilerTest.mockRequestProviderForContext());
embedWidget
.render(new MyParentPage(passOn), respond);
//assert bindings
assert myEmbeddedPage.isSet() : "variable not passed on to embedded page";
assert passOn.equals(myEmbeddedPage.getMessage()) : "variable not set on embedded page";
//the render was ok
final String resp = respond.toString();
assert String.format("<p class=\"pretty\" id=\"a-p-tag\">%s</p>", HELLO_FROM_INCLUDE).equals(resp)
: "widget not embedded correctly : " + resp;
verify(pageBook, page);
}