File: csv_build_spec.cr

package info (click to toggle)
crystal 1.14.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 24,384 kB
  • sloc: javascript: 6,400; sh: 695; makefile: 269; ansic: 121; python: 105; cpp: 77; xml: 32
file content (146 lines) | stat: -rw-r--r-- 3,712 bytes parent folder | download
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
require "spec"
require "csv"
require "spec/helpers/string"

describe CSV do
  describe "build" do
    it "builds two rows" do
      assert_prints(CSV.build do |csv|
        csv.row do |row|
          row << "one"
          row << "two"
        end
        csv.row do |row|
          row << "three"
          row << "four"
        end
      end, "one,two\nthree,four\n")
    end

    it "builds with numbers" do
      assert_prints(CSV.build do |csv|
        csv.row do |row|
          row << 1
          row << 2
        end
        csv.row do |row|
          row << 3
          row << 4
        end
      end, "1,2\n3,4\n")
    end

    it "builds with commas" do
      assert_prints(CSV.build do |csv|
        csv.row do |row|
          row << %(hello,world)
        end
      end, %("hello,world"\n))
    end

    it "builds with custom separator" do
      assert_prints(CSV.build(separator: ';') do |csv|
        csv.row do |row|
          row << "one"
          row << "two"
          row << "thr;ee"
        end
      end, %(one;two;"thr;ee"\n))
    end

    it "builds with quotes" do
      assert_prints(CSV.build do |csv|
        csv.row do |row|
          row << %(he said "no")
        end
      end, %("he said ""no"""\n))
    end

    it "builds with custom quote character" do
      assert_prints(CSV.build(quote_char: '\'') do |csv|
        csv.row do |row|
          row << %(he said 'no')
        end
      end, %('he said ''no'''\n))
    end

    it "builds row from enumerable" do
      assert_prints(CSV.build do |csv|
        csv.row [1, 2, 3]
      end, "1,2,3\n")
    end

    it "builds row from splat" do
      assert_prints(CSV.build do |csv|
        csv.row 1, 2, 3
      end, "1,2,3\n")
    end

    it "skips inside row" do
      assert_prints(CSV.build do |csv|
        csv.row do |row|
          row << 1
          row.skip_cell
          row << 2
        end
      end, "1,,2\n")
    end

    it "concats enumerable to row" do
      assert_prints(CSV.build do |csv|
        csv.row do |row|
          row << 1
          row.concat [2, 3, 4]
          row << 5
        end
      end, "1,2,3,4,5\n")
    end

    it "concats splat to row" do
      assert_prints(CSV.build do |csv|
        csv.row do |row|
          row << 1
          row.concat 2, 3, 4
          row << 5
        end
      end, "1,2,3,4,5\n")
    end

    it "builds with commas" do
      assert_prints(CSV.build do |csv|
        csv.row do |row|
          row << " , "
          row << " , "
        end
      end, %(" , "," , "\n))
    end

    it "builds with quoting" do
      assert_prints(CSV.build(quoting: CSV::Builder::Quoting::NONE) do |csv|
        csv.row 1, "doesn't", " , ", %(he said "no")
      end, %(1,doesn't, , ,he said "no"\n))

      assert_prints(CSV.build(quoting: CSV::Builder::Quoting::RFC) do |csv|
        csv.row 1, "doesn't", " , ", %(he said "no")
      end, %(1,doesn't," , ","he said ""no"""\n))

      assert_prints(CSV.build(quoting: CSV::Builder::Quoting::ALL) do |csv|
        csv.row 1, "doesn't", " , ", %(he said "no")
      end, %("1","doesn't"," , ","he said ""no"""\n))
    end

    it "builds with inside quoted chars and symbols" do
      assert_prints(CSV.build(quoting: CSV::Builder::Quoting::NONE) do |csv|
        csv.row 'c', '\'', '"', :sym, :"s'm", :"s\"m"
      end, %(c,',",sym,s'm,s"m\n))

      assert_prints(CSV.build(quoting: CSV::Builder::Quoting::RFC) do |csv|
        csv.row 'c', '\'', '"', :sym, :"s'm", :"s\"m"
      end, %(c,',"""",sym,s'm,"s""m"\n))

      assert_prints(CSV.build(quoting: CSV::Builder::Quoting::ALL) do |csv|
        csv.row 'c', '\'', '"', :sym, :"s'm", :"s\"m"
      end, %("c","'","""","sym","s'm","s""m"\n))
    end
  end
end