meson: Cleanup tests and make them execute again

The tests were previously building but not executing. Fix the build so
they execute and pass again.

Change-Id: I6e29eadd4f51cf47d05f7172f37c76688955ac61
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/meson.build b/meson.build
index 47cee55..77f7761 100644
--- a/meson.build
+++ b/meson.build
@@ -10,11 +10,7 @@
     version: '1.0',
 )
 
-build_tests = get_option('tests')
-
 fmt = dependency('fmt')
-gmock = dependency('gmock')
-gtest = dependency('gtest', main: true)
 
 conf = configuration_data()
 conf.set_quoted('BUSNAME_PREFIX', get_option('busname-prefix'))
@@ -97,5 +93,7 @@
     install: true)
 
 subdir('msl')
-subdir('test')
+if not get_option('tests').disabled()
+  subdir('test')
+endif
 subdir('tools')
diff --git a/subprojects/googletest.wrap b/subprojects/googletest.wrap
new file mode 100644
index 0000000..56da9ef
--- /dev/null
+++ b/subprojects/googletest.wrap
@@ -0,0 +1,3 @@
+[wrap-git]
+url = https://github.com/google/googletest
+revision = HEAD
diff --git a/test/meson.build b/test/meson.build
index 1cd4eef..cb9d8cc 100644
--- a/test/meson.build
+++ b/test/meson.build
@@ -1,60 +1,41 @@
-average_unittest = executable(
+gtest = dependency('gtest', main: true, disabler: true, required: false)
+gmock = dependency('gmock', disabler: true, required: false)
+if not gtest.found() or not gmock.found()
+  gtest_opts = import('cmake').subproject_options()
+  gtest_opts.add_cmake_defines({'CMAKE_CXX_FLAGS': '-Wno-pedantic'})
+  gtest_proj = import('cmake').subproject(
+    'googletest',
+    options: gtest_opts,
+    required: false)
+  if gtest_proj.found()
+    gtest = declare_dependency(
+      dependencies: [
+        dependency('threads'),
+        gtest_proj.dependency('gtest'),
+        gtest_proj.dependency('gtest_main'),
+      ])
+    gmock = gtest_proj.dependency('gmock')
+  else
+    assert(not build_tests.enabled(), 'Googletest is required')
+  endif
+endif
+
+tests = [
     'average_unittest',
-    'average_unittest.cpp',
-    dependencies: [
-        gmock,
-        gtest,
-        hwmon_dep,
-    ],
-)
-
-env_unittest = executable(
     'env_unittest',
-    'env_unittest.cpp',
-    dependencies: [
-        gmock,
-        gtest,
-        hwmon_dep,
-    ],
-)
-
-fanpwm_unittest = executable(
     'fanpwm_unittest',
-    'fanpwm_unittest.cpp',
-    dependencies: [
-        gmock,
-        gtest,
-        hwmon_dep,
-    ],
-)
-
-hwmon_unittest = executable(
     'hwmon_unittest',
-    'hwmon_unittest.cpp',
-    dependencies: [
-        gmock,
-        gtest,
-        hwmon_dep,
-    ],
-)
-
-hwmonio_default_unittest = executable(
     'hwmonio_default_unittest',
-    'hwmonio_default_unittest.cpp',
-    dependencies: [
-        gtest,
-        gmock,
-        hwmon_dep,
-    ],
-)
-
-sensor_unittest = executable(
     'sensor_unittest',
-    'gpio.cpp',
-    'sensor_unittest.cpp',
-    dependencies: [
-        gtest,
-        gmock,
-        hwmon_dep,
-    ],
-)
+]
+
+foreach t : tests
+    test(
+        t,
+        executable(
+            t.underscorify(),
+            t + '.cpp',
+            'gpio.cpp',
+            implicit_include_directories: false,
+            dependencies: [hwmon_dep, gtest, gmock]))
+endforeach