-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow to ignore invalid fields #28
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
module Vcard | ||
class Configuration | ||
|
||
attr_accessor :raise_on_invalid_line | ||
alias_method :raise_on_invalid_line?, :raise_on_invalid_line | ||
|
||
attr_accessor :ignore_invalid_vcards | ||
alias_method :ignore_invalid_vcards?, :ignore_invalid_vcards | ||
|
||
def initialize | ||
@raise_on_invalid = true | ||
@ignore_invalid_vcard = true | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,31 +21,31 @@ def test_encode_decode_text() | |
def test_field4 | ||
line = "t;e=a,b: 4 " | ||
part = Field.decode0(line) | ||
assert_equal("4", part[ 3 ]) | ||
assert_equal("4", part[ 4 ]) | ||
end | ||
|
||
def test_field3 | ||
line = "t;e=a,b:4" | ||
part = Field.decode0(line) | ||
assert_equal("4", part[ 3 ]) | ||
assert_equal( {"E" => [ "a","b" ] }, part[ 2 ]) | ||
assert_equal("4", part[ 4 ]) | ||
assert_equal( {"E" => [ "a","b" ] }, part[ 3 ]) | ||
end | ||
|
||
def test_field2 | ||
line = "tel;type=work,voice,msg:+1 313 747-4454" | ||
part = Field.decode0(line) | ||
assert_equal("+1 313 747-4454", part[ 3 ]) | ||
assert_equal( {"TYPE" => [ "work","voice","msg" ] }, part[ 2 ]) | ||
assert_equal("+1 313 747-4454", part[ 4 ]) | ||
assert_equal( {"TYPE" => [ "work","voice","msg" ] }, part[ 3 ]) | ||
end | ||
|
||
def test_field1 | ||
line = 'ORGANIZER;CN="xxxx, xxxx [SC100:370:EXCH]":MAILTO:[email protected]' | ||
parts = Field.decode0(line) | ||
|
||
assert_equal(nil, parts[0]) | ||
assert_equal("ORGANIZER", parts[1]) | ||
assert_equal({ "CN" => [ "xxxx, xxxx [SC100:370:EXCH]" ] }, parts[2]) | ||
assert_equal("MAILTO:[email protected]", parts[3]) | ||
assert_equal(nil, parts[1]) | ||
assert_equal("ORGANIZER", parts[2]) | ||
assert_equal({ "CN" => [ "xxxx, xxxx [SC100:370:EXCH]" ] }, parts[3]) | ||
assert_equal("MAILTO:[email protected]", parts[4]) | ||
end | ||
|
||
=begin this can not be done :-( | ||
|
@@ -67,20 +67,21 @@ def test_case_equiv | |
|
||
def test_field0 | ||
assert_equal("name:", line = Field.encode0(nil, "name")) | ||
assert_equal([ nil, "NAME", {}, ""], Field.decode0(line)) | ||
assert_equal([ true, nil, "NAME", {}, ""], Field.decode0(line)) | ||
|
||
assert_equal("name:value", line = Field.encode0(nil, "name", {}, "value")) | ||
assert_equal([ nil, "NAME", {}, "value"], Field.decode0(line)) | ||
assert_equal([ true, nil, "NAME", {}, "value"], Field.decode0(line)) | ||
|
||
assert_equal("name;encoding=B:dmFsdWU=", line = Field.encode0(nil, "name", { "encoding"=>:b64 }, "value")) | ||
assert_equal([ nil, "NAME", { "ENCODING"=>["B"]}, ["value"].pack("m").chomp ], Field.decode0(line)) | ||
assert_equal([ true, nil, "NAME", { "ENCODING"=>["B"]}, ["value"].pack("m").chomp ], Field.decode0(line)) | ||
|
||
line = Field.encode0("group", "name", {}, "value") | ||
assert_equal "group.name:value", line | ||
assert_equal [ "GROUP", "NAME", {}, "value"], Field.decode0(line) | ||
assert_equal [ true, "GROUP", "NAME", {}, "value"], Field.decode0(line) | ||
end | ||
|
||
def test_invalid_fields | ||
def test_invalid_fields_wih_raise_error | ||
Vcard::configuration.raise_on_invalid_line = true | ||
[ | ||
"g.:", | ||
":v", | ||
|
@@ -89,6 +90,16 @@ def test_invalid_fields | |
end | ||
end | ||
|
||
def test_invalid_fields_wihout_raise_error | ||
Vcard.configuration.raise_on_invalid_line = false | ||
[ | ||
"g.:", | ||
":v", | ||
].each do |line| | ||
assert_nothing_raised { Field.decode0(line) } | ||
end | ||
end | ||
|
||
def test_date_encode | ||
assert_equal("DTSTART:20040101\n", Field.create("DTSTART", Date.new(2004, 1, 1) ).to_s) | ||
assert_equal("DTSTART:20040101\n", Field.create("DTSTART", [Date.new(2004, 1, 1)]).to_s) | ||
|
@@ -97,6 +108,7 @@ def test_date_encode | |
def test_field_modify | ||
f = Field.create("name") | ||
|
||
|
||
assert_equal("", f.value) | ||
f.value = "" | ||
assert_equal("", f.value) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -169,12 +169,50 @@ def _test_cons # FIXME | |
end | ||
|
||
def test_bad | ||
# FIXME: this should THROW, it's badly encoded! | ||
Vcard::configuration.raise_on_invalid_line = true | ||
assert_raises(::Vcard::InvalidEncodingError) do | ||
Vcard::Vcard.decode("BEGIN:VCARD\nVERSION:3.0\nKEYencoding=b:this could be \nmy certificate\n\nEND:VCARD\n") | ||
end | ||
end | ||
|
||
def test_not_raise_error_if_configured_to_ignore | ||
Vcard::configuration.raise_on_invalid_line = false | ||
Vcard::configuration.ignore_invalid_vcards = false | ||
assert_nothing_raised do | ||
Vcard::Vcard.decode("BEGIN:VCARD\nVERSION:3.0\nKEYencoding=b:this could be \nmy certificate\n\nEND:VCARD\n") | ||
end | ||
end | ||
|
||
def test_ignore_vcards_with_invalid_fields | ||
Vcard::configuration.raise_on_invalid_line = false | ||
Vcard::configuration.ignore_invalid_vcards = true | ||
src = <<'EOF' | ||
BEGIN:VCARD | ||
VERSION:3.0 | ||
KEYencoding=b:this could be | ||
my certificate | ||
EMAIL:[email protected] | ||
END:VCARD | ||
BEGIN:VCARD | ||
VERSION:3.0 | ||
EMAIL:[email protected] | ||
END:VCARD | ||
EOF | ||
|
||
cards = Vcard::Vcard.decode(src) | ||
assert_equal 1, cards.size | ||
end | ||
|
||
def test_ignore_only_invalid_fields | ||
Vcard::configuration.raise_on_invalid_line = false | ||
Vcard::configuration.ignore_invalid_vcards = false | ||
email = '[email protected]' | ||
cards = Vcard::Vcard.decode("BEGIN:VCARD\nVERSION:3.0\nKEYencoding=b:this could be \nmy certificate\nEMAIL:#{email}\n\nEND:VCARD\n") | ||
assert_equal email, cards.first.email | ||
# [BEGIN, VERSION, EMAIL, END].size == 4 | ||
assert_equal 4, cards.first.fields.size | ||
end | ||
|
||
def test_create | ||
card = Vcard::Vcard.create | ||
key = Vcard::DirectoryInfo.decode("key;type=x509;encoding=B:dGhpcyBjb3VsZCBiZSAKbXkgY2VydGlmaWNhdGUK\n")['key'] | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should there be a condition on this line regarding whether or not to raise?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't change behavior in this place. It looks for me like a strict type check and it's not clear for me why it needed here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No worries :)