Sha256: e24268a98c58437a23051ca540a54480ef064ab5fcf2b0764b60a8f570d3617f

Contents?: true

Size: 1.67 KB

Versions: 1

Compression:

Stored size: 1.67 KB

Contents

#!/usr/local/bin/ruby -w

# tc_reading.rb
#
#  Created by James Edward Gray II on 2005-08-14.
#  Copyright 2005 Gray Productions. All rights reserved.

require "test/unit"

require "parse/input"

class TestReading < Test::Unit::TestCase
	def test_line_by_line_read
		path  = File.join(File.dirname(__FILE__), "five_lines.txt")
		lines = File.readlines(path)
		test  = self
		
		Parse::Input.new(path) do
			read { |line| test.assert_equal(lines.shift, line) }
		end
	end
	
	def test_simple_interface
		path  = File.join(File.dirname(__FILE__), "five_lines.txt")
		lines = File.readlines(path)
		test  = self

		input(path) do
			read { |line| test.assert_equal(lines.shift, line) }
		end
	end

	def test_paragraph_read
		path       = File.join(File.dirname(__FILE__), "five_paragraphs.txt")
		paragraphs = File.readlines(path, "")
		test       = self

		input(path, "") do
			read { |paragraph| test.assert_equal(paragraphs.shift, paragraph) }
		end
	end

	def test_restricted_reading
		path    = File.join(File.dirname(__FILE__), "five_lines.txt")
		numbers = %w{two three}
		test    = self

		input(path) do
			read(/ (t\w+)\./) do |number|
				test.assert_equal("This is line #{numbers.first}.\n", @read)
				test.assert_equal(numbers.shift, number)
			end
		end
	end
	
	def test_data_saving_and_retrieving
		path    = File.join(File.dirname(__FILE__), "five_lines.txt")

		data = input path do
			read(/ (o\w+)\./) { |number| @number = number }
			read { |line| (@lines ||= Array.new) << line }
		end
		
		assert_equal("one", data[:number])
		assert_equal("one", data.number)
		assert_equal(path, data.path)
		assert_equal(5, data.lines.size)
		assert_equal(File.readlines(path), data.lines)
	end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
parseinput-0.0.1 test/tc_reading.rb