aboutsummaryrefslogtreecommitdiff
path: root/adapters/gtest/config.go
blob: 4105932e908f6e42e0bdb07b353111141c351a0a (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
package gtest

import (
  "log"
  "golang.flu0r1ne.net/planr"
  "strings"
  "github.com/BurntSushi/toml"
  "path"
)

type GtestDefaults struct {
  Name          *string
  Suite         *string
  Testfile      *string
  Srcs          *[]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.Srcs == nil)          { child.Srcs = parent.Srcs }
}


type GtestConfig struct {
  GtestDefaults
}

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 (cfg GtestConfig) srcList(srcDir string) string {
  var srcList string

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

  return srcList
}

func ParseConfig(prim toml.Primitive) (planr.InheritableConfig, error) {
    config := GtestConfig{}
  
    if err := toml.PrimitiveDecode(prim, &config); err != nil {
      return nil, err
    }
    
    return &config, nil
}

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

    if err := toml.PrimitiveDecode(prim, &config); err != nil {
      return nil, err
    }

    return &config, nil
}