-
-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
# Describe Request Report factory is added. # Change Type New feature.
- Loading branch information
Showing
5 changed files
with
122 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright (c) 2021-2024 Onur Cinar. | ||
// The source code is provided under GNU AGPLv3 License. | ||
// https://github.com/cinar/indicator | ||
|
||
package backtest | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
const ( | ||
// HTMLReportBuilderName is the name for the HTML report builder. | ||
HTMLReportBuilderName = "html" | ||
) | ||
|
||
// ReportBuilderFunc defines a function to build a new report using the given configuration parameter. | ||
type ReportBuilderFunc func(config string) (Report, error) | ||
|
||
// reportBuilders provides mapping for the report builders. | ||
var reportBuilders = map[string]ReportBuilderFunc{ | ||
HTMLReportBuilderName: htmlReportBuilder, | ||
} | ||
|
||
// RegisterReportBuilder registers the given builder. | ||
func RegisterReportBuilder(name string, builder ReportBuilderFunc) { | ||
reportBuilders[name] = builder | ||
} | ||
|
||
// NewReport builds a new report by the given name type and the configuration. | ||
func NewReport(name, config string) (Report, error) { | ||
builder, ok := reportBuilders[name] | ||
if !ok { | ||
return nil, fmt.Errorf("unknown report: %s", name) | ||
} | ||
|
||
return builder(config) | ||
} | ||
|
||
// htmlReportBuilder builds a new HTML report instance. | ||
func htmlReportBuilder(config string) (Report, error) { | ||
return NewHTMLReport(config), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Copyright (c) 2021-2024 Onur Cinar. | ||
// The source code is provided under GNU AGPLv3 License. | ||
// https://github.com/cinar/indicator | ||
|
||
package backtest_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/cinar/indicator/v2/backtest" | ||
) | ||
|
||
func TestNewReportUnknown(t *testing.T) { | ||
report, err := backtest.NewReport("unknown", "") | ||
if err == nil { | ||
t.Fatalf("unknown report: %T", report) | ||
} | ||
} | ||
|
||
func TestRegisterReportBuilder(t *testing.T) { | ||
builderName := "testbuilder" | ||
|
||
report, err := backtest.NewReport(builderName, "") | ||
if err == nil { | ||
t.Fatalf("testbuilder is: %T", report) | ||
} | ||
|
||
backtest.RegisterReportBuilder(builderName, func(config string) (backtest.Report, error) { | ||
return backtest.NewHTMLReport(config), nil | ||
}) | ||
|
||
report, err = backtest.NewReport(builderName, "") | ||
if err != nil { | ||
t.Fatalf("testbuilder is not found: %v", err) | ||
} | ||
|
||
_, ok := report.(*backtest.HTMLReport) | ||
if !ok { | ||
t.Fatalf("testbuilder is: %T", report) | ||
} | ||
} | ||
|
||
func TestNewReportMemory(t *testing.T) { | ||
report, err := backtest.NewReport(backtest.HTMLReportBuilderName, "") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
_, ok := report.(*backtest.HTMLReport) | ||
if !ok { | ||
t.Fatalf("report not correct type: %T", report) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters