aboutsummaryrefslogtreecommitdiff
path: root/adapters/gtest/templating.go
blob: a9a3b07bcb8a083a83bb9e055e6463cdc1893e36 (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package gtest

import (
	"io"
	"log"
	"text/template"
        "os"
)

type cmakeUnit struct {
  Cname string
  File  string
  Srcs  string
};

func genCmake(out string, units []cmakeUnit) {
  file, err := os.OpenFile(out, os.O_RDWR | os.O_CREATE, 0644)
  defer func () {
    err := file.Close()

    if err != nil {
      log.Fatal(err)
    }
  }()

  if err != nil {
    log.Fatalf("Could not open CMakeFile (%s)\n%v", out, err)
  }

  writeBoiler(file)
  
  tmpl := unitTemplate()

  for _, unit := range units {
    if err := tmpl.Execute(file, unit); err != nil {
      log.Fatalf("Failed to generate unit %s: %v", unit.Cname, err);
    }
  }
}


func unitTemplate() *template.Template {
  tmpl, err := template.New("gtest_unit").Parse(`
add_executable(
  "{{.Cname}}"
  "{{.File}}"
  {{.Srcs}}
)

target_link_libraries(
  "{{.Cname}}"
  gtest_main
)

gtest_discover_tests(
  "{{.Cname}}"
)
`)

  if err != nil {
    log.Fatalf("Cannot load Gtest Unit Template %v", err)
  }

  return tmpl
}

func writeBoiler(w io.Writer) {
  w.Write([]byte(`
cmake_minimum_required (VERSION 3.1.0)

project(PlanRGtestAdapter)

include(FetchContent)
FetchContent_Declare(
  googletest
  URL https://github.com/google/googletest/archive/609281088cfefc76f9d0ce82e1ff6c30cc3591e5.zip
)

include(GoogleTest)
FetchContent_MakeAvailable(googletest)
`))
}