Class: Marmalade::Puzzle
- Inherits:
-
Object
- Object
- Marmalade::Puzzle
- Defined in:
- lib/marmalade/puzzle.rb
Instance Attribute Summary collapse
-
#reader ⇒ Object
Returns the value of attribute reader.
Instance Method Summary collapse
-
#initialize(file_reader, options = {}) ⇒ Puzzle
constructor
A new instance of Puzzle.
- #read(assignments, options = {}) ⇒ Object
- #read_num_cases ⇒ Object
- #run_case(&block) ⇒ Object
- #test_cases(options = {}, &block) ⇒ Object
Constructor Details
#initialize(file_reader, options = {}) ⇒ Puzzle
Returns a new instance of Puzzle.
7 8 9 10 11 12 13 14 |
# File 'lib/marmalade/puzzle.rb', line 7 def initialize(file_reader, = {}) self.reader = file_reader @options = .dup @debug = (@options[:debug] == true) if @options[:processes].to_i > 1 && !@options[:parallel] @options[:parallel] = true end end |
Instance Attribute Details
#reader ⇒ Object
Returns the value of attribute reader.
5 6 7 |
# File 'lib/marmalade/puzzle.rb', line 5 def reader @reader end |
Instance Method Details
#read(assignments, options = {}) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/marmalade/puzzle.rb', line 20 def read(assignments, = {}) assigns = reader.read(assignments, ) assigns.each do |k, v| # Set an ivar on both the puzzle and the test case so we can support using them in # subsequent calls to 'read' in the same block evaluation. instance_variable_set("@#{k.to_s}", v) if @current_case @current_case.instance_variable_set("@#{k.to_s}", v) end end end |
#read_num_cases ⇒ Object
16 17 18 |
# File 'lib/marmalade/puzzle.rb', line 16 def read_num_cases read :num_cases, :type => :int end |
#run_case(&block) ⇒ Object
91 92 93 |
# File 'lib/marmalade/puzzle.rb', line 91 def run_case(&block) @current_case.run_block = block end |
#test_cases(options = {}, &block) ⇒ Object
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 |
# File 'lib/marmalade/puzzle.rb', line 32 def test_cases( = {}, &block) unless @num_cases.is_a?(Fixnum) raise MarmaladeError.new("@num_cases has not been set or is not an integer") end = .merge(@options) # Set up all test cases and read each one's info from the file test_cases = [] 1.upto(@num_cases).each do |case_num| test_case = TestCase.new(case_num, ) test_case.debug = @debug @current_case = test_case instance_eval(&block) @current_case = nil test_cases << test_case end # If we are to run in more than one process, we'll run all of the tests without # regard to stepping or finding a single case to run if [:parallel] # Setup pipes for the output of each test case reader, writer = IO.pipe test_cases.each do |test_case| test_case.output = writer end # Run them processes = [:processes] || Parallel.processor_count Parallel.each(test_cases, :in_processes => processes) do |test_case| test_case.run end # Now output the buffers from each case writer.close outputs = [] # TODO: This could probably be done with a stable sort instead... while = reader.gets match = .match(/Case #(\d+)/) if match case_num = match[1].to_i case_output = outputs[case_num] ||= [] case_output << else outputs << end end outputs.flatten.compact.each { |msg| puts msg } else test_cases.each do |test_case| if [:case].nil? || [:case] == test_case.case_num test_case.run if [:step] == true STDIN.getc if [:step] end end end end end |