aboutsummaryrefslogtreecommitdiff
path: root/test/function.test.cpp
blob: 9aca0eba60c5f30f0f201d828debd06cd4ac3a9d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include "util/function.hpp"

#include <cstdint>
#include <doctest/doctest.h>

TEST_CASE("normalize_lamda")
{
	SUBCASE("Can return a function that returns a int")
	{
		const int number = 58;

		CHECK(normalize_lambda(
				  [number]()
				  {
					  return number;
				  })() == number);
	}

	SUBCASE("Can preserve object state")
	{
		class Book
		{
		public:
			Book() noexcept = default;

			void read_page() noexcept
			{
				_pages_read++;
			}

			[[nodiscard]] uint32_t pages_read() const noexcept
			{
				return _pages_read;
			}

		private:
			uint32_t _pages_read{0U};
		};

		auto book = Book();

		book.read_page();
		book.read_page();
		book.read_page();

		CHECK(normalize_lambda(
				  [book]()
				  {
					  return book.pages_read();
				  })() == 3);
	}
}