Skip to content

Commit

Permalink
Fixed merge conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
zoeyfyi committed Apr 16, 2017
2 parents 336ffbb + 02c6752 commit 1e1977a
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 64 deletions.
10 changes: 9 additions & 1 deletion import.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ package main

import "strings"

var Imports = []string{"fmt"}
var Imports []string

func init() {
initImports()
}

func addImport(importName string) {
for _, i := range Imports {
Expand All @@ -25,3 +29,7 @@ func importType(typeName string) string {

return typeName
}

func initImports() {
Imports = []string{"fmt"}
}
3 changes: 3 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@ func Start(args []string) string {

all += ")\n\n" + go_out.String()

// Reset the imports
initImports()

return all
}

Expand Down
79 changes: 72 additions & 7 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,87 @@
package main

import (
"bytes"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
)

// This test exists for code coverage and does not actually test anything. The
// real tests are performed with run-tests.h.
//
// In the future it would be nice to combine them so that the files only have to
// be compiled once and we don't need the extra bash script.
var (
cPath = "build/a.out"
goPath = "build/go.out"
stdin = "7"
args = []string{"some", "args"}
)

type programOut struct {
stdout bytes.Buffer
stderr bytes.Buffer
isZero bool
}

// TestIntegrationScripts tests all programs in the tests directory
func TestIntegrationScripts(t *testing.T) {
files, err := filepath.Glob("tests/*/*.c")
if err != nil {
panic(err)
t.Fatal(err)
}

for _, file := range files {
Start([]string{file})
// Create build folder
os.Mkdir("build/", os.ModePerm)

t.Run(file, func(t *testing.T) {
cProgram := programOut{}
goProgram := programOut{}

// Compile C
err := exec.Command("clang", "-lm", "-o", cPath, file).Run()
if err != nil {
t.Fatal(err)
}

// Run C program
cmd := exec.Command(cPath, args...)
cmd.Stdin = strings.NewReader(stdin)
cmd.Stdout = &cProgram.stdout
cmd.Stderr = &cProgram.stderr
err = cmd.Run()
cProgram.isZero = err == nil

// Compile Go
goSrc := Start([]string{file})
ioutil.WriteFile("build/main.go", []byte(goSrc), os.ModePerm)
err = exec.Command("go", "build", "-o", goPath, "build/main.go").Run()
if err != nil {
t.Fatal(err)
}

// Run Go program
cmd = exec.Command(goPath, args...)
cmd.Stdin = strings.NewReader(stdin)
cmd.Stdout = &goProgram.stdout
cmd.Stderr = &goProgram.stderr
err = cmd.Run()
goProgram.isZero = err == nil

// Check if both exit codes are zero (or non-zero)
if cProgram.isZero != goProgram.isZero {
t.Fatalf("Expected: %t, Got: %t", cProgram.isZero, goProgram.isZero)
}

// Check stderr
if cProgram.stderr.String() != goProgram.stderr.String() {
t.Fatalf("Expected %q, Got: %q", cProgram.stderr.String(), goProgram.stderr.String())
}

// Check stdout
if cProgram.stdout.String() != goProgram.stdout.String() {
t.Fatalf("Expected %q, Got: %q", cProgram.stdout.String(), goProgram.stdout.String())
}
})
}
}
56 changes: 0 additions & 56 deletions run-tests.sh

This file was deleted.

0 comments on commit 1e1977a

Please sign in to comment.