void TestMustache::testValues() { QVariantHash map; map["name"] = "John Smith"; map["age"] = 42; map["sex"] = "Male"; map["company"] = "Smith & Co"; map["signature"] = "John Smith of <b>Smith & Co</b>"; QString _template = "Name: {{name}}, Age: {{age}}, Sex: {{sex}}\n" "Company: {{company}}\n" " {{{signature}}}" "{{missing-key}}"; QString expectedOutput = "Name: John Smith, Age: 42, Sex: Male\n" "Company: Smith & Co\n" " John Smith of <b>Smith & Co</b>"; Mustache::Renderer renderer; Mustache::QtVariantContext context(map); QString output = renderer.render(_template, &context); QCOMPARE(output, expectedOutput); }
void TestMustache::testSetDelimiters() { // test changing the markers within a template QVariantHash map; map["name"] = "John Smith"; map["phone"] = "01234 567890"; QString _template = "{{=<% %>=}}" "<%name%>{{ }}<%phone%>" "<%={{ }}=%>" " {{name}}<% %>{{phone}}"; QString expectedOutput = "John Smith{{ }}01234 567890 John Smith<% %>01234 567890"; Mustache::Renderer renderer; Mustache::QtVariantContext context(map); QString output = renderer.render(_template, &context); QCOMPARE(output, expectedOutput); // test changing the default markers renderer.setTagMarkers("%", "%"); output = renderer.render("%name%'s phone number is %phone%", &context); QCOMPARE(output, QString("John Smith's phone number is 01234 567890")); }
void TestMustache::testPartials() { QHash<QString, QString> partials; partials["file-info"] = "{{name}} {{size}} {{type}}\n"; QString _template = "{{#files}}{{>file-info}}{{/files}}"; QVariantHash map; QVariantList fileList; QVariantHash file1; file1["name"] = "mustache.pdf"; file1["size"] = "200KB"; file1["type"] = "PDF Document"; QVariantHash file2; file2["name"] = "cv.doc"; file2["size"] = "300KB"; file2["type"] = "Microsoft Word Document"; fileList << file1 << file2; map["files"] = fileList; Mustache::Renderer renderer; Mustache::PartialMap partialMap(partials); Mustache::QtVariantContext context(map, &partialMap); QString output = renderer.render(_template, &context); QCOMPARE(output, QString("mustache.pdf 200KB PDF Document\n" "cv.doc 300KB Microsoft Word Document\n")); }
void TestMustache::testEscaping() { QVariantHash map; map["escape"] = "<b>foo</b>"; map["unescape"] = "One & Two "quoted""; map["raw"] = "<b>foo</b>"; QString _template = "{{escape}} {{&unescape}} {{{raw}}}"; Mustache::Renderer renderer; Mustache::QtVariantContext context(map); QString output = renderer.render(_template, &context); QCOMPARE(output, QString("<b>foo</b> One & Two \"quoted\" <b>foo</b>")); }
void TestMustache::testPartialFile() { QString path = QCoreApplication::applicationDirPath(); QVariantHash map = contactInfo("Jim Smith", "*****@*****.**"); QString _template = "{{>partial}}"; Mustache::Renderer renderer; Mustache::PartialFileLoader partialLoader(path); Mustache::QtVariantContext context(map, &partialLoader); QString output = renderer.render(_template, &context); QCOMPARE(output, QString("Jim Smith -- [email protected]\n")); }
void Generation::testTemplate() { QVariantHash contact; contact["name"] = "John Smith"; contact["email"] = "*****@*****.**"; QString contactTemplate = "<b>{{name}}</b> <a href=\"mailto:{{email}}\">{{email}}</a>"; Mustache::Renderer renderer; Mustache::QtVariantContext context(contact); QVERIFY(renderer.render(contactTemplate, &context) == "<b>John Smith</b> <a href=\"mailto:[email protected]\">[email protected]</a>"); }
void TestMustache::testEval() { QVariantHash map; QVariantList list; list << contactInfo("Rob Knight", "*****@*****.**"); list << contactInfo("Jim Smith", "*****@*****.**"); map["list"] = list; QString _template = "{{#list}}{{#counter}}#{{count}} {{name}} {{email}}{{/counter}}\n{{/list}}"; Mustache::Renderer renderer; CounterContext context(map); QString output = renderer.render(_template, &context); QCOMPARE(output, QString("#1 Rob Knight [email protected]\n" "#2 Jim Smith [email protected]\n")); }
void Generation::testFileTemplate() { QFile file(":/tpl/generation"); QVERIFY(file.open(QFile::ReadOnly|QFile::Text)); QTextStream stream(&file); QVariantHash contact; QString contactTemplate = stream.readAll(); contactTemplate.remove(contactTemplate.count()-1, contactTemplate.count()); // Remove last \n contact["name"] = "John Smith"; contact["email"] = "*****@*****.**"; Mustache::Renderer renderer; Mustache::QtVariantContext context(contact); QVERIFY(renderer.render(contactTemplate, &context) == "<b>John Smith</b> <a href=\"mailto:[email protected]\">[email protected]</a>"); }
void GlobalChildMainWindow::update_html(){ m_global_vals["mbps_color"]= "<FONT COLOR=green> Total Tx :"+double_to_human_str(m_stats.m_global[CGlobalInfo::grBPS][CGlobalInfo::gtMEASURE],"bps",KBYE_1000)+"</FONT>"; m_global_vals["mpps_color"]="<FONT COLOR=blue> "+double_to_human_str(m_stats.m_global[CGlobalInfo::grPPS][CGlobalInfo::gtMEASURE],"pps",KBYE_1000)+"</FONT>"; m_global_vals["ebps"]= double_to_human_str(m_stats.m_global[CGlobalInfo::grBPS][CGlobalInfo::gtEXP],"bps",KBYE_1000); m_global_vals["mbps"]= double_to_human_str(m_stats.m_global[CGlobalInfo::grBPS][CGlobalInfo::gtMEASURE],"bps",KBYE_1000); double r=100.0*m_stats.m_global[CGlobalInfo::grBPS][CGlobalInfo::gtMEASURE]/m_stats.m_global[CGlobalInfo::grBPS][CGlobalInfo::gtEXP]; QString sr; sr.sprintf("%3.0f ",r); m_global_vals["pbps"]= sr; m_global_vals["ecps"]= double_to_human_str(m_stats.m_global[CGlobalInfo::grCPS][CGlobalInfo::gtEXP],"cps",KBYE_1000); m_global_vals["mcps"]= double_to_human_str(m_stats.m_global[CGlobalInfo::grCPS][CGlobalInfo::gtMEASURE],"cps",KBYE_1000); r=100.0*m_stats.m_global[CGlobalInfo::grCPS][CGlobalInfo::gtMEASURE]/m_stats.m_global[CGlobalInfo::grCPS][CGlobalInfo::gtEXP]; sr.sprintf("%3.0f ",r); m_global_vals["pcps"]= sr; m_global_vals["epps"]= double_to_human_str(m_stats.m_global[CGlobalInfo::grPPS][CGlobalInfo::gtEXP],"pps",KBYE_1000); m_global_vals["mpps"]= double_to_human_str(m_stats.m_global[CGlobalInfo::grPPS][CGlobalInfo::gtMEASURE],"pps",KBYE_1000); r=100.0*m_stats.m_global[CGlobalInfo::grPPS][CGlobalInfo::gtMEASURE]/m_stats.m_global[CGlobalInfo::grPPS][CGlobalInfo::gtEXP]; sr.sprintf("%3.0f ",r); m_global_vals["ppps"]= sr; m_global_vals["active_flows"]=m_stats.m_active_flows; m_global_vals["active_flows_h"]=double_to_human_str(m_stats.m_active_flows,"",KBYE_1000); m_global_vals["open_flows"]=m_stats.m_open_flows; if (m_stats.m_rx_drop_bps>1.0) { m_global_vals["total_drop"]="<FONT COLOR=red>"+double_to_human_str(m_stats.m_rx_drop_bps,"bps",KBYE_1000)+"</FONT>"; }else{ m_global_vals["total_drop"]=double_to_human_str(m_stats.m_rx_drop_bps,"bps",KBYE_1000); //m_global_vals["total_drop"]=double_to_human_str(m_stats.m_rx_drop_bps,"bps",KBYE_1000); } Mustache::Renderer renderer; Mustache::QtVariantContext context(m_global_vals); //qDebug()<<m_global_html_tp; QString s=renderer.render(m_global_html_tp, &context); ui->webView->setHtml(s); }
void TestMustache::testContextLookup() { QVariantHash fileMap; fileMap["dir"] = "/home/robert"; fileMap["name"] = "robert"; QVariantList files; QVariantHash file; file["name"] = "test.pdf"; files << file; fileMap["files"] = files; QString _template = "{{#files}}{{dir}}/{{name}}{{/files}}"; Mustache::Renderer renderer; Mustache::QtVariantContext context(fileMap); QString output = renderer.render(_template, &context); QCOMPARE(output, QString("/home/robert/test.pdf")); }
void TestMustache::testSections() { QVariantHash map = contactInfo("John Smith", "*****@*****.**"); QVariantList contacts; contacts << contactInfo("James Dee", "*****@*****.**"); contacts << contactInfo("Jim Jones", "*****@*****.**"); map["contacts"] = contacts; QString _template = "Name: {{name}}, Email: {{email}}\n" "{{#contacts}} {{name}} - {{email}}\n{{/contacts}}" "{{^contacts}} No contacts{{/contacts}}"; QString expectedOutput = "Name: John Smith, Email: [email protected]\n" " James Dee - [email protected]\n" " Jim Jones - [email protected]\n"; Mustache::Renderer renderer; Mustache::QtVariantContext context(map); QString output = renderer.render(_template, &context); QCOMPARE(output, expectedOutput); // test inverted sections map.remove("contacts"); context = Mustache::QtVariantContext(map); output = renderer.render(_template, &context); expectedOutput = "Name: John Smith, Email: [email protected]\n" " No contacts"; QCOMPARE(output, expectedOutput); // test with an empty list instead of an empty key map["contacts"] = QVariantHash(); context = Mustache::QtVariantContext(map); output = renderer.render(_template, &context); QCOMPARE(output, expectedOutput); }
QString Mustache::renderTemplate(const QString& templateString, const QVariantHash& args) { Mustache::QtVariantContext context(args); Mustache::Renderer renderer; return renderer.render(templateString, &context); }
void TestMustache::testErrors() { QVariantHash map; map["name"] = "Jim Jones"; QHash<QString, QString> partials; partials["buggy-partial"] = "--{{/one}}--"; QString _template = "{{name}}"; Mustache::Renderer renderer; Mustache::PartialMap partialMap(partials); Mustache::QtVariantContext context(map, &partialMap); QString output = renderer.render(_template, &context); QCOMPARE(output, QString("Jim Jones")); QCOMPARE(renderer.error(), QString()); QCOMPARE(renderer.errorPos(), -1); _template = "{{#one}} {{/two}}"; output = renderer.render(_template, &context); QCOMPARE(renderer.error(), QString("Tag start/end key mismatch")); QCOMPARE(renderer.errorPos(), 9); QCOMPARE(renderer.errorPartial(), QString()); _template = "Hello {{>buggy-partial}}"; output = renderer.render(_template, &context); QCOMPARE(renderer.error(), QString("Unexpected end tag")); QCOMPARE(renderer.errorPos(), 2); QCOMPARE(renderer.errorPartial(), QString("buggy-partial")); }