jtest 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,15 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "shoulda", ">= 0"
10
+ gem "rdoc", "~> 3.12"
11
+ gem "bundler", ">= 1.0.0"
12
+ gem "jeweler", "~> 1.8.4"
13
+ gem "thor"
14
+ gem "simplecov", :require => false, :group => :test
15
+ end
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Héctor Ramón Jiménez
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,19 @@
1
+ = jtest
2
+
3
+ Tool for automatic testing and creating problems from Jutge.org
4
+
5
+ == Contributing to jtest
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
9
+ * Fork the project.
10
+ * Start a feature/bugfix branch.
11
+ * Commit and push until you are happy with your contribution.
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2012 Héctor Ramón Jiménez. See LICENSE.txt for
18
+ further details.
19
+
@@ -0,0 +1,45 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "jtest"
18
+ gem.homepage = "http://github.com/hecrj/jtest"
19
+ gem.license = "MIT"
20
+ gem.summary = "Jutge.org testing tool!"
21
+ gem.description = "Tool for automatic testing and creating problems from Jutge.org"
22
+ gem.email = "hector0193@gmail.com"
23
+ gem.authors = ["Héctor Ramón Jiménez"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rake/testtask'
29
+ Rake::TestTask.new(:test) do |test|
30
+ test.libs << 'lib' << 'test'
31
+ test.pattern = 'test/**/test_*.rb'
32
+ test.verbose = true
33
+ end
34
+
35
+ task :default => :test
36
+
37
+ require 'rdoc/task'
38
+ Rake::RDocTask.new do |rdoc|
39
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
40
+
41
+ rdoc.rdoc_dir = 'rdoc'
42
+ rdoc.title = "jtest #{version}"
43
+ rdoc.rdoc_files.include('README*')
44
+ rdoc.rdoc_files.include('lib/**/*.rb')
45
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "jtest/cli"
4
+
5
+ begin
6
+ Jtest::CLI.start
7
+ rescue
8
+ Thor::Shell::Color.new.say_status :exception, $!.message, :red
9
+ exit 1
10
+ end
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/ruby
2
+
3
+ module Jtest
4
+
5
+ end
@@ -0,0 +1,23 @@
1
+ require "thor"
2
+ require "thor/group"
3
+ require "jtest/commands"
4
+
5
+ module Jtest
6
+ class CLI < Thor
7
+ include Thor::Actions
8
+
9
+ add_runtime_options!
10
+
11
+ register Jtest::Commands::New, "new", "new [ID]",
12
+ "Creates a workspace to solve problem with the given id"
13
+ tasks["new"].options = Jtest::Commands::New.class_options
14
+
15
+ register Jtest::Commands::Test, "test", "test",
16
+ "Performs tests for all the problems on the current working directory"
17
+ tasks["test"].options = Jtest::Commands::Test.class_options
18
+
19
+ register Jtest::Commands::Update, "update", "update",
20
+ "Downloads the samples for every problem in the current working directory"
21
+ tasks["update"].options = Jtest::Commands::Update.class_options
22
+ end
23
+ end
@@ -0,0 +1,9 @@
1
+ require "jtest/commands/new"
2
+ require "jtest/commands/test"
3
+ require "jtest/commands/update"
4
+
5
+ module Jtest
6
+ module Commands
7
+
8
+ end
9
+ end
@@ -0,0 +1,39 @@
1
+ require "jtest/problem"
2
+
3
+ module Jtest
4
+ module Commands
5
+ class New < Thor::Group
6
+ include Thor::Actions
7
+
8
+ desc "Creates a workspace to solve problem with the given id"
9
+
10
+ argument :id, :type => :numeric, :desc => "The problem identifier"
11
+ class_option :lang, :type => :string, :aliases => '-l', :desc => "Language of the problem",
12
+ :default => 'en'
13
+ class_option :prefix, :type => :string, :aliases => '-p', :desc => "Prefix of the problem",
14
+ :default => 'P'
15
+
16
+ def get_problem_info
17
+ @problem = Problem.new(id, options)
18
+
19
+ say_status :connecting, "Getting info about problem #{@problem.id}...", :yellow
20
+ @problem.retrieve_info
21
+
22
+ raise "Problem #{@problem.id} not found :(" unless @problem.exists?
23
+
24
+ say_status :found, @problem.title, :green
25
+ end
26
+
27
+ def create_workspace
28
+ say_status :working, "Creating workspace for #{@problem.title}", :yellow
29
+
30
+ empty_directory @problem.dirname
31
+
32
+ @problem.samples.each_with_index do |sample, index|
33
+ create_file "#{@problem.dirname}/sample#{index+1}.dat", sample[0]
34
+ create_file "#{@problem.dirname}/sample#{index+1}.out", sample[1]
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,97 @@
1
+ module Jtest
2
+ module Commands
3
+ class Test < Thor::Group
4
+ include Thor::Actions
5
+
6
+ desc "Performs tests for all the problems on the current working directory"
7
+ argument :ids, :optional => true, :default => ["all"],
8
+ :type => :array, :desc => "The problems ids to test, if not provided all problems are tested"
9
+
10
+
11
+ def select_problem_dirs
12
+ @problems = Problem.select(ids)
13
+ end
14
+
15
+ def perform_tests
16
+ @counter = { :failed => 0, :missing => 0, :wrong => 0, :passed => 0 }
17
+
18
+ @problems.each do |problem|
19
+ next unless File.directory?(problem.dirname)
20
+
21
+ say_status :testing, "Running tests on #{problem.dirname}...", :blue
22
+ Dir.chdir(problem.dirname) { perform_test(problem.dirname) }
23
+ end
24
+
25
+ colors = { :failed => :magenta, :missing => :yellow, :wrong => :red, :passed => :green }
26
+ summary(colors)
27
+ end
28
+
29
+ private
30
+ def perform_test(dir)
31
+ unless File.exists?('main.cc')
32
+ say_status :missing, "#{dir}/main.cc", :magenta
33
+ return
34
+ end
35
+
36
+ samples = Dir["sample*.dat"].collect { |x| x.chomp('.dat') }
37
+ samples.sort!
38
+
39
+ if samples.empty?
40
+ say_status :nosamples, "#{dir}/", :blue
41
+ return
42
+ end
43
+
44
+ say_status :compile, "#{dir}/main.cc", :blue
45
+ compiled = system("g++ main.cc -o test")
46
+
47
+ unless compiled
48
+ result :failed, "#{dir}/main.cc", :red
49
+ return
50
+ end
51
+
52
+ samples.each do |sample|
53
+ say_status :running, "#{dir}/#{sample}.dat", :blue
54
+ execution = system("./test < #{sample}.dat > #{sample}_test.out")
55
+
56
+ unless execution
57
+ result :failed, "#{dir}/#{sample}.dat", :magenta
58
+ next
59
+ end
60
+
61
+ unless File.exists?("#{sample}.out")
62
+ result :missing, "#{dir}/#{sample}.out", :yellow
63
+ next
64
+ end
65
+
66
+ if FileUtils.cmp("#{sample}.out", "#{sample}_test.out")
67
+ result :passed, "#{dir}/#{sample}.out", :green
68
+ remove_file "#{dir}/#{sample}_test.out", :verbose => false
69
+ else
70
+ result :wrong, "#{dir}/#{sample}.out", :red
71
+ end
72
+ end
73
+
74
+ remove_file "#{dir}/test", :verbose => false
75
+ end
76
+
77
+ def result(type, msg, color)
78
+ say_status type, msg, color
79
+ @counter[type] += 1
80
+ end
81
+
82
+ def summary(colors)
83
+ say "-" * terminal_width
84
+
85
+ total = 0
86
+ @counter.each do |key, value|
87
+ say_status key, value, colors[key]
88
+ total += value
89
+ end
90
+
91
+ say_status :total, total, :blue
92
+ say "-" * terminal_width
93
+ say_status :perfect, "All tests passed correctly!", :green if @counter[:passed] == total
94
+ end
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,43 @@
1
+ module Jtest
2
+ module Commands
3
+ class Update < Thor::Group
4
+ include Thor::Actions
5
+
6
+ desc "Downloads the samples for every problem in the current working directory"
7
+ argument :ids, :optional => true, :default => ["all"],
8
+ :type => :array, :desc => "The problems ids to update, if not provided all problems are updated"
9
+ class_option :lang, :type => :string, :aliases => '-l', :desc => "Language of the problem",
10
+ :default => 'en'
11
+ class_option :prefix, :type => :string, :aliases => '-p', :desc => "Prefix of the problem",
12
+ :default => 'P'
13
+
14
+ def select_problems
15
+ @problems = Problem.select(ids, options)
16
+ end
17
+
18
+ def update_problems
19
+ @problems.each do |problem|
20
+ say_status :connecting, "Getting info about problem #{problem.id}..."
21
+ problem.retrieve_info
22
+
23
+ unless problem.exists?
24
+ say_status :not_found, problem.id, :red
25
+ next
26
+ end
27
+
28
+ say_status :found, problem.title, :green
29
+
30
+ create_samples(problem)
31
+ end
32
+ end
33
+
34
+ private
35
+ def create_samples(problem)
36
+ problem.samples.each_with_index do |sample, index|
37
+ create_file "#{problem.dirname}/sample#{index+1}.dat", sample[0]
38
+ create_file "#{problem.dirname}/sample#{index+1}.out", sample[1]
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,81 @@
1
+ require "open-uri"
2
+ require "openssl"
3
+
4
+ module Jtest
5
+ class Problem
6
+ URL = "https://www.jutge.org/problems/"
7
+ REGEXPS = {
8
+ :title => /<title>Jutge :: Problem (.+?)<\/title>/,
9
+ :samples => /<pre.+?>(.+?)<\/pre>/m,
10
+ :dir_id => /[0-9]+/,
11
+ :invalid_url => "Wrong URL."
12
+ }
13
+ DEFAULT_OPTIONS = {:lang => 'en', :prefix => 'P', :dirname => nil}
14
+
15
+ attr_reader :id
16
+ attr_reader :title
17
+ attr_reader :samples
18
+ attr_reader :dirname
19
+
20
+ def initialize(id, options = {})
21
+ options = DEFAULT_OPTIONS.merge(options)
22
+
23
+ @id = options[:prefix] + id.to_s + '_' + options[:lang]
24
+ @title = nil
25
+ @samples = []
26
+ @dirname = options[:dirname]
27
+ end
28
+
29
+ def connect
30
+ url = URL + @id
31
+ @source = open(url, :ssl_verify_mode => OpenSSL::SSL::VERIFY_NONE).read
32
+ end
33
+
34
+ def exists?
35
+ connect if @source.nil?
36
+
37
+ not @source.include? REGEXPS[:invalid_url]
38
+ end
39
+
40
+ def retrieve_info
41
+ connect
42
+
43
+ return false unless exists?
44
+
45
+ @title = @source.scan(REGEXPS[:title])[0][0]
46
+ @samples = @source.scan(REGEXPS[:samples]).flatten.each_slice(2).to_a
47
+ @dirname = @title.gsub(/_(.+?)\:/, '').gsub(/\s/, '_') if @dirname.nil?
48
+
49
+ return true
50
+ end
51
+
52
+ class << self
53
+ def select(ids, options = {})
54
+ options = DEFAULT_OPTIONS.merge(options)
55
+ dirs = dirs_match(ids)
56
+
57
+ problems = []
58
+ dirs.each do |dir|
59
+ REGEXPS[:dir_id].match(dir) do |match|
60
+ options[:dirname] = dir
61
+ problems << Problem.new(match, options) unless match.nil?
62
+ end
63
+ end
64
+
65
+ return problems
66
+ end
67
+
68
+ private
69
+ def dirs_match(ids)
70
+ if ids == ["all"]
71
+ dirs = Dir["[PX]*_*"]
72
+ else
73
+ dirs = []
74
+ ids.each { |id| dirs += Dir["#{id}*"] }
75
+ end
76
+
77
+ return dirs
78
+ end
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+ require 'shoulda'
12
+
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
15
+ require 'jtest/cli'
16
+
17
+ class Test::Unit::TestCase
18
+ end
@@ -0,0 +1,21 @@
1
+ require 'helper'
2
+
3
+ class TestProblem < Test::Unit::TestCase
4
+ def setup
5
+
6
+ end
7
+
8
+ should "retrieve problem information correctly" do
9
+ problem = Jtest::Problem.new("93774") # Fractal pictures
10
+ problem.retrieve_info
11
+
12
+ assert problem.exists?
13
+ assert_equal("P93774_en: Fractal pictures", problem.title)
14
+ end
15
+
16
+ should "if an invalid problem id is given" do
17
+ problem = Jtest::Problem.new("Chunky_bacon")
18
+
19
+ assert !problem.exists?
20
+ end
21
+ end
@@ -0,0 +1,15 @@
1
+ require 'helper'
2
+
3
+ class TestJtest < Test::Unit::TestCase
4
+ def setup
5
+ @jtest = Jtest::CLI.new
6
+ end
7
+
8
+ should "create a problem with its samples" do
9
+ @jtest.new("93774")
10
+
11
+ assert(Dir.exists?("P93774_Fractal_pictures"))
12
+
13
+ @jtest.remove_file("P93774_Fractal_pictures")
14
+ end
15
+ end
metadata ADDED
@@ -0,0 +1,164 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jtest
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Héctor Ramón Jiménez
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-05 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: shoulda
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rdoc
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '3.12'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '3.12'
46
+ - !ruby/object:Gem::Dependency
47
+ name: bundler
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: 1.0.0
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: 1.0.0
62
+ - !ruby/object:Gem::Dependency
63
+ name: jeweler
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: 1.8.4
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: 1.8.4
78
+ - !ruby/object:Gem::Dependency
79
+ name: thor
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: simplecov
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ description: Tool for automatic testing and creating problems from Jutge.org
111
+ email: hector0193@gmail.com
112
+ executables:
113
+ - jtest
114
+ extensions: []
115
+ extra_rdoc_files:
116
+ - LICENSE.txt
117
+ - README.rdoc
118
+ files:
119
+ - .document
120
+ - Gemfile
121
+ - LICENSE.txt
122
+ - README.rdoc
123
+ - Rakefile
124
+ - VERSION
125
+ - bin/jtest
126
+ - lib/jtest.rb
127
+ - lib/jtest/cli.rb
128
+ - lib/jtest/commands.rb
129
+ - lib/jtest/commands/new.rb
130
+ - lib/jtest/commands/test.rb
131
+ - lib/jtest/commands/update.rb
132
+ - lib/jtest/problem.rb
133
+ - test/helper.rb
134
+ - test/jtest/test_problem.rb
135
+ - test/test_jtest.rb
136
+ homepage: http://github.com/hecrj/jtest
137
+ licenses:
138
+ - MIT
139
+ post_install_message:
140
+ rdoc_options: []
141
+ require_paths:
142
+ - lib
143
+ required_ruby_version: !ruby/object:Gem::Requirement
144
+ none: false
145
+ requirements:
146
+ - - ! '>='
147
+ - !ruby/object:Gem::Version
148
+ version: '0'
149
+ segments:
150
+ - 0
151
+ hash: -3827708754852138719
152
+ required_rubygems_version: !ruby/object:Gem::Requirement
153
+ none: false
154
+ requirements:
155
+ - - ! '>='
156
+ - !ruby/object:Gem::Version
157
+ version: '0'
158
+ requirements: []
159
+ rubyforge_project:
160
+ rubygems_version: 1.8.24
161
+ signing_key:
162
+ specification_version: 3
163
+ summary: Jutge.org testing tool!
164
+ test_files: []