/t/structured.t

http://github.com/gshank/html-formhandler · Raku · 144 lines · 125 code · 11 blank · 8 comment · 0 complexity · 00312799b4eaa5430dbaffb2aee2e14a MD5 · raw file

  1. use strict;
  2. use warnings;
  3. use Test::More;
  4. my $struct = {
  5. username => 'Joe Blow',
  6. occupation => 'Programmer',
  7. tags => ['Perl', 'programming', 'Moose' ],
  8. employer => {
  9. name => 'TechTronix',
  10. country => 'Utopia',
  11. },
  12. options => {
  13. flags => {
  14. opt_in => 1,
  15. email => 0,
  16. },
  17. cc_cards => [
  18. {
  19. type => 'Visa',
  20. number => '4248999900001010',
  21. },
  22. {
  23. type => 'MasterCard',
  24. number => '4335992034971010',
  25. },
  26. ],
  27. },
  28. addresses => [
  29. {
  30. street => 'First Street',
  31. city => 'Prime City',
  32. country => 'Utopia',
  33. id => 0,
  34. },
  35. {
  36. street => 'Second Street',
  37. city => 'Secondary City',
  38. country => 'Graustark',
  39. id => 1,
  40. },
  41. {
  42. street => 'Third Street',
  43. city => 'Tertiary City',
  44. country => 'Atlantis',
  45. id => 2,
  46. }
  47. ]
  48. };
  49. {
  50. package Structured::Form;
  51. use HTML::FormHandler::Moose;
  52. extends 'HTML::FormHandler';
  53. has_field 'username';
  54. has_field 'occupation';
  55. has_field 'tags' => ( type => 'Repeatable' );
  56. has_field 'tags.contains' => ( type => 'Text' );
  57. has_field 'employer' => ( type => 'Compound' );
  58. has_field 'employer.name';
  59. has_field 'employer.country';
  60. has_field 'options' => ( type => 'Compound' );
  61. has_field 'options.flags' => ( type => 'Compound' );
  62. has_field 'options.flags.opt_in' => ( type => 'Boolean' );
  63. has_field 'options.flags.email' => ( type => 'Boolean' );
  64. has_field 'options.cc_cards' => ( type => 'Repeatable' );
  65. has_field 'options.cc_cards.type';
  66. has_field 'options.cc_cards.number';
  67. has_field 'addresses' => ( type => 'Repeatable' );
  68. has_field 'addresses.street';
  69. has_field 'addresses.city';
  70. has_field 'addresses.country';
  71. has_field 'addresses.id';
  72. }
  73. #===========
  74. # test structured params
  75. my $form = Structured::Form->new;
  76. ok( $form, 'form created' );
  77. $form->process( params => $struct );
  78. is( $form->result->num_results, 6, 'correct number of results' );
  79. ok( $form->validated, 'form validated');
  80. is_deeply( $form->field('tags')->value, ['Perl', 'programming', 'Moose' ],
  81. 'list field tags has right values' );
  82. is( $form->field('addresses.0.city')->value, 'Prime City', 'get address field OK' );
  83. is( $form->field('options.flags.opt_in')->value, 1, 'get opt_in flag');
  84. #============
  85. # test structured init_object/item
  86. my $form2 = Structured::Form->new;
  87. ok( $form2, 'form created' );
  88. $form2->process( init_object => $struct, params => {} );
  89. is( $form2->result->num_results, 6, 'correct number of results' );
  90. ok( !$form2->validated, 'form validated');
  91. is_deeply( $form2->field('employer')->item, { name => 'TechTronix', country => 'Utopia', }, 'has item');
  92. is_deeply( $form2->field('addresses')->item, $struct->{addresses}, 'item for repeatable' );
  93. #=============
  94. my $fif = {
  95. 'addresses.0.city' => 'Prime City',
  96. 'addresses.0.country' => 'Utopia',
  97. 'addresses.0.id' => 0,
  98. 'addresses.0.street' => 'First Street',
  99. 'addresses.1.city' => 'Secondary City',
  100. 'addresses.1.country' => 'Graustark',
  101. 'addresses.1.id' => 1,
  102. 'addresses.1.street' => 'Second Street',
  103. 'addresses.2.city' => 'Tertiary City',
  104. 'addresses.2.country' => 'Atlantis',
  105. 'addresses.2.id' => 2,
  106. 'addresses.2.street' => 'Third Street',
  107. 'employer.country' => 'Utopia',
  108. 'employer.name' => 'TechTronix',
  109. 'occupation' => 'Programmer',
  110. 'options.cc_cards.0.number' => '4248999900001010',
  111. 'options.cc_cards.0.type' => 'Visa',
  112. 'options.cc_cards.1.number' => '4335992034971010',
  113. 'options.cc_cards.1.type' => 'MasterCard',
  114. 'options.flags.email' => 0,
  115. 'options.flags.opt_in' => 1,
  116. 'tags.0' => 'Perl',
  117. 'tags.1' => 'programming',
  118. 'tags.2' => 'Moose',
  119. 'username' => 'Joe Blow'
  120. };
  121. #=========
  122. is_deeply( $form->fif, $fif, 'fif is correct' );
  123. $form->process( $fif );
  124. ok( $form->validated, 'form processed from fif' );
  125. is_deeply( $form->values, $struct, 'values round-tripped from fif');
  126. #=========
  127. # works with item and params
  128. $form2->process( item => $struct, params => $fif );
  129. ok( $form2->validated, 'form processed from fif' );
  130. is( $form2->result->num_results, 6, 'correct number of results' );
  131. is_deeply( $form2->field('employer')->item, { name => 'TechTronix', country => 'Utopia', }, 'has item');
  132. is_deeply( $form2->field('addresses')->item, $struct->{addresses}, 'item for repeatable' );
  133. done_testing;