aboutsummaryrefslogtreecommitdiff
path: root/adapters/gtest_adapter.go
blob: fd06a486556790b29757697242ee08b44f346396 (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package adapters

import (
	"bytes"
	"log"
	"os"
	"path"
	"strings"
	"text/template"

	"github.com/BurntSushi/toml"
	"golang.flu0r1ne.net/planr"
)

/*
  CONFIGURATION
*/
type GtestDefaults struct {
  Name      *string
  Suite     *string
  Testfile  *string
  Test_root *string
  Srcs      *[]string
  Srcs_root *string
}

func (child *GtestDefaults) Inherit(p interface{}) {
  parent := p.(*GtestDefaults)

  if(child.Name == nil)      { child.Name = parent.Name }
  if(child.Suite == nil)     { child.Suite = parent.Suite }
  if(child.Testfile == nil)  { child.Testfile = parent.Testfile }
  if(child.Test_root == nil) { child.Test_root = parent.Test_root }
  if(child.Srcs == nil)      { child.Srcs = parent.Srcs }
  if(child.Srcs_root == nil) { child.Srcs_root = parent.Srcs_root }
}


type GtestConfig struct {
  GtestDefaults
}


func (cfg GtestConfig) joinTests(path_ string) string {
  if cfg.Test_root == nil {
    return planr.JoinConfigDir("tests", path_)
  }
   
  return planr.JoinConfigDir(*cfg.Test_root, path_)
}

func (cfg GtestConfig) joinSrcs(path_ string) string {
  if cfg.Srcs_root == nil {
    return planr.JoinConfigDir("../src", path_)
  }

  return planr.JoinConfigDir(*cfg.Srcs_root, path_)
}

func (cfg GtestConfig) srcList() string {
  var srcList string

  if cfg.Srcs != nil {
    srcs := make([]string, len(*cfg.Srcs))
    for i, src := range *cfg.Srcs {
      srcs[i] = cfg.joinSrcs(src)
    }
    
    srcList = strings.Join(srcs, "\n  ")
  }

  return srcList
}

func (g GtestConfig) EnsureSatisfied(path string) {
  if g.Name == nil {
    log.Fatalf("\"name\" is not defined for unit: %s\n", path)
  } else if g.Suite == nil {
    log.Fatalf("\"suite\" is not defined for unit: %s\n", path)
  } else if g.Testfile == nil {
    log.Fatalf("\"testfile\" is not defined for unit: %s\n", path)
  }
}

func primitiveDecode(primitive toml.Primitive, config interface{}) {
  if err := toml.PrimitiveDecode(primitive, config); err != nil {
    log.Fatal(err)
  }
}

func ParseConfig(prim toml.Primitive) planr.InheritableConfig {
    config := GtestConfig{}

    primitiveDecode(prim, &config)

    return &config
}

func ParseDefaultConfig(prim toml.Primitive) planr.InheritableConfig {
    config := GtestDefaults{}

    primitiveDecode(prim, &config)

    return &config
}

/*
  BUILD PROCESS
*/

type GtestAdapter struct {
  unitTmpl  *template.Template
  cbuf      bytes.Buffer
}

func (a *GtestAdapter) Config() planr.AdapterConfig {
  return planr.AdapterConfig {
    Name: "gtest",
    ParseConfig: ParseConfig,
    ParseDefaultConfig: ParseDefaultConfig,
 }
}

func (a *GtestAdapter) InitializeBuild() {
  a.unitTmpl  = UnitTemplate()
  a.cbuf = bytes.Buffer{}

  WriteCMakeBoiler(&a.cbuf)
}

const GTEST_CMAKE = "CMakeLists.txt"

func Chdir(dir string) {
  if err := os.Chdir(dir); err != nil {
    log.Fatal(err)
  }
}

func (a *GtestAdapter) Build(tc planr.TestCase) {
  cfg := tc.AdapterConfig("gtest").(*GtestConfig)
  cfg.EnsureSatisfied(tc.Path)

  cname    := tc.Cname
  testfile := cfg.joinTests(*cfg.Testfile)
  srcList  := cfg.srcList()

  err := a.unitTmpl.Execute(&a.cbuf, struct {Cname, File, Srcs string} {
    cname, testfile, srcList,
  })

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

func (a *GtestAdapter) FinalizeBuild() {
  dir :=  a.Config().ConfigDir()
  cmakeFile := path.Join(dir, GTEST_CMAKE)
  
  file, err := os.OpenFile(cmakeFile, 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", cmakeFile, err)
  }

  file.Write(a.cbuf.Bytes())

  Chdir(dir)

  planr.RunCmd("cmake", "-S", ".", "-B", ".")
}

func (a *GtestAdapter) Make() {
  planr.RunCmd("make", "-k")
}

func (a *GtestAdapter) Evaluate(tc planr.TestCase) planr.TestResult {
  return planr.TestResult {}
}

func (a *GtestAdapter) Cleanup() { }