github.com/rendon/cf@v0.0.0-20220806233241-5cb5d3939c03/cf_test.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  	"strings"
     8  	"testing"
     9  
    10  	"github.com/rendon/asserting"
    11  	"github.com/rendon/testcli"
    12  )
    13  
    14  type TestSuite struct {
    15  	*asserting.TestCase
    16  }
    17  
    18  func TestAll(t *testing.T) {
    19  	ts := &TestSuite{asserting.NewTestCase(t)}
    20  	asserting.Run(ts)
    21  }
    22  
    23  func (t *TestSuite) BeforeAll() {
    24  	testcli.Run("make", "clean")
    25  	t.Assert(testcli.Success())
    26  
    27  	testcli.Run("make")
    28  	t.Assert(testcli.Success())
    29  
    30  	t.AssertNil(os.RemoveAll("arena/"))
    31  	t.AssertNil(os.Mkdir("arena/", 0775))
    32  }
    33  
    34  func (t *TestSuite) TestGenAndTest() {
    35  	supportedLangs := map[string]string{
    36  		"c":      "c",
    37  		"cpp":    "cpp",
    38  		"golang": "go",
    39  		"ruby":   "rb",
    40  	}
    41  
    42  	root := os.Getenv("PWD")
    43  	cf := fmt.Sprintf("%s%cbin%ccf", root, os.PathSeparator, os.PathSeparator)
    44  	for lang, ext := range supportedLangs {
    45  		dir := fmt.Sprintf("arena/%s", lang)
    46  		sourceFile := fmt.Sprintf("%s/%s.%s", dir, lang, ext)
    47  		fmt.Printf("sourceFile: %s\n", sourceFile)
    48  		testcli.Run(cf, "gen", sourceFile)
    49  		if !testcli.Success() {
    50  			fmt.Printf(testcli.Stderr())
    51  			t.Fail("Failed to run `cf gen` for lang " + lang)
    52  		}
    53  
    54  		body, err := ioutil.ReadFile(sourceFile)
    55  		t.AssertNil(err)
    56  		t.Assert(len(body) > 0)
    57  
    58  		t.AssertNil(os.Chdir(dir))
    59  		body, err = ioutil.ReadFile(".settings.yml")
    60  		t.AssertNil(err)
    61  		t.Assert(len(body) > 0)
    62  
    63  		testcli.Run(cf, "config", "tests", "1")
    64  		t.Assert(testcli.Success())
    65  		testcli.Run("touch", ".in1.txt", ".out1.txt")
    66  		t.Assert(testcli.Success())
    67  
    68  		testcli.Run(cf, "-v", "test")
    69  		if !testcli.Success() {
    70  			fmt.Printf("Error: " + testcli.Stderr())
    71  			t.Fail("Failed to run `cf test` for lang " + lang)
    72  		}
    73  		t.AssertNil(os.Chdir(root))
    74  	}
    75  }
    76  
    77  func (t *TestSuite) TestSetup() {
    78  	root := os.Getenv("PWD")
    79  	dir := fmt.Sprintf("%s%carena", root, os.PathSeparator)
    80  	t.AssertNil(os.Chdir(dir))
    81  
    82  	cf := fmt.Sprintf("%s%cbin%ccf", root, os.PathSeparator, os.PathSeparator)
    83  	testcli.Run(cf, "setup", "399")
    84  	if !testcli.Success() {
    85  		fmt.Fprintf(os.Stderr, testcli.Stderr())
    86  		t.Fail("Failed to run `cf setup`")
    87  	}
    88  	text := "\nContest directory: CodeforcesRound233Div.2/\n"
    89  	t.AssertContainsStr(testcli.Stdout(), text)
    90  	t.AssertEqualInt(1, strings.Count(testcli.Stdout(), text))
    91  }