Skip to content

Commit 77ab50f

Browse files
committed
Init tests
1 parent f8c1900 commit 77ab50f

File tree

3 files changed

+152
-54
lines changed

3 files changed

+152
-54
lines changed

commands/case.go

+63-43
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
package commands
1+
package commands
22

33
import (
44
"bufio"
5+
"fmt"
6+
"github.com/chermehdi/egor/config"
57
"github.com/fatih/color"
68
"github.com/urfave/cli/v2"
7-
"fmt"
89
"os"
9-
"github.com/chermehdi/egor/config"
1010
"path"
1111
"strconv"
1212
)
1313

14-
func readFromStdin() ([]string, error) {
14+
func readFromStdin() []string {
1515
scn := bufio.NewScanner(os.Stdin)
1616
var lines []string
1717
for scn.Scan() {
@@ -24,32 +24,54 @@ func readFromStdin() ([]string, error) {
2424
}
2525
lines = append(lines, line)
2626
}
27-
28-
// TODO(Eroui) add check for errors
29-
return lines, nil
27+
return lines
3028
}
3129

3230
func writeLinesToFile(filename string, lines []string) {
3331
f, err := os.Create(filename)
3432
if err != nil {
35-
fmt.Println(err)
36-
return
33+
fmt.Println(err)
34+
return
3735
}
3836

3937
for _, line := range lines {
40-
fmt.Fprintln(f, line)
41-
if err != nil {
42-
fmt.Println(err)
43-
return
44-
}
45-
}
38+
fmt.Fprintln(f, line)
39+
if err != nil {
40+
fmt.Println(err)
41+
return
42+
}
43+
}
44+
}
45+
46+
func AddNewCaseInput(input_lines []string,
47+
case_name string,
48+
meta_data config.EgorMeta) (config.EgorMeta, error) {
49+
50+
input_file_name := case_name + ".in"
51+
writeLinesToFile("inputs/"+input_file_name, input_lines)
52+
input_file := config.NewIoFile(input_file_name, "inputs/"+input_file_name, true)
53+
meta_data.Inputs = append(meta_data.Inputs, input_file)
54+
55+
return meta_data, nil
56+
}
57+
58+
func AddNewCaseOutput(output_lines []string,
59+
case_name string,
60+
meta_data config.EgorMeta) (config.EgorMeta, error) {
61+
62+
output_file_name := case_name + ".ans"
63+
writeLinesToFile("outputs/"+output_file_name, output_lines)
64+
output_file := config.NewIoFile(output_file_name, "outputs/"+output_file_name, true)
65+
meta_data.Outputs = append(meta_data.Outputs, output_file)
66+
67+
return meta_data, nil
4668
}
4769

