#######################################################
#
# Test readstringarray(), introduce a non-parsed comment
#
# The 4xx tests are all related, and 400-419 are the readstringarray tests,
# 420-439 the same for readstringarrayidx, 440-459 parsestringarray, and
# 460-479 parsestringarrayidx
#
#######################################################
body common control
{
  inputs => { "../../default.sub.cf" };
  bundlesequence => { "g", default("$(this.promise_filename)") };
  version => "1.0";
}

bundle common g
{
  vars:
    "testfile" string => "/tmp/TEST.cfengine";
}

#######################################################
bundle agent init
{
  files:
    "$(G.testfile)" delete => init_delete;

    "$(G.testfile)"
      create => "true",
      edit_line => init_fill_in;
}

bundle edit_line init_fill_in
{
  insert_lines:
    "0:1:2
# Not parsed as a comment
this:is:a:test
1:2:3";
}

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

#######################################################
bundle agent test
{
  vars:
    "cnt"
      int => readstringarray(
        "ary", "$(G.testfile)", "NoComment", ":", 10, 1000
      );

    "num" int => "4";
}

#######################################################
bundle agent check
{
  vars:
    "idx" slist => getindices("test.ary");

  classes:
    "good"
      and => {
        strcmp("$(test.num)", "$(test.cnt)"),
        strcmp("$(test.ary[0][0])", "0"),
        strcmp("$(test.ary[0][1])", "1"),
        strcmp("$(test.ary[0][2])", "2"),
        strcmp(
          "$(test.ary[# Not parsed as a comment][0])",
          "# Not parsed as a comment"
        ),
        strcmp("$(test.ary[this][0])", "this"),
        strcmp("$(test.ary[this][1])", "is"),
        strcmp("$(test.ary[this][2])", "a"),
        strcmp("$(test.ary[this][3])", "test"),
        strcmp("$(test.ary[1][0])", "1"),
        strcmp("$(test.ary[1][1])", "2"),
        strcmp("$(test.ary[1][2])", "3"),
      };

    # Make sure there are no trailing elements
    "bad"
      or => {
        isvariable("test.ary[0][3]"),
        isvariable("test.ary[# Not parsed as a comment][1]"),
        isvariable("test.ary[this][4]"),
        isvariable("test.ary[1][3]"),
      };

    "ok" expression => "good&!bad";

  reports:
    DEBUG::
      "expected $(test.num) entries, saw $(test.cnt)";
      "saw array indices '$(idx)'";
      "expected test.ary[0][0] = '0', saw '$(test.ary[0][0])'";
      "expected test.ary[0][1] = '1', saw '$(test.ary[0][1])'";
      "expected test.ary[0][2] = '2', saw '$(test.ary[0][2])'";
      "expected test.ary[# Not parsed as a comment][0] = '# Not parsed as a comment', saw 'expected $(test.ary[# Not parsed as a comment][0])'";
      "expected test.ary[this][0] = 'this', saw '$(test.ary[this][0])'";
      "expected test.ary[this][1] = 'is', saw '$(test.ary[this][1])'";
      "expected test.ary[this][2] = 'a', saw '$(test.ary[this][2])'";
      "expected test.ary[this][3] = 'test', saw '$(test.ary[this][3])'";
      "expected test.ary[1][0] = '1', saw '$(test.ary[1][0])'";
      "expected test.ary[1][1] = '2', saw '$(test.ary[1][1])'";
      "expected test.ary[1][2] = '3', saw '$(test.ary[1][2])'";

    ok::
      "$(this.promise_filename) Pass";

    !ok::
      "$(this.promise_filename) FAIL";
}
