# file      : tests/directive/config.testscript
# license   : MIT; see accompanying LICENSE file

buildfile = true
test.arguments =

: default-value
:
{{
  .include ../common.testscript

  +cat <<EOI >+build/bootstrap.build
    using config
    EOI

  +cat <<EOI >=build/root.build
    config [bool, null] config.test.fancy ?= false
    print ($defined(config.test.fancy) ? $config.test.fancy : undefined)
    EOI

  # This must be a single, serial test since we are sharing config.build.
  #
  : test
  :
  cat <<EOI >=buildfile;
    ./:
    EOI

  # Unconfigured.
  #
  $* noop                               >'false' ;
  $* noop      config.test.fancy=true   >'true'  ;

  # Configured as default.
  #
  $* configure                          >'false' ;
  cat ../build/config.build >>~/EOO/             ;
    /.*
    config.test.fancy = false
    /.*
    EOO
  $* disfigure                                   ;

  # Configured as specified.
  #
  $* configure config.test.fancy=true   >'true'  ;
  $* noop                               >'true'  ;
  $* noop      config.test.fancy=false  >'false' ;
  $* configure config.test.fancy=false  >'false' ;
  $* noop                               >'false' ;
  $* configure config.test.fancy=true   >'true'  ;
  $* disfigure                                   ;
  $* noop                               >'false' ;

  $* noop      config.test.fancy=[null] >'[null]';

  $* noop      config.test.fancy=junk 2>>EOE != 0
    error: invalid bool value 'junk' in variable config.test.fancy
      info: while converting 'junk'
    EOE
}}

: default-null
:
{{
  .include ../common.testscript

  +cat <<EOI >+build/bootstrap.build
    using config
    EOI

  +cat <<EOI >=build/root.build
    config [bool] config.test.fancy ?= [null]
    print ($defined(config.test.fancy) ? $config.test.fancy : undefined)
    EOI

  # This must be a single, serial test since we are sharing config.build.
  #
  : test
  :
  cat <<EOI >=buildfile;
    ./:
    EOI

  # Unconfigured.
  #
  $* noop                               >'[null]';
  $* noop      config.test.fancy=true   >'true'  ;

  # Configured as default.
  #
  $* configure                          >'[null]';
  cat ../build/config.build >>~/EOO/             ;
    /.*
    config.test.fancy = [null]
    /.*
    EOO
  $* disfigure                                   ;

  # Configured as specified.
  #
  $* configure config.test.fancy=true   >'true'  ;
  $* noop                               >'true'  ;
  $* noop      config.test.fancy=false  >'false' ;
  $* noop      config.test.fancy=[null] >'[null]';
  $* configure config.test.fancy=false  >'false' ;
  $* noop                               >'false' ;
  $* disfigure                                   ;
  $* noop                               >'[null]';

  $* noop      config.test.fancy=junk 2>>EOE != 0
    error: invalid bool value 'junk' in variable config.test.fancy
      info: while converting 'junk'
    EOE
}}

: default-none
:
{{
  .include ../common.testscript

  +cat <<EOI >+build/bootstrap.build
    using config
    EOI

  +cat <<EOI >=build/root.build
    config [bool, null] config.test.fancy
    print ($defined(config.test.fancy) ? $config.test.fancy : undefined)
    EOI

  # This must be a single, serial test since we are sharing config.build.
  #
  : test
  :
  cat <<EOI >=buildfile;
    ./:
    EOI

  # Unconfigured.
  #
  $* noop                               >'undefined' ;
  $* noop      config.test.fancy=true   >'true'      ;

  # Configured as default.
  #
  $* configure                          >'undefined' ;
  sed -n -e 's/(config.test.fancy)/\1/p' ../build/config.build ;
  $* disfigure                                       ;

  # Configured as specified.
  #
  $* configure config.test.fancy=true   >'true'      ;
  $* noop                               >'true'      ;
  $* noop      config.test.fancy=false  >'false'     ;
  $* configure config.test.fancy=false  >'false'     ;
  $* noop                               >'false'     ;
  $* disfigure                                       ;
  $* noop                               >'undefined' ;

  $* noop      config.test.fancy=[null] >'[null]';

  $* noop      config.test.fancy=junk 2>>EOE != 0
    error: invalid bool value 'junk' in variable config.test.fancy
      info: while converting 'junk'
    EOE
}}

: non-nullable
:
{{
  .include ../common.testscript

  +cat <<EOI >+build/bootstrap.build
    using config
    EOI

  +cat <<EOI >=build/root.build
    config [bool] config.test.fancy ?= false
    print ($defined(config.test.fancy) ? $config.test.fancy : undefined)
    EOI

  # This must be a single, serial test since we are sharing config.build.
  #
  : test
  :
  cat <<EOI >=buildfile;
    ./:
    EOI

  $* noop                               >'false' ;
  $* noop      config.test.fancy=false  >'false' ;
  $* noop      config.test.fancy=true   >'true'  ;

  $* noop      config.test.fancy=[null] 2>>~/EOE/ != 0
    /.+root.build:1:1: error: null value in non-nullable variable config\.test\.fancy/
    EOE
}}

: report
:
{{
  .include ../common.testscript

  +cat <<EOI >+build/bootstrap.build
    using config
    EOI

  +cat <<EOI >=build/root.build
    config [bool]                              config.test.a
    config [bool]                              config.test.b ?= false
    config [bool,    config.report=false]      config.test.c ?= true
    config [strings, config.report=multiline]  config.test.d ?= 1 2 3
    config [string,  config.report.variable=e] config.test.e ?= abc
    config [         config.report]            f
    config [         config.report.variable=g] gg
    config [bool]                              config.test.n ?= [null]
    config [bool]                              config.test.p
    config [bool]                              config.test.p ?= true

    e = "'$config.test.e'"
    f = ($config.test.b || $config.test.c)
    g = abc

    EOI

  # This must be a single, serial test since we are sharing config.build.
  #
  : test
  :
  cat <<EOI >=buildfile;
    ./:
    EOI

  # Unconfigured.
  #
  $* noop -v 2>>~/EOO/;
    /config test@.+/
      b          false
      d
        1
        2
        3
      e          'abc'
      f          true
      gg         abc
      n          [null]
      p          true
    EOO

  # Configured.
  #
  $* configure          \
     config.test.a=true \
     config.test.e=xyz  \
     config.test.n=true \
     config.test.p=false;
  $* noop -v;
  $* noop -V 2>>~/EOO/;
    /config test@.+/
      a          true
      b          false
      d
        1
        2
        3
      e          'xyz'
      f          true
      gg         abc
      n          true
      p          false
    EOO
  $* disfigure
}}
