Skip to content

Commit bde0eb8

Browse files
authored
Merge pull request #10 from chermehdi/feature/show-cases
Feature/show cases
2 parents 5f5d2be + e093153 commit bde0eb8

File tree

5 files changed

+180
-4
lines changed

5 files changed

+180
-4
lines changed

commands/case.go

+9-4
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func writeLinesToFile(filename string, lines []string) error {
4444
return nil
4545
}
4646

47-
// Create and save user specified custom case input, and update the given egor meta data
47+
// Create and save user specified custom case input, and update the given egor meta data
4848
func AddNewCaseInput(inputLines []string,
4949
caseName string,
5050
metaData config.EgorMeta) (config.EgorMeta, error) {
@@ -60,7 +60,7 @@ func AddNewCaseInput(inputLines []string,
6060
return metaData, nil
6161
}
6262

63-
// Create and save user specified custom csae output, and update the given egor meta data
63+
// Create and save user specified custom csae output, and update the given egor meta data
6464
func AddNewCaseOutput(outputLines []string,
6565
caseName string,
6666
metaData config.EgorMeta) (config.EgorMeta, error) {
@@ -87,14 +87,13 @@ func CustomCaseAction(context *cli.Context) error {
8787
return err
8888
}
8989

90-
9190
configuration, err := config.LoadDefaultConfiguration()
9291
if err != nil {
9392
color.Red("Failed to load egor configuration")
9493
return err
9594
}
9695

97-
configFileName := configuration.ConfigFileName
96+
configFileName := configuration.ConfigFileName
9897
metaData, err := config.LoadMetaFromPath(path.Join(cwd, configFileName))
9998
if err != nil {
10099
color.Red("Failed to load egor MetaData ")
@@ -133,6 +132,12 @@ func CustomCaseAction(context *cli.Context) error {
133132
return nil
134133
}
135134

135+
// Command to add costum test cases to the current task.
136+
// Running this command will ask the user to provide their input and output, then
137+
// saves the new test case data into appropriate files and add their meta data into
138+
// the current task egor meta data.
139+
// The user can add a flag --no-output to specify that this test case have no output
140+
// associated with it. The user will not be asked to provide output in this case.
136141
var CaseCommand = cli.Command{
137142
Name: "case",
138143
Aliases: []string{"tc", "testcase"},

commands/showcases.go

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package commands
2+
3+
import (
4+
"github.com/chermehdi/egor/config"
5+
"github.com/fatih/color"
6+
"github.com/jedib0t/go-pretty/table"
7+
"github.com/urfave/cli/v2"
8+
"os"
9+
"path"
10+
)
11+
12+
type TestCaseIO struct {
13+
Id int
14+
Name string
15+
InputPath string
16+
OutputPath string
17+
Custom bool
18+
}
19+
20+
// parse input and output from egor meta to TestCase
21+
func GetTestCases(egorMeta config.EgorMeta) []*TestCaseIO {
22+
testCasesMap := make(map[string]*TestCaseIO)
23+
testCases := make([]*TestCaseIO, 0)
24+
for _, input := range egorMeta.Inputs {
25+
testCase := &TestCaseIO {
26+
Id : input.GetId(),
27+
Name : input.Name,
28+
InputPath : input.Path,
29+
OutputPath: "",
30+
Custom: input.Custom,
31+
}
32+
testCasesMap[input.Name] = testCase
33+
testCases = append(testCases, testCase)
34+
}
35+
36+
for _, output := range egorMeta.Outputs {
37+
if testCase, ok := testCasesMap[output.Name]; ok {
38+
testCase.OutputPath = output.Path
39+
}
40+
}
41+
42+
return testCases
43+
}
44+
45+
// print test cases table
46+
func PrintTestCasesTable(testCases []*TestCaseIO) {
47+
t := table.NewWriter()
48+
t.SetOutputMirror(os.Stdout)
49+
t.AppendHeader(table.Row{"#", "Test Name", "Input Path", "Output Path", "Custon"})
50+
for _, testCase := range testCases {
51+
t.AppendRow([]interface{}{
52+
testCase.Id,
53+
testCase.Name,
54+
testCase.InputPath,
55+
testCase.OutputPath,
56+
testCase.Custom,
57+
})
58+
}
59+
t.SetStyle(table.StyleLight)
60+
t.Render()
61+
}
62+
63+
// print task test cases
64+
func PrintTestCases(egorMeta config.EgorMeta) {
65+
PrintTestCasesTable(GetTestCases(egorMeta))
66+
}
67+
68+
// list test cases information command action
69+
// TODO(Eroui): [Refactoring] Duplicate code while loading meta data, consider refactoring...
70+
func ShowCasesAction(context *cli.Context) error {
71+
cwd, err := os.Getwd()
72+
if err != nil {
73+
color.Red("Failed to list test cases!")
74+
return err
75+
}
76+
77+
configuration, err := config.LoadDefaultConfiguration()
78+
if err != nil {
79+
color.Red("Failed to load egor configuration")
80+
return err
81+
}
82+
83+
configFileName := configuration.ConfigFileName
84+
metaData, err := config.LoadMetaFromPath(path.Join(cwd, configFileName))
85+
if err != nil {
86+
color.Red("Failed to load egor MetaData ")
87+
return err
88+
}
89+
90+
color.Green("Listing %d testcase(s)...", metaData.CountTestCases())
91+
color.Green("")
92+
93+
PrintTestCases(metaData)
94+
95+
return nil
96+
}
97+
98+
// Command to print the list of test cases input and outputs into the consol.
99+
// Running this command will fetch egor meta data and load all inputs and outputs meta data
100+
// and prints it as an array into the consol.
101+
var ShowCasesCommand = cli.Command{
102+
Name: "showcases",
103+
Aliases: []string{"sc"},
104+
Usage: "list information about test cases",
105+
UsageText: "list meta data about of the tests cases in the current task",
106+
Action: ShowCasesAction,
107+
}

commands/showcases_test.go

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package commands
2+
3+
import (
4+
"github.com/chermehdi/egor/config"
5+
"github.com/stretchr/testify/assert"
6+
"testing"
7+
)
8+
9+
func createSimpleDummyMetaData() config.EgorMeta{
10+
meteData := config.EgorMeta {
11+
TaskName: "Dummy Task",
12+
TaskLang: "cpp",
13+
Inputs: []config.IoFile {
14+
config.IoFile {
15+
Name: "test-0",
16+
Path: "inputs/test-0.in",
17+
Custom: true,
18+
},
19+
},
20+
Outputs: []config.IoFile {
21+
config.IoFile {
22+
Name: "test-0",
23+
Path: "outputs/test-0.ans",
24+
Custom: true,
25+
},
26+
},
27+
}
28+
29+
return meteData
30+
}
31+
32+
func TestGetTestCases(t *testing.T) {
33+
metaData := createDummyMetaData()
34+
testCases := GetTestCases(metaData)
35+
inputs := metaData.Inputs
36+
outputs := metaData.Outputs
37+
38+
assert.Equal(t, len(testCases), len(metaData.Inputs))
39+
assert.Equal(t, testCases[0].Id, inputs[0].GetId())
40+
assert.Equal(t, testCases[0].Name, inputs[0].Name)
41+
assert.Equal(t, testCases[0].Name, outputs[0].Name)
42+
assert.Equal(t, testCases[0].InputPath, inputs[0].Path)
43+
assert.Equal(t, testCases[0].OutputPath, outputs[0].Path)
44+
assert.Equal(t, testCases[0].Custom, inputs[0].Custom)
45+
}

config/meta.go

+18
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"io"
99
"os"
1010
"path"
11+
"strings"
12+
"strconv"
1113
)
1214

1315
type IoFile struct {
@@ -24,6 +26,15 @@ func NewIoFile(fileName, filePath string, customCase bool) IoFile {
2426
}
2527
}
2628

29+
func (ioFile *IoFile) GetId() int {
30+
tokens := strings.Split(ioFile.Name, "-")
31+
id, err := strconv.Atoi(tokens[1])
32+
if err != nil {
33+
return 0
34+
}
35+
return id
36+
}
37+
2738
// Type mapping to the `egor-meta.json` file.
2839
// The egor meta configuration is the source of truth for the task runner
2940
// so an update to it (either from the outside, or by invoking egor commands) can change
@@ -71,6 +82,13 @@ func NewEgorMeta(task Task, config Config) EgorMeta {
7182
TaskFile: taskFile,
7283
}
7384
}
85+
86+
// count the number of tests cases in the metadata. The number of tests is
87+
// the number of inputs
88+
func (egor *EgorMeta) CountTestCases() int {
89+
return len(egor.Inputs)
90+
}
91+
7492
func (egor *EgorMeta) toJson() (string, error) {
7593
var buffer bytes.Buffer
7694
encoder := json2.NewEncoder(&buffer)

main.go

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ func main() {
4444
&commands.ParseCommand,
4545
&commands.ConfigCommand,
4646
&commands.CaseCommand,
47+
&commands.ShowCasesCommand,
4748
},
4849
}
4950

0 commit comments

Comments
 (0)