## autogenerated unit-test
## from codegen/generate_unit-tests.sh
## the file will be overwritten in the next iteration
##
## author: Serkan Korkmaz

## alias and function similarity
## checks this ensures that
## momentum and MOM produces the same results
testthat::test_that(desc = 'Alias and function similarity', code = {
	## 1) test that the alias and
	##    function returns the same values
	output <- momentum(SPY)
	alias <- MOM(SPY)

	## 1.1) check if the values
	##      are equal
	testthat::expect_equal(
		object = output,
		expected = alias
	)
})

## type-checks for data.frames and
## matrices
##
## <matrix> object
testthat::test_that(desc = 'Class in, class out (<matrix>)', code = {
	## 1) check that the output class
	##    matches the input class
	testthat::expect_true(
		inherits(momentum(SPY), class(SPY))
	)
})

## <data.frame> object
testthat::test_that(desc = 'Class in, class out (<data.frame>)', code = {
	## 1) check that the output class
	##    matches the input class
	testthat::expect_true(
		inherits(momentum(BTC), class(BTC))
	)
})

## check that the default calls
## matches that of the constructed call
## with default values.
##
## NOTE: This test is more of a test of the internal
##       series() function
testthat::test_that(desc = 'Default calls', code = {
	testthat::expect_equal(
		object = momentum(
			BTC
		),
		expected = momentum(
			BTC,
			cols = ~close
		)
	)
})

## check that the length of the input
## matches the output length with na.bridge = TRUE
##
## <data.frame> object
testthat::test_that(desc = 'Equal length of input and output for <data.frame> with na.bridge = TRUE', code = {
	testthat::expect_equal(
		object = nrow(momentum(
			ATOM,
			na.bridge = TRUE
		)),
		expected = nrow(ATOM)
	)
})

## check that the rownames are being
## respected for <data.frame> with na.bridge = TRUE
testthat::test_that(desc = 'Row names are respected for <data.frame>, na.bridge = TRUE', code = {
	## extract row names
	x_names <- row.names(ATOM)

	## calculate indicator
	indicator <- momentum(ATOM, na.bridge = TRUE)

	testthat::expect_equal(
		object = x_names,
		expected = rownames(indicator)
	)
})

## check that the length of the input
## matches the output length
##
## <data.frame> object
testthat::test_that(desc = 'Equal length of input and output for <data.frame>', code = {
	testthat::expect_equal(
		object = nrow(momentum(
			BTC
		)),
		expected = nrow(BTC)
	)
})

## check that the rownames are being
## respected for <data.frame>
testthat::test_that(desc = 'Row names are respected for <data.frame>', code = {
	## extract row names
	x_names <- row.names(BTC)

	## calculate indicator
	indicator <- momentum(BTC)

	testthat::expect_equal(
		object = x_names,
		expected = rownames(indicator)
	)
})

## <matrix> object
testthat::test_that(desc = 'Equal length of input and output for <matrix>', code = {
	testthat::expect_equal(
		object = nrow(momentum(
			SPY
		)),
		expected = nrow(SPY)
	)
})

## check that the rownames are being
## respected for <matrix>
testthat::test_that(desc = 'Row names are respected for <matrix>', code = {
	## extract row names
	## NOTE: by default the SPY has no
	##       rownames
	rownames(SPY) <- paste0("row", 1:nrow(SPY))

	## calculate indicator
	indicator <- momentum(SPY)

	testthat::expect_equal(
		object = paste0("row", 1:nrow(SPY)),
		expected = rownames(indicator)
	)
})


## <plotly>-method checks for <data.frame>
## and <matrix>
##
## <data.frame> checks
testthat::test_that(desc = '<plotly>-methods for <data.frame>', code = {
	## check that momentum can
	## use <plotly> without any issues
	output <- testthat::expect_no_error(
		{
			chart(BTC)
			indicator(momentum)
		}
	)

	## check that the output
	## is a <plotly>-object
	testthat::expect_true(
		inherits(output, "plotly")
	)
})

## <matrix> checks
testthat::test_that(desc = '<plotly>-methods for <matrix>', code = {
	## check that momentum can
	## use <plotly> without any issues
	output <- testthat::expect_no_error(
		{
			chart(SPY)
			indicator(momentum)
		}
	)

	## check that the output
	## is a <plotly>-object
	testthat::expect_true(
		inherits(output, "plotly")
	)
})

## <ggplot>-method checks for <data.frame>
## and <matrix>
##
## <data.frame> checks
testthat::test_that(desc = '<ggplot>-methods for <data.frame>', code = {
	testthat::skip_if_not_installed("ggplot2")

	## check that momentum can
	## use <ggplot> without any issues
	output <- testthat::expect_no_error(
		{
			options(talib.chart.backend = "ggplot2")
			on.exit(options(talib.chart.backend = "plotly"))
			chart(BTC)
			indicator(momentum)
		}
	)

	## check that the output
	## is a <gg>-object or <talib_chart>
	testthat::expect_true(
		inherits(output, "gg") || inherits(output, "talib_chart")
	)
})

## <matrix> checks
testthat::test_that(desc = '<ggplot>-methods for <matrix>', code = {
	testthat::skip_if_not_installed("ggplot2")

	## check that momentum can
	## use <ggplot> without any issues
	output <- testthat::expect_no_error(
		{
			options(talib.chart.backend = "ggplot2")
			on.exit(options(talib.chart.backend = "plotly"))
			chart(SPY)
			indicator(momentum)
		}
	)

	## check that the output
	## is a <gg>-object or <talib_chart>
	testthat::expect_true(
		inherits(output, "gg") || inherits(output, "talib_chart")
	)
})

## check that <numeric> methods runs without
## issues and returns proper lengths
testthat::test_that(desc = '<numeric> methods', code = {
	## check that the <numeric> method
	## runs
	x <- testthat::expect_no_condition(
		momentum(BTC[[1]])
	)

	## the numeric methods returns <matrix>
	## depending on the underlying functions
	## so the checks for equal lengths is conditional
	target_length <- length(BTC[[1]])

	if (is.null(dim(x))) {
		testthat::expect_true(
			length(x) == target_length
		)
	} else {
		testthat::expect_true(
			nrow(x) == target_length
		)
	}
})
