codeforces-solutions-downloader 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,42 @@
1
+ require 'fakefs/safe'
2
+ require 'fakefs/spec_helpers'
3
+ require 'webmock'
4
+
5
+ WebMock.disable_net_connect!
6
+ RSpec.configure do |config|
7
+ config.include FakeFS::SpecHelpers
8
+ end
9
+
10
+ module SpecHelpers
11
+ def self.read_file array_of_path
12
+ File.read get_path(array_of_path)
13
+ end
14
+
15
+ def self.get_path array_of_path
16
+ File.expand_path(File.join(File.dirname(__FILE__), File.join(array_of_path)))
17
+ end
18
+
19
+ def self.capture_stdout(&block)
20
+ original_stdout = $stdout
21
+ $stdout = fake = StringIO.new
22
+ begin
23
+ yield
24
+ ensure
25
+ $stdout = original_stdout
26
+ end
27
+ fake.string
28
+ end
29
+
30
+ def self.ignore_stdout(&block)
31
+ original_stdout = $stdout
32
+ $stdout = fake = StringIO.new
33
+ begin
34
+ yield
35
+ ensure
36
+ $stdout = original_stdout
37
+ end
38
+ end
39
+ end
40
+
41
+ require 'codeforces/solutions/downloader'
42
+
@@ -0,0 +1,118 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Application" do
4
+ before do
5
+ @app = Codeforces::Solutions::Downloader::Application.new
6
+ @app.option[:user_id] = "sh19910711"
7
+ @app.stub(:sleep).and_return(0)
8
+ end
9
+
10
+ before do
11
+ FakeFS.deactivate!
12
+ @dummy_submissions_respoinse = SpecHelpers.read_file(File.join('mock', 'codeforces', 'submissions', 'sh19910711.html'))
13
+ @dummy_submissions_respoinse_second = SpecHelpers.read_file(File.join('mock', 'codeforces', 'submissions', 'sh19910711_2.html'))
14
+ @dummy_submission_999999999_respopnse = SpecHelpers.read_file(File.join('mock', 'codeforces', 'submission', '999999999.html'))
15
+ FakeFS.activate!
16
+ end
17
+
18
+ context "#get_submissions" do
19
+ before do
20
+ # page any
21
+ WebMock.stub_request(:get, /^http:\/\/codeforces.com\/submissions\/sh19910711\/page\/[0-9]*$/)
22
+ .to_return(:status => 200, :body => @dummy_submissions_respoinse_second)
23
+
24
+ # page 1
25
+ WebMock.stub_request(:get, /^http:\/\/codeforces.com\/submissions\/sh19910711\/page\/1$/)
26
+ .to_return(:status => 200, :body => @dummy_submissions_respoinse)
27
+ end
28
+
29
+ before do
30
+ @app.instance_variable_set :@page_limit, 7
31
+ end
32
+
33
+ context "return value submission id" do
34
+ subject { SpecHelpers.ignore_stdout { @app.send(:get_submissions) }.map {|submission| submission[:submission_id]} }
35
+
36
+ it "should return 7 * 50 elements" do
37
+ subject.length.should eq 7 * 50
38
+ end
39
+
40
+ it "should contain submission id" do
41
+ # first
42
+ subject.should include "5828986"
43
+ subject.should include "5828956"
44
+ subject.should include "5857785"
45
+ # second
46
+ subject.should include "999999999"
47
+ end
48
+ end
49
+
50
+ context "return value contest id" do
51
+ subject { SpecHelpers.ignore_stdout { @app.send(:get_submissions) }.map {|submission| submission[:contest_id]} }
52
+
53
+ it "should contain contest id" do
54
+ subject.should include "387"
55
+ subject.should include "149"
56
+ subject.should include "146"
57
+ end
58
+ end
59
+
60
+ context "output" do
61
+ subject { SpecHelpers.capture_stdout { @app.send(:get_submissions) } }
62
+ it "should have 7 URLs" do
63
+ (1..7).each {|page_id|
64
+ subject.should match "http://codeforces.com/submissions/sh19910711/page/#{page_id}"
65
+ }
66
+ end
67
+ end
68
+ end
69
+
70
+ context "#get_page_limit" do
71
+ before do
72
+ @app.instance_variable_set :@last_body, @dummy_submissions_respoinse
73
+ end
74
+
75
+ subject { @app.send(:get_page_limit) }
76
+
77
+ it "should return 35" do
78
+ should eq 35
79
+ end
80
+ end
81
+
82
+ context "#fetch_submission" do
83
+ before do
84
+ WebMock.stub_request(:get, /http:\/\/codeforces.com\/contest\/165\/submission\/999999999/)
85
+ .to_return(:body => @dummy_submission_999999999_respopnse)
86
+ end
87
+
88
+ subject { SpecHelpers.ignore_stdout { @app.fetch_submission "165", "999999999" } }
89
+
90
+ it "code" do
91
+ subject[:source].should eq "this is test"
92
+ subject[:language].should eq "GNU C++0x"
93
+ end
94
+ end
95
+
96
+ context "#resolve_language" do
97
+ it { @app.send(:resolve_language, "GNU C").should eq "c" }
98
+ it { @app.send(:resolve_language, "GNU C++").should eq "cpp" }
99
+ it { @app.send(:resolve_language, "GNU C++0x").should eq "cpp" }
100
+ it { @app.send(:resolve_language, "MS C++").should eq "cpp" }
101
+ it { @app.send(:resolve_language, "Mono C#").should eq "cs" }
102
+ it { @app.send(:resolve_language, "MS C#").should eq "cs" }
103
+ it { @app.send(:resolve_language, "D").should eq "d" }
104
+ it { @app.send(:resolve_language, "Go").should eq "go" }
105
+ it { @app.send(:resolve_language, "Haskell").should eq "hs" }
106
+ it { @app.send(:resolve_language, "Java 6").should eq "java" }
107
+ it { @app.send(:resolve_language, "Java 7").should eq "java" }
108
+ it { @app.send(:resolve_language, "Ocaml").should eq "ml" }
109
+ it { @app.send(:resolve_language, "Delphi").should eq "pas" }
110
+ it { @app.send(:resolve_language, "FPC").should eq "pas" }
111
+ it { @app.send(:resolve_language, "Perl").should eq "pl" }
112
+ it { @app.send(:resolve_language, "PHP").should eq "php" }
113
+ it { @app.send(:resolve_language, "Python 2").should eq "py" }
114
+ it { @app.send(:resolve_language, "Python 3").should eq "py" }
115
+ it { @app.send(:resolve_language, "Ruby").should eq "rb" }
116
+ end
117
+ end
118
+
metadata ADDED
@@ -0,0 +1,149 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: codeforces-solutions-downloader
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Hiroyuki Sano
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-01-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: nokogiri
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.5'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.5'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: webmock
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: fakefs
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Codeforces Solutions Downloader
98
+ email:
99
+ - sh19910711@gmail.com
100
+ executables:
101
+ - codeforces-solutions-downloader
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - ".gitignore"
106
+ - Gemfile
107
+ - LICENSE.txt
108
+ - README.md
109
+ - Rakefile
110
+ - bin/codeforces-solutions-downloader
111
+ - codeforces-solutions-downloader.gemspec
112
+ - lib/codeforces/solutions/downloader.rb
113
+ - lib/codeforces/solutions/downloader/application.rb
114
+ - lib/codeforces/solutions/downloader/version.rb
115
+ - spec/mock/codeforces/submission/999999999.html
116
+ - spec/mock/codeforces/submissions/sh19910711.html
117
+ - spec/mock/codeforces/submissions/sh19910711_2.html
118
+ - spec/spec_helper.rb
119
+ - spec/test/tyem3_application_spec.rb
120
+ homepage: ''
121
+ licenses:
122
+ - MIT
123
+ metadata: {}
124
+ post_install_message:
125
+ rdoc_options: []
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ required_rubygems_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ requirements: []
139
+ rubyforge_project:
140
+ rubygems_version: 2.2.0
141
+ signing_key:
142
+ specification_version: 4
143
+ summary: Codeforces Solutions Downloader
144
+ test_files:
145
+ - spec/mock/codeforces/submission/999999999.html
146
+ - spec/mock/codeforces/submissions/sh19910711.html
147
+ - spec/mock/codeforces/submissions/sh19910711_2.html
148
+ - spec/spec_helper.rb
149
+ - spec/test/tyem3_application_spec.rb