#######################################################
#
# Same as 105.cf, but two separate edit_line promises (second promise is not kept because files are different)
# Test promise_kept+repaired and cancel_kept+repaired (multiple promises set classes)
# Insert lines to a file (setting/clearing classes), verify that insert
# promise is kept (which sets/clears more classes)
#
#######################################################

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

bundle agent init
{
  vars:
      "body" string =>
      "BEGIN
    One potato
    Two potato
    Three potatoes
    Four
END";

      "body2" string =>
      "BEGIN
    One potato
    Three potatoes
    Four
END";

  files:
      "$(G.testfile)"
      create => "true",
      edit_line => init_insert("$(body)");
}

bundle edit_line init_insert(str)
{
  insert_lines:
      "$(str)"
      classes => all_classes;
}

body edit_defaults init_empty
{
      empty_file_before_editing => "true";
}

# These set/clear global classes, so they can be tested in another bundle
body classes all_classes
{
      promise_kept => { "promise_kept" };
      promise_repaired => { "promise_repaired" };
      repair_failed => { "repair_failed" };
      repair_denied => { "repair_denied" };
      repair_timeout => { "repair_timeout" };
      cancel_kept => { "cancel_kept" };
      cancel_repaired => { "cancel_repaired" };
      cancel_notkept => { "cancel_notkept" };
}

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

bundle agent test
{
  files:
      "$(G.testfile)"
      edit_line => test_insert,
      edit_defaults => init_empty;
}

bundle edit_line test_insert
{
  insert_lines:
      "$(init.body2)"
      classes => all_classes;
}

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

bundle agent check
{
      # These variables determine the conditions being tested
  vars:
      # NOTE: bundle edit_line init_insert(str) is CACHED, so even though it
      # is mentioned in init and we call test_insert from test, the promises are
      # the same, and are only executed ONCE, so it only sets the repaired
      # promises.
      "expect[promise_kept]" string	=> "ON";
      "expect[promise_repaired]" string	=> "ON";
      "expect[repair_failed]" string	=> "";
      "expect[repair_denied]" string	=> "";
      "expect[repair_timeout]" string	=> "";
      "expect[cancel_kept]" string	=> "";
      "expect[cancel_repaired]" string	=> "";
      "expect[cancel_notkept]" string	=> "ON";

      # Everything from here down is "boilerplate" to these 1xx.cf tests
      "lookfor" slist => {
			   "promise_kept", "promise_repaired", "repair_failed", "repair_denied",
			   "repair_timeout", "cancel_kept", "cancel_repaired", "cancel_notkept",
      };

  classes:
      "p1_$(lookfor)" and => {
			       strcmp("$(expect[$(lookfor)])", "ON"),
			       "$(lookfor)",
      };
      "p2_$(lookfor)" and => {
			       strcmp("$(expect[$(lookfor)])", ""),
			       "!$(lookfor)",
      };
      "pass_$(lookfor)" xor => { "p1_$(lookfor)", "p2_$(lookfor)" };
      
      "f1_$(lookfor)" and => {
			       strcmp("$(expect[$(lookfor)])", "ON"),
			       "!$(lookfor)",
      };
      "f2_$(lookfor)" and => {
			       strcmp("$(expect[$(lookfor)])", ""),
			       "$(lookfor)",
      };
      "f3_$(lookfor)" not => isvariable("expect[$(lookfor)]");
      "fail_$(lookfor)" or => {"f1_$(lookfor)", "f2_$(lookfor)", "f3_$(lookfor)"};

      "oops" expression => classmatch("fail.*");
      "ok" and => { classmatch("pass.*"), "!oops" };

  reports:
    DEBUG::
      "ok: class '$(lookfor)' was set (should be)"
      if => "p1_$(lookfor)";
      "ok: class '$(lookfor)' was not set (should not be)"
      if => "p2_$(lookfor)";
      "ERROR: class '$(lookfor)' WAS NOT set (should be)"
      if => "f1_$(lookfor)";
      "ERROR: class '$(lookfor)' was set (should NOT be)"
      if => "f2_$(lookfor)";
      "ERROR: missing variable expect['$(lookfor)']"
      if => "f3_$(lookfor)";
    ok::
      "$(this.promise_filename) Pass";
    !ok::
      "$(this.promise_filename) FAIL";
}
### PROJECT_ID: core
### CATEGORY_ID: 27
