// Copyright 2020 Misha Brukman
// SPDX-License-Identifier: Apache-2.0
// https://misha.brukman.net/blog/2020/03/oo-style-testing-in-go/

// Note: the `-self_package` value here is synthetic; there isn't actually a
// Git repo at that URL. Feel free to change this to anything you want, but be
// sure to run `go mod init [...path..]` with that same path.
//
// In my case, I actually have `go.mod` and `go.sum` one level higher than this
// package:
//
// workdir
// ├── go.mod
// ├── go.sum
// ├── oo1/
// └── oo2/
//     ├── oo.go  <-- this file
//     └── oo_test.go
//
// Thus, I ran:
//
//     $ cd workdir
//     $ go mod init gitlab.com/mbrukman/oo-testing-in-go
//
//go:generate mockgen -source oo.go -destination mock_oo.go -package oo2 -self_package gitlab.com/mbrukman/oo-testing-in-go/oo2

package oo2

type MyInterface interface {
	IsOdd(n uint) bool
	IsEven(n uint) bool
}

type MyInterfaceHelper interface {
	isOddHelper(m MyInterface, n uint) bool
	isEvenHelper(m MyInterface, n uint) bool
}

type MyImpl struct {
	isOddImpl  func(m MyInterface, n uint) bool
	isEvenImpl func(m MyInterface, n uint) bool
}

func NewMyImpl() *MyImpl {
	m := new(MyImpl)
	m.isOddImpl = isOddHelperImpl
	m.isEvenImpl = isEvenHelperImpl
	return m
}

func (m *MyImpl) IsOdd(n uint) bool {
	return m.isOddImpl(m, n)
}

func (m *MyImpl) IsEven(n uint) bool {
	return m.isEvenImpl(m, n)
}

func isOddHelperImpl(m MyInterface, n uint) bool {
	if n == 0 {
		return false
	} else if n == 1 {
		return true
	}
	return m.IsEven(n - 1)
}

func isEvenHelperImpl(m MyInterface, n uint) bool {
	if n == 0 {
		return true
	} else if n == 1 {
		return false
	}
	return m.IsOdd(n - 1)
}
