--[[----------------------------------------------------------------------------

WFLoggerTest.lua

--------------------------------------------------------------------------------

ADOBE CONFIDENTIAL
------------------
Copyright 2013 Adobe Systems Incorporated
All Rights Reserved.

NOTICE: All information contained herein is, and remain the property of
Adobe Systems Incorporated and its suppliers, if any. The intellectual and
technical concepts contained herein are proprietary to Adobe Systems
Incorporated and its suppliers and may be covered by U.S. and Foreign
Patents, patents in process, and are protected by trade secret or copyright
law.  Dissemination of this information or reproduction of this material is
strictly forbidden unless prior written permission is obtained from
Adobe Systems Incorporated.

------------------------------------------------------------------------------]]

	-- WF.core.logging
local WFLogger = require 'WFLogger'

	-- WF.core.utils
local KSPathUtils = require 'KSPathUtils'

	-- WF.core.os
local WFPlatformInfo = require 'WFPlatformInfo'

--------------------------------------------------------------------------------

local loggerPath = KSPathUtils.child( 
			KSPathUtils.getStandardFilePath( 'carouselDocuments' ),
				'WFLoggerTest' )

-- By default, the logger creates log files in ~/Documents. Access to this folder 
-- requires permissions on later versions of OSX, causing an access prompt on test execution.
-- If we instead force the logger to use a different path, we can avoid the prompt.

local logger = WFLogger( 'test', loggerPath )

--============================================================================--

local WFLoggerTest = {}

--------------------------------------------------------------------------------

function WFLoggerTest.testFileLogging()

	-- FIXME_WIN32:
	if WFPlatformInfo.isWindows then return end
	
	logger:enable 'logfile'
	logger:trace 'Test logfile trace'
	logger:debug 'Test logfile debug'
	logger:info 'Test logfile info'
	logger:warn 'Test logfile warn'
	logger:error 'Test logfile error'
	logger:fatal 'Test logfile fatal'
	
	logger:enable 'print'
	logger:trace 'Test print trace'
	logger:debug 'Test print debug'
	logger:info 'Test print info'
	logger:warn 'Test print warn'
	logger:error 'Test print error'
	logger:fatal 'Test print fatal'

	logger:enable 'traceback'
	logger:trace 'Test traceback trace'
	logger:debug 'Test traceback debug'
	logger:info 'Test traceback info'
	logger:warn 'Test traceback warn'
	logger:error 'Test traceback error'
	logger:fatal 'Test traceback fatal'
	
	
end

--------------------------------------------------------------------------------

function WFLoggerTest.testFormattedLogging()

	logger:enable 'print'
	
	logger:warnf( "Expected warning log from thread %s", ':thread:' )
	
	logger:errorf( "Expected error log from traceback %s", ':traceback:' )
	
	logger:errorf( "Expected info log with table %s", { foo = 'bar' } )

	logger:errorf( "Expected info log with string %s", "blah" )

	logger:errorf( "Expected info log with number %s", 42 )

end

--------------------------------------------------------------------------------

function WFLoggerTest.testIsEnabled()

	local levelLoggerPath = KSPathUtils.child( 
			KSPathUtils.getStandardFilePath( 'carouselDocuments' ),
				'WFLoggerTest-testIsEnabled' )

	local levelLogger = WFLogger( 'testIsEnabled', levelLoggerPath )

	levelLogger:setLevel( 'warn' )

	assert( levelLogger:isEnabled( 'debug' ) == false )

	levelLogger:setLevel( 'info' )

	assert( levelLogger:isEnabled( 'debug' ) == false )

	levelLogger:setLevel( 'debug' )

	assert( levelLogger:isEnabled( 'debug' ) )

end

--------------------------------------------------------------------------------

local function diagLogContainsStr( someLogger, logLevel, str )

	local msgs = someLogger:DEBUG_UNIT_TEST_diagLogMessagesForLevel( logLevel )

	for _, msg in pairs( msgs ) do

		if string.find( msg, str ) then

			return true

		end

	end

	return false

end

--------------------------------------------------------------------------------

