wjordan213-csvlint 0.2.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (77) hide show
  1. checksums.yaml +7 -0
  2. data/.coveralls.yml +1 -0
  3. data/.gitattributes +2 -0
  4. data/.gitignore +28 -0
  5. data/.ruby-version +1 -0
  6. data/.travis.yml +32 -0
  7. data/CHANGELOG.md +361 -0
  8. data/Gemfile +7 -0
  9. data/LICENSE.md +22 -0
  10. data/README.md +328 -0
  11. data/Rakefile +17 -0
  12. data/bin/create_schema +32 -0
  13. data/bin/csvlint +10 -0
  14. data/features/check_format.feature +46 -0
  15. data/features/cli.feature +210 -0
  16. data/features/csv_options.feature +35 -0
  17. data/features/csvupload.feature +145 -0
  18. data/features/csvw_schema_validation.feature +127 -0
  19. data/features/fixtures/cr-line-endings.csv +0 -0
  20. data/features/fixtures/crlf-line-endings.csv +0 -0
  21. data/features/fixtures/inconsistent-line-endings-unquoted.csv +0 -0
  22. data/features/fixtures/inconsistent-line-endings.csv +0 -0
  23. data/features/fixtures/invalid-byte-sequence.csv +0 -0
  24. data/features/fixtures/invalid_many_rows.csv +0 -0
  25. data/features/fixtures/lf-line-endings.csv +0 -0
  26. data/features/fixtures/spreadsheet.xls +0 -0
  27. data/features/fixtures/spreadsheet.xlsx +0 -0
  28. data/features/fixtures/title-row.csv +0 -0
  29. data/features/fixtures/valid.csv +0 -0
  30. data/features/fixtures/valid_many_rows.csv +0 -0
  31. data/features/fixtures/windows-line-endings.csv +0 -0
  32. data/features/information.feature +22 -0
  33. data/features/parse_csv.feature +90 -0
  34. data/features/schema_validation.feature +105 -0
  35. data/features/sources.feature +17 -0
  36. data/features/step_definitions/cli_steps.rb +11 -0
  37. data/features/step_definitions/csv_options_steps.rb +24 -0
  38. data/features/step_definitions/information_steps.rb +13 -0
  39. data/features/step_definitions/parse_csv_steps.rb +42 -0
  40. data/features/step_definitions/schema_validation_steps.rb +33 -0
  41. data/features/step_definitions/sources_steps.rb +7 -0
  42. data/features/step_definitions/validation_errors_steps.rb +90 -0
  43. data/features/step_definitions/validation_info_steps.rb +22 -0
  44. data/features/step_definitions/validation_warnings_steps.rb +60 -0
  45. data/features/support/aruba.rb +56 -0
  46. data/features/support/env.rb +26 -0
  47. data/features/support/load_tests.rb +114 -0
  48. data/features/support/webmock.rb +1 -0
  49. data/features/validation_errors.feature +147 -0
  50. data/features/validation_info.feature +16 -0
  51. data/features/validation_warnings.feature +86 -0
  52. data/lib/csvlint.rb +27 -0
  53. data/lib/csvlint/cli.rb +165 -0
  54. data/lib/csvlint/csvw/column.rb +359 -0
  55. data/lib/csvlint/csvw/date_format.rb +182 -0
  56. data/lib/csvlint/csvw/metadata_error.rb +13 -0
  57. data/lib/csvlint/csvw/number_format.rb +211 -0
  58. data/lib/csvlint/csvw/property_checker.rb +761 -0
  59. data/lib/csvlint/csvw/table.rb +204 -0
  60. data/lib/csvlint/csvw/table_group.rb +165 -0
  61. data/lib/csvlint/error_collector.rb +27 -0
  62. data/lib/csvlint/error_message.rb +15 -0
  63. data/lib/csvlint/field.rb +196 -0
  64. data/lib/csvlint/schema.rb +92 -0
  65. data/lib/csvlint/validate.rb +599 -0
  66. data/lib/csvlint/version.rb +3 -0
  67. data/spec/csvw/column_spec.rb +112 -0
  68. data/spec/csvw/date_format_spec.rb +49 -0
  69. data/spec/csvw/number_format_spec.rb +417 -0
  70. data/spec/csvw/table_group_spec.rb +143 -0
  71. data/spec/csvw/table_spec.rb +90 -0
  72. data/spec/field_spec.rb +252 -0
  73. data/spec/schema_spec.rb +211 -0
  74. data/spec/spec_helper.rb +17 -0
  75. data/spec/validator_spec.rb +619 -0
  76. data/wjordan213_csvlint.gemspec +46 -0
  77. metadata +490 -0
