3 use Test::Deep qw(bag cmp_deeply);
 
   8 use Support::TestSetup;
 
   9 use_ok 'SL::Helper::UserPreferences';
 
  11 Support::TestSetup::login();
 
  14 $prefs = new_ok 'SL::Helper::UserPreferences', [ current_version => 1 ];
 
  17 $prefs->store('test1', "val");
 
  18 $prefs->store('test2', "val2");
 
  20 cmp_deeply [ $prefs->get_keys ], bag('test1', 'test2'), 'get_keys works';
 
  22 is $prefs->get('test1'), 'val', 'get works';
 
  23 is $prefs->get_tuple('test2')->{value}, 'val2', 'get tuple works';
 
  24 is $prefs->get_all->[1]{value}, 'val2', 'get all works';
 
  25 is scalar @{ $prefs->get_all }, 2, 'get all works 2';
 
  27 $prefs = new_ok 'SL::Helper::UserPreferences', [
 
  29   upgrade_callbacks => {
 
  30     2 => sub { my ($val) = @_; $val . ' in space!'; }
 
  34 is $prefs->get('test1'), 'val in space!', 'upgrading works';
 
  36 $prefs = new_ok 'SL::Helper::UserPreferences', [ current_version => 2 ];
 
  37 is $prefs->get('test1'), 'val in space!', 'auto store back works';
 
  39 $prefs = new_ok 'SL::Helper::UserPreferences', [ current_version => 1, namespace => 'namespace2' ];
 
  40 is $prefs->get('test1'), undef, 'other namespace does not find prior data';
 
  42 $prefs->store('test1', "namespace2 test");
 
  43 is $prefs->get('test1'), 'namespace2 test', 'other namespace finds data with same key';
 
  45 $prefs = new_ok 'SL::Helper::UserPreferences', [ current_version => 2 ];
 
  46 is $prefs->get('test1'), 'val in space!', 'original namepsace is not affected';
 
  48 $prefs = new_ok 'SL::Helper::UserPreferences', [ current_version => 1, login => 'demo2' ];
 
  49 $prefs->store('test1', "login test");
 
  51 $prefs = new_ok 'SL::Helper::UserPreferences', [ current_version => 2 ];
 
  52 is $prefs->get('test1'), 'val in space!', 'original login is not affected';
 
  54 $prefs->store('test1', 'new value');
 
  55 is scalar @{ $prefs->get_all }, 2, 'storing an existing value overwrites';
 
  57 my @array = $prefs->get_all;
 
  58 is scalar @array, 1, 'get_all in list context returns 1 element';
 
  59 isa_ok $array[0], 'ARRAY', 'get_all in list context returns 1 arrayref';
 
  61 $prefs = new_ok 'SL::Helper::UserPreferences', [ current_version => 1 ];
 
  62 dies_ok { $prefs->get('test1') } 'reading newer version dies';
 
  64 $prefs = new_ok 'SL::Helper::UserPreferences', [ current_version => 2 ];
 
  65 $prefs->delete('test1');
 
  66 is $prefs->get('test1'), undef, 'deleting works';
 
  69 is $prefs->get('test2'), undef, 'delete_all works';