function WFLoggerTest.testDiagLogMessageCapture()

	local diagLogLoggerPath = KSPathUtils.child( 
			KSPathUtils.getStandardFilePath( 'carouselDocuments' ),
				'WFLoggerTest-diagLogLogger' )

	local diagLogLogger = WFLogger( 'diagLogLogger', diagLogLoggerPath )

	diagLogLogger:DEBUG_resetDiagnosticLogErrors()

	assert( #diagLogLogger:DEBUG_UNIT_TEST_diagLogMessagesForLevel( "warn" ) == 0,
				"expected no warn diag log msgs" )

	assert( #diagLogLogger:DEBUG_UNIT_TEST_diagLogMessagesForLevel( "error" ) == 0,
				"expected no error diag log msgs" )

	diagLogLogger:DEBUG_resetDiagnosticLogErrors()

	logger:error( "%B", "second arg" )

	assert( #diagLogLogger:DEBUG_UNIT_TEST_diagLogMessagesForLevel( "warn" ) == 0,
				"expected no warn diag log msgs" )

	assert( diagLogContainsStr( diagLogLogger, "error", "second arg" ),
			string.format( "'%s' contains '%s'", table.concat( diagLogLogger:DEBUG_UNIT_TEST_diagLogMessagesForLevel( "error" ), "" ), "second arg" ) )

	diagLogLogger:DEBUG_resetDiagnosticLogErrors()

	logger:warn( "%B", "second arg" )

	assert( #diagLogLogger:DEBUG_UNIT_TEST_diagLogMessagesForLevel( "error" ) == 0,
				"expected no error diag log msgs" )

	assert( diagLogContainsStr( diagLogLogger, "warn", "second arg" ),
			string.format( "'%s' contains '%s'", table.concat( diagLogLogger:DEBUG_UNIT_TEST_diagLogMessagesForLevel( "warn" ), "" ), "second arg" ) )

	diagLogLogger:DEBUG_resetDiagnosticLogErrors()

	logger:errorf( "%s", "second arg" )

	assert( #diagLogLogger:DEBUG_UNIT_TEST_diagLogMessagesForLevel( "warn" ) == 0,
				"expected no warn diag log msgs" )

	assert( diagLogContainsStr( diagLogLogger, "error", "second arg" ),
			string.format( "'%s' contains '%s'", table.concat( diagLogLogger:DEBUG_UNIT_TEST_diagLogMessagesForLevel( "error" ), "" ), "second arg" ) )

	diagLogLogger:DEBUG_resetDiagnosticLogErrors()

	logger:warnf( "%s", "second arg" )

	assert( #diagLogLogger:DEBUG_UNIT_TEST_diagLogMessagesForLevel( "error" ) == 0,
				"expected no error diag log msgs" )

	assert( diagLogContainsStr( diagLogLogger, "warn", "second arg" ),
			string.format( "'%s' contains '%s'", table.concat( diagLogLogger:DEBUG_UNIT_TEST_diagLogMessagesForLevel( "warn" ), "" ), "second arg" ) )

	diagLogLogger:DEBUG_resetDiagnosticLogErrors()

	logger:errorf( "%B", "second arg" )

	assert( #diagLogLogger:DEBUG_UNIT_TEST_diagLogMessagesForLevel( "warn" ) == 0,
				"expected no warn diag log msgs" )

	assert( #diagLogLogger:DEBUG_UNIT_TEST_diagLogMessagesForLevel( "error" ) == 1,
				"expected one error diag log msgs" )

	assert( not diagLogContainsStr( diagLogLogger, "error", "second arg" ),
			string.format( "'%s' contains '%s'", table.concat( diagLogLogger:DEBUG_UNIT_TEST_diagLogMessagesForLevel( "error" ), "" ), "second arg" ) )

	diagLogLogger:DEBUG_resetDiagnosticLogErrors()

	logger:warnf( "%B", "second arg" )

	assert( #diagLogLogger:DEBUG_UNIT_TEST_diagLogMessagesForLevel( "error" ) == 0,
				"expected no error diag log msgs" )

	assert( #diagLogLogger:DEBUG_UNIT_TEST_diagLogMessagesForLevel( "warn" ) == 1,
				"expected one warn diag log msgs" )

	assert( not diagLogContainsStr( diagLogLogger, "warn", "second arg" ),
			string.format( "'%s' contains '%s'", table.concat( diagLogLogger:DEBUG_UNIT_TEST_diagLogMessagesForLevel( "warn" ), "" ), "second arg" ) )

end

--------------------------------------------------------------------------------

return WFLoggerTest
