require 'rubygems'
require 'bundler/setup'
require 'stringio'
require 'rainbow'
require 'require_relative'
require_relative '../../../lib/happy_robots/robots/msbuild_robot'

describe HappyRobots::MSBuildRobot do

  before :each do
    @output = StringIO.new
    @sut = HappyRobots::MSBuildRobot.new(@output)
  end

  it 'is named "MSBuild"' do
    HappyRobots::MSBuildRobot.name.should == 'MSBuild'
  end

  it 'activates for "Microsoft (R) Build Engine Version 4.0.30319.1"' do
    result = HappyRobots::MSBuildRobot.should_activate_for? 'Microsoft (R) Build Engine Version 4.0.30319.1'
    result.should {|value| value} # be a true predicate
  end

  it 'colors a "file starting" line with bright cyan' do
    line = 'Project "Project.sln" on node 1 (default targets).'
    @sut.process(line)
    @output.string.should == line.foreground(:cyan).bright
  end

  it 'colors a "file succeeded" line with bright cyan' do
    line = 'Done Building Project "Project.sln" (default targets).'
    @sut.process(line)
    @output.string.should == line.foreground(:cyan).bright
  end

  it 'colors a "file failed" line with bright cyan' do
    line = 'Done Building Project "Project.sln" (default targets) -- FAILED.'
    @sut.process(line)
    @output.string.should == line.foreground(:cyan).bright
  end

  it 'colors a "target starting" line with bright cyan' do
    line = 'TargetName:'
    @sut.process(line)
    @output.string.should == line.foreground(:cyan).bright
  end

  it 'colors a "build configuration" line with dark gray' do
    line = 'Building solution configuration "Debug|x86".'
    @sut.process(line)
    @output.string.should == line.foreground(:black).bright
  end

  it 'colors a "skipping target" line with dark gray' do
    line = 'Skipping target "TargetName" because of some reason.'
    @sut.process(line)
    @output.string.should == line.foreground(:black).bright
  end

  it 'colors a "build succeeded" line with bright green' do
    line = 'Build succeeded.'
    @sut.process(line)
    @output.string.should == line.foreground(:green).bright
  end

  it 'returns nil for "location tree" line' do
    line = '"Project.sln" (default target) (1) ->'
    @sut.process(line)
    @output.string.should == ""
  end

  it 'colors a "warning" line with bright yellow' do
    line = ' FileName.cs(100,30): warning CS0100: Here is the warning text. [Project.csproj]'
    @sut.process(line)
    @output.string.should == line.foreground(:yellow).bright
  end

  it 'colors a "warnings summary" line with bright yellow' do
    line = '    99 Warning(s)'
    @sut.process(line)
    @output.string.should == line.foreground(:yellow).bright
  end

  it 'colors lines from first "location tree" to "warning" with bright yellow' do
    lines = [
      "\"Project.sln\" (default target) (1) ->\n",
      "\"Project.csproj\" (default target) (6) ->\n",
      "(TargetName target) ->\n",
      "  FileName.cs(100,30): warning CS0100: Here is the warning text. [Project.csproj]"
    ]
    lines.each {|line| @sut.process(line)}
    @output.string.should == lines.join().foreground(:yellow).bright
  end

  it 'colors an "error" line with bright red' do
    line = ' FileName.cs(100,30): error CS0100: Here is the error text. [Project.csproj]'
    @sut.process(line)
    @output.string.should == line.foreground(:red).bright
  end

  it 'colors lines from first "location tree" to "error" with bright red' do
    lines = [
      "\"Project.sln\" (default target) (1) ->\n",
      "\"Project.csproj\" (default target) (6) ->\n",
      "(TargetName target) ->\n",
      "  FileName.cs(100,30): error CS0100: Here is the warning text. [Project.csproj]"
    ]
    lines.each {|line| @sut.process(line)}
    @output.string.should == lines.join().foreground(:red).bright
  end

  it 'colors an "errors summary" line with bright red' do
    line = '    99 Error(s)'
    @sut.process(line)
    @output.string.should == line.foreground(:red).bright
  end

end