source2swagger 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/.travis.yml +6 -0
- data/Gemfile +2 -0
- data/LICENCE +22 -0
- data/README.md +189 -0
- data/Rakefile +10 -0
- data/bin/source2swagger +84 -0
- data/lib/swagger_hash.rb +70 -0
- data/lib/swagger_reader.rb +113 -0
- data/source2swagger.gemspec +20 -0
- data/test/data/sample1.json +27 -0
- data/test/data/sample1.rb +18 -0
- data/test/data/sample2.json +86 -0
- data/test/data/sample2.rb +42 -0
- data/test/data/sample3.json +221 -0
- data/test/data/sample3.rb +156 -0
- data/test/data/sample4.rb +44 -0
- data/test/data/sample5.rb +46 -0
- data/test/data/sample_bad1.rb +20 -0
- data/test/data/sample_bad2.rb +16 -0
- data/test/data/sample_bad3.rb +44 -0
- data/test/test_helper.rb +10 -0
- data/test/test_helpers/compare_hashes.rb +57 -0
- data/test/unit/swagger_hash_test.rb +177 -0
- data/test/unit/swagger_reader_test.rb +181 -0
- metadata +98 -0
@@ -0,0 +1,44 @@
|
|
1
|
+
##~ sapi = source2swagger.namespace("sentiment")
|
2
|
+
##~ sapi.basePath = "http://helloworld.3scale.net"
|
3
|
+
##~ sapi.swagrVersion = "0.1a"
|
4
|
+
##~ sapi.apiVersion = "1.0"
|
5
|
+
|
6
|
+
##~ sapi = source2swagger.namespace("sentiment")
|
7
|
+
##~ a = sapi.apis.add
|
8
|
+
##
|
9
|
+
##~ a.set :path => "/word/{word}", :format => "json"
|
10
|
+
##~ a.description = "Access to the sentiment of a given word"
|
11
|
+
##
|
12
|
+
## declaring errors
|
13
|
+
##~ @error_sanitize = {:reason => "failure to sanitize: \"input\"", :code => 422}
|
14
|
+
##~ @error_api_down = {:reason => "API down", :code => 500}
|
15
|
+
|
16
|
+
##
|
17
|
+
##~ a.errorResponses.add @error_sanitize
|
18
|
+
##~ a.errorResponses.add @error_api_down
|
19
|
+
##~ a.errorResponses.add :reason => "failure to sanitize: \"input\", returns empty set", :code => 422
|
20
|
+
##~ a.errorResponses.add :reason => "access denied, either your access credentials are incorrect or you are about the limits of your quota", :code => 403
|
21
|
+
##~
|
22
|
+
##~ op = a.operations.add
|
23
|
+
##~ op.set :httpMethod => "GET", :tags => ["production"], :nickname => "get_word", :deprecated => false
|
24
|
+
##~ op.summary = "Returns the sentiment values of a given word"
|
25
|
+
##~ op.parameters.add :name => "word", :description => "The word whose sentiment is returned", :dataType => "string", :allowMultiple => false, :required => true, :paramType => "path"
|
26
|
+
##~ op.parameters.add :name => "app_id", :description => "Your access application id", :dataType => "string", :allowMultiple => false, :required => true, :paramType => "query"
|
27
|
+
##~ op.parameters.add :name => "app_key", :description => "Your access application key", :dataType => "string", :allowMultiple => false, :required => false, :paramType => "query"
|
28
|
+
##
|
29
|
+
##~ sapi = source2swagger.namespace("sentiment")
|
30
|
+
##
|
31
|
+
##~ a = sapi.apis.add
|
32
|
+
##
|
33
|
+
##~ a.set :path => "/ping", :format => "text"
|
34
|
+
##~ a.description = "Check the status to see if it's up and running"
|
35
|
+
##
|
36
|
+
## declaring errors
|
37
|
+
##
|
38
|
+
##~ a.errorResponses.add @error_api_down
|
39
|
+
##
|
40
|
+
##~ op = a.operations.add
|
41
|
+
##~ op.set :httpMethod => "GET", :tags => ["test"], :nickname => "ping", :deprecated => true
|
42
|
+
##~ op.summary = "This operation is DEPRECATED. It returns the string \"that's getting old... pong \" if the API is up and running"
|
43
|
+
##
|
44
|
+
|
@@ -0,0 +1,46 @@
|
|
1
|
+
##~ sapi = source2swagger.namespace("sentiment")
|
2
|
+
##~ sapi.basePath = "http://helloworld.3scale.net"
|
3
|
+
##~ sapi.swagrVersion = "0.1a"
|
4
|
+
##~ sapi.apiVersion = "1.0"
|
5
|
+
|
6
|
+
##~ sapi = source2swagger.namespace("sentiment")
|
7
|
+
##~ a = sapi.apis.add
|
8
|
+
##
|
9
|
+
##~ a.set :path => "/word/{word}", :format => "json"
|
10
|
+
##~ a.description = "Access to the sentiment of a given word"
|
11
|
+
##
|
12
|
+
## REMOVING THE DECLARATION ON @error_sanitize and @error_api_dow TO THE END OF THE FILE
|
13
|
+
## COULD ALSO BE A DIFFERENT FILE
|
14
|
+
##
|
15
|
+
##~ a.errorResponses.add @error_sanitize
|
16
|
+
##~ a.errorResponses.add @error_api_down
|
17
|
+
##~ a.errorResponses.add :reason => "failure to sanitize: \"input\", returns empty set", :code => 422
|
18
|
+
##~ a.errorResponses.add :reason => "access denied, either your access credentials are incorrect or you are about the limits of your quota", :code => 403
|
19
|
+
##~
|
20
|
+
##~ op = a.operations.add
|
21
|
+
##~ op.set :httpMethod => "GET", :tags => ["production"], :nickname => "get_word", :deprecated => false
|
22
|
+
##~ op.summary = "Returns the sentiment values of a given word"
|
23
|
+
##~ op.parameters.add :name => "word", :description => "The word whose sentiment is returned", :dataType => "string", :allowMultiple => false, :required => true, :paramType => "path"
|
24
|
+
##~ op.parameters.add :name => "app_id", :description => "Your access application id", :dataType => "string", :allowMultiple => false, :required => true, :paramType => "query"
|
25
|
+
##~ op.parameters.add :name => "app_key", :description => "Your access application key", :dataType => "string", :allowMultiple => false, :required => false, :paramType => "query"
|
26
|
+
##
|
27
|
+
##~ sapi = source2swagger.namespace("sentiment")
|
28
|
+
##
|
29
|
+
##~ a = sapi.apis.add
|
30
|
+
##
|
31
|
+
##~ a.set :path => "/ping", :format => "text"
|
32
|
+
##~ a.description = "Check the status to see if it's up and running"
|
33
|
+
##
|
34
|
+
## declaring errors
|
35
|
+
##
|
36
|
+
##~ a.errorResponses.add @error_api_down
|
37
|
+
##
|
38
|
+
##~ op = a.operations.add
|
39
|
+
##~ op.set :httpMethod => "GET", :tags => ["test"], :nickname => "ping", :deprecated => true
|
40
|
+
##~ op.summary = "This operation is DEPRECATED. It returns the string \"that's getting old... pong \" if the API is up and running"
|
41
|
+
##
|
42
|
+
## MOVED VAR DECLARATION
|
43
|
+
##~ @error_sanitize = {:reason => "failure to sanitize: \"input\"", :code => 422}
|
44
|
+
##~ @error_api_down = {:reason => "API down", :code => 500}
|
45
|
+
|
46
|
+
|
@@ -0,0 +1,20 @@
|
|
1
|
+
##~ sapi = source2swagger.namespace("sentiment")
|
2
|
+
##~ sapi.basePath = "http://helloworld.3scale.net"
|
3
|
+
##~ sapi.swagrVersion = "0.1a"
|
4
|
+
##~ sapi.apiVersion = "1.0"
|
5
|
+
##
|
6
|
+
##~ a = sapi.apis.add
|
7
|
+
##
|
8
|
+
##~ a.set :path => "/ping", :format => "text"
|
9
|
+
##~ a.description = "Check the status to see if it's up and running"
|
10
|
+
##
|
11
|
+
## declaring errors
|
12
|
+
##
|
13
|
+
##~ a.errorResponses.add :reason => "API down", :code => 500
|
14
|
+
##
|
15
|
+
## end CANNOT BE USED AS VARIABLE SINCE IT'S A RUBY RESERVED WORD
|
16
|
+
##
|
17
|
+
##~ end = a.operations.add
|
18
|
+
##~ end.set :httpMethod => "GET", :tags => ["test"], :nickname => "ping", :deprecated => true
|
19
|
+
##~ end.summary = "This operation is DEPRECATED. It returns the string \"that's getting old... pong \" if the API is up and running"
|
20
|
+
##
|
@@ -0,0 +1,16 @@
|
|
1
|
+
##~ sapi = source2swagger.namespace("sentiment")
|
2
|
+
##~ sapi.basePath = "http://helloworld.3scale.net"
|
3
|
+
##~ sapi.swagrVersion = "0.1a"
|
4
|
+
##~ sapi.apiVersion = "1.0"
|
5
|
+
##
|
6
|
+
##~ a = sapi.apis.add
|
7
|
+
##
|
8
|
+
##~ a.set :path => "/ping", :format => "text"
|
9
|
+
##~ a.description = Check the status to see if it's up and running
|
10
|
+
##
|
11
|
+
## THE STRING ABOVE IS NOT WITHIN QUOTES
|
12
|
+
## declaring errors
|
13
|
+
##
|
14
|
+
##~ a.errorResponses.add :reason => "API down", :code => 500
|
15
|
+
##
|
16
|
+
|
@@ -0,0 +1,44 @@
|
|
1
|
+
##~ sapi = source2swagger.namespace("sentiment")
|
2
|
+
##~ sapi.basePath = "http://helloworld.3scale.net"
|
3
|
+
##~ sapi.swagrVersion = "0.1a"
|
4
|
+
##~ sapi.apiVersion = "1.0"
|
5
|
+
|
6
|
+
##~ sapi = source2swagger.namespace("sentiment")
|
7
|
+
##~ a = sapi.apis.add
|
8
|
+
##
|
9
|
+
##~ a.set :path => "/word/{word}", :format => "json"
|
10
|
+
##~ a.description = "Access to the sentiment of a given word"
|
11
|
+
##
|
12
|
+
## declaring errors
|
13
|
+
##~ @error_sanitize = {:reason => "failure to sanitize: \"input\"", :code => 422}
|
14
|
+
##~ @error_api_down = {:reason => "API down", :code => 500}
|
15
|
+
|
16
|
+
##
|
17
|
+
##~ a.errorResponses.add @error_sanitize_NOT_YET_DEFINED
|
18
|
+
##~ a.errorResponses.add @error_api_down
|
19
|
+
##~ a.errorResponses.add :reason => "failure to sanitize: \"input\", returns empty set", :code => 422
|
20
|
+
##~ a.errorResponses.add :reason => "access denied, either your access credentials are incorrect or you are about the limits of your quota", :code => 403
|
21
|
+
##~
|
22
|
+
##~ op = a.operations.add
|
23
|
+
##~ op.set :httpMethod => "GET", :tags => ["production"], :nickname => "get_word", :deprecated => false
|
24
|
+
##~ op.summary = "Returns the sentiment values of a given word"
|
25
|
+
##~ op.parameters.add :name => "word", :description => "The word whose sentiment is returned", :dataType => "string", :allowMultiple => false, :required => true, :paramType => "path"
|
26
|
+
##~ op.parameters.add :name => "app_id", :description => "Your access application id", :dataType => "string", :allowMultiple => false, :required => true, :paramType => "query"
|
27
|
+
##~ op.parameters.add :name => "app_key", :description => "Your access application key", :dataType => "string", :allowMultiple => false, :required => false, :paramType => "query"
|
28
|
+
##
|
29
|
+
##~ sapi = source2swagger.namespace("sentiment")
|
30
|
+
##
|
31
|
+
##~ a = sapi.apis.add
|
32
|
+
##
|
33
|
+
##~ a.set :path => "/ping", :format => "text"
|
34
|
+
##~ a.description = "Check the status to see if it's up and running"
|
35
|
+
##
|
36
|
+
## declaring errors
|
37
|
+
##
|
38
|
+
##~ a.errorResponses.add @error_api_down
|
39
|
+
##
|
40
|
+
##~ op = a.operations.add
|
41
|
+
##~ op.set :httpMethod => "GET", :tags => ["test"], :nickname => "ping", :deprecated => true
|
42
|
+
##~ op.summary = "This operation is DEPRECATED. It returns the string \"that's getting old... pong \" if the API is up and running"
|
43
|
+
##
|
44
|
+
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
module TestHelpers
|
2
|
+
module CompareHashes
|
3
|
+
|
4
|
+
def paths(path,h)
|
5
|
+
|
6
|
+
if h.class==Hash
|
7
|
+
v = []
|
8
|
+
h.each do |k,val|
|
9
|
+
v << paths("#{path}/#{k}",val)
|
10
|
+
end
|
11
|
+
return v.flatten
|
12
|
+
else
|
13
|
+
if h.class==Array
|
14
|
+
v = []
|
15
|
+
if h.size>0
|
16
|
+
h.each do |item|
|
17
|
+
v << paths("#{path}/[]",item)
|
18
|
+
end
|
19
|
+
else
|
20
|
+
v << ["#{path}/[]"]
|
21
|
+
end
|
22
|
+
|
23
|
+
return v.flatten
|
24
|
+
else
|
25
|
+
return ["#{path}/#{h}:(#{h.class})"]
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def diff(h1, h2)
|
31
|
+
|
32
|
+
v1 = paths("",h1)
|
33
|
+
v2 = paths("",h2)
|
34
|
+
|
35
|
+
return [] if v1.size==v2.size && v1-v2 == []
|
36
|
+
|
37
|
+
v1 = v1.sort
|
38
|
+
v2 = v2.sort
|
39
|
+
|
40
|
+
num = v1.size
|
41
|
+
num = v2.size if v2.size < num
|
42
|
+
|
43
|
+
v1.size.times do |i|
|
44
|
+
if v1[i]!=v2[i]
|
45
|
+
return [v1[i], v2[i]]
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
return [nil, nil]
|
50
|
+
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
Test::Unit::TestCase.send(:include, TestHelpers::CompareHashes)
|
56
|
+
|
57
|
+
|
@@ -0,0 +1,177 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
|
2
|
+
|
3
|
+
class SwaggerHashTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
|
6
|
+
def test_basics
|
7
|
+
h = SwaggerHash.new
|
8
|
+
|
9
|
+
h.o = 1
|
10
|
+
assert_equal h, {:o => 1}
|
11
|
+
|
12
|
+
h.u.u.u = {:y => "1"}
|
13
|
+
assert_equal h, {:o => 1, :u => {:u => {:u => {:y => "1"}}}}
|
14
|
+
|
15
|
+
h.x
|
16
|
+
assert_equal h, {:o => 1, :u => {:u => {:u => {:y => "1"}}}, :x => {}}
|
17
|
+
|
18
|
+
h.x = []
|
19
|
+
assert_equal h, {:o => 1, :u => {:u => {:u => {:y => "1"}}}, :x => []}
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_basics_add
|
24
|
+
|
25
|
+
h = SwaggerHash.new
|
26
|
+
|
27
|
+
v = h.x.y.z.add
|
28
|
+
assert_equal v, {}
|
29
|
+
assert_equal h, {:x => {:y => {:z => {:_array => [{}]}}}}
|
30
|
+
|
31
|
+
v.a = 10
|
32
|
+
assert_equal h, {:x => {:y => {:z => {:_array =>[{:a => 10}]}}}}
|
33
|
+
|
34
|
+
v.b.b = "5"
|
35
|
+
assert_equal h, {:x => {:y => {:z => {:_array =>[{:a => 10, :b => {:b => "5"}}]}}}}
|
36
|
+
|
37
|
+
v = h.x.y.z.add
|
38
|
+
assert_equal h, {:x => {:y => {:z => {:_array =>[{:a => 10, :b => {:b => "5"}},{}]}}}}
|
39
|
+
|
40
|
+
v.c.c = "3"
|
41
|
+
assert_equal h, {:x => {:y => {:z => {:_array =>[{:a => 10, :b => {:b => "5"}}, {:c => {:c => "3"}}]}}}}
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_basics_add_params
|
46
|
+
|
47
|
+
h = SwaggerHash.new
|
48
|
+
|
49
|
+
h.z.add :a => 1
|
50
|
+
h.z.add :a => 1
|
51
|
+
|
52
|
+
assert_equal h.to_hash, {:z => [{:a => 1}, {:a => 1}]}
|
53
|
+
|
54
|
+
h = SwaggerHash.new
|
55
|
+
|
56
|
+
h.z.add :a => 1, :b => 2, :c => 3
|
57
|
+
h.z.add :a => 1, :b => 2, :c => 3
|
58
|
+
|
59
|
+
assert_equal h.to_hash, {:z => [{:a => 1, :b => 2, :c => 3}, {:a => 1, :b => 2, :c => 3}]}
|
60
|
+
|
61
|
+
h = SwaggerHash.new
|
62
|
+
|
63
|
+
h.z.add :a => 1, :b => 2, :c => 3
|
64
|
+
h.z.add :a => 1, :b => 2, :c => 3
|
65
|
+
i = h.z.add
|
66
|
+
i.bs = "k"
|
67
|
+
|
68
|
+
assert_equal h.to_hash, {:z => [{:a => 1, :b => 2, :c => 3}, {:a => 1, :b => 2, :c => 3}, {:bs => "k"}]}
|
69
|
+
|
70
|
+
end
|
71
|
+
|
72
|
+
def test_to_hash
|
73
|
+
|
74
|
+
h = SwaggerHash.new
|
75
|
+
assert_equal h.to_hash, {}
|
76
|
+
|
77
|
+
h = SwaggerHash.new
|
78
|
+
a = h.x.y.add
|
79
|
+
a.a1 = "1"
|
80
|
+
a.a2 = "2"
|
81
|
+
|
82
|
+
a = h.x.y.add
|
83
|
+
a.a1 = "1"
|
84
|
+
|
85
|
+
a = h.y.add
|
86
|
+
|
87
|
+
a.set :bs => "k", :bs2 => "k2"
|
88
|
+
|
89
|
+
a.bs3 = "k3"
|
90
|
+
|
91
|
+
assert_equal h.to_hash, {:x => {:y => [{:a1 => "1", :a2 => "2"}, {:a1 => "1"}]}, :y => [{:bs => "k", :bs2 => "k2", :bs3 => "k3"}]}
|
92
|
+
|
93
|
+
end
|
94
|
+
|
95
|
+
|
96
|
+
def test_load_same_api_space
|
97
|
+
|
98
|
+
source2swagger = SwaggerHash.new
|
99
|
+
|
100
|
+
a = source2swagger.namespace("namespace1")
|
101
|
+
a.bla = 10
|
102
|
+
a.foo = "100"
|
103
|
+
|
104
|
+
a = source2swagger.namespace("namespace2")
|
105
|
+
assert_equal a.to_hash, {}
|
106
|
+
|
107
|
+
a = source2swagger.namespace("namespace1")
|
108
|
+
assert_equal a.to_hash, {:bla => 10, :foo => "100"}
|
109
|
+
|
110
|
+
a.bar.add :a => 1, :b => 2
|
111
|
+
assert_equal a.to_hash, {:bla => 10, :foo => "100", :bar => [{:a => 1, :b => 2}]}
|
112
|
+
|
113
|
+
a = source2swagger.namespace("namespace2")
|
114
|
+
assert_equal a.to_hash, {}
|
115
|
+
|
116
|
+
a = source2swagger.namespace("namespace1")
|
117
|
+
assert_equal a.to_hash, {:bla => 10, :foo => "100", :bar => [{:a => 1, :b => 2}]}
|
118
|
+
|
119
|
+
end
|
120
|
+
|
121
|
+
def test_save
|
122
|
+
|
123
|
+
source2swagger = SwaggerHash.new
|
124
|
+
|
125
|
+
a = source2swagger.namespace("namespace1")
|
126
|
+
a.bla = 10
|
127
|
+
a.foo = "100"
|
128
|
+
|
129
|
+
b = source2swagger.namespace("namespace2")
|
130
|
+
b.foo.bar = 42
|
131
|
+
|
132
|
+
assert_equal source2swagger.namespace("namespace1").to_hash, {:bla => 10, :foo => "100"}
|
133
|
+
assert_equal source2swagger.namespace("namespace2").to_hash, {:foo => {:bar => 42}}
|
134
|
+
|
135
|
+
a = source2swagger.namespace("namespace1")
|
136
|
+
assert_equal a.to_hash, {:bla => 10, :foo => "100"}
|
137
|
+
|
138
|
+
end
|
139
|
+
|
140
|
+
|
141
|
+
def test_compare
|
142
|
+
|
143
|
+
reader = SwaggerReader.new
|
144
|
+
|
145
|
+
assert_equal [], diff([],[])
|
146
|
+
assert_equal [], diff({},{})
|
147
|
+
assert_equal [], diff("10","10")
|
148
|
+
assert_not_equal [], diff("10","11")
|
149
|
+
|
150
|
+
assert_equal [], diff({:a=>1,:b=>{:a=>1}},{:b=>{:a=>1},:a=>1})
|
151
|
+
assert_not_equal [], diff({:a=>1,:b=>{:a=>1}},{:b=>{:a=>1},:a=>"1"})
|
152
|
+
assert_not_equal [], diff({:a=>1,:b=>{:a=>1}},{:b=>{:a=>2},:a=>1})
|
153
|
+
|
154
|
+
assert_equal [], diff({:a=>1,:b=>{:a=>1,:c=>[ 11, 10, 9] }},{:b=>{:c=>[9, 10, 11],:a=>1},:a=>1})
|
155
|
+
|
156
|
+
a = {:a=>1, :b=>{:a=>1, :c => [{:z=>1}, {}, {:z2=>"2"}] }}
|
157
|
+
b = {:b=> {:c => [ {}, {:z2=>"2"}, {:z=>1}], :a=>1}, :a=>1}
|
158
|
+
|
159
|
+
assert_equal [], diff(a,b)
|
160
|
+
|
161
|
+
a = {:z => [{}, {:a => {:b => [1,2]}}]}
|
162
|
+
b = {:z => [{:a => {:b => [1,2]}}, {}]}
|
163
|
+
|
164
|
+
assert_equal [], diff(a,b)
|
165
|
+
|
166
|
+
b = {:z => [{}, {:a => {:b => [2,1]}}]}
|
167
|
+
|
168
|
+
assert_equal [], diff(a,b)
|
169
|
+
|
170
|
+
a = {:z => [{}, {:a => {:b => [{:c => 3}, {:d => 4}]}}]}
|
171
|
+
b = {:z => [{}, {:a => {:b => [{:d => 4}, {:c => 3}]}}]}
|
172
|
+
|
173
|
+
assert_equal [], diff(a,b)
|
174
|
+
|
175
|
+
end
|
176
|
+
|
177
|
+
end
|
@@ -0,0 +1,181 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
|
2
|
+
|
3
|
+
class SwaggerReaderTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def setup
|
6
|
+
source2swagger = SwaggerHash.new
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_swagger_simple
|
10
|
+
|
11
|
+
source2swagger = SwaggerHash.new
|
12
|
+
sapi = source2swagger.namespace("test")
|
13
|
+
ep = sapi.endpoint.add
|
14
|
+
ep.set :path => "/sentence/{sentence}", :format => "json"
|
15
|
+
ep.description = "Returns the aggregated sentiment of a sentence"
|
16
|
+
|
17
|
+
expected = {:endpoint => [{:path => "/sentence/{sentence}", :format => "json", :description => "Returns the aggregated sentiment of a sentence"}]}
|
18
|
+
assert_equal source2swagger.namespace("test").to_hash, expected
|
19
|
+
|
20
|
+
|
21
|
+
|
22
|
+
code = ["source2swagger = SwaggerHash.new", "sapi = source2swagger.namespace(\"test\")", "ep = sapi.endpoint.add", "ep.set :path => \"/sentence/{sentence}\", :format => \"json\"", "ep.description = \"Returns the aggregated sentiment of a sentence\""]
|
23
|
+
code << "source2swagger.namespace(\"test\").to_hash"
|
24
|
+
res = eval(code.join(";"))
|
25
|
+
assert_equal res, expected
|
26
|
+
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_samples_1
|
30
|
+
|
31
|
+
reader = SwaggerReader.new
|
32
|
+
|
33
|
+
code = reader.analyze_file("#{File.dirname(__FILE__)}/../data/sample1.rb","##~")
|
34
|
+
|
35
|
+
api1 = reader.process_code(code)
|
36
|
+
|
37
|
+
api1 = api1["sentiment"].to_hash
|
38
|
+
|
39
|
+
api2 = JSON::load(File.new("#{File.dirname(__FILE__)}/../data/sample1.json","r"))
|
40
|
+
|
41
|
+
assert_equal [], diff(api1, api2)
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_samples_2
|
46
|
+
|
47
|
+
reader = SwaggerReader.new
|
48
|
+
|
49
|
+
code = reader.analyze_file("#{File.dirname(__FILE__)}/../data/sample2.rb","##~")
|
50
|
+
api1 = reader.process_code(code)
|
51
|
+
|
52
|
+
api1 = api1["sentiment"].to_hash
|
53
|
+
|
54
|
+
api2 = JSON::load(File.new("#{File.dirname(__FILE__)}/../data/sample2.json","r"))
|
55
|
+
|
56
|
+
assert_equal [], diff(api1, api2)
|
57
|
+
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_samples_3
|
61
|
+
|
62
|
+
reader = SwaggerReader.new
|
63
|
+
|
64
|
+
code = reader.analyze_file("#{File.dirname(__FILE__)}/../data/sample3.rb","##~")
|
65
|
+
api1 = reader.process_code(code)
|
66
|
+
|
67
|
+
api1 = api1["sentiment"].to_hash
|
68
|
+
|
69
|
+
api2 = JSON::load(File.new("#{File.dirname(__FILE__)}/../data/sample3.json","r"))
|
70
|
+
|
71
|
+
assert_equal [], diff(api1, api2)
|
72
|
+
|
73
|
+
end
|
74
|
+
|
75
|
+
def test_samples_4
|
76
|
+
|
77
|
+
## sample4.rb is same as sample2.rb with the use of variables
|
78
|
+
|
79
|
+
reader = SwaggerReader.new
|
80
|
+
|
81
|
+
code = reader.analyze_file("#{File.dirname(__FILE__)}/../data/sample4.rb","##~")
|
82
|
+
api1 = reader.process_code(code)
|
83
|
+
api1 = api1["sentiment"].to_hash
|
84
|
+
|
85
|
+
api2 = JSON::load(File.new("#{File.dirname(__FILE__)}/../data/sample2.json","r"))
|
86
|
+
|
87
|
+
assert_equal [], diff(api1, api2)
|
88
|
+
|
89
|
+
end
|
90
|
+
|
91
|
+
|
92
|
+
def test_incorrect_samples
|
93
|
+
|
94
|
+
reader = SwaggerReader.new
|
95
|
+
|
96
|
+
code = reader.analyze_file("#{File.dirname(__FILE__)}/../data/sample1.rb","##~")
|
97
|
+
api1 = reader.process_code(code)
|
98
|
+
|
99
|
+
api1 = api1["sentiment"].to_hash
|
100
|
+
|
101
|
+
api2 = JSON::load(File.new("#{File.dirname(__FILE__)}/../data/sample2.json","r"))
|
102
|
+
|
103
|
+
assert_not_equal [], diff(api1, api2)
|
104
|
+
|
105
|
+
api3 = JSON::load(File.new("#{File.dirname(__FILE__)}/../data/sample3.json","r"))
|
106
|
+
assert_not_equal [], diff(api1, api3)
|
107
|
+
|
108
|
+
|
109
|
+
end
|
110
|
+
|
111
|
+
def test_exception_single_file
|
112
|
+
|
113
|
+
reader = SwaggerReader.new
|
114
|
+
|
115
|
+
begin
|
116
|
+
code = reader.analyze_file("#{File.dirname(__FILE__)}/../data/sample_bad2.rb","##~")
|
117
|
+
api1 = reader.process_code(code)
|
118
|
+
assert_equal true, false
|
119
|
+
rescue Exception => e
|
120
|
+
assert_equal "Error parsing source files at #{File.dirname(__FILE__)}/../data/sample_bad2.rb:9\n#<SyntaxError: (eval):1: unterminated string meets end of file>", e.to_s
|
121
|
+
end
|
122
|
+
|
123
|
+
end
|
124
|
+
|
125
|
+
def test_exception_all_files
|
126
|
+
|
127
|
+
reader = SwaggerReader.new
|
128
|
+
|
129
|
+
begin
|
130
|
+
code = reader.analyze_all_files("#{File.dirname(__FILE__)}/../data","rb","##~")
|
131
|
+
api1 = reader.process_code(code)
|
132
|
+
assert_equal true, false
|
133
|
+
rescue Exception => e
|
134
|
+
assert_equal "Error parsing source files at #{File.dirname(__FILE__)}/../data/sample_bad1.rb:17\n#<SyntaxError: (eval):1: syntax error, unexpected keyword_end, expecting $end\n...=> \"API down\", :code => 500;end = a.operations.add;out = {:a...\n... ^>", e.to_s
|
135
|
+
end
|
136
|
+
|
137
|
+
end
|
138
|
+
|
139
|
+
def test_exception_variable_not_defined
|
140
|
+
|
141
|
+
reader = SwaggerReader.new
|
142
|
+
|
143
|
+
begin
|
144
|
+
code = reader.analyze_file("#{File.dirname(__FILE__)}/../data/sample_bad3.rb","##~")
|
145
|
+
api1 = reader.process_code(code)
|
146
|
+
assert_equal true, false
|
147
|
+
rescue Exception => e
|
148
|
+
assert_equal "Error parsing source files at #{File.dirname(__FILE__)}/../data/sample_bad3.rb:17\n#<NameError: undefined local variable or method `error_sanitize_NOT_YET_DEFINED' ", e.to_s.split("for")[0].to_s
|
149
|
+
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
def test_unordered_variables_do_not_matter
|
154
|
+
|
155
|
+
reader = SwaggerReader.new
|
156
|
+
|
157
|
+
code = reader.analyze_file("#{File.dirname(__FILE__)}/../data/sample5.rb","##~")
|
158
|
+
api1 = reader.process_code(code)
|
159
|
+
api1 = api1["sentiment"].to_hash
|
160
|
+
|
161
|
+
api2 = JSON::load(File.new("#{File.dirname(__FILE__)}/../data/sample2.json","r"))
|
162
|
+
|
163
|
+
assert_equal [], diff(api1, api2)
|
164
|
+
end
|
165
|
+
|
166
|
+
def test_safe_level
|
167
|
+
|
168
|
+
assert_raises SwaggerReaderException do
|
169
|
+
reader = SwaggerReader.new
|
170
|
+
|
171
|
+
code = {:code => [], :line_number => [], :file => []}
|
172
|
+
|
173
|
+
code[:code] << "system \"rm -rf /tmp/test.txt\""
|
174
|
+
code[:line_number] << 0
|
175
|
+
code[:file] << "foo"
|
176
|
+
|
177
|
+
reader.process_code(code)
|
178
|
+
end
|
179
|
+
end
|
180
|
+
|
181
|
+
end
|