Skip to content
Browse files

began implementing delimiter switching

  • Loading branch information...
1 parent 64f0feb commit 4b10e1832baa00452fe9858a676cc2da0bf67161 @edwindj committed
View
1 DESCRIPTION
@@ -18,5 +18,6 @@ Collate:
'inverted.R'
'pkg.R'
'iteratelist.R'
+ 'delim.R'
Depends:
View
94 R/delim.R
@@ -0,0 +1,94 @@
+ALTDELIM <- "~~~~~~~~~~~~~ ~~~~~~~~~~~~~"
+
+#' change a delimiter tag into two escaped characters
+#'
+#' @param tag character with delimiter tag seperated with a space
+#' @param tag character with delimiter tag seperated with a space
+#' @keywords internal
+tag2delim <- function(tag="{{ }}", escape=TRUE){
+ delim <- strsplit(tag," ")[[1]]
+ if (escape)
+ gsub("([[{}*?+])", "\\\\\\1", delim)
+ else
+ delim
+}
+
+#' Split a character in three parts
+#'
+#' It differs from strsplit in that it only splits on the first occurrence
+#' and returns all parts of the string given
+#' @param x character text to be split
+#' @param pattern pattern used for splitting
+#' @keywords internal
+rxsplit <- function(x, pattern){
+ matched <- regexpr(pattern, x)
+ if (matched == -1){
+ return(x)
+ }
+ ml <- attr(matched, "match.length")
+ c( substring(x,1,matched-1)
+ , substring(x,matched, matched+ml-1)
+ , substring(x, matched + ml)
+ )
+}
+
+#' enclose a key with delimiters
+#'
+#' @param x character with delimiter seperated with a space
+#' @param delim character vector with escaped delimiters
+#' @keywords internal
+delimit <- function(x, delim=tag2delim()){
+ paste(delim[1], x, delim[2], sep="")
+}
+
+replace_delim_tags <- function(template){
+ text <- list()
+ defdelim <- tag2delim()
+ altdelim <- tag2delim(ALTDELIM, escape=FALSE)
+
+ defkeytag <- delimit("(.+?)", defdelim)
+ altkeytag <- delimit("\\1", altdelim)
+
+ tag <- "{{ }}"
+
+ while (!is.na(template)){
+ delim <- tag2delim(tag)
+ delimtag <- delimit(DELIM, delim)
+ keytag <- delimit("(.+?)", delim)
+
+ rx <- rxsplit(template, delimtag)
+ txt <- rx[1]
+
+ if (tag != "{{ }}"){
+ txt <- gsub(defkeytag, altkeytag, txt)
+ }
+ txt <- gsub(keytag, "{{\\1}}", txt)
+
+ text[length(text)+1] <- txt
+
+ tag <- sub(delimtag, "\\1", rx[2])
+ template <- rx[3]
+ }
+ #print(text)
+ paste(text, collapse="")
+}
+
+literal_tags <- function(txt){
+ altdelim <- tag2delim(ALTDELIM)
+ defdelim <- tag2delim("{{ }}", escape=FALSE)
+
+ altkeytag <- delimit("(.+?)", altdelim)
+ defkeytag <- delimit("\\1", defdelim)
+ gsub(altkeytag, defkeytag, txt)
+}
+
+### quick testing
+#delim2rx("<% %>")
+
+# template <- "test {{=<% %>}} <%key%> {{key1}} <%=[[ ]]%> bla, [[key2]] [[={{ }}]] {{key3}} 1, 2, 3"
+#rxsplit(template, delimtag)
+# r <- replace_delim_tags(template)
+# r
+# literal_tags(r)
+
+#replace_delim_tags(template)
View
42 R/parseTemplate.R
@@ -6,27 +6,33 @@ INVERTEDSECTION <- "\\^([ A-z0-9.]+)"
ENDSECTION <- "/([ A-z0-9.]+)"
PARTIAL <- ">\\s*(.+?)\\s*"
COMMENT <- "!.+?"
+DELIM <- "=\\s*(.+?)\\s*="
#keytypes
keytypes <- c("", "{}", "&", "#", "^", "/", ">")
+
+# current parsing code is not a clean parsing state machine!
+# This is partly due to that this would be clumsy in R,
+# It's on my list to do the parsing in C (would be significantly faster)
parseTemplate <- function(template, partials=list(), debug=FALSE){
#TODO add delimiter switching
- delim <- strsplit("{{ }}"," ")[[1]]
-
- DELIM <- gsub("([{<>}*?+])", "\\\\\\1", delim)
+
+ delim <- tag2delim()
template <- paste(template, collapse="\n")
- template <- removeComments(template, DELIM)
+ template <- replace_delim_tags(template)
+ template <- removeComments(template, delim)
- template <- inlinePartial(template, DELIM)
- template <- inlineStandAlone(template, DELIM, ENDSECTION)
- template <- inlineStandAlone(template, DELIM, SECTION)
- template <- inlineStandAlone(template, DELIM, INVERTEDSECTION)
+ template <- inlinePartial(template, delim)
+ template <- inlineStandAlone(template, delim, ENDSECTION)
+ template <- inlineStandAlone(template, delim, SECTION)
+ template <- inlineStandAlone(template, delim, INVERTEDSECTION)
+
+ KEY <- delimit("(.+?)", delim)
- KEY <- paste(DELIM[1],"(.+?)", DELIM[2], sep="")
-
text <- strsplit(template, KEY)[[1]]
+ text <- literal_tags(text)
key <- getKeyInfo(template, KEY)
n <- nrow(key)
@@ -127,11 +133,11 @@ getKeyInfo <- function(template, KEY){
key
}
-inlineStandAlone <- function(text, DELIM, keyregexp){
+inlineStandAlone <- function(text, delim, keyregexp){
# remove groups from regexp
keyregexp <- gsub("\\(|\\)","",keyregexp)
- dKEY <- paste(DELIM[1],keyregexp, DELIM[2], sep="")
+ dKEY <- paste(delim[1],keyregexp, delim[2], sep="")
re <- paste("(^|\n)([ \t]*)(",dKEY,")\\s*?(\n|$)", sep="")
@@ -139,18 +145,18 @@ inlineStandAlone <- function(text, DELIM, keyregexp){
gsub(re, "\\1\\3", text)
}
-removeComments <- function(text, DELIM){
- text <- inlineStandAlone(text, DELIM, COMMENT)
+removeComments <- function(text, delim){
+ text <- inlineStandAlone(text, delim, COMMENT)
#remove inline comments
- dCOMMENT <- paste(DELIM[1],COMMENT, DELIM[2], sep="")
+ dCOMMENT <- paste(delim[1],COMMENT, delim[2], sep="")
gsub(dCOMMENT, "", text)
}
-inlinePartial <- function(text, DELIM){
- dKEY <- paste(DELIM[1],PARTIAL, DELIM[2], sep="")
+inlinePartial <- function(text, delim){
+ dKEY <- paste(delim[1],PARTIAL, delim[2], sep="")
text <- gsub(dKEY, "{{>\\1}}", text)
re <- paste("(^|\n)([ \t]*)",dKEY,"\\s*?(\n|$)", sep="")
- rep <- paste("\\1\\2", DELIM[1],">\\2\\3",DELIM[2], sep="")
+ rep <- paste("\\1\\2", delim[1],">\\2\\3",delim[2], sep="")
gsub(re, rep, text)
}
View
2 inst/specs/convert.R
@@ -66,7 +66,7 @@ spec <- c( "interpolation.json"
, "inverted.json"
, "sections.json"
, "partials.json"
- #, "delimiters.json"
+ , "delimiters.json"
#, "lambdas.json"
)
convertToTest(spec)
View
11 inst/tests/testComments.R
@@ -16,6 +16,7 @@ test_that( "Inline", {
template <- "12345{{! Comment Block! }}67890"
data <- list()
+
str <- whisker.render(template, data=data)
expect_equal(str, "1234567890", label=deparse(str), info="Comment blocks should be removed from the template.")
@@ -26,6 +27,7 @@ test_that( "Multiline", {
template <- "12345{{!\n This is a\n multi-line comment...\n}}67890\n"
data <- list()
+
str <- whisker.render(template, data=data)
expect_equal(str, "1234567890\n", label=deparse(str), info="Multiline comments should be permitted.")
@@ -36,6 +38,7 @@ test_that( "Standalone", {
template <- "Begin.\n{{! Comment Block! }}\nEnd.\n"
data <- list()
+
str <- whisker.render(template, data=data)
expect_equal(str, "Begin.\nEnd.\n", label=deparse(str), info="All standalone comment lines should be removed.")
@@ -46,6 +49,7 @@ test_that( "Indented Standalone", {
template <- "Begin.\n {{! Indented Comment Block! }}\nEnd.\n"
data <- list()
+
str <- whisker.render(template, data=data)
expect_equal(str, "Begin.\nEnd.\n", label=deparse(str), info="All standalone comment lines should be removed.")
@@ -56,6 +60,7 @@ test_that( "Standalone Line Endings", {
template <- "|\r\n{{! Standalone Comment }}\r\n|"
data <- list()
+
str <- whisker.render(template, data=data)
expect_equal(str, "|\r\n|", label=deparse(str), info="\"\\r\\n\" should be considered a newline for standalone tags.")
@@ -66,6 +71,7 @@ test_that( "Standalone Without Previous Line", {
template <- " {{! I'm Still Standalone }}\n!"
data <- list()
+
str <- whisker.render(template, data=data)
expect_equal(str, "!", label=deparse(str), info="Standalone tags should not require a newline to precede them.")
@@ -76,6 +82,7 @@ test_that( "Standalone Without Newline", {
template <- "!\n {{! I'm Still Standalone }}"
data <- list()
+
str <- whisker.render(template, data=data)
expect_equal(str, "!\n", label=deparse(str), info="Standalone tags should not require a newline to follow them.")
@@ -86,6 +93,7 @@ test_that( "Multiline Standalone", {
template <- "Begin.\n{{!\nSomething's going on here...\n}}\nEnd.\n"
data <- list()
+
str <- whisker.render(template, data=data)
expect_equal(str, "Begin.\nEnd.\n", label=deparse(str), info="All standalone comment lines should be removed.")
@@ -96,6 +104,7 @@ test_that( "Indented Multiline Standalone", {
template <- "Begin.\n {{!\n Something's going on here...\n }}\nEnd.\n"
data <- list()
+
str <- whisker.render(template, data=data)
expect_equal(str, "Begin.\nEnd.\n", label=deparse(str), info="All standalone comment lines should be removed.")
@@ -106,6 +115,7 @@ test_that( "Indented Inline", {
template <- " 12 {{! 34 }}\n"
data <- list()
+
str <- whisker.render(template, data=data)
expect_equal(str, " 12 \n", label=deparse(str), info="Inline comments should not strip whitespace")
@@ -116,6 +126,7 @@ test_that( "Surrounding Whitespace", {
template <- "12345 {{! Comment Block! }} 67890"
data <- list()
+
str <- whisker.render(template, data=data)
expect_equal(str, "12345 67890", label=deparse(str), info="Comment removal should preserve surrounding whitespace.")
View
168 inst/tests/testdelimiters.R
@@ -0,0 +1,168 @@
+# Automatically generated from specification file: 'delimiters.json'
+#
+# Set Delimiter tags are used to change the tag delimiters for all content
+# following the tag in the current compilation unit.
+#
+# The tag's content MUST be any two non-whitespace sequences (separated by
+# whitespace) EXCEPT an equals sign ('=') followed by the current closing
+# delimiter.
+#
+# Set Delimiter tags SHOULD be treated as standalone when appropriate.
+#
+library(testthat)
+context('Spec v1.1, delimiters')
+
+test_that( "Pair Behavior", {
+ #"The equals sign (used on both sides) should permit delimiter changes."
+ template <- "{{=<% %>=}}(<%text%>)"
+ data <- list(text = "Hey!")
+
+
+ str <- whisker.render(template, data=data)
+
+ expect_equal(str, "(Hey!)", label=deparse(str), info="The equals sign (used on both sides) should permit delimiter changes.")
+})
+
+test_that( "Special Characters", {
+ #"Characters with special meaning regexen should be valid delimiters."
+ template <- "({{=[ ]=}}[text])"
+ data <- list(text = "It worked!")
+
+
+ str <- whisker.render(template, data=data)
+
+ expect_equal(str, "(It worked!)", label=deparse(str), info="Characters with special meaning regexen should be valid delimiters.")
+})
+
+test_that( "Sections", {
+ #"Delimiters set outside sections should persist."
+ template <- "[\n{{#section}}\n {{data}}\n |data|\n{{/section}}\n\n{{= | | =}}\n|#section|\n {{data}}\n |data|\n|/section|\n]\n"
+ data <- list(section = TRUE, data = "I got interpolated.")
+
+
+ str <- whisker.render(template, data=data)
+
+ expect_equal(str, "[\n I got interpolated.\n |data|\n\n {{data}}\n I got interpolated.\n]\n", label=deparse(str), info="Delimiters set outside sections should persist.")
+})
+
+test_that( "Inverted Sections", {
+ #"Delimiters set outside inverted sections should persist."
+ template <- "[\n{{^section}}\n {{data}}\n |data|\n{{/section}}\n\n{{= | | =}}\n|^section|\n {{data}}\n |data|\n|/section|\n]\n"
+ data <- list(section = FALSE, data = "I got interpolated.")
+
+
+ str <- whisker.render(template, data=data)
+
+ expect_equal(str, "[\n I got interpolated.\n |data|\n\n {{data}}\n I got interpolated.\n]\n", label=deparse(str), info="Delimiters set outside inverted sections should persist.")
+})
+
+test_that( "Partial Inheritence", {
+ #"Delimiters set in a parent template should not affect a partial."
+ template <- "[ {{>include}} ]\n{{= | | =}}\n[ |>include| ]\n"
+ data <- list(value = "yes")
+
+ partials <- list(include = ".{{value}}.")
+ str <- whisker.render(template, partials=partials, data=data)
+
+ expect_equal(str, "[ .yes. ]\n[ .yes. ]\n", label=deparse(str), info="Delimiters set in a parent template should not affect a partial.")
+})
+
+test_that( "Post-Partial Behavior", {
+ #"Delimiters set in a partial should not affect the parent template."
+ template <- "[ {{>include}} ]\n[ .{{value}}. .|value|. ]\n"
+ data <- list(value = "yes")
+
+ partials <- list(include = ".{{value}}. {{= | | =}} .|value|.")
+ str <- whisker.render(template, partials=partials, data=data)
+
+ expect_equal(str, "[ .yes. .yes. ]\n[ .yes. .|value|. ]\n", label=deparse(str), info="Delimiters set in a partial should not affect the parent template.")
+})
+
+test_that( "Surrounding Whitespace", {
+ #"Surrounding whitespace should be left untouched."
+ template <- "| {{=@ @=}} |"
+ data <- list()
+
+
+ str <- whisker.render(template, data=data)
+
+ expect_equal(str, "| |", label=deparse(str), info="Surrounding whitespace should be left untouched.")
+})
+
+test_that( "Outlying Whitespace (Inline)", {
+ #"Whitespace should be left untouched."
+ template <- " | {{=@ @=}}\n"
+ data <- list()
+
+
+ str <- whisker.render(template, data=data)
+
+ expect_equal(str, " | \n", label=deparse(str), info="Whitespace should be left untouched.")
+})
+
+test_that( "Standalone Tag", {
+ #"Standalone lines should be removed from the template."
+ template <- "Begin.\n{{=@ @=}}\nEnd.\n"
+ data <- list()
+
+
+ str <- whisker.render(template, data=data)
+
+ expect_equal(str, "Begin.\nEnd.\n", label=deparse(str), info="Standalone lines should be removed from the template.")
+})
+
+test_that( "Indented Standalone Tag", {
+ #"Indented standalone lines should be removed from the template."
+ template <- "Begin.\n {{=@ @=}}\nEnd.\n"
+ data <- list()
+
+
+ str <- whisker.render(template, data=data)
+
+ expect_equal(str, "Begin.\nEnd.\n", label=deparse(str), info="Indented standalone lines should be removed from the template.")
+})
+
+test_that( "Standalone Line Endings", {
+ #"\"\\r\\n\" should be considered a newline for standalone tags."
+ template <- "|\r\n{{= @ @ =}}\r\n|"
+ data <- list()
+
+
+ str <- whisker.render(template, data=data)
+
+ expect_equal(str, "|\r\n|", label=deparse(str), info="\"\\r\\n\" should be considered a newline for standalone tags.")
+})
+
+test_that( "Standalone Without Previous Line", {
+ #"Standalone tags should not require a newline to precede them."
+ template <- " {{=@ @=}}\n="
+ data <- list()
+
+
+ str <- whisker.render(template, data=data)
+
+ expect_equal(str, "=", label=deparse(str), info="Standalone tags should not require a newline to precede them.")
+})
+
+test_that( "Standalone Without Newline", {
+ #"Standalone tags should not require a newline to follow them."
+ template <- "=\n {{=@ @=}}"
+ data <- list()
+
+
+ str <- whisker.render(template, data=data)
+
+ expect_equal(str, "=\n", label=deparse(str), info="Standalone tags should not require a newline to follow them.")
+})
+
+test_that( "Pair with Padding", {
+ #"Superfluous in-tag whitespace should be ignored."
+ template <- "|{{= @ @ =}}|"
+ data <- list()
+
+
+ str <- whisker.render(template, data=data)
+
+ expect_equal(str, "||", label=deparse(str), info="Superfluous in-tag whitespace should be ignored.")
+})
+
View
30 inst/tests/testinterpolation.R
@@ -34,6 +34,7 @@ test_that( "No Interpolation", {
template <- "Hello from {Mustache}!\n"
data <- list()
+
str <- whisker.render(template, data=data)
expect_equal(str, "Hello from {Mustache}!\n", label=deparse(str), info="Mustache-free templates should render as-is.")
@@ -44,6 +45,7 @@ test_that( "Basic Interpolation", {
template <- "Hello, {{subject}}!\n"
data <- list(subject = "world")
+
str <- whisker.render(template, data=data)
expect_equal(str, "Hello, world!\n", label=deparse(str), info="Unadorned tags should interpolate content into the template.")
@@ -54,6 +56,7 @@ test_that( "HTML Escaping", {
template <- "These characters should be HTML escaped: {{forbidden}}\n"
data <- list(forbidden = "& \" < >")
+
str <- whisker.render(template, data=data)
expect_equal(str, "These characters should be HTML escaped: &amp; &quot; &lt; &gt;\n", label=deparse(str), info="Basic interpolation should be HTML escaped.")
@@ -64,6 +67,7 @@ test_that( "Triple Mustache", {
template <- "These characters should not be HTML escaped: {{{forbidden}}}\n"
data <- list(forbidden = "& \" < >")
+
str <- whisker.render(template, data=data)
expect_equal(str, "These characters should not be HTML escaped: & \" < >\n", label=deparse(str), info="Triple mustaches should interpolate without HTML escaping.")
@@ -74,6 +78,7 @@ test_that( "Ampersand", {
template <- "These characters should not be HTML escaped: {{&forbidden}}\n"
data <- list(forbidden = "& \" < >")
+
str <- whisker.render(template, data=data)
expect_equal(str, "These characters should not be HTML escaped: & \" < >\n", label=deparse(str), info="Ampersand should interpolate without HTML escaping.")
@@ -84,6 +89,7 @@ test_that( "Basic Integer Interpolation", {
template <- "\"{{mph}} miles an hour!\""
data <- list(mph = 85)
+
str <- whisker.render(template, data=data)
expect_equal(str, "\"85 miles an hour!\"", label=deparse(str), info="Integers should interpolate seamlessly.")
@@ -94,6 +100,7 @@ test_that( "Triple Mustache Integer Interpolation", {
template <- "\"{{{mph}}} miles an hour!\""
data <- list(mph = 85)
+
str <- whisker.render(template, data=data)
expect_equal(str, "\"85 miles an hour!\"", label=deparse(str), info="Integers should interpolate seamlessly.")
@@ -104,6 +111,7 @@ test_that( "Ampersand Integer Interpolation", {
template <- "\"{{&mph}} miles an hour!\""
data <- list(mph = 85)
+
str <- whisker.render(template, data=data)
expect_equal(str, "\"85 miles an hour!\"", label=deparse(str), info="Integers should interpolate seamlessly.")
@@ -114,6 +122,7 @@ test_that( "Basic Decimal Interpolation", {
template <- "\"{{power}} jiggawatts!\""
data <- list(power = 1.21)
+
str <- whisker.render(template, data=data)
expect_equal(str, "\"1.21 jiggawatts!\"", label=deparse(str), info="Decimals should interpolate seamlessly with proper significance.")
@@ -124,6 +133,7 @@ test_that( "Triple Mustache Decimal Interpolation", {
template <- "\"{{{power}}} jiggawatts!\""
data <- list(power = 1.21)
+
str <- whisker.render(template, data=data)
expect_equal(str, "\"1.21 jiggawatts!\"", label=deparse(str), info="Decimals should interpolate seamlessly with proper significance.")
@@ -134,6 +144,7 @@ test_that( "Ampersand Decimal Interpolation", {
template <- "\"{{&power}} jiggawatts!\""
data <- list(power = 1.21)
+
str <- whisker.render(template, data=data)
expect_equal(str, "\"1.21 jiggawatts!\"", label=deparse(str), info="Decimals should interpolate seamlessly with proper significance.")
@@ -144,6 +155,7 @@ test_that( "Basic Context Miss Interpolation", {
template <- "I ({{cannot}}) be seen!"
data <- list()
+
str <- whisker.render(template, data=data)
expect_equal(str, "I () be seen!", label=deparse(str), info="Failed context lookups should default to empty strings.")
@@ -154,6 +166,7 @@ test_that( "Triple Mustache Context Miss Interpolation", {
template <- "I ({{{cannot}}}) be seen!"
data <- list()
+
str <- whisker.render(template, data=data)
expect_equal(str, "I () be seen!", label=deparse(str), info="Failed context lookups should default to empty strings.")
@@ -164,6 +177,7 @@ test_that( "Ampersand Context Miss Interpolation", {
template <- "I ({{&cannot}}) be seen!"
data <- list()
+
str <- whisker.render(template, data=data)
expect_equal(str, "I () be seen!", label=deparse(str), info="Failed context lookups should default to empty strings.")
@@ -174,6 +188,7 @@ test_that( "Dotted Names - Basic Interpolation", {
template <- "\"{{person.name}}\" == \"{{#person}}{{name}}{{/person}}\""
data <- list(person = list(name = "Joe"))
+
str <- whisker.render(template, data=data)
expect_equal(str, "\"Joe\" == \"Joe\"", label=deparse(str), info="Dotted names should be considered a form of shorthand for sections.")
@@ -184,6 +199,7 @@ test_that( "Dotted Names - Triple Mustache Interpolation", {
template <- "\"{{{person.name}}}\" == \"{{#person}}{{{name}}}{{/person}}\""
data <- list(person = list(name = "Joe"))
+
str <- whisker.render(template, data=data)
expect_equal(str, "\"Joe\" == \"Joe\"", label=deparse(str), info="Dotted names should be considered a form of shorthand for sections.")
@@ -194,6 +210,7 @@ test_that( "Dotted Names - Ampersand Interpolation", {
template <- "\"{{&person.name}}\" == \"{{#person}}{{&name}}{{/person}}\""
data <- list(person = list(name = "Joe"))
+
str <- whisker.render(template, data=data)
expect_equal(str, "\"Joe\" == \"Joe\"", label=deparse(str), info="Dotted names should be considered a form of shorthand for sections.")
@@ -204,6 +221,7 @@ test_that( "Dotted Names - Arbitrary Depth", {
template <- "\"{{a.b.c.d.e.name}}\" == \"Phil\""
data <- list(a = list(b = list(c = list(d = list(e = list(name = "Phil"))))))
+
str <- whisker.render(template, data=data)
expect_equal(str, "\"Phil\" == \"Phil\"", label=deparse(str), info="Dotted names should be functional to any level of nesting.")
@@ -214,6 +232,7 @@ test_that( "Dotted Names - Broken Chains", {
template <- "\"{{a.b.c}}\" == \"\""
data <- list(a = list())
+
str <- whisker.render(template, data=data)
expect_equal(str, "\"\" == \"\"", label=deparse(str), info="Any falsey value prior to the last part of the name should yield ''.")
@@ -224,6 +243,7 @@ test_that( "Dotted Names - Broken Chain Resolution", {
template <- "\"{{a.b.c.name}}\" == \"\""
data <- list(a = list(b = list()), c = list(name = "Jim"))
+
str <- whisker.render(template, data=data)
expect_equal(str, "\"\" == \"\"", label=deparse(str), info="Each part of a dotted name should resolve only against its parent.")
@@ -235,6 +255,7 @@ test_that( "Dotted Names - Initial Resolution", {
data <- list(a = list(b = list(c = list(d = list(e = list(name = "Phil"))))),
b = list(c = list(d = list(e = list(name = "Wrong")))))
+
str <- whisker.render(template, data=data)
expect_equal(str, "\"Phil\" == \"Phil\"", label=deparse(str), info="The first part of a dotted name should resolve as any other name.")
@@ -245,6 +266,7 @@ test_that( "Interpolation - Surrounding Whitespace", {
template <- "| {{string}} |"
data <- list(string = "---")
+
str <- whisker.render(template, data=data)
expect_equal(str, "| --- |", label=deparse(str), info="Interpolation should not alter surrounding whitespace.")
@@ -255,6 +277,7 @@ test_that( "Triple Mustache - Surrounding Whitespace", {
template <- "| {{{string}}} |"
data <- list(string = "---")
+
str <- whisker.render(template, data=data)
expect_equal(str, "| --- |", label=deparse(str), info="Interpolation should not alter surrounding whitespace.")
@@ -265,6 +288,7 @@ test_that( "Ampersand - Surrounding Whitespace", {
template <- "| {{&string}} |"
data <- list(string = "---")
+
str <- whisker.render(template, data=data)
expect_equal(str, "| --- |", label=deparse(str), info="Interpolation should not alter surrounding whitespace.")
@@ -275,6 +299,7 @@ test_that( "Interpolation - Standalone", {
template <- " {{string}}\n"
data <- list(string = "---")
+
str <- whisker.render(template, data=data)
expect_equal(str, " ---\n", label=deparse(str), info="Standalone interpolation should not alter surrounding whitespace.")
@@ -285,6 +310,7 @@ test_that( "Triple Mustache - Standalone", {
template <- " {{{string}}}\n"
data <- list(string = "---")
+
str <- whisker.render(template, data=data)
expect_equal(str, " ---\n", label=deparse(str), info="Standalone interpolation should not alter surrounding whitespace.")
@@ -295,6 +321,7 @@ test_that( "Ampersand - Standalone", {
template <- " {{&string}}\n"
data <- list(string = "---")
+
str <- whisker.render(template, data=data)
expect_equal(str, " ---\n", label=deparse(str), info="Standalone interpolation should not alter surrounding whitespace.")
@@ -305,6 +332,7 @@ test_that( "Interpolation With Padding", {
template <- "|{{ string }}|"
data <- list(string = "---")
+
str <- whisker.render(template, data=data)
expect_equal(str, "|---|", label=deparse(str), info="Superfluous in-tag whitespace should be ignored.")
@@ -315,6 +343,7 @@ test_that( "Triple Mustache With Padding", {
template <- "|{{{ string }}}|"
data <- list(string = "---")
+
str <- whisker.render(template, data=data)
expect_equal(str, "|---|", label=deparse(str), info="Superfluous in-tag whitespace should be ignored.")
@@ -325,6 +354,7 @@ test_that( "Ampersand With Padding", {
template <- "|{{& string }}|"
data <- list(string = "---")
+
str <- whisker.render(template, data=data)
expect_equal(str, "|---|", label=deparse(str), info="Superfluous in-tag whitespace should be ignored.")
View
21 inst/tests/testinverted.R
@@ -43,6 +43,7 @@ test_that( "Falsey", {
template <- "\"{{^boolean}}This should be rendered.{{/boolean}}\""
data <- list(boolean = FALSE)
+
str <- whisker.render(template, data=data)
expect_equal(str, "\"This should be rendered.\"", label=deparse(str), info="Falsey sections should have their contents rendered.")
@@ -53,6 +54,7 @@ test_that( "Truthy", {
template <- "\"{{^boolean}}This should not be rendered.{{/boolean}}\""
data <- list(boolean = TRUE)
+
str <- whisker.render(template, data=data)
expect_equal(str, "\"\"", label=deparse(str), info="Truthy sections should have their contents omitted.")
@@ -63,6 +65,7 @@ test_that( "Context", {
template <- "\"{{^context}}Hi {{name}}.{{/context}}\""
data <- list(context = list(name = "Joe"))
+
str <- whisker.render(template, data=data)
expect_equal(str, "\"\"", label=deparse(str), info="Objects and hashes should behave like truthy values.")
@@ -73,6 +76,7 @@ test_that( "List", {
template <- "\"{{^list}}{{n}}{{/list}}\""
data <- list(list = list(list(n = 1), list(n = 2), list(n = 3)))
+
str <- whisker.render(template, data=data)
expect_equal(str, "\"\"", label=deparse(str), info="Lists should behave like truthy values.")
@@ -83,6 +87,7 @@ test_that( "Empty List", {
template <- "\"{{^list}}Yay lists!{{/list}}\""
data <- list(list = list())
+
str <- whisker.render(template, data=data)
expect_equal(str, "\"Yay lists!\"", label=deparse(str), info="Empty lists should behave like falsey values.")
@@ -93,6 +98,7 @@ test_that( "Doubled", {
template <- "{{^bool}}\n* first\n{{/bool}}\n* {{two}}\n{{^bool}}\n* third\n{{/bool}}\n"
data <- list(two = "second", bool = FALSE)
+
str <- whisker.render(template, data=data)
expect_equal(str, "* first\n* second\n* third\n", label=deparse(str), info="Multiple inverted sections per template should be permitted.")
@@ -103,6 +109,7 @@ test_that( "Nested (Falsey)", {
template <- "| A {{^bool}}B {{^bool}}C{{/bool}} D{{/bool}} E |"
data <- list(bool = FALSE)
+
str <- whisker.render(template, data=data)
expect_equal(str, "| A B C D E |", label=deparse(str), info="Nested falsey sections should have their contents rendered.")
@@ -113,6 +120,7 @@ test_that( "Nested (Truthy)", {
template <- "| A {{^bool}}B {{^bool}}C{{/bool}} D{{/bool}} E |"
data <- list(bool = TRUE)
+
str <- whisker.render(template, data=data)
expect_equal(str, "| A E |", label=deparse(str), info="Nested truthy sections should be omitted.")
@@ -123,6 +131,7 @@ test_that( "Context Misses", {
template <- "[{{^missing}}Cannot find key 'missing'!{{/missing}}]"
data <- list()
+
str <- whisker.render(template, data=data)
expect_equal(str, "[Cannot find key 'missing'!]", label=deparse(str), info="Failed context lookups should be considered falsey.")
@@ -133,6 +142,7 @@ test_that( "Dotted Names - Truthy", {
template <- "\"{{^a.b.c}}Not Here{{/a.b.c}}\" == \"\""
data <- list(a = list(b = list(c = TRUE)))
+
str <- whisker.render(template, data=data)
expect_equal(str, "\"\" == \"\"", label=deparse(str), info="Dotted names should be valid for Inverted Section tags.")
@@ -143,6 +153,7 @@ test_that( "Dotted Names - Falsey", {
template <- "\"{{^a.b.c}}Not Here{{/a.b.c}}\" == \"Not Here\""
data <- list(a = list(b = list(c = FALSE)))
+
str <- whisker.render(template, data=data)
expect_equal(str, "\"Not Here\" == \"Not Here\"", label=deparse(str), info="Dotted names should be valid for Inverted Section tags.")
@@ -153,6 +164,7 @@ test_that( "Dotted Names - Broken Chains", {
template <- "\"{{^a.b.c}}Not Here{{/a.b.c}}\" == \"Not Here\""
data <- list(a = list())
+
str <- whisker.render(template, data=data)
expect_equal(str, "\"Not Here\" == \"Not Here\"", label=deparse(str), info="Dotted names that cannot be resolved should be considered falsey.")
@@ -163,6 +175,7 @@ test_that( "Surrounding Whitespace", {
template <- " | {{^boolean}}\t|\t{{/boolean}} | \n"
data <- list(boolean = FALSE)
+
str <- whisker.render(template, data=data)
expect_equal(str, " | \t|\t | \n", label=deparse(str), info="Inverted sections should not alter surrounding whitespace.")
@@ -173,6 +186,7 @@ test_that( "Internal Whitespace", {
template <- " | {{^boolean}} {{! Important Whitespace }}\n {{/boolean}} | \n"
data <- list(boolean = FALSE)
+
str <- whisker.render(template, data=data)
expect_equal(str, " | \n | \n", label=deparse(str), info="Inverted should not alter internal whitespace.")
@@ -183,6 +197,7 @@ test_that( "Indented Inline Sections", {
template <- " {{^boolean}}NO{{/boolean}}\n {{^boolean}}WAY{{/boolean}}\n"
data <- list(boolean = FALSE)
+
str <- whisker.render(template, data=data)
expect_equal(str, " NO\n WAY\n", label=deparse(str), info="Single-line sections should not alter surrounding whitespace.")
@@ -193,6 +208,7 @@ test_that( "Standalone Lines", {
template <- "| This Is\n{{^boolean}}\n|\n{{/boolean}}\n| A Line\n"
data <- list(boolean = FALSE)
+
str <- whisker.render(template, data=data)
expect_equal(str, "| This Is\n|\n| A Line\n", label=deparse(str), info="Standalone lines should be removed from the template.")
@@ -203,6 +219,7 @@ test_that( "Standalone Indented Lines", {
template <- "| This Is\n {{^boolean}}\n|\n {{/boolean}}\n| A Line\n"
data <- list(boolean = FALSE)
+
str <- whisker.render(template, data=data)
expect_equal(str, "| This Is\n|\n| A Line\n", label=deparse(str), info="Standalone indented lines should be removed from the template.")
@@ -213,6 +230,7 @@ test_that( "Standalone Line Endings", {
template <- "|\r\n{{^boolean}}\r\n{{/boolean}}\r\n|"
data <- list(boolean = FALSE)
+
str <- whisker.render(template, data=data)
expect_equal(str, "|\r\n|", label=deparse(str), info="\"\\r\\n\" should be considered a newline for standalone tags.")
@@ -223,6 +241,7 @@ test_that( "Standalone Without Previous Line", {
template <- " {{^boolean}}\n^{{/boolean}}\n/"
data <- list(boolean = FALSE)
+
str <- whisker.render(template, data=data)
expect_equal(str, "^\n/", label=deparse(str), info="Standalone tags should not require a newline to precede them.")
@@ -233,6 +252,7 @@ test_that( "Standalone Without Newline", {
template <- "^{{^boolean}}\n/\n {{/boolean}}"
data <- list(boolean = FALSE)
+
str <- whisker.render(template, data=data)
expect_equal(str, "^\n/\n", label=deparse(str), info="Standalone tags should not require a newline to follow them.")
@@ -243,6 +263,7 @@ test_that( "Padding", {
template <- "|{{^ boolean }}={{/ boolean }}|"
data <- list(boolean = FALSE)
+
str <- whisker.render(template, data=data)
expect_equal(str, "|=|", label=deparse(str), info="Superfluous in-tag whitespace should be ignored.")
View
25 inst/tests/testsections.R
@@ -44,6 +44,7 @@ test_that( "Truthy", {
template <- "\"{{#boolean}}This should be rendered.{{/boolean}}\""
data <- list(boolean = TRUE)
+
str <- whisker.render(template, data=data)
expect_equal(str, "\"This should be rendered.\"", label=deparse(str), info="Truthy sections should have their contents rendered.")
@@ -54,6 +55,7 @@ test_that( "Falsey", {
template <- "\"{{#boolean}}This should not be rendered.{{/boolean}}\""
data <- list(boolean = FALSE)
+
str <- whisker.render(template, data=data)
expect_equal(str, "\"\"", label=deparse(str), info="Falsey sections should have their contents omitted.")
@@ -64,6 +66,7 @@ test_that( "Context", {
template <- "\"{{#context}}Hi {{name}}.{{/context}}\""
data <- list(context = list(name = "Joe"))
+
str <- whisker.render(template, data=data)
expect_equal(str, "\"Hi Joe.\"", label=deparse(str), info="Objects and hashes should be pushed onto the context stack.")
@@ -75,6 +78,7 @@ test_that( "Deeply Nested Contexts", {
data <- list(a = list(one = 1), b = list(two = 2), c = list(three = 3),
d = list(four = 4), e = list(five = 5))
+
str <- whisker.render(template, data=data)
expect_equal(str, "1\n121\n12321\n1234321\n123454321\n1234321\n12321\n121\n1\n", label=deparse(str), info="All elements on the context stack should be accessible.")
@@ -85,6 +89,7 @@ test_that( "List", {
template <- "\"{{#list}}{{item}}{{/list}}\""
data <- list(list = list(list(item = 1), list(item = 2), list(item = 3)))
+
str <- whisker.render(template, data=data)
expect_equal(str, "\"123\"", label=deparse(str), info="Lists should be iterated; list items should visit the context stack.")
@@ -95,6 +100,7 @@ test_that( "Empty List", {
template <- "\"{{#list}}Yay lists!{{/list}}\""
data <- list(list = list())
+
str <- whisker.render(template, data=data)
expect_equal(str, "\"\"", label=deparse(str), info="Empty lists should behave like falsey values.")
@@ -105,6 +111,7 @@ test_that( "Doubled", {
template <- "{{#bool}}\n* first\n{{/bool}}\n* {{two}}\n{{#bool}}\n* third\n{{/bool}}\n"
data <- list(two = "second", bool = TRUE)
+
str <- whisker.render(template, data=data)
expect_equal(str, "* first\n* second\n* third\n", label=deparse(str), info="Multiple sections per template should be permitted.")
@@ -115,6 +122,7 @@ test_that( "Nested (Truthy)", {
template <- "| A {{#bool}}B {{#bool}}C{{/bool}} D{{/bool}} E |"
data <- list(bool = TRUE)
+
str <- whisker.render(template, data=data)
expect_equal(str, "| A B C D E |", label=deparse(str), info="Nested truthy sections should have their contents rendered.")
@@ -125,6 +133,7 @@ test_that( "Nested (Falsey)", {
template <- "| A {{#bool}}B {{#bool}}C{{/bool}} D{{/bool}} E |"
data <- list(bool = FALSE)
+
str <- whisker.render(template, data=data)
expect_equal(str, "| A E |", label=deparse(str), info="Nested falsey sections should be omitted.")
@@ -135,6 +144,7 @@ test_that( "Context Misses", {
template <- "[{{#missing}}Found key 'missing'!{{/missing}}]"
data <- list()
+
str <- whisker.render(template, data=data)
expect_equal(str, "[]", label=deparse(str), info="Failed context lookups should be considered falsey.")
@@ -145,6 +155,7 @@ test_that( "Implicit Iterator - String", {
template <- "\"{{#list}}({{.}}){{/list}}\""
data <- list(list = c("a", "b", "c", "d", "e"))
+
str <- whisker.render(template, data=data)
expect_equal(str, "\"(a)(b)(c)(d)(e)\"", label=deparse(str), info="Implicit iterators should directly interpolate strings.")
@@ -155,6 +166,7 @@ test_that( "Implicit Iterator - Integer", {
template <- "\"{{#list}}({{.}}){{/list}}\""
data <- list(list = c(1, 2, 3, 4, 5))
+
str <- whisker.render(template, data=data)
expect_equal(str, "\"(1)(2)(3)(4)(5)\"", label=deparse(str), info="Implicit iterators should cast integers to strings and interpolate.")
@@ -165,6 +177,7 @@ test_that( "Implicit Iterator - Decimal", {
template <- "\"{{#list}}({{.}}){{/list}}\""
data <- list(list = c(1.1, 2.2, 3.3, 4.4, 5.5))
+
str <- whisker.render(template, data=data)
expect_equal(str, "\"(1.1)(2.2)(3.3)(4.4)(5.5)\"", label=deparse(str), info="Implicit iterators should cast decimals to strings and interpolate.")
@@ -175,6 +188,7 @@ test_that( "Dotted Names - Truthy", {
template <- "\"{{#a.b.c}}Here{{/a.b.c}}\" == \"Here\""
data <- list(a = list(b = list(c = TRUE)))
+
str <- whisker.render(template, data=data)
expect_equal(str, "\"Here\" == \"Here\"", label=deparse(str), info="Dotted names should be valid for Section tags.")
@@ -185,6 +199,7 @@ test_that( "Dotted Names - Falsey", {
template <- "\"{{#a.b.c}}Here{{/a.b.c}}\" == \"\""
data <- list(a = list(b = list(c = FALSE)))
+
str <- whisker.render(template, data=data)
expect_equal(str, "\"\" == \"\"", label=deparse(str), info="Dotted names should be valid for Section tags.")
@@ -195,6 +210,7 @@ test_that( "Dotted Names - Broken Chains", {
template <- "\"{{#a.b.c}}Here{{/a.b.c}}\" == \"\""
data <- list(a = list())
+
str <- whisker.render(template, data=data)
expect_equal(str, "\"\" == \"\"", label=deparse(str), info="Dotted names that cannot be resolved should be considered falsey.")
@@ -205,6 +221,7 @@ test_that( "Surrounding Whitespace", {
template <- " | {{#boolean}}\t|\t{{/boolean}} | \n"
data <- list(boolean = TRUE)
+
str <- whisker.render(template, data=data)
expect_equal(str, " | \t|\t | \n", label=deparse(str), info="Sections should not alter surrounding whitespace.")
@@ -215,6 +232,7 @@ test_that( "Internal Whitespace", {
template <- " | {{#boolean}} {{! Important Whitespace }}\n {{/boolean}} | \n"
data <- list(boolean = TRUE)
+
str <- whisker.render(template, data=data)
expect_equal(str, " | \n | \n", label=deparse(str), info="Sections should not alter internal whitespace.")
@@ -225,6 +243,7 @@ test_that( "Indented Inline Sections", {
template <- " {{#boolean}}YES{{/boolean}}\n {{#boolean}}GOOD{{/boolean}}\n"
data <- list(boolean = TRUE)
+
str <- whisker.render(template, data=data)
expect_equal(str, " YES\n GOOD\n", label=deparse(str), info="Single-line sections should not alter surrounding whitespace.")
@@ -235,6 +254,7 @@ test_that( "Standalone Lines", {
template <- "| This Is\n{{#boolean}}\n|\n{{/boolean}}\n| A Line\n"
data <- list(boolean = TRUE)
+
str <- whisker.render(template, data=data)
expect_equal(str, "| This Is\n|\n| A Line\n", label=deparse(str), info="Standalone lines should be removed from the template.")
@@ -245,6 +265,7 @@ test_that( "Indented Standalone Lines", {
template <- "| This Is\n {{#boolean}}\n|\n {{/boolean}}\n| A Line\n"
data <- list(boolean = TRUE)
+
str <- whisker.render(template, data=data)
expect_equal(str, "| This Is\n|\n| A Line\n", label=deparse(str), info="Indented standalone lines should be removed from the template.")
@@ -255,6 +276,7 @@ test_that( "Standalone Line Endings", {
template <- "|\r\n{{#boolean}}\r\n{{/boolean}}\r\n|"
data <- list(boolean = TRUE)
+
str <- whisker.render(template, data=data)
expect_equal(str, "|\r\n|", label=deparse(str), info="\"\\r\\n\" should be considered a newline for standalone tags.")
@@ -265,6 +287,7 @@ test_that( "Standalone Without Previous Line", {
template <- " {{#boolean}}\n#{{/boolean}}\n/"
data <- list(boolean = TRUE)
+
str <- whisker.render(template, data=data)
expect_equal(str, "#\n/", label=deparse(str), info="Standalone tags should not require a newline to precede them.")
@@ -275,6 +298,7 @@ test_that( "Standalone Without Newline", {
template <- "#{{#boolean}}\n/\n {{/boolean}}"
data <- list(boolean = TRUE)
+
str <- whisker.render(template, data=data)
expect_equal(str, "#\n/\n", label=deparse(str), info="Standalone tags should not require a newline to follow them.")
@@ -285,6 +309,7 @@ test_that( "Padding", {
template <- "|{{# boolean }}={{/ boolean }}|"
data <- list(boolean = TRUE)
+
str <- whisker.render(template, data=data)
expect_equal(str, "|=|", label=deparse(str), info="Superfluous in-tag whitespace should be ignored.")
View
14 man/delim2rx.Rd
@@ -0,0 +1,14 @@
+\name{delim2rx}
+\alias{delim2rx}
+\title{change a delimiter string into two escaped characters}
+\usage{
+ delim2rx(x = "{{ }}")
+}
+\arguments{
+ \item{x}{character with delimiter seperated with a space}
+}
+\description{
+ change a delimiter string into two escaped characters
+}
+\keyword{internal}
+
View
1 man/isFalsey.Rd
@@ -13,4 +13,5 @@
\description{
This function is used to test a value before rendering
}
+\keyword{internal}
View
2 man/whisker.render.Rd
@@ -12,7 +12,7 @@
variables that will be used during rendering}
\item{partials}{named \code{list} with partial templates,
- will be used during template contruction}
+ will be used during template construction}
\item{debug}{Used for debugging purposes, likely to
disappear}

0 comments on commit 4b10e18

Please sign in to comment.
Something went wrong with that request. Please try again.