ConfigSystem.t 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #!/usr/bin/perl
  2. # ------------------------------------------------------------------------------
  3. # (C) Crown copyright Met Office. All rights reserved.
  4. # For further details please refer to the file COPYRIGHT.txt
  5. # which you should have received as part of this distribution.
  6. # ------------------------------------------------------------------------------
  7. use strict;
  8. use warnings;
  9. use Fcm::CfgLine;
  10. use Fcm::Config;
  11. use Scalar::Util qw{reftype};
  12. use Test::More (tests => 90);
  13. BEGIN: {
  14. use_ok('Fcm::ConfigSystem');
  15. }
  16. my $CONFIG = undef;
  17. # ------------------------------------------------------------------------------
  18. if (!caller()) {
  19. main(@ARGV);
  20. }
  21. # ------------------------------------------------------------------------------
  22. sub main {
  23. local @ARGV = @_;
  24. test_compare_setting_in_config();
  25. }
  26. # ------------------------------------------------------------------------------
  27. # Tests "compare_setting_in_config".
  28. sub test_compare_setting_in_config {
  29. my $PREFIX = 'TEST';
  30. my %S = (egg => [qw{boiled poached}], ham => 'roasted', bacon => 'fried');
  31. my %S_MOD = (ham => 'boiled');
  32. my %S_MOD_ARRAY = (egg => [qw{scrambled omelette}]);
  33. my %S_ADD = (mushroom => 'sauteed');
  34. my %S_DEL = (bacon => undef);
  35. my @ITEMS = (
  36. {
  37. name => 'empty',
  38. original => {},
  39. added => {},
  40. removed => {},
  41. modified => {},
  42. },
  43. {
  44. name => 'add keys to empty',
  45. original => {},
  46. added => {%S},
  47. removed => {},
  48. modified => {%S},
  49. },
  50. {
  51. name => 'remove all',
  52. original => {%S},
  53. added => {},
  54. removed => {},
  55. modified => {map {($_, undef)} keys(%S)},
  56. },
  57. {
  58. name => 'no change',
  59. original => {%S},
  60. added => {%S},
  61. removed => {},
  62. modified => {},
  63. },
  64. {
  65. name => 'modify key',
  66. original => {%S},
  67. added => {%S, %S_MOD},
  68. removed => {},
  69. modified => {%S_MOD},
  70. },
  71. {
  72. name => 'modify an array key',
  73. original => {%S},
  74. added => {%S, %S_MOD_ARRAY},
  75. removed => {},
  76. modified => {%S_MOD_ARRAY},
  77. },
  78. {
  79. name => 'add a key',
  80. original => {%S},
  81. added => {%S, %S_ADD},
  82. removed => {},
  83. modified => {%S_ADD},
  84. },
  85. {
  86. name => 'delete a key',
  87. original => {%S},
  88. added => {%S},
  89. removed => {%S_DEL},
  90. modified => {%S_DEL},
  91. },
  92. {
  93. name => 'modify a key and delete a key',
  94. original => {%S},
  95. added => {%S, %S_MOD},
  96. removed => {%S_DEL},
  97. modified => {%S_MOD, %S_DEL},
  98. },
  99. {
  100. name => 'add a key and delete a key',
  101. original => {%S},
  102. added => {%S, %S_ADD},
  103. removed => {%S_DEL},
  104. modified => {%S_ADD, %S_DEL},
  105. },
  106. );
  107. # A naive function to serialise an array reference
  108. my $flatten = sub {
  109. if (ref($_[0]) && reftype($_[0]) eq 'ARRAY') {
  110. join(q{ }, sort(@{$_[0]}))
  111. }
  112. else {
  113. $_[0];
  114. }
  115. };
  116. my $CONFIG = Fcm::Config->instance();
  117. for my $item (@ITEMS) {
  118. # New settings
  119. $CONFIG->{setting}{$PREFIX} = {%{$item->{added}}};
  120. for my $key (keys(%{$item->{removed}})) {
  121. delete($CONFIG->{setting}{$PREFIX}{$key});
  122. }
  123. # Old lines
  124. my @old_lines = map {
  125. Fcm::CfgLine->new(
  126. LABEL => $PREFIX . $Fcm::Config::DELIMITER . $_,
  127. VALUE => $flatten->($item->{original}{$_}),
  128. )
  129. } keys(%{$item->{original}});
  130. # Invokes the method
  131. my $system = Fcm::ConfigSystem->new();
  132. my ($changed_hash_ref, $new_cfg_lines_ref)
  133. = $system->compare_setting_in_config($PREFIX, \@old_lines);
  134. # Tests the return values
  135. my $T = $item->{name};
  136. is_deeply(
  137. $changed_hash_ref, $item->{modified},
  138. "$T: \$changed_hash_ref content",
  139. );
  140. is(
  141. scalar(@{$new_cfg_lines_ref}),
  142. scalar(keys(%{$item->{added}})) - scalar(keys(%{$item->{removed}})),
  143. "$T: \$new_cfg_lines_ref length",
  144. );
  145. for my $line (@{$new_cfg_lines_ref}) {
  146. my $key = $line->label_from_field(1);
  147. ok(exists($item->{added}{$key}), "$T: expected label $key");
  148. ok(!exists($item->{removed}{$key}), "$T: unexpected label $key");
  149. is(
  150. $line->value(), $flatten->($item->{added}{$key}),
  151. "$T: line content $key",
  152. );
  153. }
  154. }
  155. }
  156. __END__