summaryrefslogtreecommitdiff
path: root/t/11-ledgersmb.t
blob: ad8ba58df6083b4f7b1ac77828cad18f54f94de4 (plain)
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use Test::More 'no_plan';
  5. use Test::Exception;
  6. use Test::Trap qw(trap $trap);
  7. use Math::BigFloat;
  8. use LedgerSMB;
  9. ##line subroutine
  10. ##108 new
  11. ##235 redirect
  12. ##254 format_amount
  13. ##364 parse_amount
  14. ##408 round_amount
  15. ##423 call_procedure
  16. ##454 date_to_number
  17. ##490 db_init
  18. ##522 redo_rows
  19. ##547 merge
  20. my $lsmb = new LedgerSMB;
  21. my %myconfig;
  22. my $utfstr;
  23. my @r;
  24. ok(defined $lsmb);
  25. isa_ok($lsmb, 'LedgerSMB');
  26. # $lsmb->escape checks
  27. $lsmb = new LedgerSMB;
  28. $utfstr = "\xd8\xad";
  29. utf8::decode($utfstr);
  30. ok(!$lsmb->escape, 'escape: (undef)');
  31. ok(!$lsmb->escape('foo' => 'bar'), 'escape: (invalid args)');
  32. cmp_ok($lsmb->escape('string' => ' '), 'eq', '%20',
  33. 'escape: \' \'');
  34. cmp_ok($lsmb->escape('string' => 'foo'), 'eq', 'foo',
  35. 'escape: foo');
  36. cmp_ok($lsmb->escape('string' => 'foo bar'), 'eq', 'foo%20bar',
  37. 'escape: foo bar');
  38. TODO: {
  39. local $TODO = 'Fun with Unicode';
  40. cmp_ok($lsmb->escape('string' => $utfstr), 'eq', '%d8%ad',
  41. 'escape: U+D8AD');
  42. }
  43. # $lsmb->is_blank checks
  44. $lsmb = new LedgerSMB;
  45. $lsmb->{blank} = ' ';
  46. $lsmb->{notblank} = ' d ';
  47. TODO: {
  48. local $TODO = 'Errors should be thrown';
  49. throws_ok{$lsmb->is_blank} 'Error::Simple', 'is_blank: (undef)';
  50. throws_ok{$lsmb->is_blank('foo' => 'bar')} 'Error::Simple',
  51. 'is_blank: (invalid args)';
  52. }
  53. is($lsmb->is_blank('name' => 'notblank'), 0, 'is_blank: notblank');
  54. is($lsmb->is_blank('name' => 'blank'), 1, 'is_blank: blank');
  55. # $lsmb->is_run_mode checks
  56. $lsmb = new LedgerSMB;
  57. $ENV{GATEWAY_INTERFACE} = 'foo';
  58. is($lsmb->is_run_mode('cgi'), 1, 'is_run_mode: CGI - CGI');
  59. is($lsmb->is_run_mode('cli'), 0, 'is_run_mode: CGI - CLI');
  60. is($lsmb->is_run_mode('mod_perl'), 0, 'is_run_mode: CGI - mod_perl');
  61. is($lsmb->is_run_mode('foo'), 0, 'is_run_mode: CGI - (bad mode)');
  62. is($lsmb->is_run_mode, 0, 'is_run_mode: CGI - (unknown mode)');
  63. $ENV{MOD_PERL} = 'foo';
  64. is($lsmb->is_run_mode('cgi'), 1, 'is_run_mode: CGI/mod_perl - CGI');
  65. is($lsmb->is_run_mode('cli'), 0, 'is_run_mode: CGI/mod_perl - CLI');
  66. is($lsmb->is_run_mode('mod_perl'), 1, 'is_run_mode: CGI/mod_perl - mod_perl');
  67. is($lsmb->is_run_mode('foo'), 0, 'is_run_mode: CGI/mod_perl - (bad mode)');
  68. is($lsmb->is_run_mode, 0, 'is_run_mode: CGI/mod_perl - (unknown mode)');
  69. delete $ENV{GATEWAY_INTERFACE};
  70. is($lsmb->is_run_mode('cgi'), 0, 'is_run_mode: mod_perl - CGI');
  71. is($lsmb->is_run_mode('cli'), 0, 'is_run_mode: mod_perl - CLI');
  72. is($lsmb->is_run_mode('mod_perl'), 1, 'is_run_mode: mod_perl - mod_perl');
  73. is($lsmb->is_run_mode('foo'), 0, 'is_run_mode: mod_perl - (bad mode)');
  74. is($lsmb->is_run_mode, 0, 'is_run_mode: mod_perl - (unknown mode)');
  75. delete $ENV{MOD_PERL};
  76. is($lsmb->is_run_mode('cgi'), 0, 'is_run_mode: CLI - CGI');
  77. is($lsmb->is_run_mode('cli'), 1, 'is_run_mode: CLI - CLI');
  78. is($lsmb->is_run_mode('mod_perl'), 0, 'is_run_mode: CLI - mod_perl');
  79. is($lsmb->is_run_mode('foo'), 0, 'is_run_mode: CLI - (bad mode)');
  80. is($lsmb->is_run_mode, 0, 'is_run_mode: CLI - (unknown mode)');
  81. # $lsmb->num_text_rows checks
  82. $lsmb = new LedgerSMB;
  83. is($lsmb->num_text_rows('string' => "apple\npear", 'cols' => 10, 'max' => 5),
  84. 2, 'num_text_rows: 2 rows, no column breakage, max 5 rows');
  85. is($lsmb->num_text_rows('string' => "apple\npear", 'cols' => 10, 'max' => 1),
  86. 1, 'num_text_rows: 2 rows, no column breakage, max 1 row');
  87. is($lsmb->num_text_rows('string' => "apple\npear", 'cols' => 10, 'max' => 2),
  88. 2, 'num_text_rows: 2 rows, no column breakage, max 2 rows');
  89. is($lsmb->num_text_rows('string' => "apple\npear", 'cols' => 10),
  90. 2, 'num_text_rows: 2 rows, no column breakage, no max row count');
  91. is($lsmb->num_text_rows('string' => "01234567890123456789", 'cols' => 10),
  92. 2, 'num_text_rows: 2 rows, non-word column breakage, no max row count');
  93. is($lsmb->num_text_rows('string' => "012345 67890123 456789", 'cols' => 10),
  94. 3, 'num_text_rows: 3 rows, word column breakage, no max row count');
  95. is($lsmb->num_text_rows('string' => "0123456789", 'cols' => 10),
  96. 1, 'num_text_rows: 1 rows, no breakage, max cols, no max row count');
  97. is($lsmb->num_text_rows('string' => "01234567890", 'cols' => 10),
  98. 2, 'num_text_rows: 2 rows, no breakage, max cols+1, no max row count');
  99. is($lsmb->num_text_rows('string' => "1\n\n2", 'cols' => 10),
  100. 3, 'num_text_rows: 3 rows, no breakage, blank line, no max row count');
  101. is($lsmb->num_text_rows('string' => "012345 67890123456789", 'cols' => 10),
  102. 3, 'num_text_rows: 3 rows, word and non column breakage, no max row count');
  103. # $lsmb->debug checks
  104. $lsmb = new LedgerSMB;
  105. @r = trap{$lsmb->debug($lsmb)};
  106. like($trap->stdout, qr|\n\$VAR1 = bless\( {[\n\s]+'action' => '',[\n\s]+'dbversion' => '\d+\.\d+\.\d+',[\n\s]+'path' => 'bin/mozilla',[\n\s]+'version' => '$lsmb->{version}'[\n\s]+}, 'LedgerSMB' \);|,
  107. 'debug: $lsmb, no file');
  108. SKIP: {
  109. skip 'Environment for file test not clean' if -f "t/var/lsmb-11.$$";
  110. $lsmb->{file} = "t/var/lsmb-11.$$";
  111. $lsmb->debug('file' => $lsmb->{file}, $lsmb);
  112. ok(-f "t/var/lsmb-11.$$", "debug: output file t/var/lsmb-11.$$ created");
  113. open(my $FH, '<', "t/var/lsmb-11.$$");
  114. my @str = <$FH>;
  115. close($FH);
  116. chomp(@str);
  117. like(join("\n", @str), qr|\$VAR1 = 'file';\n\$VAR2 = 't/var/lsmb-11.$$';\n\$VAR3 = bless\( {[\n\s]+'action' => '',[\n\s]+'dbversion' => '\d+\.\d+\.\d+',[\n\s]+'file' => 't/var/lsmb-11.$$',[\n\s]+'path' => 'bin/mozilla',[\n\s]+'version' => '$lsmb->{version}'[\n\s]+}, 'LedgerSMB' \);|,
  118. 'debug: $lsmb with file, contents');
  119. is(unlink("t/var/lsmb-11.$$"), 1, "debug: removing t/var/lsmb-11.$$");
  120. ok(!-e "t/var/lsmb-11.$$", "debug: t/var/lsmb-11.$$ removed");
  121. };