# encoding: utf-8

require 'test/unit'

require_relative '../lib/Meter'
require_relative '../lib/Projects'
require_relative '../lib/Results'

class TestVoidMeter < Test::Unit::TestCase
	def assert_outside(detector, line)
		detector.consume(line)
		assert(!detector.block_comment?)
	end

	def assert_inside(detector, line)
		detector.consume(line)
		assert(detector.block_comment?)
	end

	def test_0
		block_comment = { open: '/*', close: '*/' }

		files = VoidMeter::Files.new
		files.add_file(extension: '.txt', block_comment: block_comment)

		@project = VoidMeter::Project.new(name: 'test', files: files, encoding: 'iso-8859-1')
		@project.add_release(version: 'r0', path: 'test')
		@results = VoidMeter::Results.new('TestVoidMeter.results')

		meter = VoidMeter::Meter.new
		meter.meter!(@project, @results)

		@project.releases.each do |release|
			result = @results.get_result(@project.name, release.version)
			assert_equal(2, result[:totals][:files])
			assert_equal(50, result[:totals][:lines])
			assert_equal(14, result[:indents][:not])
			assert_equal(10, result[:indents][:tabs])
			assert_equal(8, result[:indents][:spaces])
			assert_equal(2, result[:indents][:both])
			assert_equal(16, result[:indents][:block_comment])
			assert_equal(6, result[:indents][:trailing])
		end
	end

	def test_1
		test = <<-STOP
			/* 0 */
			/* 1 */ /* 1 */ /* 1 */
		STOP

		detector = VoidMeter::BlockCommentDetector.new
		assert(!detector.block_comment?)

		lines = test.each_line.to_a
		assert_inside(detector, lines[0])
		assert_inside(detector, lines[1])
	end

	def test_2
		test = <<-STOP
			0
			/* 1
			* 2
			* 3
			*/ 4
			5
		STOP

		detector = VoidMeter::BlockCommentDetector.new
		assert(!detector.block_comment?)

		lines = test.each_line.to_a
		assert_outside(detector, lines[0])
		assert_inside(detector, lines[1])
		assert_inside(detector, lines[2])
		assert_inside(detector, lines[3])
		assert_inside(detector, lines[4])
		assert_outside(detector, lines[5])
	end

	def test_3
		test = <<-STOP
			0
			/* 1
			/* 2
			/* 3
			* 4
			* 5 */
			* 6 */
			*/ 7
			8
		STOP

		detector = VoidMeter::BlockCommentDetector.new
		assert(!detector.block_comment?)

		lines = test.each_line.to_a
		assert_outside(detector, lines[0])
		assert_inside(detector, lines[1])
		assert_inside(detector, lines[2])
		assert_inside(detector, lines[3])
		assert_inside(detector, lines[4])
		assert_inside(detector, lines[5])
		assert_inside(detector, lines[6])
		assert_inside(detector, lines[7])
		assert_outside(detector, lines[8])
	end

	def test_4
		test = <<-STOP
			0
			/* 1 */ /*
			* 2
			* 3
			*/ 4
			5
		STOP

		detector = VoidMeter::BlockCommentDetector.new
		assert(!detector.block_comment?)

		lines = test.each_line.to_a
		assert_outside(detector, lines[0])
		assert_inside(detector, lines[1])
		assert_inside(detector, lines[2])
		assert_inside(detector, lines[3])
		assert_inside(detector, lines[4])
		assert_outside(detector, lines[5])
	end

	def test_5
		test = <<-STOP
			0
			/* 1
			*/ 2
			3
			/* 4
			* 5
			*/ 6
			7
			/* 8 */
		STOP

		detector = VoidMeter::BlockCommentDetector.new
		assert(!detector.block_comment?)

		lines = test.each_line.to_a
		assert_outside(detector, lines[0])
		assert_inside(detector, lines[1])
		assert_inside(detector, lines[2])
		assert_outside(detector, lines[3])
		assert_inside(detector, lines[4])
		assert_inside(detector, lines[5])
		assert_inside(detector, lines[6])
		assert_outside(detector, lines[7])
		assert_inside(detector, lines[8])
	end

	def test_6
		test = <<-STOP
			0
			1 /*
			2
			3
			4 */
			5
			/* 6
			*/ 7
			8 /*
			/* 9
			*/ 10
			*/ 11
			12
		STOP

		detector = VoidMeter::BlockCommentDetector.new
		assert(!detector.block_comment?)

		lines = test.each_line.to_a
		assert_outside(detector, lines[0])
		assert_outside(detector, lines[1])
		assert_inside(detector, lines[2])
		assert_inside(detector, lines[3])
		assert_inside(detector, lines[4])
		assert_outside(detector, lines[5])
		assert_inside(detector, lines[6])
		assert_inside(detector, lines[7])
		assert_outside(detector, lines[8])
		assert_inside(detector, lines[9])
		assert_inside(detector, lines[10])
		assert_inside(detector, lines[11])
		assert_outside(detector, lines[12])
	end

end
