From 5e6f10c9b857f4e9027f48df0f4fa74f4b3f189d Mon Sep 17 00:00:00 2001 From: yyforyongyu Date: Thu, 12 Jan 2023 21:16:18 +0800 Subject: [PATCH] lnmock: add new package `lnmock` to host mocks This commit introduces a new package to host mocked objects that can be used for all the unit tests. --- .golangci.yml | 7 +++++++ lnmock/clock.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 lnmock/clock.go diff --git a/.golangci.yml b/.golangci.yml index 2ab612f58..7b97fee00 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -238,3 +238,10 @@ issues: - govet # itest case can be very long so we disable long function check. - funlen + + - path: lnmock/* + linters: + # forcetypeassert is skipped for the mock because the test would fail + # if the returned value doesn't match the type, so there's no need to + # check the convert. + - forcetypeassert diff --git a/lnmock/clock.go b/lnmock/clock.go new file mode 100644 index 000000000..bd00c42c8 --- /dev/null +++ b/lnmock/clock.go @@ -0,0 +1,30 @@ +// NOTE: forcetypeassert is skipped for the mock because the test would fail if +// the returned value doesn't match the type. +package lnmock + +import ( + "time" + + "github.com/lightningnetwork/lnd/clock" + "github.com/stretchr/testify/mock" +) + +// MockClock implements the `clock.Clock` interface. +type MockClock struct { + mock.Mock +} + +// Compile time assertion that MockClock implements clock.Clock. +var _ clock.Clock = (*MockClock)(nil) + +func (m *MockClock) Now() time.Time { + args := m.Called() + + return args.Get(0).(time.Time) +} + +func (m *MockClock) TickAfter(d time.Duration) <-chan time.Time { + args := m.Called(d) + + return args.Get(0).(chan time.Time) +}