Skip to content

Commit 8e8addf

Browse files
chermehdiMehdi Cheracher
authored and
Mehdi Cheracher
committed
add primary tests for parse task.
1 parent 9df851f commit 8e8addf

File tree

3 files changed

+58
-12
lines changed

3 files changed

+58
-12
lines changed

commands/parse.go

+9-10
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,12 @@ func CreateFile(filePath string) (*os.File, error) {
3434
return os.OpenFile(filePath, os.O_TRUNC|os.O_CREATE|os.O_WRONLY, 0777)
3535
}
3636

37-
func CreateDirectoryStructure(task Task, config Config) error {
38-
dir, err := os.Getwd()
39-
if err != nil {
40-
return err
41-
}
42-
taskDir := path.Join(dir, task.Name)
43-
if err = os.Mkdir(taskDir, 0777); err != nil {
37+
func CreateDirectoryStructure(task Task, config Config, rootDir string) error {
38+
taskDir := path.Join(rootDir, task.Name)
39+
if err := os.Mkdir(taskDir, 0777); err != nil {
4440
return err
4541
}
46-
if err = os.Chdir(taskDir); err != nil {
42+
if err := os.Chdir(taskDir); err != nil {
4743
return err
4844
}
4945
egorMeta := NewEgorMeta(task, config)
@@ -168,12 +164,15 @@ func ParseAction(context *cli.Context) error {
168164
if err != nil {
169165
return err
170166
}
171-
fmt.Printf("task %v\n", task)
172167
config, err := LoadDefaultConfiguration()
173168
if err != nil {
174169
return err
175170
}
176-
err = CreateDirectoryStructure(*task, *config)
171+
cwd, err := os.Getwd()
172+
if err != nil {
173+
return err
174+
}
175+
err = CreateDirectoryStructure(*task, *config, cwd)
177176
if err != nil {
178177
color.Red("Error happened %v", err)
179178
}

commands/parse_test.go

+45-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package commands
22

33
import (
4+
"github.com/chermehdi/egor/config"
45
"github.com/stretchr/testify/assert"
6+
"os"
7+
"path"
58
"testing"
69
)
710

@@ -51,6 +54,46 @@ func TestExtractTaskFromString(t *testing.T) {
5154
assert.Equal(t, task.Languages["java"].TaskClass, "CMinimizeTheInteger")
5255
}
5356

54-
func TestCurrentDirectory(t *testing.T) {
55-
//CreateDirectoryStructure(config.Task{});
57+
func DeleteDir(dirPath string) {
58+
_ = os.RemoveAll(dirPath)
59+
}
60+
61+
func TestCreateDirectoryStructure(t *testing.T) {
62+
task := config.Task{
63+
Name: "Dummy task",
64+
Group: "Codeforces",
65+
Url: "http://codeforces.com/test",
66+
Interactive: false,
67+
MemoryLimit: 250,
68+
TimeLimit: 100,
69+
Tests: []config.TestCase{
70+
{Input: "1", Output: "2"},
71+
},
72+
TestType: "",
73+
Input: config.IOType{"stdin"},
74+
Output: config.IOType{"stdout"},
75+
Languages: nil,
76+
}
77+
configuration := config.Config{
78+
Lang: struct {
79+
Default string `yaml:"default"`
80+
}{Default: "cpp"},
81+
ConfigFileName: "egor-meta.json",
82+
Version: "1.0",
83+
Author: "MaxHeap",
84+
}
85+
rootDir := os.TempDir()
86+
defer DeleteDir(rootDir)
87+
88+
err := CreateDirectoryStructure(task, configuration, rootDir)
89+
assert.NoError(t, err)
90+
91+
taskDir := path.Join(rootDir, task.Name)
92+
93+
assert.FileExists(t, path.Join(taskDir, configuration.ConfigFileName))
94+
assert.DirExists(t, path.Join(taskDir, "inputs"))
95+
assert.DirExists(t, path.Join(taskDir, "outputs"))
96+
assert.FileExists(t, path.Join(taskDir, "main.cpp"))
97+
assert.FileExists(t, path.Join(taskDir, "inputs", "test-0.in"))
98+
assert.FileExists(t, path.Join(taskDir, "outputs", "test-0.ans"))
5699
}

config/task.go

+4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
package config
22

3+
// Represents a Competitive programming task's input and output for a some test case.
34
type TestCase struct {
45
Input string
56
Output string
67
}
78

9+
// The type of the input, can be "stdin", "stdout" or maybe file-based
10+
// See Google Code Jam for file based IOTypes.
811
type IOType struct {
912
Type string
1013
}
1114

15+
// Specific to Java, Gives the name of the class and the computed task name.
1216
type LanguageDescription struct {
1317
MainClass string
1418
TaskClass string

0 commit comments

Comments
 (0)