Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed ParseBytes and ParseFile by adding agclearerrors in C #66

Merged
merged 5 commits into from
Mar 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cgraph/cgraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ func toEdge(e *ccall.Agedge) *Edge {
}

func ParseBytes(bytes []byte) (*Graph, error) {
ccall.Agclearerrors()
graph, err := ccall.Agmemread(string(bytes))
if err != nil {
return nil, err
Expand All @@ -113,6 +114,7 @@ func ParseFile(path string) (*Graph, error) {
if err != nil {
return nil, err
}
ccall.Agclearerrors()
graph, err := ccall.Agmemread(string(file))
if err != nil {
return nil, err
Expand Down
69 changes: 69 additions & 0 deletions graphviz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package graphviz_test

import (
"bytes"
"io/ioutil"
"os"
"testing"

"github.com/goccy/go-graphviz"
Expand Down Expand Up @@ -80,3 +82,70 @@ func TestGraphviz_Image(t *testing.T) {
})
})
}

func TestParseBytes(t *testing.T) {
type test struct {
input string
expected_valid bool
}

tests := []test{
{input: "graph test { a -- b }", expected_valid: true},
{input: "graph test { a -- b", expected_valid: false},
{input: "graph test { a -- b }", expected_valid: true},
{input: "graph test { a -- }", expected_valid: false},
{input: "graph test { a -- c }", expected_valid: true},
{input: "graph test { a - b }", expected_valid: false},
{input: "graph test { d -- e }", expected_valid: true},
}

for i, test := range tests {
_, err := graphviz.ParseBytes([]byte(test.input))
actual_valid := err == nil
if actual_valid != test.expected_valid {
t.Errorf("Test %d of TestParseBytes failed. Parsing error: %+v", i+1, err)
}
}
}

func TestParseFile(t *testing.T) {
type test struct {
input string
expected_valid bool
}

tests := []test{
{input: "graph test { a -- b }", expected_valid: true},
{input: "graph test { a -- b", expected_valid: false},
{input: "graph test { a -- b }", expected_valid: true},
{input: "graph test { a -- }", expected_valid: false},
{input: "graph test { a -- c }", expected_valid: true},
{input: "graph test { a - b }", expected_valid: false},
{input: "graph test { d -- e }", expected_valid: true},
}

createTempFile := func(t *testing.T, content string) *os.File {
file, err := ioutil.TempFile("", "*")
if err != nil {
t.Fatalf("There was an error creating a temporary file. Error: %+v", err)
return nil
}
_, err = file.WriteString(content)
if err != nil {
t.Fatalf("There was an error writing '%s' to a temporary file. Error: %+v", content, err)
return nil
}
return file
}

for i, test := range tests {
tmpfile := createTempFile(t, test.input)
defer os.Remove(tmpfile.Name())

_, err := graphviz.ParseFile(tmpfile.Name())
actual_valid := err == nil
if actual_valid != test.expected_valid {
t.Errorf("Test %d of TestParseFile failed. Parsing error: %+v", i+1, err)
}
}
}
4 changes: 4 additions & 0 deletions internal/ccall/cgraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -1182,6 +1182,10 @@ func Agerr(msg string) {
C.free(unsafe.Pointer(s))
}

func Agclearerrors() {
C.agclearerrors()
}

func init() {
C.agseterr(C.AGMAX)
}
17 changes: 12 additions & 5 deletions internal/ccall/cgraph/agerror.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/* vim:set shiftwidth=4 ts=8: */

/*************************************************************************
* Copyright (c) 2011 AT&T Intellectual Property
* Copyright (c) 2011 AT&T Intellectual Property
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
Expand Down Expand Up @@ -59,7 +59,7 @@ char *aglasterr()
}

/* userout:
* Report messages using a user-supplied write function
* Report messages using a user-supplied write function
*/
static void
userout (agerrlevel_t level, const char *fmt, va_list args)
Expand Down Expand Up @@ -169,10 +169,17 @@ void agwarningf(const char *fmt, ...)

int agerrors() { return agmaxerr; }

int agreseterrors()
{
int agreseterrors()
{
int rc = agmaxerr;
agmaxerr = 0;
return rc;
return rc;
}

void agclearerrors()
denk0403 marked this conversation as resolved.
Show resolved Hide resolved
{
if (agerrout)
fclose(agerrout);
agerrout = NULL;
aglast = 0;
}
3 changes: 2 additions & 1 deletion internal/ccall/cgraph/cgraph.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/* vim:set shiftwidth=4 ts=8: */

/*************************************************************************
* Copyright (c) 2011 AT&T Intellectual Property
* Copyright (c) 2011 AT&T Intellectual Property
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
Expand Down Expand Up @@ -388,6 +388,7 @@ extern void agerrorf(const char *fmt, ...);
extern void agwarningf(const char *fmt, ...);
extern int agerrors(void);
extern int agreseterrors(void);
extern void agclearerrors(void);
extern agusererrf agseterrf(agusererrf);

/* data access macros */
Expand Down