github.com/ernestokarim/closurer@v0.0.0-20130119214741-f245d086c750/test/test.go (about)

     1  package test
     2  
     3  import (
     4  	"path/filepath"
     5  
     6  	"github.com/ernestokarim/closurer/app"
     7  	"github.com/ernestokarim/closurer/config"
     8  	"github.com/ernestokarim/closurer/scan"
     9  	"github.com/gorilla/mux"
    10  )
    11  
    12  type TestData struct {
    13  	Name string
    14  }
    15  
    16  func Main(r *app.Request) error {
    17  	name := mux.Vars(r.Req)["name"]
    18  	name = name[:len(name)-5] + ".js"
    19  
    20  	tdata := &TestData{
    21  		Name: name,
    22  	}
    23  	return r.ExecuteTemplate([]string{"test"}, tdata)
    24  }
    25  
    26  // ========================================================
    27  
    28  type TestListData struct {
    29  	AllTests []string
    30  }
    31  
    32  func TestAll(r *app.Request) error {
    33  	tests, err := scanTests()
    34  	if err != nil {
    35  		return err
    36  	}
    37  
    38  	tdata := &TestListData{
    39  		AllTests: tests,
    40  	}
    41  	return r.ExecuteTemplate([]string{"global-test"}, tdata)
    42  }
    43  
    44  // ========================================================
    45  
    46  func TestList(r *app.Request) error {
    47  	tests, err := scanTests()
    48  	if err != nil {
    49  		return err
    50  	}
    51  
    52  	tdata := &TestListData{
    53  		AllTests: tests,
    54  	}
    55  	return r.ExecuteTemplate([]string{"test-list"}, tdata)
    56  }
    57  
    58  // ========================================================
    59  
    60  // Search for "_test.js" files and relativize them to
    61  // the root directory. It replaces the .js ext with .html.
    62  func scanTests() ([]string, error) {
    63  	conf := config.Current()
    64  
    65  	tests, err := scan.Do(conf.Js.Root, "_test.js")
    66  	if err != nil {
    67  		return nil, err
    68  	}
    69  
    70  	for i, test := range tests {
    71  		// Relativize the path adding .html instead of .js
    72  		p, err := filepath.Rel(conf.Js.Root, test[:len(test)-2]+"html")
    73  		if err != nil {
    74  			return nil, app.Error(err)
    75  		}
    76  		tests[i] = p
    77  	}
    78  
    79  	return tests, nil
    80  }