@@ -0,0 +1,3 @@
1
+ module Csvlint
2
+ VERSION = "0.2.8"
3
+ end
@@ -0,0 +1,112 @@
1
+ require 'spec_helper'
2
+
3
+ describe Csvlint::Csvw::Column do
4
+
5
+ it "shouldn't generate errors for string values" do
6
+ column = Csvlint::Csvw::Column.new(1, "foo")
7
+ valid = column.validate("bar", 2)
8
+ expect(valid).to eq(true)
9
+ end
10
+
11
+ it "should generate errors for string values that aren't long enough" do
12
+ column = Csvlint::Csvw::Column.new(1, "foo", datatype: { "base" => "http://www.w3.org/2001/XMLSchema#string", "minLength" => 4 })
13
+ valid = column.validate("bar", 2)
14
+ expect(valid).to eq(false)
15
+ expect(column.errors.length).to eq(1)
16
+ end
17
+
18
+ it "shouldn't generate errors for string values that are long enough" do
19
+ column = Csvlint::Csvw::Column.new(1, "foo", datatype: { "base" => "http://www.w3.org/2001/XMLSchema#string", "minLength" => 4 })
20
+ valid = column.validate("barn", 2)
21
+ expect(valid).to eq(true)
22
+ expect(column.errors.length).to eq(0)
23
+ end
24
+
25
+ context "when parsing CSVW column descriptions" do
26
+ it "should provide appropriate default values" do
27
+ @desc=<<-EOL
28
+ {
29
+ "name": "countryCode"
30
+ }
31
+ EOL
32
+ json = JSON.parse( @desc )
33
+ column = Csvlint::Csvw::Column.from_json(1, json)
34
+
35
+ expect(column).to be_a(Csvlint::Csvw::Column)
36
+ expect(column.number).to eq(1)
37
+ expect(column.name).to eq("countryCode")
38
+ expect(column.about_url).to eq(nil)
39
+ expect(column.datatype).to eq({ "@id" => "http://www.w3.org/2001/XMLSchema#string" })
40
+ expect(column.default).to eq("")
41
+ expect(column.lang).to eq("und")
42
+ expect(column.null).to eq([""])
43
+ expect(column.ordered).to eq(false)
44
+ expect(column.property_url).to eq(nil)
45
+ expect(column.required).to eq(false)
46
+ expect(column.separator).to eq(nil)
47
+ expect(column.source_number).to eq(1)
48
+ expect(column.suppress_output).to eq(false)
49
+ expect(column.text_direction).to eq(:inherit)
50
+ expect(column.titles).to eq(nil)
51
+ expect(column.value_url).to eq(nil)
52
+ expect(column.virtual).to eq(false)
53
+ expect(column.annotations).to eql({})
54
+ end
55
+
56
+ it "should override default values" do
57
+ @desc=<<-EOL
58
+ {
59
+ "name": "countryCode",
60
+ "titles": "countryCode",
61
+ "propertyUrl": "http://www.geonames.org/ontology{#_name}"
62
+ }
63
+ EOL
64
+ json = JSON.parse( @desc )
65
+ column = Csvlint::Csvw::Column.from_json(2, json)
66
+
67
+ expect(column).to be_a(Csvlint::Csvw::Column)
68
+ expect(column.number).to eq(2)
69
+ expect(column.name).to eq("countryCode")
70
+ expect(column.about_url).to eq(nil)
71
+ expect(column.datatype).to eq({ "@id" => "http://www.w3.org/2001/XMLSchema#string" })
72
+ expect(column.default).to eq("")
73
+ expect(column.lang).to eq("und")
74
+ expect(column.null).to eq([""])
75
+ expect(column.ordered).to eq(false)
76
+ expect(column.property_url).to eq("http://www.geonames.org/ontology{#_name}")
77
+ expect(column.required).to eq(false)
78
+ expect(column.separator).to eq(nil)
79
+ expect(column.source_number).to eq(2)
80
+ expect(column.suppress_output).to eq(false)
81
+ expect(column.text_direction).to eq(:inherit)
82
+ expect(column.titles).to eql({ "und" => [ "countryCode" ]})
83
+ expect(column.value_url).to eq(nil)
84
+ expect(column.virtual).to eq(false)
85
+ expect(column.annotations).to eql({})
86
+ end
87
+
88
+ it "should include the datatype" do
89
+ @desc=<<-EOL
90
+ { "name": "Id", "required": true, "datatype": { "base": "string", "minLength": 3 } }
91
+ EOL
92
+ json = JSON.parse(@desc)
93
+ column = Csvlint::Csvw::Column.from_json(1, json)
94
+ expect(column.name).to eq("Id")
95
+ expect(column.required).to eq(true)
96
+ expect(column.datatype).to eql({ "base" => "http://www.w3.org/2001/XMLSchema#string", "minLength" => 3 })
97
+ end
98
+
99
+ it "should generate warnings for invalid null values" do
100
+ @desc=<<-EOL
101
+ {
102
+ "name": "countryCode",
103
+ "null": true
104
+ }
105
+ EOL
106
+ json = JSON.parse( @desc )
107
+ column = Csvlint::Csvw::Column.from_json(1, json)
108
+ expect(column.warnings.length).to eq(1)
109
+ expect(column.warnings[0].type).to eq(:invalid_value)
110
+ end
111
+ end
112
+ end
@@ -0,0 +1,49 @@
1
+ require 'spec_helper'
2
+
3
+ describe Csvlint::Csvw::NumberFormat do
4
+
5
+ it "should parse dates that match yyyy-MM-dd correctly" do
6
+ format = Csvlint::Csvw::DateFormat.new("yyyy-MM-dd")
7
+ expect(format.parse("2015-03-22")).to eql(Date.new(2015, 3, 22))
8
+ expect(format.parse("2015-02-30")).to eql(nil)
9
+ expect(format.parse("22/03/2015")).to eq(nil)
10
+ end
11
+
12
+ it "should parse times that match HH:mm:ss correctly" do
13
+ format = Csvlint::Csvw::DateFormat.new("HH:mm:ss")
14
+ expect(format.parse("12:34:56")).to eql({ "hour" => 12, "minute" => 34, "second" => 56.0 })
15
+ expect(format.parse("22/03/2015")).to eq(nil)
16
+ end
17
+
18
+ it "should parse times that match HH:mm:ss.SSS correctly" do
19
+ format = Csvlint::Csvw::DateFormat.new("HH:mm:ss.SSS")
20
+ expect(format.parse("12:34:56")).to eql({ "hour" => 12, "minute" => 34, "second" => 56.0 })
21
+ expect(format.parse("12:34:56.78")).to eql({ "hour" => 12, "minute" => 34, "second" => 56.78 })
22
+ expect(format.parse("12:34:56.789")).to eql({ "hour" => 12, "minute" => 34, "second" => 56.789 })
23
+ expect(format.parse("12:34:56.7890")).to eql(nil)
24
+ expect(format.parse("22/03/2015")).to eq(nil)
25
+ end
26
+
27
+ it "should parse dateTimes that match yyyy-MM-ddTHH:mm:ss correctly" do
28
+ format = Csvlint::Csvw::DateFormat.new("yyyy-MM-ddTHH:mm:ss")
29
+ expect(format.parse("2015-03-15T15:02:37")).to eql(DateTime.new(2015, 3, 15, 15, 2, 37))
30
+ expect(format.parse("12:34:56")).to eql(nil)
31
+ expect(format.parse("22/03/2015")).to eq(nil)
32
+ end
33
+
34
+ it "should parse dateTimes that match yyyy-MM-ddTHH:mm:ss.S correctly" do
35
+ format = Csvlint::Csvw::DateFormat.new("yyyy-MM-ddTHH:mm:ss.S")
36
+ expect(format.parse("2015-03-15T15:02:37")).to eql(DateTime.new(2015, 3, 15, 15, 2, 37.0))
37
+ expect(format.parse("2015-03-15T15:02:37.4")).to eql(DateTime.new(2015, 3, 15, 15, 2, 37.4))
38
+ expect(format.parse("2015-03-15T15:02:37.45")).to eql(nil)
39
+ expect(format.parse("12:34:56")).to eql(nil)
40
+ expect(format.parse("22/03/2015")).to eq(nil)
41
+ end
42
+
43
+ it "should parse dateTimes that match M/d/yyyy HH:mm correctly" do
44
+ format = Csvlint::Csvw::DateFormat.new("M/d/yyyy HH:mm")
45
+ expect(format.parse("2015-03-15T15:02:37")).to eql(nil)
46
+ expect(format.parse("3/15/2015 15:02")).to eql(DateTime.new(2015, 3, 15, 15, 2))
47
+ end
48
+
49
+ end
@@ -0,0 +1,417 @@
1
+ require 'spec_helper'
2
+
3
+ describe Csvlint::Csvw::NumberFormat do
4
+
5
+ it "should correctly parse #,##0.##" do
6
+ format = Csvlint::Csvw::NumberFormat.new("#,##0.##")
7
+ expect(format.pattern).to eq("#,##0.##")
8
+ expect(format.prefix).to eq("")
9
+ expect(format.numeric_part).to eq("#,##0.##")
10
+ expect(format.suffix).to eq("")
11
+ expect(format.grouping_separator).to eq(",")
12
+ expect(format.decimal_separator).to eq(".")
13
+ expect(format.primary_grouping_size).to eq(3)
14
+ expect(format.secondary_grouping_size).to eq(3)
15
+ expect(format.fractional_grouping_size).to eq(0)
16
+ end
17
+
18
+ it "should correctly parse ###0.#####" do
19
+ format = Csvlint::Csvw::NumberFormat.new("###0.#####")
20
+ expect(format.primary_grouping_size).to eq(0)
21
+ expect(format.secondary_grouping_size).to eq(0)
22
+ expect(format.fractional_grouping_size).to eq(0)
23
+ end
24
+
25
+ it "should correctly parse ###0.0000#" do
26
+ format = Csvlint::Csvw::NumberFormat.new("###0.0000#")
27
+ expect(format.primary_grouping_size).to eq(0)
28
+ expect(format.secondary_grouping_size).to eq(0)
29
+ expect(format.fractional_grouping_size).to eq(0)
30
+ end
31
+
32
+ it "should correctly parse #,##,###,####" do
33
+ format = Csvlint::Csvw::NumberFormat.new("#,##,###,####")
34
+ expect(format.primary_grouping_size).to eq(4)
35
+ expect(format.secondary_grouping_size).to eq(3)
36
+ expect(format.fractional_grouping_size).to eq(0)
37
+ end
38
+
39
+ it "should correctly parse #,##0.###,#" do
40
+ format = Csvlint::Csvw::NumberFormat.new("#,##0.###,#")
41
+ expect(format.primary_grouping_size).to eq(3)
42
+ expect(format.secondary_grouping_size).to eq(3)
43
+ expect(format.fractional_grouping_size).to eq(3)
44
+ end
45
+
46
+ it "should correctly parse #0.###E#0" do
47
+ format = Csvlint::Csvw::NumberFormat.new("#0.###E#0")
48
+ expect(format.prefix).to eq("")
49
+ expect(format.numeric_part).to eq("#0.###E#0")
50
+ expect(format.suffix).to eq("")
51
+ end
52
+
53
+ it "should match numbers that match ##0 correctly" do
54
+ format = Csvlint::Csvw::NumberFormat.new("##0")
55
+ expect(format.match("1")).to eq(true)
56
+ expect(format.match("12")).to eq(true)
57
+ expect(format.match("123")).to eq(true)
58
+ expect(format.match("1234")).to eq(true)
59
+ expect(format.match("1,234")).to eq(false)
60
+ expect(format.match("123.4")).to eq(false)
61
+ end
62
+
63
+ it "should match numbers that match #,#00 correctly" do
64
+ format = Csvlint::Csvw::NumberFormat.new("#,#00")
65
+ expect(format.match("1")).to eq(false)
66
+ expect(format.match("12")).to eq(true)
67
+ expect(format.match("123")).to eq(true)
68
+ expect(format.match("1234")).to eq(false)
69
+ expect(format.match("1,234")).to eq(true)
70
+ expect(format.match("1,234,568")).to eq(true)
71
+ expect(format.match("12,34,568")).to eq(false)
72
+ expect(format.match("12,34")).to eq(false)
73
+ expect(format.match("123.4")).to eq(false)
74
+ end
75
+
76
+ it "should match numbers that match #,000 correctly" do
77
+ format = Csvlint::Csvw::NumberFormat.new("#,000")
78
+ expect(format.match("1")).to eq(false)
79
+ expect(format.match("12")).to eq(false)
80
+ expect(format.match("123")).to eq(true)
81
+ expect(format.match("1234")).to eq(false)
82
+ expect(format.match("1,234")).to eq(true)
83
+ expect(format.match("1,234,568")).to eq(true)
84
+ expect(format.match("12,34,568")).to eq(false)
85
+ expect(format.match("12,34")).to eq(false)
86
+ expect(format.match("123.4")).to eq(false)
87
+ end
88
+
89
+ it "should match numbers that match #,##,#00 correctly" do
90
+ format = Csvlint::Csvw::NumberFormat.new("#,##,#00")
91
+ expect(format.match("1")).to eq(false)
92
+ expect(format.match("12")).to eq(true)
93
+ expect(format.match("123")).to eq(true)
94
+ expect(format.match("1234")).to eq(false)
95
+ expect(format.match("1,234")).to eq(true)
96
+ expect(format.match("1,234,568")).to eq(false)
97
+ expect(format.match("12,34,568")).to eq(true)
98
+ expect(format.match("12,34")).to eq(false)
99
+ expect(format.match("123.4")).to eq(false)
100
+ end
101
+
102
+ it "should match numbers that match #0.# correctly" do
103
+ format = Csvlint::Csvw::NumberFormat.new("#0.#")
104
+ expect(format.match("1")).to eq(true)
105
+ expect(format.match("12")).to eq(true)
106
+ expect(format.match("12.3")).to eq(true)
107
+ expect(format.match("12.34")).to eq(false)
108
+ expect(format.match("1234.5")).to eq(true)
109
+ expect(format.match("1,234.5")).to eq(false)
110
+ end
111
+
112
+ it "should match numbers that match #0.0 correctly" do
113
+ format = Csvlint::Csvw::NumberFormat.new("#0.0")
114
+ expect(format.match("1")).to eq(false)
115
+ expect(format.match("12")).to eq(false)
116
+ expect(format.match("12.3")).to eq(true)
117
+ expect(format.match("12.34")).to eq(false)
118
+ expect(format.match("1234.5")).to eq(true)
119
+ expect(format.match("1,234.5")).to eq(false)
120
+ end
121
+
122
+ it "should match numbers that match #0.0# correctly" do
123
+ format = Csvlint::Csvw::NumberFormat.new("#0.0#")
124
+ expect(format.match("1")).to eq(false)
125
+ expect(format.match("12")).to eq(false)
126
+ expect(format.match("12.3")).to eq(true)
127
+ expect(format.match("12.34")).to eq(true)
128
+ expect(format.match("12.345")).to eq(false)
129
+ end
130
+
131
+ it "should match numbers that match #0.0#,# correctly" do
132
+ format = Csvlint::Csvw::NumberFormat.new("#0.0#,#")
133
+ expect(format.match("1")).to eq(false)
134
+ expect(format.match("12.3")).to eq(true)
135
+ expect(format.match("12.34")).to eq(true)
136
+ expect(format.match("12.345")).to eq(false)
137
+ expect(format.match("12.34,5")).to eq(true)
138
+ expect(format.match("12.34,56")).to eq(false)
139
+ expect(format.match("12.34,567")).to eq(false)
140
+ expect(format.match("12.34,56,7")).to eq(false)
141
+ end
142
+
143
+ it "should match numbers that match #0.###E#0 correctly" do
144
+ format = Csvlint::Csvw::NumberFormat.new("#0.###E#0")
145
+ expect(format.match("1")).to eq(false)
146
+ expect(format.match("12.3")).to eq(false)
147
+ expect(format.match("12.34")).to eq(false)
148
+ expect(format.match("12.3E4")).to eq(true)
149
+ expect(format.match("12.3E45")).to eq(true)
150
+ expect(format.match("12.34E5")).to eq(true)
151
+ end
152
+
153
+ it "should parse numbers that match ##0 correctly" do
154
+ format = Csvlint::Csvw::NumberFormat.new("##0")
155
+ expect(format.parse("1")).to eql(1)
156
+ expect(format.parse("12")).to eql(12)
157
+ expect(format.parse("123")).to eql(123)
158
+ expect(format.parse("1234")).to eql(1234)
159
+ expect(format.parse("1,234")).to eq(nil)
160
+ expect(format.parse("123.4")).to eq(nil)
161
+ end
162
+
163
+ it "should parse numbers that match #,#00 correctly" do
164
+ format = Csvlint::Csvw::NumberFormat.new("#,#00")
165
+ expect(format.parse("1")).to eq(nil)
166
+ expect(format.parse("12")).to eql(12)
167
+ expect(format.parse("123")).to eql(123)
168
+ expect(format.parse("1234")).to eq(nil)
169
+ expect(format.parse("1,234")).to eql(1234)
170
+ expect(format.parse("1,234,568")).to eql(1234568)
171
+ expect(format.parse("12,34,568")).to eq(nil)
172
+ expect(format.parse("12,34")).to eq(nil)
173
+ expect(format.parse("123.4")).to eq(nil)
174
+ end
175
+
176
+ it "should parse numbers that match #,000 correctly" do
177
+ format = Csvlint::Csvw::NumberFormat.new("#,000")
178
+ expect(format.parse("1")).to eq(nil)
179
+ expect(format.parse("12")).to eq(nil)
180
+ expect(format.parse("123")).to eql(123)
181
+ expect(format.parse("1234")).to eq(nil)
182
+ expect(format.parse("1,234")).to eql(1234)
183
+ expect(format.parse("1,234,568")).to eql(1234568)
184
+ expect(format.parse("12,34,568")).to eq(nil)
185
+ expect(format.parse("12,34")).to eq(nil)
186
+ expect(format.parse("123.4")).to eq(nil)
187
+ end
188
+
189
+ it "should parse numbers that match #0,000 correctly" do
190
+ format = Csvlint::Csvw::NumberFormat.new("#0,000")
191
+ expect(format.parse("1")).to eq(nil)
192
+ expect(format.parse("12")).to eq(nil)
193
+ expect(format.parse("123")).to eql(nil)
194
+ expect(format.parse("1234")).to eq(nil)
195
+ expect(format.parse("1,234")).to eql(1234)
196
+ expect(format.parse("1,234,568")).to eql(1234568)
197
+ expect(format.parse("12,34,568")).to eq(nil)
198
+ expect(format.parse("12,34")).to eq(nil)
199
+ expect(format.parse("123.4")).to eq(nil)
200
+ end
201
+
202
+ it "should parse numbers that match #,##,#00 correctly" do
203
+ format = Csvlint::Csvw::NumberFormat.new("#,##,#00")
204
+ expect(format.parse("1")).to eq(nil)
205
+ expect(format.parse("12")).to eql(12)
206
+ expect(format.parse("123")).to eql(123)
207
+ expect(format.parse("1234")).to eq(nil)
208
+ expect(format.parse("1,234")).to eql(1234)
209
+ expect(format.parse("12,345")).to eql(12345)
210
+ expect(format.parse("1,234,568")).to eq(nil)
211
+ expect(format.parse("12,34,568")).to eql(1234568)
212
+ expect(format.parse("12,34")).to eq(nil)
213
+ expect(format.parse("123.4")).to eq(nil)
214
+ end
215
+
216
+ it "should parse numbers that match #,00,000 correctly" do
217
+ format = Csvlint::Csvw::NumberFormat.new("#,00,000")
218
+ expect(format.parse("1")).to eq(nil)
219
+ expect(format.parse("12")).to eql(nil)
220
+ expect(format.parse("123")).to eql(nil)
221
+ expect(format.parse("1234")).to eq(nil)
222
+ expect(format.parse("1,234")).to eql(nil)
223
+ expect(format.parse("12,345")).to eql(12345)
224
+ expect(format.parse("1,234,568")).to eq(nil)
225
+ expect(format.parse("1,34,568")).to eql(134568)
226
+ expect(format.parse("12,34,568")).to eql(1234568)
227
+ expect(format.parse("1,23,45,678")).to eql(12345678)
228
+ expect(format.parse("12,34")).to eq(nil)
229
+ expect(format.parse("123.4")).to eq(nil)
230
+ end
231
+
232
+ it "should parse numbers that match 0,00,000 correctly" do
233
+ format = Csvlint::Csvw::NumberFormat.new("0,00,000")
234
+ expect(format.parse("1")).to eq(nil)
235
+ expect(format.parse("12")).to eql(nil)
236
+ expect(format.parse("123")).to eql(nil)
237
+ expect(format.parse("1234")).to eq(nil)
238
+ expect(format.parse("1,234")).to eql(nil)
239
+ expect(format.parse("12,345")).to eql(nil)
240
+ expect(format.parse("1,234,568")).to eq(nil)
241
+ expect(format.parse("1,34,568")).to eql(134568)
242
+ expect(format.parse("12,34,568")).to eql(1234568)
243
+ expect(format.parse("1,23,45,678")).to eql(12345678)
244
+ expect(format.parse("12,34")).to eq(nil)
245
+ expect(format.parse("123.4")).to eq(nil)
246
+ end
247
+
248
+ it "should parse numbers that match #0.# correctly" do
249
+ format = Csvlint::Csvw::NumberFormat.new("#0.#")
250
+ expect(format.parse("1")).to eql(1.0)
251
+ expect(format.parse("12")).to eql(12.0)
252
+ expect(format.parse("12.3")).to eql(12.3)
253
+ expect(format.parse("12.34")).to eq(nil)
254
+ expect(format.parse("1234.5")).to eql(1234.5)
255
+ expect(format.parse("1,234.5")).to eq(nil)
256
+ end
257
+
258
+ it "should parse numbers that match #0.0 correctly" do
259
+ format = Csvlint::Csvw::NumberFormat.new("#0.0")
260
+ expect(format.parse("1")).to eq(nil)
261
+ expect(format.parse("12")).to eq(nil)
262
+ expect(format.parse("12.3")).to eql(12.3)
263
+ expect(format.parse("12.34")).to eq(nil)
264
+ expect(format.parse("1234.5")).to eql(1234.5)
265
+ expect(format.parse("1,234.5")).to eq(nil)
266
+ end
267
+
268
+ it "should parse numbers that match #0.0# correctly" do
269
+ format = Csvlint::Csvw::NumberFormat.new("#0.0#")
270
+ expect(format.parse("1")).to eq(nil)
271
+ expect(format.parse("12")).to eq(nil)
272
+ expect(format.parse("12.3")).to eql(12.3)
273
+ expect(format.parse("12.34")).to eql(12.34)
274
+ expect(format.parse("12.345")).to eq(nil)
275
+ end
276
+
277
+ it "should parse numbers that match #0.0#,# correctly" do
278
+ format = Csvlint::Csvw::NumberFormat.new("#0.0#,#")
279
+ expect(format.parse("1")).to eq(nil)
280
+ expect(format.parse("12.3")).to eql(12.3)
281
+ expect(format.parse("12.34")).to eql(12.34)
282
+ expect(format.parse("12.345")).to eq(nil)
283
+ expect(format.parse("12.34,5")).to eql(12.345)
284
+ expect(format.parse("12.34,56")).to eql(nil)
285
+ expect(format.parse("12.34,567")).to eq(nil)
286
+ expect(format.parse("12.34,56,7")).to eql(nil)
287
+ end
288
+
289
+ it "should parse numbers that match 0.0##,### correctly" do
290
+ format = Csvlint::Csvw::NumberFormat.new("0.0##,###")
291
+ expect(format.parse("1")).to eq(nil)
292
+ expect(format.parse("12.3")).to eql(12.3)
293
+ expect(format.parse("12.34")).to eql(12.34)
294
+ expect(format.parse("12.345")).to eq(12.345)
295
+ expect(format.parse("12.3456")).to eql(nil)
296
+ expect(format.parse("12.345,6")).to eql(12.3456)
297
+ expect(format.parse("12.34,56")).to eql(nil)
298
+ expect(format.parse("12.345,67")).to eq(12.34567)
299
+ expect(format.parse("12.345,678")).to eql(12.345678)
300
+ expect(format.parse("12.345,67,8")).to eql(nil)
301
+ end
302
+
303
+ it "should parse numbers that match 0.###,### correctly" do
304
+ format = Csvlint::Csvw::NumberFormat.new("0.###,###")
305
+ expect(format.parse("1")).to eq(1)
306
+ expect(format.parse("12.3")).to eql(12.3)
307
+ expect(format.parse("12.34")).to eql(12.34)
308
+ expect(format.parse("12.345")).to eq(12.345)
309
+ expect(format.parse("12.3456")).to eql(nil)
310
+ expect(format.parse("12.345,6")).to eql(12.3456)
311
+ expect(format.parse("12.34,56")).to eql(nil)
312
+ expect(format.parse("12.345,67")).to eq(12.34567)
313
+ expect(format.parse("12.345,678")).to eql(12.345678)
314
+ expect(format.parse("12.345,67,8")).to eql(nil)
315
+ end
316
+
317
+ it "should parse numbers that match 0.000,### correctly" do
318
+ format = Csvlint::Csvw::NumberFormat.new("0.000,###")
319
+ expect(format.parse("1")).to eq(nil)
320
+ expect(format.parse("12.3")).to eql(nil)
321
+ expect(format.parse("12.34")).to eql(nil)
322
+ expect(format.parse("12.345")).to eq(12.345)
323
+ expect(format.parse("12.3456")).to eql(nil)
324
+ expect(format.parse("12.345,6")).to eql(12.3456)
325
+ expect(format.parse("12.34,56")).to eql(nil)
326
+ expect(format.parse("12.345,67")).to eq(12.34567)
327
+ expect(format.parse("12.345,678")).to eql(12.345678)
328
+ expect(format.parse("12.345,67,8")).to eql(nil)
329
+ end
330
+
331
+ it "should parse numbers that match 0.000,0# correctly" do
332
+ format = Csvlint::Csvw::NumberFormat.new("0.000,0#")
333
+ expect(format.parse("1")).to eq(nil)
334
+ expect(format.parse("12.3")).to eql(nil)
335
+ expect(format.parse("12.34")).to eql(nil)
336
+ expect(format.parse("12.345")).to eq(nil)
337
+ expect(format.parse("12.3456")).to eql(nil)
338
+ expect(format.parse("12.345,6")).to eql(12.3456)
339
+ expect(format.parse("12.34,56")).to eql(nil)
340
+ expect(format.parse("12.345,67")).to eq(12.34567)
341
+ expect(format.parse("12.345,678")).to eql(nil)
342
+ expect(format.parse("12.345,67,8")).to eql(nil)
343
+ end
344
+
345
+ it "should parse numbers that match 0.000,0## correctly" do
346
+ format = Csvlint::Csvw::NumberFormat.new("0.000,0##")
347
+ expect(format.parse("1")).to eq(nil)
348
+ expect(format.parse("12.3")).to eql(nil)
349
+ expect(format.parse("12.34")).to eql(nil)
350
+ expect(format.parse("12.345")).to eq(nil)
351
+ expect(format.parse("12.3456")).to eql(nil)
352
+ expect(format.parse("12.345,6")).to eql(12.3456)
353
+ expect(format.parse("12.34,56")).to eql(nil)
354
+ expect(format.parse("12.345,67")).to eq(12.34567)
355
+ expect(format.parse("12.345,678")).to eql(12.345678)
356
+ expect(format.parse("12.345,67,8")).to eql(nil)
357
+ end
358
+
359
+ it "should parse numbers that match 0.000,000 correctly" do
360
+ format = Csvlint::Csvw::NumberFormat.new("0.000,000")
361
+ expect(format.parse("1")).to eq(nil)
362
+ expect(format.parse("12.3")).to eql(nil)
363
+ expect(format.parse("12.34")).to eql(nil)
364
+ expect(format.parse("12.345")).to eq(nil)
365
+ expect(format.parse("12.3456")).to eql(nil)
366
+ expect(format.parse("12.345,6")).to eql(nil)
367
+ expect(format.parse("12.34,56")).to eql(nil)
368
+ expect(format.parse("12.345,67")).to eq(nil)
369
+ expect(format.parse("12.345,678")).to eql(12.345678)
370
+ expect(format.parse("12.345,67,8")).to eql(nil)
371
+ end
372
+
373
+ it "should parse numbers that match #0.###E#0 correctly" do
374
+ format = Csvlint::Csvw::NumberFormat.new("#0.###E#0")
375
+ expect(format.parse("1")).to eq(nil)
376
+ expect(format.parse("12.3")).to eq(nil)
377
+ expect(format.parse("12.34")).to eq(nil)
378
+ expect(format.parse("12.3E4")).to eql(12.3E4)
379
+ expect(format.parse("12.3E45")).to eql(12.3E45)
380
+ expect(format.parse("12.34E5")).to eql(12.34E5)
381
+ end
382
+
383
+ it "should parse numbers normally when there is no pattern" do
384
+ format = Csvlint::Csvw::NumberFormat.new()
385
+ expect(format.parse("1")).to eql(1)
386
+ expect(format.parse("-1")).to eql(-1)
387
+ expect(format.parse("12.3")).to eql(12.3)
388
+ expect(format.parse("12.34")).to eql(12.34)
389
+ expect(format.parse("12.3E4")).to eql(12.3E4)
390
+ expect(format.parse("12.3E45")).to eql(12.3E45)
391
+ expect(format.parse("12.34E5")).to eql(12.34E5)
392
+ expect(format.parse("12.34e5")).to eql(12.34E5)
393
+ expect(format.parse("-12.34")).to eql(-12.34)
394
+ expect(format.parse("1,234")).to eq(nil)
395
+ expect(format.parse("NaN").nan?).to eq(true)
396
+ expect(format.parse("INF")).to eql(Float::INFINITY)
397
+ expect(format.parse("-INF")).to eql(-Float::INFINITY)
398
+ expect(format.parse("123456.789F10")).to eq(nil)
399
+ end
400
+
401
+ it "should parse numbers including grouping separators when they are specified" do
402
+ format = Csvlint::Csvw::NumberFormat.new(nil, ",")
403
+ expect(format.parse("1")).to eql(1)
404
+ expect(format.parse("12.3")).to eql(12.3)
405
+ expect(format.parse("12.34")).to eql(12.34)
406
+ expect(format.parse("12.3E4")).to eql(12.3E4)
407
+ expect(format.parse("12.3E45")).to eql(12.3E45)
408
+ expect(format.parse("12.34E5")).to eql(12.34E5)
409
+ expect(format.parse("1,234")).to eql(1234)
410
+ expect(format.parse("1,234,567")).to eql(1234567)
411
+ expect(format.parse("1,,234")).to eq(nil)
412
+ expect(format.parse("NaN").nan?).to eq(true)
413
+ expect(format.parse("INF")).to eql(Float::INFINITY)
414
+ expect(format.parse("-INF")).to eql(-Float::INFINITY)
415
+ end
416
+
417
+ end