#######################################################
#
# Demo of Mustache templates with no external JSON data
#
#######################################################

body common control
{
      inputs => { "../../default.sub.cf" };
      bundlesequence  => { default("$(this.promise_filename)") };
      version => "1.0";

}

#######################################################

bundle agent init
{
  vars:
      "origtestdir" string => dirname("$(this.promise_filename)");

  files:
      "$(G.testfile)"
      delete => init_delete;
}

body delete init_delete
{
      dirlinks => "delete";
      rmdirs   => "true";
}

#######################################################

bundle agent test
{
  classes:
      "empty" expression => "any";

  vars:
      "template_file" string => "$(init.origtestdir)/demo.datastate.mustache";
      "header" string => "Colors";
      "items" slist => { "red", "green", "blue" };
      "d" data => parsejson('[4,5,6]');
      "h" data => parsejson('{ "a": "x", "b": "y"}');
      "h_under_q" data => mergedata('{ "q": h }'); # wrap h in a map
      "h_in_array" data => mergedata('[ h ]'); # wrap h in an array
      "h2_in_array" data => mergedata(h_in_array, h_in_array); # more arrays!

  files:
      "$(G.testfile)"
      create => "true",
      edit_template => "$(template_file)",
      template_method => "mustache";

  reports:
    DEBUG::
      "Rendering template file $(template_file) to $(G.testfile)";
}

#######################################################

bundle agent check
{
  vars:
      "expect" string => '<h1>Colors</h1>

 red
 green
 blue

  <p>The list is empty.</p>

 key "0" value "4"
 key "1" value "5"
 key "2" value "6"

 key "a" value "x"
 key "b" value "y"

 key "q" value "{
  "a": "x",
  "b": "y"
}"

 key "0" value "{
  "a": "x",
  "b": "y"
}"

 key "0" value "{
  "a": "x",
  "b": "y"
}"
 key "1" value "{
  "a": "x",
  "b": "y"
}"

 nested key should be "q": "q" value "{
  "a": "x",
  "b": "y"
}"
 nested key should be "q": "q" value "{
  "a": "x",
  "b": "y"
}"
';

      "actual" string => readfile("$(G.testfile)", inf);

  classes:
      "ok" expression => strcmp($(expect), $(actual));

  reports:
    DEBUG::
      "'$(expect)' != '$(actual)'" if => "!ok";
      "'$(expect)' == '$(actual)'" if => "ok";
      "expect: '$(expect)'";
      "actual: '$(actual)'";
    ok::
      "$(this.promise_filename) Pass";
    !ok::
      "$(this.promise_filename) FAIL";
}
