#!/usr/bin/env ruby

# $Dwarf: yenc_test.rb,v 1.1 2003/04/20 18:32:39 ward Exp $
# $Source$

require '../yenc.rb'
require 'fileutils'

def test1
	puts "Test 1: decoding a file"
	file = File.open("testdata.ync", "r")
	tmpfile = Tempfile.new("ynctmp")
	tmpfile.sync=true
	mode, filename, body = YEnc.ydecode(file, tmpfile)
	if filename != "testdata"
		puts "Failed, filename should be \"testdata\", but is \"#{filename}\""
	elsif ! FileUtils.compare_file("testdata", tmpfile.path)
		puts "Failed, result doesn't match reference data"
	else
		puts "Succesful"
	end
	file.close
	tmpfile.close
end

def test2
	puts "Test 2: decoding an array"
	file = File.open("testdata.ync", "r")
	lines = file.readlines
	file.close
	file = File.open("testdata", mode = "r")
	reference = file.readlines
	file.close

	puts "	with dos linebreaks"
	mode, filename, body = YEnc.ydecode(lines)

	if filename != "testdata"
		puts "Failed, filename should be \"testdata\", but is \"#{filename}\""
	elsif reference != body
		puts "Failed, result doesn't match reference data"
	else
		puts "Succesful"
	end

	lines.collect!{|x| x.chomp("\r\n")}
	puts "	without linebreaks\n"
	mode, filename, body = YEnc.ydecode(lines)

	if filename != "testdata"
		puts "Failed, filename should be \"testdata\", but is \"#{filename}\""
	elsif reference != body
		puts "Failed, result doesn't match reference data"
	else
		puts "Succesful"
	end

	lines.collect!{|x| x.sub(/$/, "\n")}
	puts "	with unix linebreaks"
	mode, filename, body = YEnc.ydecode(lines)

	if filename != "testdata"
		puts "Failed, filename should be \"testdata\", but is \"#{filename}\""
	elsif reference != body
		puts "Failed, result doesn't match reference data"
	else
		puts "Succesful"
	end
	

end

def test3
	puts "Test 3: is_yencoded"
	file = File.open("testdata.ync", "r")
	lines = file.readlines
	file.close
	if YEnc.is_yencoded(lines)
		puts "Succesful"
	else
		puts "Failed"
	end
end

def test4
	puts "Test 4: get_filename"
	file = File.open("testdata.ync", "r")
	lines = file.readlines
	file.close

	filename = YEnc.get_filename(lines)
	if filename == "testdata"
		puts "Succesful"
	else
		puts "Failed"
	end
end

def test5
	puts "Test 5: decoding a larger file"
	file = File.open("lorem-gibson.txt.ync", "r")
	tmpfile = Tempfile.new("ynctmp")
	tmpfile.sync=true
	mode, filename, body = YEnc.ydecode(file, tmpfile)
	if filename != "lorem-gibson.txt"
		puts "Failed, filename should be \"lorem-gibson.txt\", but is \"#{filename}\""
	elsif ! FileUtils.compare_file("lorem-gibson.txt", tmpfile.path)
		puts "Failed, result doesn't match reference data"
	else
		puts "Succesful"
	end
	file.close
	tmpfile.close
end

def test6
	puts "Test 6: decoding a multipart file"
	file = File.open("lorem-gibson2.txt-multi.ync", "r")
	tmpfile = Tempfile.new("ynctmp")
	tmpfile.sync=true
	mode, filename, body = YEnc.ydecode(file, tmpfile)
	if filename != "lorem-gibson2.txt"
		puts "Failed, filename should be \"lorem-gibson2.txt\", but is \"#{filename}\""
	elsif ! FileUtils.compare_file("lorem-gibson2.txt", tmpfile.path)
		puts "Failed, result doesn't match reference data"
	else
		puts "Succesful"
	end
	file.close
	tmpfile.close
end

def test7
	puts "Test 7: decoding a downloaded file as file"
	file = File.open("testdata-download.ync", "r")
	tmpfile = Tempfile.new("ynctmp")
	tmpfile.sync=true
	mode, filename, body = YEnc.ydecode(file, tmpfile)
	if filename != "23597-7908-23799.rar"
		puts "Failed, filename should be \"lorem-gibson2.txt\", but is \"#{filename}\""
	elsif ! FileUtils.compare_file("testdata-download-ydecoded.rar", tmpfile.path)
		puts "Failed, result doesn't match reference data"
	else
		puts "Succesful"
	end
	file.close
	tmpfile.close
end

def test8
	puts "Test 8: decoding a downloaded file as array"
	puts "array is not build for multipart, so this breaks (for now)"
	file = File.open("testdata-download.ync", "r")
	lines = file.readlines
	file.close
	file = File.open("testdata-download-ydecoded.rar", mode = "r")
	reference = file.readlines
	file.close

	puts "	with dos linebreaks"
	mode, filename, body = YEnc.ydecode(lines)

	if filename != "23597-7908-23799.rar"
		puts "Failed, filename should be \"testdata\", but is \"#{filename}\""
	elsif reference != body
		puts "Failed, result doesn't match reference data"
	else
		puts "Succesful"
	end

	lines.collect!{|x| x.chomp("\r\n")}
	puts "	without linebreaks\n"
	mode, filename, body = YEnc.ydecode(lines)

	if filename != "23597-7908-23799.rar"
		puts "Failed, filename should be \"testdata\", but is \"#{filename}\""
	elsif reference != body
		puts "Failed, result doesn't match reference data"
	else
		puts "Succesful"
	end

	lines.collect!{|x| x.sub(/$/, "\n")}
	puts "	with unix linebreaks\n"
	mode, filename, body = YEnc.ydecode(lines)

	if filename != "23597-7908-23799.rar"
		puts "Failed, filename should be \"testdata\", but is \"#{filename}\""
	elsif reference != body
		puts "Failed, result doesn't match reference data"
	else
		puts "Succesful"
	end
	
end

test1
test2
test3
test4
test5
test6
test7
test8
