You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

csv_test.rb 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2023 Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. require_relative '../../../../test_helper'
  19. class CsvTest < ActiveSupport::TestCase
  20. include Redmine::I18n
  21. def test_should_include_bom_when_utf8_encoded
  22. with_locale 'sk' do
  23. string = Redmine::Export::CSV.generate {|csv| csv << %w(Foo Bar)}
  24. assert_equal 'UTF-8', string.encoding.name
  25. assert string.starts_with?("\xEF\xBB\xBF")
  26. end
  27. end
  28. def test_generate_should_return_strings_with_given_encoding
  29. with_locale 'en' do
  30. string = Redmine::Export::CSV.generate({encoding: 'ISO-8859-3'}) {|csv| csv << %w(Foo Bar)}
  31. assert_equal 'ISO-8859-3', string.encoding.name
  32. assert_not_equal l(:general_csv_encoding), string.encoding.name
  33. end
  34. end
  35. def test_generate_should_return_strings_with_general_csv_encoding_if_invalid_encoding_is_given
  36. with_locale 'en' do
  37. string = Redmine::Export::CSV.generate({encoding: 'invalid-encoding-name'}) {|csv| csv << %w(Foo Bar)}
  38. assert_equal l(:general_csv_encoding), string.encoding.name
  39. end
  40. end
  41. def test_generate_should_use_general_csv_separator_by_default
  42. with_locale 'fr' do
  43. string = Redmine::Export::CSV.generate {|csv| csv << %w(Foo Bar)}
  44. assert_equal ';', l(:general_csv_separator)
  45. assert 'Foo;Bar', string
  46. end
  47. end
  48. def test_generate_should_use_given_separator
  49. with_locale 'fr' do
  50. string = Redmine::Export::CSV.generate({field_separator: ','}) {|csv| csv << %w(Foo Bar)}
  51. assert_equal ';', l(:general_csv_separator)
  52. assert 'Foo,Bar', string
  53. end
  54. end
  55. end