4870
// TODO(Eroui): add checks on errors
4971
func CustomCaseAction(context *cli.Context) error {
5072
color.Green("Creating Custom Test Case...")
51-
52-
// Load meta data
73+
74+
// Load meta data
5375
cwd, err := os.Getwd()
5476
if err != nil {
5577
return err
@@ -60,41 +82,39 @@ func CustomCaseAction(context *cli.Context) error {
6082
return err
6183
}
6284

63-
color.Green("Provide your input:")
64-
input_lines, _ := readFromStdin()
65-
66-
color.Green("Provide your output:")
67-
output_lines, _ := readFromStdin()
68-
6985
case_name := "test-" + strconv.Itoa(len(meta_data.Inputs))
86+
color.Green("Provide your input:")
87+
input_lines := readFromStdin()
88+
meta_data, err = AddNewCaseInput(input_lines, case_name, meta_data)
7089

71-
input_file_name := case_name + ".in"
72-
output_file_name := case_name + ".out"
73-
74-
writeLinesToFile("inputs/" + input_file_name, input_lines)
75-
writeLinesToFile("outputs/" + output_file_name, output_lines)
76-
77-
input_file := config.NewIoFile(input_file_name, "inputs/" + input_file_name, true)
78-
output_file := config.NewIoFile(input_file_name, "outputs/" + output_file_name, true)
79-
80-
meta_data.Inputs = append(meta_data.Inputs, input_file)
81-
meta_data.Outputs = append(meta_data.Outputs, output_file)
90+
if !context.Bool("no-output") {
91+
color.Green("Provide your output:")
92+
output_lines := readFromStdin()
93+
meta_data, err = AddNewCaseOutput(output_lines, case_name, meta_data)
94+
}
8295

8396
meta_data.SaveToFile(path.Join(cwd, "egor-meta.json"))
84-
97+
8598
if err != nil {
86-
fmt.Println(err)
99+
color.Red("Failed to Generate Custom Case")
100+
return err
87101
}
88-
102+
89103
color.Green("Created Custom Test Case...")
90104
return nil
91105
}
92106

93107
var CaseCommand = cli.Command{
94-
Name: "case",
95-
Aliases: []string{"c"},
96-
Usage: "Create a custom test case.",
97-
UsageText: "Add custom test cases to egor task.",
98-
Action: CustomCaseAction,
99-
// TODO(Eroui): add necessary flags
108+
Name: "case",
109+
Aliases: []string{"c"},
110+
Usage: "Create a custom test case.",
111+
UsageText: "Add custom test cases to egor task.",
112+
Action: CustomCaseAction,
113+
Flags: []cli.Flag{
114+
&cli.BoolFlag{
115+
Name: "no-output",
116+
Usage: "This test case doesnt have output",
117+
Value: false,
118+
},
119+
},
100120
}

commands/case_test.go

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package commands
2+
3+
import (
4+
"github.com/chermehdi/egor/config"
5+
"github.com/stretchr/testify/assert"
6+
"os"
7+
"path"
8+
"testing"
9+
10+
)
11+
12+
func DeleteDir(dirPath string) {
13+
_ = os.RemoveAll(dirPath)
14+
}
15+
16+
func createDummyMetaData() (config.EgorMeta) {
17+
meta_data := config.EgorMeta {
18+
TaskName: "Dummy Task",
19+
TaskLang: "cpp",
20+
Inputs: []config.IoFile {
21+
config.IoFile {
22+
Name: "test-0",
23+
Path: "inputs/test-0.in",
24+
Custom: false,
25+
},
26+
config.IoFile {
27+
Name: "test-1",
28+
Path: "inputs/test-1.in",
29+
Custom: true,
30+
},
31+
},
32+
Outputs: []config.IoFile {
33+
config.IoFile {
34+
Name: "test-0",
35+
Path: "outputs/test-0.ans",
36+
Custom: false,
37+
},
38+
},
39+
}
40+
41+
return meta_data
42+
}
43+
44+
45+
func TestAddNewCaseInput(t *testing.T) {
46+
meta_data := createDummyMetaData()
47+
48+
// create temp inputs directory
49+
_ = os.Mkdir("inputs", 0777)
50+
51+
input_lines := [2]string{"Hello", "World"}
52+
case_name := "test-2"
53+
meta_data = AddNewCaseInput(input_lines, case_name, meta_data)
54+
55+
assert.Equal(t, len(meta_data.Inputs), 3)
56+
assert.Equal(t, meta_data.Inputs[2].Name, case_name + ".in")
57+
assert.Equal(t, meta_data.Inputs[2].Custom, true)
58+
59+
DeleteDir("inputs")
60+
}
61+
62+
63+
func TestAddNewCaseInput(t *testing.T) {
64+
meta_data := createDummyMetaData()
65+
66+
// create temp outputs directory
67+
_ = os.Mkdir("outputs", 0777)
68+
69+
input_lines := [2]string{"Hello", "World"}
70+
case_name := "test-2"
71+
meta_data = AddNewCaseOutput(input_lines, case_name, meta_data)
72+
73+
assert.Equal(t, len(meta_data.Inputs), 2)
74+
assert.Equal(t, meta_data.Outputs[1].Name, case_name + ".ans")
75+
assert.Equal(t, meta_data.Outputs[1].Custom, true)
76+
77+
DeleteDir("outputs")
78+
}

config/meta.go

+11-11
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,20 @@ import (
66
"errors"
77
"fmt"
88
"io"
9-
"path"
109
"os"
10+
"path"
1111
)
1212

1313
type IoFile struct {
14-
Name string
15-
Path string
14+
Name string
15+
Path string
1616
Custom bool
1717
}
1818

1919
func NewIoFile(fileName, filePath string, custom_case bool) IoFile {
2020
return IoFile{
21-
Name: fileName,
22-
Path: filePath,
21+
Name: fileName,
22+
Path: filePath,
2323
Custom: custom_case,
2424
}
2525
}
@@ -56,7 +56,7 @@ func NewEgorMeta(task Task, config Config) EgorMeta {
5656
outputs := make([]IoFile, testCount)
5757
for i := 0; i < testCount; i++ {
5858
fileName := fmt.Sprintf("test-%d", i)
59-
inputs[i] = NewIoFile(fileName, path.Join("inputs", fileName+".in",), false)
59+
inputs[i] = NewIoFile(fileName, path.Join("inputs", fileName+".in"), false)
6060
outputs[i] = NewIoFile(fileName, path.Join("outputs", fileName+".ans"), false)
6161
}
6262
taskFile, err := GetTaskName(config)
@@ -102,10 +102,10 @@ func (egor *EgorMeta) Load(r io.Reader) error {
102102
}
103103

104104
func LoadMeta(r io.Reader) (EgorMeta, error) {
105-
var egor_meta EgorMeta
106-
decoder := json2.NewDecoder(r)
107-
err := decoder.Decode(&egor_meta)
108-
return egor_meta, err
105+
var egor_meta EgorMeta
106+
decoder := json2.NewDecoder(r)
107+
err := decoder.Decode(&egor_meta)
108+
return egor_meta, err
109109
}
110110

111111
func LoadMetaFromPath(file_path string) (EgorMeta, error) {
@@ -122,4 +122,4 @@ func CreateFile(filePath string) (*os.File, error) {
122122
func OpenFileFromPath(filePath string) (*os.File, error) {
123123
file, err := os.Open(filePath)
124124
return file, err
125-
}
125+
}

0 commit comments

Comments
 (0)