Skip to content

Commit

Permalink
New() now has exactly one (mandatory) argument
Browse files Browse the repository at this point in the history
  • Loading branch information
rveen committed May 10, 2018
1 parent 0a9e97f commit fa610bf
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 57 deletions.
66 changes: 29 additions & 37 deletions all_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,15 +205,15 @@ func TestBinParser3(t *testing.T) {
r := []byte{1, 'G', 0, 1, 'a', 0, 2, 'b', 0, 0}

// Starting from a NilGraph
g := New()
g := New(nil)
b := g.Binary()

if len(b) != 4 {
t.Error("Binary() on NilGraph")
}

// Starting from a NilGraph
g = New()
g = New(nil)
g.Add("a").Add("b")
b = g.Binary()

Expand Down Expand Up @@ -263,20 +263,12 @@ func TestBinParser4(t *testing.T) {
func TestParser00(t *testing.T) {
p := FromString("a")

if p.This != nil {
t.Error("parse should always return a nil root")
}

if p.Text() != "a" {
t.Error("e1", p.Text())
}

p = FromString("a\nb")

if p.This != nil {
t.Error("parse should always return a nil root")
}

if p.Text() != "a\nb" {
t.Error("e2", p.Show())
}
Expand Down Expand Up @@ -518,7 +510,7 @@ func TestGet2(t *testing.T) {
t.Error("ogdl.Get")
}

g = New()
g = New(nil)
g.Add("a").Add("b")

n = g.Get("a")
Expand Down Expand Up @@ -619,7 +611,7 @@ func TestGet3(t *testing.T) {
func TestCopyAndSubstitute(t *testing.T) {
g := FromString("a b, c d, aa a")

g2 := New()
g2 := New("_")
g2.Copy(g)

if !g.Equals(g2) {
Expand Down Expand Up @@ -696,7 +688,7 @@ func TestGet1(t *testing.T) {

func TestNilGraph(t *testing.T) {

g := New()
g := New(nil)

if g.Len() != 0 {
t.Error("nil node size not 0")
Expand Down Expand Up @@ -732,13 +724,13 @@ func TestAddChaining(t *testing.T) {
t.Error("Add chaining")
}

g = New().Add("b")
g = New(nil).Add("b")
s = g.Show()
if s != "b" {
t.Error("Add after NewGraph")
}

g = New()
g = New(nil)
g.Add("a").Add("b")
s = g.Show()
if s != "_\n a\n b" {
Expand All @@ -747,7 +739,7 @@ func TestAddChaining(t *testing.T) {
}

func TestGraph_String(t *testing.T) {
g := New()
g := New("*")
s := g.String()
if len(s) != 0 {
t.Error("g.String() returns something with a nil node")
Expand All @@ -756,7 +748,7 @@ func TestGraph_String(t *testing.T) {

func TestGraph_Delete(t *testing.T) {

g := New()
g := New(nil)

g.Add(1)
g.Add(2)
Expand Down Expand Up @@ -928,7 +920,7 @@ func TestEvalPath1(t *testing.T) {
t.Error("NewPath", s)
}

g := New()
g := New(nil)
g.Add("a").Add("b")

i, _ := g.Eval(path)
Expand All @@ -937,7 +929,7 @@ func TestEvalPath1(t *testing.T) {
t.Error("EvalPath", _show(i))
}

g = New()
g = New(nil)
g.Add("a").Add(1)

i, _ = g.Eval(path)
Expand All @@ -946,7 +938,7 @@ func TestEvalPath1(t *testing.T) {
t.Error("EvalPath 1", _show(i), _typeOf(i))
}

g = New()
g = New(nil)
g.Add("a").Add("id").Add("100")
i, _ = g.Eval(path)

Expand Down Expand Up @@ -1020,7 +1012,7 @@ func TestEvalPath3(t *testing.T) {

func TestEvalScalar(t *testing.T) {

g := New()
g := New(nil)
p := New("1")

// constants
Expand Down Expand Up @@ -1074,7 +1066,7 @@ func TestEvalScalar(t *testing.T) {
//
func TestEvalArgOfGraph(t *testing.T) {

g := New()
g := New(nil)
g.Add("a").Add("c").Add(int64(1))
g.Add("b").Add("c")

Expand Down Expand Up @@ -1149,7 +1141,7 @@ func TestEvalExpression(t *testing.T) {
}

// Assign
g = New()
g = New(nil)
e = "a=1"
p = NewExpression(e)
g.Eval(p)
Expand Down Expand Up @@ -1309,7 +1301,7 @@ func TestEvalExpression(t *testing.T) {

func TestEvalBool(t *testing.T) {

g := New()
g := New(nil)
g.Add("a").Add(1)

p := NewExpression("1=='1'")
Expand Down Expand Up @@ -1352,7 +1344,7 @@ func TestGetTypes(t *testing.T) {
t.Error("Float64")
}

g = New()
g = New(nil)
g.Add(float32(111.2))
if g.Float64() != 111.2 {
t.Error("Float64")
Expand Down Expand Up @@ -1425,7 +1417,7 @@ func TestI2string(t *testing.T) {

func TestTemplate1(ts *testing.T) {
// Context
g := New()
g := New(nil)
g.Add("b").Add(1)

t := NewTemplate("a $b")
Expand All @@ -1439,7 +1431,7 @@ func TestTemplate1(ts *testing.T) {

func TestTemplateOperatorConfusion(ts *testing.T) {
// Context
g := New()
g := New(nil)
g.Add("b").Add(1)

t := NewTemplate("$(a='/') $a $b")
Expand All @@ -1453,7 +1445,7 @@ func TestTemplateOperatorConfusion(ts *testing.T) {

func TestTemplateIfEmptyString(ts *testing.T) {
// Context
g := New()
g := New(nil)
g.Add("b").Add("")

t := NewTemplate("$if(b=='') text $end")
Expand All @@ -1473,7 +1465,7 @@ func TestTemplateIf0(ts *testing.T) {

func TestTemplateIf(ts *testing.T) {
// Context
g := New()
g := New(nil)

t := NewTemplate("$if('false') a $else b $end")

Expand All @@ -1493,7 +1485,7 @@ func TestTemplateIf(ts *testing.T) {

func TestTemplateFor1(ts *testing.T) {
// Context
g := New()
g := New(nil)
c := g.Add("b")
c.Add(1)
c.Add(2)
Expand Down Expand Up @@ -1543,7 +1535,7 @@ func ExampleTemplate_For2() {

func TestFunction3(ts *testing.T) {
// Context
g := New()
g := New(nil)

t := NewTemplate("$(R.y='h')$(R.x0='user')")
t.Process(g)
Expand Down Expand Up @@ -1581,7 +1573,7 @@ func Sin(x float64) float64 {

func TestFunction2b(t *testing.T) {

g := New()
g := New(nil)
f := g.Add("math")
f.Add(&Math{})

Expand All @@ -1600,7 +1592,7 @@ func TestFunction2b(t *testing.T) {

func TestFunction2c(t *testing.T) {

g := New()
g := New(nil)
f := g.Add("Sin")
f.Add(Sin)

Expand Down Expand Up @@ -1692,7 +1684,7 @@ func ExampleGraph_Set_index() {

func ExampleGraph_Set_a() {

g := New()
g := New(nil)

g.Add("R").Add("b")
r := g.Node("R")
Expand Down Expand Up @@ -1728,7 +1720,7 @@ func ExampleGraph_Get() {
func ExampleNewTemplate() {
p := NewTemplate("Hello, $user")

g := New()
g := New("_")
g.Add("user").Add("Jenny")

fmt.Println(string(p.Process(g)))
Expand All @@ -1738,7 +1730,7 @@ func ExampleNewTemplate() {

func ExampleNewExpression() {
e := NewExpression("1-2+3")
g := New()
g := New(nil)
i, _ := g.Eval(e)

fmt.Println(i)
Expand All @@ -1758,7 +1750,7 @@ func ExampleGraph_Check() {
}

func ExampleGraph_Eval() {
g := New()
g := New("_")
g.Add("a").Add(4)
g.Add("b").Add("4")

Expand Down
2 changes: 1 addition & 1 deletion encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func FromJSON(buf []byte) (*Graph, error) {

func toGraph(v interface{}) *Graph {

g := New()
g := New("_")

switch v.(type) {

Expand Down
9 changes: 4 additions & 5 deletions eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (g *Graph) getPath(p *Graph) (*Graph, error) {
return nil, ErrInvalidIndex
}

r := New()
r := New("[")
r.Add(ctx.GetAt(ix))
ctxPrev = ctx
ctx = r
Expand All @@ -82,7 +82,7 @@ func (g *Graph) getPath(p *Graph) (*Graph, error) {
return nil, ErrInvalidIndex
}

r := New()
r := New("{")
ix := pathNode.index(g) + 1 // 0 is {}, {n} becomes ix = n+1

if ix < 0 {
Expand Down Expand Up @@ -208,7 +208,7 @@ func (g *Graph) evalPath(p *Graph) (interface{}, error) {
return nil, ErrInvalidIndex
}

r := New()
r := New("{")
ix := pathNode.index(g) + 1 // 0 is {}, {n} becomes ix = n+1

if ix < 0 {
Expand Down Expand Up @@ -301,8 +301,7 @@ func (g *Graph) evalPath(p *Graph) (interface{}, error) {
}

if addRoot {
r := New()
r.This = "["
r := New("[")
r.Add(ctx)
ctx = r
}
Expand Down
2 changes: 1 addition & 1 deletion event.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (e *SimpleEventHandler) Dec() {
func (e *SimpleEventHandler) Tree() *Graph {

g := make([]*Graph, e.max+2)
g[0] = New()
g[0] = New("_")
/*
println("ev.Tree", len(e.items), len(e.levels), e.max)
Expand Down
2 changes: 1 addition & 1 deletion function.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (g *Graph) function(path *Graph, typ interface{}) (interface{}, error) {

if v.Type() == rfType {
// Remote function
n := New()
n := New("_")
nn := n.Add(path.Out[1].This)
if len(path.Out) > 2 {
for _, arg := range path.Out[2].Out {
Expand Down
2 changes: 1 addition & 1 deletion gettypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (g *Graph) Find(re string) (*Graph, error) {
return nil, err
}

r := New()
r := New("_")

for _, node := range g.Out {
if exp.MatchString(node.ThisString()) {
Expand Down
12 changes: 4 additions & 8 deletions graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,9 @@ type Graph struct {
Out []*Graph
}

// New returns a pointer to Graph, which will be either empty or contain the
// (optional) object given.
func New(n ...interface{}) *Graph {
if len(n) == 0 {
return &Graph{}
}
return &Graph{n[0], nil}
// New returns a pointer to Graph initialized to the object given.
func New(n interface{}) *Graph {
return &Graph{n, nil}
}

// Len returns the number of subnodes (outgoing edges, out degree) of this node.
Expand Down Expand Up @@ -150,7 +146,7 @@ func (g *Graph) Clone() *Graph {
return nil
}

c := New()
c := New(nil)
c.This = g.This

for _, n := range g.Out {
Expand Down
Loading

0 comments on commit fa610bf

Please sign in to comment.