nyara 0.1.pre.0 → 0.1.pre.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (67) hide show
  1. checksums.yaml +4 -4
  2. data/bin/nyara +2 -14
  3. data/changes +3 -0
  4. data/example/factorial.rb +19 -0
  5. data/example/hello.rb +5 -0
  6. data/example/project.rb +11 -0
  7. data/example/stream.rb +14 -0
  8. data/ext/extconf.rb +19 -0
  9. data/ext/hashes.c +160 -57
  10. data/ext/inc/ary_intern.h +36 -0
  11. data/ext/route.cc +2 -1
  12. data/ext/url_encoded.c +1 -0
  13. data/lib/nyara.rb +1 -0
  14. data/lib/nyara/command.rb +60 -79
  15. data/lib/nyara/config.rb +19 -1
  16. data/lib/nyara/controller.rb +64 -7
  17. data/lib/nyara/hashes/config_hash.rb +3 -20
  18. data/lib/nyara/hashes/header_hash.rb +2 -2
  19. data/lib/nyara/hashes/param_hash.rb +1 -0
  20. data/lib/nyara/nyara.rb +45 -37
  21. data/lib/nyara/part.rb +2 -2
  22. data/lib/nyara/reload.rb +63 -64
  23. data/lib/nyara/route.rb +7 -6
  24. data/lib/nyara/session.rb +4 -0
  25. data/lib/nyara/templates/{Gemfile → Gemfile.tt} +4 -0
  26. data/lib/nyara/templates/Linnerfile +28 -0
  27. data/lib/nyara/templates/Rakefile +16 -2
  28. data/lib/nyara/templates/app/assets/files/favicon.ico +1 -0
  29. data/lib/nyara/templates/app/assets/files/robots.txt +5 -0
  30. data/lib/nyara/templates/app/assets/scripts/app.coffee +4 -0
  31. data/lib/nyara/templates/app/assets/scripts/module-example.coffee +4 -0
  32. data/lib/nyara/templates/app/assets/styles/app.scss +2 -0
  33. data/lib/nyara/templates/app/controllers/application_controller.rb +8 -0
  34. data/lib/nyara/templates/app/views/layouts/application.erb.tt +12 -0
  35. data/lib/nyara/templates/config/application.rb +4 -0
  36. data/lib/nyara/templates/config/{database.yml → database.yml.tt} +3 -3
  37. data/lib/nyara/templates/config/production.rb +2 -0
  38. data/lib/nyara/view.rb +6 -2
  39. data/nyara.gemspec +3 -1
  40. data/rakefile +7 -26
  41. data/spec/apps/reload.rb +35 -0
  42. data/spec/command_spec.rb +24 -10
  43. data/spec/config_spec.rb +19 -8
  44. data/spec/controller_spec.rb +14 -0
  45. data/spec/evented_io_spec.rb +3 -1
  46. data/spec/ext_route_spec.rb +25 -3
  47. data/spec/hashes_spec.rb +45 -21
  48. data/spec/integration_spec.rb +28 -2
  49. data/spec/path_helper_spec.rb +7 -0
  50. data/spec/performance_spec.rb +1 -1
  51. data/spec/public/test.css +1 -0
  52. data/{lib/nyara/templates/public/robot.txt → spec/public/test.jpg} +0 -0
  53. data/spec/public/test.js +1 -0
  54. data/spec/reload_spec.rb +50 -0
  55. data/spec/route_spec.rb +4 -4
  56. data/spec/spec_helper.rb +15 -9
  57. data/spec/url_encoded_spec.rb +5 -0
  58. data/spec/view_spec.rb +7 -0
  59. data/spec/views/_partial.slim +1 -1
  60. data/tools/bug.rb +53 -0
  61. data/tools/hello.rb +49 -0
  62. data/tools/memcheck.rb +33 -0
  63. metadata +73 -41
  64. data/ext/inc/status_codes.inc +0 -64
  65. data/lib/nyara/templates/app/views/layouts/application.erb +0 -12
  66. data/lib/nyara/templates/public/css/app.css +0 -1
  67. data/lib/nyara/templates/public/js/app.js +0 -1
