Skip to content

Commit

Permalink
Working version of egor case
Browse files Browse the repository at this point in the history
  • Loading branch information
Eroui committed Feb 16, 2020
1 parent caa2684 commit f8c1900
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 5 deletions.
40 changes: 35 additions & 5 deletions commands/case.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import (
"github.com/fatih/color"
"github.com/urfave/cli/v2"
"fmt"
"os"
"os"
"github.com/chermehdi/egor/config"
"path"
"strconv"
)

func readFromStdin() ([]string, error) {
Expand All @@ -22,7 +25,7 @@ func readFromStdin() ([]string, error) {
lines = append(lines, line)
}

// TODO add check for errors
// TODO(Eroui) add check for errors
return lines, nil
}

Expand All @@ -42,19 +45,46 @@ func writeLinesToFile(filename string, lines []string) {
}
}


// TODO(Eroui): add checks on errors
func CustomCaseAction(context *cli.Context) error {
color.Green("Creating Custom Test Case...")

// Load meta data
cwd, err := os.Getwd()
if err != nil {
return err
}

meta_data, err := config.LoadMetaFromPath(path.Join(cwd, "egor-meta.json"))
if err != nil {
return err
}

color.Green("Provide your input:")
input_lines, _ := readFromStdin()

color.Green("Provide your output:")
output_lines, _ := readFromStdin()

case_name := "test-" + strconv.Itoa(len(meta_data.Inputs))

writeLinesToFile("sample.in", input_lines)
writeLinesToFile("sample.out", output_lines)
input_file_name := case_name + ".in"
output_file_name := case_name + ".out"

writeLinesToFile("inputs/" + input_file_name, input_lines)
writeLinesToFile("outputs/" + output_file_name, output_lines)

input_file := config.NewIoFile(input_file_name, "inputs/" + input_file_name, true)
output_file := config.NewIoFile(input_file_name, "outputs/" + output_file_name, true)

meta_data.Inputs = append(meta_data.Inputs, input_file)
meta_data.Outputs = append(meta_data.Outputs, output_file)

meta_data.SaveToFile(path.Join(cwd, "egor-meta.json"))

if err != nil {
fmt.Println(err)
}

color.Green("Created Custom Test Case...")
return nil
Expand Down
29 changes: 29 additions & 0 deletions config/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"io"
"path"
"os"
)

type IoFile struct {
Expand Down Expand Up @@ -88,9 +89,37 @@ func (egor *EgorMeta) Save(w io.Writer) error {
return err
}

func (egor *EgorMeta) SaveToFile(file_path string) error {
file, _ := CreateFile(file_path)
return egor.Save(file)
}

// TODO(chermehdi): probably this should't be a member function
func (egor *EgorMeta) Load(r io.Reader) error {
decoder := json2.NewDecoder(r)
err := decoder.Decode(egor)
return err
}

func LoadMeta(r io.Reader) (EgorMeta, error) {
var egor_meta EgorMeta
decoder := json2.NewDecoder(r)
err := decoder.Decode(&egor_meta)
return egor_meta, err
}

func LoadMetaFromPath(file_path string) (EgorMeta, error) {
file, _ := OpenFileFromPath(file_path)
return LoadMeta(file)
}

// TODO(Eroui): this is a duplicate function from parse.go
// consider moving this somewhere common or use the other one
func CreateFile(filePath string) (*os.File, error) {
return os.OpenFile(filePath, os.O_TRUNC|os.O_CREATE|os.O_WRONLY, 0777)
}

func OpenFileFromPath(filePath string) (*os.File, error) {
file, err := os.Open(filePath)
return file, err
}

0 comments on commit f8c1900

Please sign in to comment.