File: test-columnize.rb

package info (click to toggle)
ruby-columnize 0.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 232 kB
  • ctags: 88
  • sloc: ruby: 477; makefile: 22
file content (74 lines) | stat: -rwxr-xr-x 3,181 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env ruby
require 'test/unit'

# Test of Columnize module
class TestColumnize < Test::Unit::TestCase
  # Ruby 1.8 form of require_relative
  TOP_SRC_DIR = File.join(File.expand_path(File.dirname(__FILE__)), '..', 'lib')
  require 'columnize'

  # test columnize
  def test_basic
    assert_equal("1, 2, 3", Columnize::columnize([1, 2, 3], 10, ', '))
    assert_equal("", Columnize::columnize(5))
    assert_equal("1  3\n2  4", Columnize::columnize(['1', '2', '3', '4'], 4))
    assert_equal("1  2\n3  4", Columnize::columnize(['1', '2', '3', '4'], 4, '  ', false))
    assert_equal("<empty>\n", Columnize::columnize([]))

    assert_equal("oneitem", Columnize::columnize(["oneitem"]))

    data = (0..54).map{|i| i.to_s}
    assert_equal(
            "0,  6, 12, 18, 24, 30, 36, 42, 48, 54\n" +
            "1,  7, 13, 19, 25, 31, 37, 43, 49\n" +
            "2,  8, 14, 20, 26, 32, 38, 44, 50\n" +
            "3,  9, 15, 21, 27, 33, 39, 45, 51\n" +
            "4, 10, 16, 22, 28, 34, 40, 46, 52\n" +
            "5, 11, 17, 23, 29, 35, 41, 47, 53",
            Columnize::columnize(data, 39, ', ', true, false))

    assert_equal(
            " 0,  1,  2,  3,  4,  5,  6,  7,  8,  9\n" +
            "10, 11, 12, 13, 14, 15, 16, 17, 18, 19\n" +
            "20, 21, 22, 23, 24, 25, 26, 27, 28, 29\n" +
            "30, 31, 32, 33, 34, 35, 36, 37, 38, 39\n" +
            "40, 41, 42, 43, 44, 45, 46, 47, 48, 49\n" +
            "50, 51, 52, 53, 54",
            Columnize::columnize(data, 39, ', ', false, false))


    assert_equal(
            "   0,  1,  2,  3,  4,  5,  6,  7,  8\n" +
            "   9, 10, 11, 12, 13, 14, 15, 16, 17\n" +
            "  18, 19, 20, 21, 22, 23, 24, 25, 26\n" +
            "  27, 28, 29, 30, 31, 32, 33, 34, 35\n" +
            "  36, 37, 38, 39, 40, 41, 42, 43, 44\n" +
            "  45, 46, 47, 48, 49, 50, 51, 52, 53\n" +
            "  54",
            Columnize::columnize(data, 39, ', ', false, false, '  '))


    data = ["one",       "two",         "three",
            "for",       "five",        "six",
            "seven",     "eight",       "nine",
            "ten",       "eleven",      "twelve",
            "thirteen",  "fourteen",    "fifteen",
            "sixteen",   "seventeen",   "eightteen",
            "nineteen",  "twenty",      "twentyone",
            "twentytwo", "twentythree", "twentyfour",
            "twentyfive","twentysix",   "twentyseven"]

     assert_equal(
"one         two        three        for        five         six       \n" +
"seven       eight      nine         ten        eleven       twelve    \n" +
"thirteen    fourteen   fifteen      sixteen    seventeen    eightteen \n" +
"nineteen    twenty     twentyone    twentytwo  twentythree  twentyfour\n" +
"twentyfive  twentysix  twentyseven", Columnize::columnize(data, 80, '  ', false))

    assert_equal(
"one    five   nine    thirteen  seventeen  twentyone    twentyfive \n" +
"two    six    ten     fourteen  eightteen  twentytwo    twentysix  \n" +
"three  seven  eleven  fifteen   nineteen   twentythree  twentyseven\n" +
"for    eight  twelve  sixteen   twenty     twentyfour ", Columnize::columnize(data, 80))
  end
end