data/spec/hashes_spec.rb CHANGED
@@ -21,14 +21,14 @@ module Nyara
21
21
  end
22
22
 
23
23
  %w( a[ [ a] a[b]c a[[b] a[[b]] ).each do |k|
24
- it "detects bad key: #{k}" do
24
+ it "does not accept bad key: #{k}" do
25
25
  assert_raise ArgumentError do
26
26
  ParamHash.split_name k
27
27
  end
28
28
  end
29
29
  end
30
30
 
31
- it "detects empty key" do
31
+ it "does not accept empty key" do
32
32
  assert_raise ArgumentError do
33
33
  ParamHash.split_name ''
34
34
  end
@@ -37,7 +37,7 @@ module Nyara
37
37
 
38
38
  it "#nested_aset" do
39
39
  h = ParamHash.new
40
- h.nested_aset ['a', '', 'b'], 'c'
40
+ h.send :nested_aset, ['a', '', 'b'], 'c'
41
41
  assert_equal({'a' => [{'b' => 'c'}]}, h)
42
42
  end
43
43
 
@@ -73,17 +73,15 @@ module Nyara
73
73
  h = ParamHash.parse_cookie ParamHash.new, 'a ;'
74
74
  assert_equal 1, h.size
75
75
  assert_equal '', h['a']
76
+ end
76
77
 
78
+ it "parses cookie with space around =" do
77
79
  h = ParamHash.parse_cookie ParamHash.new, 'b = 1; a ;'
78
80
  assert_equal 2, h.size
79
81
  assert_equal '', h['a']
80
82
  assert_equal '1', h['b']
81
83
  end
82
84
 
83
- it "parses cookie with space around =" do
84
-
85
- end
86
-
87
85
  it "refuses to parse cookie into HeaderHash" do
88
86
  assert_raise ArgumentError do
89
87
  ParamHash.parse_cookie HeaderHash.new, 'session=3'
@@ -113,29 +111,55 @@ module Nyara
113
111
  assert_equal "", h["\xE2"]
114
112
  end
115
113
 
116
- it "parses nested kv and preserves hash class" do
117
- h = ParamHash.parse_param ParamHash.new, "a[b][]=c"
118
- assert_equal({'a' => {'b' => ['c']}}, h)
119
- assert_equal ParamHash, h['a'].class
120
- end
121
-
122
- it "parses k without v and preserves hash class" do
123
- h = ParamHash.parse_param ConfigHash.new, "a%5B%5d[b]"
124
- assert_equal({'a' => [{'b' => ''}]}, h)
125
- assert_equal Array, h[:a].class
126
- assert_equal ConfigHash, h[:a].first.class
127
- end
128
-
129
114
  it "parses blank string" do
130
115
  h = ParamHash.parse_param({}, '')
131
116
  assert h.empty?
132
117
  end
133
118
 
134
- it "raises for HeaderHash" do
119
+ it "does not accept HeaderHash" do
135
120
  assert_raise ArgumentError do
136
121
  ParamHash.parse_param(HeaderHash.new, '')
137
122
  end
138
123
  end
124
+
125
+ context "nested" do
126
+ it "parses nested kv and output with ParamHash" do
127
+ h = ParamHash.parse_param ParamHash.new, "a[b][]=c"
128
+ assert_equal({'a' => {'b' => ['c']}}, h)
129
+ assert_equal ParamHash, h['a'].class
130
+ end
131
+
132
+ it "parses k without v and output preserves hash class" do
133
+ h = ParamHash.parse_param ConfigHash.new, "a%5B%5d[b+]"
134
+ assert_equal({'a' => [{'b ' => ''}]}, h)
135
+ assert_equal Array, h[:a].class
136
+ assert_equal ConfigHash, h[:a].first.class
137
+ end
138
+
139
+ it "increases array index when there's existing key" do
140
+ h = ParamHash.parse_param({}, %w'
141
+ a[][foo]=0
142
+ a[][foo]=1
143
+ a[][bar]=1
144
+ a[][foo]=2
145
+ '.join('&'))
146
+ assert_equal({"a"=>[{"foo"=>"0"}, {"foo"=>"1", "bar"=>"1"}, {"foo"=>"2"}]}, h)
147
+ end
148
+
149
+ it "raises error for conflict key" do
150
+ assert_raise TypeError do
151
+ ParamHash.parse_param({}, 'a[a]=1&a[]=1')
152
+ end
153
+ end
154
+
155
+ it "raises error for conflict key in 2 parses" do
156
+ h = {}
157
+ ParamHash.parse_param(h, 'a[a]=1')
158
+ assert_raise TypeError do
159
+ ParamHash.parse_param(h, 'a[]=1')
160
+ end
161
+ end
162
+ end
139
163
  end
140
164
  end
141
165
 
@@ -1,4 +1,5 @@
1
1
  require_relative "spec_helper"
2
+ require 'logger'
2
3
 
3
4
  class TestController < Nyara::Controller
4
5
  attr_reader :before_invoked
@@ -15,7 +16,7 @@ class TestController < Nyara::Controller
15
16
 
16
17
  meta '#create'
17
18
  post '/create' do
18
- set_header 'partial', partial('_partial').strip
19
+ set_header 'partial', partial('_partial', locals: { a: '1' }).strip
19
20
  redirect_to '#index'
20
21
  end
21
22
 
@@ -86,13 +87,21 @@ module Nyara
86
87
 
87
88
  it "redirect" do
88
89
  @test.post @test.path_to('test#create')
89
- assert_equal 'This is a partial', @test.response.header['Partial']
90
+ assert_equal 'This is a partial 1', @test.response.header['Partial']
90
91
  assert @test.response.success?
91
92
  assert_equal 'http://localhost:3000/', @test.redirect_location
92
93
  @test.follow_redirect
93
94
  assert_equal '/', @test.request.path
94
95
  end
95
96
 
97
+ it "post params log output" do
98
+ data = { name: 1, sex: 0 }
99
+ Nyara.config.stub(:logger).and_return(Logger.new($stdout))
100
+ out = capture(:stdout) { @test.post @test.path_to('test#create'), {}, data }
101
+ # puts out
102
+ assert_include out, 'params: {"name"=>"1", "sex"=>"0"}'
103
+ end
104
+
96
105
  it "session continuation" do
97
106
  @test.session['a'] = '3'
98
107
  @test.get "/"
@@ -162,6 +171,23 @@ module Nyara
162
171
  @test.get "/empty"
163
172
  assert_equal 404, @test.response.status
164
173
  end
174
+
175
+ it "found test.css and text/css content_type" do
176
+ @test.get "/test.css"
177
+ assert_equal "test css", @test.response.body
178
+ assert_include @test.response.header['Content-Type'],"text/css"
179
+ end
180
+
181
+ it "found test.js" do
182
+ @test.get "/test.js"
183
+ assert_equal "test js", @test.response.body
184
+ assert_include @test.response.header['Content-Type'],"application/javascript"
185
+ end
186
+
187
+ it "found test.jpg" do
188
+ @test.get "/test.jpg"
189
+ assert_include @test.response.header['Content-Type'],"image/jpeg"
190
+ end
165
191
  end
166
192
 
167
193
  context "before / after" do
@@ -79,6 +79,13 @@ module Nyara
79
79
  generated = c.path_to '#index', 1, format: 'js', 'utm_source' => 'a spam'
80
80
  assert_equal "/00001.js?utm_source=a+spam", generated
81
81
  end
82
+
83
+ it "raise info with wrong route" do
84
+ c = FooController.new :stub_request
85
+ assert_raise ArgumentError do
86
+ c.path_to('#aksdgjajksdg')
87
+ end
88
+ end
82
89
  end
83
90
 
84
91
  context '#url_to' do
@@ -25,7 +25,7 @@ describe 'performance' do
25
25
 
26
26
  it "[parse_param] faster than parse in pure ruby" do
27
27
  res = bm 'parse_param'
28
- assert res[:nyara] * 7 < res[:ruby], res.inspect
28
+ assert res[:nyara] * 5 < res[:ruby], res.inspect
29
29
  end
30
30
 
31
31
  it "[layout_render] nearly as fast as using tilt..." do
@@ -0,0 +1 @@
1
+ test css
@@ -0,0 +1 @@
1
+ test js
@@ -0,0 +1,50 @@
1
+ require_relative "spec_helper"
2
+ require_relative "../lib/nyara/reload"
3
+
4
+ module Nyara
5
+ describe Reload do
6
+ before :all do
7
+ GC.stress = false
8
+ @reload_root = ENV['RELOAD_ROOT'] = Dir.mktmpdir 'root'
9
+ Dir.mkdir @reload_root + '/views'
10
+ touch_files 'app before', 'views before'
11
+ @server = fork do
12
+ Dir.chdir __dir__ + '/apps' do
13
+ exec "ruby reload.rb"
14
+ end
15
+ end
16
+ sleep 2
17
+ end
18
+
19
+ def touch_files app_content, views_content
20
+ File.open @reload_root + '/reloadee.rb', 'w' do |f|
21
+ f << 'RELOADEE = ' << app_content.inspect
22
+ end
23
+ File.open @reload_root + '/views/index.slim', 'w' do |f|
24
+ f << '== ' << views_content.inspect
25
+ end
26
+ end
27
+
28
+ after :all do
29
+ Process.kill :TERM, @server
30
+ sleep 0.2
31
+ Process.kill :KILL, @server
32
+ end
33
+
34
+ it "reloads" do
35
+ GC.stress = false
36
+ data = open 'http://localhost:3004/app', &:read
37
+ assert_equal 'app before', data
38
+ data = open 'http://localhost:3004/views', &:read
39
+ assert_equal 'views before', data
40
+
41
+ touch_files 'app after', 'views after'
42
+ sleep 1.2
43
+
44
+ data = open 'http://localhost:3004/views', &:read
45
+ assert_equal 'views after', data
46
+ data = open 'http://localhost:3004/app', &:read
47
+ assert_equal 'app after', data
48
+ end
49
+ end
50
+ end
data/spec/route_spec.rb CHANGED
@@ -15,7 +15,7 @@ module Nyara
15
15
 
16
16
  @r.path = '/'
17
17
  @r.compile :controller_stub, '/scope'
18
- assert_equal '/scope', @r.prefix
18
+ assert_equal '/scope/', @r.prefix
19
19
  assert_equal '', @r.suffix
20
20
  assert_equal [], @r.conv
21
21
 
@@ -59,7 +59,7 @@ module Nyara
59
59
  end
60
60
 
61
61
  it "#analyse_path" do
62
- r = @r.analyse_path '/hello/%d-world%u/%s/'
62
+ r = @r.analyse_path '/hello/%d-world%u/%s'
63
63
  assert_equal ['/hello/', '%d-world%u/%s'], r
64
64
 
65
65
  prefix, suffix = @r.analyse_path '/hello'
@@ -127,8 +127,8 @@ module Nyara
127
127
  end
128
128
 
129
129
  it ".print_routes" do
130
- out = stdout { Route.print_routes }
131
- assert_include(out, "all routes:")
130
+ out = capture(:stdout) { Route.print_routes }
131
+ assert_include(out, "All routes:")
132
132
  end
133
133
  end
134
134
  end
data/spec/spec_helper.rb CHANGED
@@ -20,16 +20,22 @@ if ENV['COVERAGE']
20
20
  end
21
21
  require_relative "../lib/nyara/nyara"
22
22
  require_relative "../lib/nyara/test"
23
+ require_relative "../lib/nyara/command"
23
24
 
24
- def stdout(&block)
25
- original_stdout = $stdout
26
- $stdout = fake = StringIO.new
27
- begin
28
- yield
29
- ensure
30
- $stdout = original_stdout
31
- end
32
- fake.string
25
+ def capture(stream)
26
+ stream = stream.to_s
27
+ captured_stream = Tempfile.new(stream)
28
+ stream_io = eval("$#{stream}")
29
+ origin_stream = stream_io.dup
30
+ stream_io.reopen(captured_stream)
31
+
32
+ yield
33
+
34
+ stream_io.rewind
35
+ return captured_stream.read
36
+ ensure
37
+ captured_stream.unlink
38
+ stream_io.reopen(origin_stream)
33
39
  end
34
40
 
35
41
  RSpec.configure do |config|
@@ -33,6 +33,11 @@ module Nyara
33
33
  assert_equal '', v
34
34
  end
35
35
 
36
+ it "with escaped chars" do
37
+ k, v = Ext.decode_uri_kv "[b+]"
38
+ assert_equal '[b ]', k
39
+ end
40
+
36
41
  it "raises for bad kv" do
37
42
  assert_raise ArgumentError do
38
43
  Ext.decode_uri_kv 'a=&b'
data/spec/view_spec.rb CHANGED
@@ -47,11 +47,18 @@ module Nyara
47
47
  assert_raise ArgumentError do
48
48
  render 'edit', nil, nil, {}
49
49
  end
50
+
50
51
  render 'edit.slim', nil, nil, {}
51
52
  assert_equal "<div>slim:edit</div>", @instance.result.gsub(/\s/, '')
52
53
  render 'edit.haml', nil, nil, {}
53
54
  assert_equal "<div>haml:edit</div>", @instance.result.gsub(/\s/, '')
54
55
  end
56
+
57
+ it "raises error with template not found" do
58
+ assert_raise ArgumentError do
59
+ render 'asdgasdgasdg'
60
+ end
61
+ end
55
62
 
56
63
  context "fallback to tilt" do
57
64
  it "inline render" do
@@ -1 +1 @@
1
- | This is a partial
1
+ | This is a partial #{a}
data/tools/bug.rb ADDED
@@ -0,0 +1,53 @@
1
+ require_relative "../lib/nyara/nyara"
2
+
3
+ configure do
4
+ map '/posts', 'posts'
5
+ end
6
+
7
+ class ApplicationController < Nyara::Controller
8
+
9
+ end
10
+
11
+ class PostsController < ApplicationController
12
+ meta '#index'
13
+ get '/' do
14
+ send_string 'index'
15
+ end
16
+
17
+ meta '#show'
18
+ get '/%u' do |id|
19
+ @post = Post.find(id)
20
+ render "posts/show"
21
+ end
22
+
23
+ meta '#new'
24
+ get '/new' do
25
+ @post = Post.new
26
+ render "posts/new"
27
+ end
28
+
29
+ meta '#create'
30
+ post '/' do
31
+ @post = Post.new(params[:post])
32
+ if @post.save
33
+ success "Post created."
34
+ redirect_to 'posts#show', @post.id
35
+ else
36
+ render "posts/new"
37
+ end
38
+ end
39
+
40
+ meta '#edit'
41
+ get '/%u/edit' do
42
+ @post = Post.find(id)
43
+ render 'posts/edit'
44
+ end
45
+
46
+ meta '#update'
47
+ post '/%u' do |id|
48
+ @post = Post.find(id)
49
+ end
50
+ end
51
+
52
+ Nyara.setup
53
+ Nyara.start_server
data/tools/hello.rb ADDED
@@ -0,0 +1,49 @@
1
+ require_relative "../lib/nyara/nyara"
2
+ # require "open-uri"
3
+ require "pry"
4
+
5
+ configure do
6
+ # set :env, 'production'
7
+ # set :workers, 3
8
+ map '/', 'my'
9
+ set :root, __dir__
10
+ set :public, '/'
11
+ # set :logger, false
12
+ end
13
+
14
+ class MyController < Nyara::Controller
15
+ get '/' do
16
+ # p param
17
+ raise 'a'
18
+ send_string 'hello world'
19
+ end
20
+
21
+ get '/exit' do
22
+ exit
23
+ end
24
+
25
+ get '/form' do
26
+ send_string <<-HTML
27
+ <form action="/form" enctype="multipart/form-data" method="post" accept-charset="utf-8">
28
+ bar
29
+ <input type="file" name="bar">
30
+ <br>
31
+ baz
32
+ <input type="file" name="baz[你好]">
33
+ <br>
34
+ foo<input name="foo">
35
+ <br>
36
+ </form>
37
+ HTML
38
+ end
39
+
40
+ post '/form' do
41
+ b = request.body
42
+ binding.pry
43
+ end
44
+ end
45
+
46
+ Nyara.setup
47
+ Nyara.start_server
48
+
49
+ # GC.stress = true