- Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathinclude_directive_spec.rb
94 lines (78 loc) · 3.15 KB
/
include_directive_spec.rb
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
require'spec_helper'
require'jsonapi/include_directive'
describeJSONAPI::IncludeDirective,'.initialize'do
context'raises InvalidKey when'do
(
["\u002B","\u002C","\u002E","\u005B","\u005D","\u002A","\u002F",
"\u0040","\u005C","\u005E","\u0060"] + ("\u0021".."\u0029").to_a \
+ ("\u003A".."\u003F").to_a \
+ ("\u007B".."\u007F").to_a \
+ ("\u0000".."\u001F").to_a \
- ['*','.',',']
).eachdo |invalid_character|
it"invalid character provided: '#{invalid_character}'"do
expect{JSONAPI::IncludeDirective.new(invalid_character)}
.toraise_error(JSONAPI::IncludeDirective::InvalidKey)
end
end
[' ','_','-'].eachdo |char|
it"starts with following character: '#{char}'"do
expect{JSONAPI::IncludeDirective.new("#{char}_with_valid")}
.toraise_error(JSONAPI::IncludeDirective::InvalidKey,"#{char}_with_valid")
end
it"ends with following character: '#{char}'"do
expect{JSONAPI::IncludeDirective.new("valid_with_#{char}")}
.toraise_error(JSONAPI::IncludeDirective::InvalidKey,"valid_with_#{char}")
end
end
end
context'does not raise InvalidKey'do
["\u0080","B","t","5","\u0100","\u10FFFAA"].eachdo |char|
it"when provided character '#{char}'"do
expect(JSONAPI::IncludeDirective.new(char).key?(char)).tobetrue
end
end
[' ','_','-'].eachdo |char|
it"when '#{char}' is not at beginning or end"do
key="valid#{char}valid"
expect(JSONAPI::IncludeDirective.new(key).key?(key)).tobetrue
end
end
end
end
describeJSONAPI::IncludeDirective,'.key?'do
it'handles existing keys'do
str='posts.comments'
include_directive=JSONAPI::IncludeDirective.new(str)
expect(include_directive.key?(:posts)).tobe_truthy
end
it'handles absent keys'do
str='posts.comments'
include_directive=JSONAPI::IncludeDirective.new(str)
expect(include_directive.key?(:author)).tobe_falsy
end
it'handles wildcards'do
str='posts.*'
include_directive=JSONAPI::IncludeDirective.new(
str,allow_wildcard: true)
expect(include_directive[:posts].key?(:author)).tobe_truthy
expect(include_directive[:posts][:author].key?(:comments)).tobe_falsy
end
it'handles wildcards'do
str='posts.**'
include_directive=JSONAPI::IncludeDirective.new(
str,allow_wildcard: true)
expect(include_directive[:posts].key?(:author)).tobe_truthy
expect(include_directive[:posts][:author].key?(:comments)).tobe_truthy
end
end
describeJSONAPI::IncludeDirective,'.to_string'do
it'works'do
str='friends,comments.author,posts.author,posts.comments.author'
include_directive=JSONAPI::IncludeDirective.new(str)
expected=include_directive.to_hash
actual=JSONAPI::IncludeDirective.new(include_directive.to_string)
.to_hash
expect(actual).toeqexpected
end
end