require 'spec_helper'

module Beaker
  describe LoggerJunit do
    let(:xml_file) { '/fake/file/location1' }
    let(:stylesheet) { '/fake/file/location2' }

    describe '#is_valid_xml' do
      it 'rejects all invalid values' do
        invalid_values = [0x8, 0x10, 0xB, 0x0019, 0xD800, 0xDFFF, 0xFFFE, 0x99999, 0x110000]
        invalid_values.each do |value|
          expect(described_class.is_valid_xml(value)).to be === false
        end
      end

      it 'accepts valid values' do
        valid_values = [0x9, 0xA, 0x0020, 0xD7FF, 0xE000, 0xFFFD, 0x100000, 0x10FFFF]
        valid_values.each do |value|
          expect(described_class.is_valid_xml(value)).to be === true
        end
      end
    end

    describe '#escape_invalid_xml_chars' do
      it 'escapes invalid xml characters correctly' do
        testing_string = 'pants'
        testing_string << 0x8
        expect(described_class.escape_invalid_xml_chars(testing_string)).to be === 'pants\8'
      end

      it 'leaves a string of all valid xml characters alone' do
        testing_string = 'pants man, pants!'
        expect(described_class.escape_invalid_xml_chars(testing_string)).to be === testing_string
      end
    end

    describe '#copy_stylesheet_into_xml_dir' do
      it 'copies the stylesheet into the correct location' do
        allow(File).to receive(:file?).and_return(false)
        correct_location = File.join(File.dirname(xml_file), File.basename(stylesheet))
        expect(FileUtils).to receive(:copy).with(stylesheet, correct_location)
        described_class.copy_stylesheet_into_xml_dir(stylesheet, xml_file)
      end

      it 'skips action if the file doesn\'t exist' do
        allow(File).to receive(:file?).and_return(true)
        expect(FileUtils).not_to receive(:copy)
        described_class.copy_stylesheet_into_xml_dir(stylesheet, xml_file)
      end
    end

    describe '#finish' do
      it 'opens the given file for writing, and writes the doc to it' do
        mock_doc = Object.new
        allow(mock_doc).to receive(:write).with(File, 2)
        expect(File).to receive(:open).with(xml_file, 'w')
        described_class.finish(mock_doc, xml_file)
      end
    end

    describe '#write_xml' do
      it 'throws an error with 1-arity in the given block' do
        allow(described_class).to receive(:get_xml_contents)
        expect { described_class.write_xml(xml_file, stylesheet) { |hey| } }.to raise_error(ArgumentError)
      end

      it 'doesn\'t throw an error with 2-arity in the given block' do
        allow(described_class).to receive(:get_xml_contents)
        allow(described_class).to receive(:finish)
        expect { described_class.write_xml(xml_file, stylesheet) { |hey1, hey2| } }.not_to raise_error
      end

      it 'throws an error with 3-arity in the given block' do
        allow(described_class).to receive(:get_xml_contents)
        expect { described_class.write_xml(xml_file, stylesheet) { |hey1, hey2, hey3| } }.to raise_error(ArgumentError)
      end
    end
  end
end