Skip to content

Commit

Permalink
Add tests and Github action
Browse files Browse the repository at this point in the history
  • Loading branch information
kakilangit committed May 4, 2024
1 parent ea79686 commit 57664e4
Show file tree
Hide file tree
Showing 9 changed files with 184 additions and 32 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# This workflow will build a golang project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go

name: Go

on:
push:
branches: ["main"]
pull_request:
branches: ["main"]

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: "1.22"

- name: Build
run: go build -v ./...

- name: Test
run: go test -v -cover ./...
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@

# Dependency directories (remove the comment below to include it)
# vendor/

cover.html
10 changes: 0 additions & 10 deletions .travis.yml

This file was deleted.

11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Excelsior
[<img alt="github" src="https://img.shields.io/badge/github-kakilangit/excelsior-37a8e0?style=for-the-badge&labelColor=555555&logo=github" height="20">](https://github.com/kakilangit/excelsior)
[<img alt="pkg.go.dev" src="https://img.shields.io/badge/go-%2300ADD8.svg?style=for-the-badge&logo=go&logoColor=white" height="20">](https://pkg.go.dev/github.com/kakilangit/excelsior)

[![PkgGoDev](https://pkg.go.dev/badge/github.com/kakilangit/excelsior)](https://pkg.go.dev/github.com/kakilangit/excelsior)
[![Build Status](https://app.travis-ci.com/kakilangit/excelsior.svg?branch=main)](https://app.travis-ci.com/github/kakilangit/excelsior)
# Excelsior

An excelize wrapper to separate the presentation and business logic.

Expand Down Expand Up @@ -73,3 +73,8 @@ func main() {
}

```

## License

MIT
Copyright (c) 2022 kakilangit
4 changes: 2 additions & 2 deletions serialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ func Serialize(f SerializeFn) ([]byte, error) {
return f(file, style)
}

// DefaultStyleSetter is helper for the default excelsior row style.
func DefaultStyleSetter(_ int) int {
// DefaultGetStyleFn is helper for the default excelsior row style.
func DefaultGetStyleFn(int) int {
return DefaultStyleID
}

Expand Down
65 changes: 65 additions & 0 deletions serialize_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package excelsior_test

import (
"net/http"
"testing"

"github.com/kakilangit/excelsior"
"github.com/xuri/excelize/v2"
)

type nopResponseWriter struct{}

func (nopResponseWriter) Header() http.Header {
return http.Header{}
}

func (nopResponseWriter) Write([]byte) (int, error) {
return 0, nil
}

func (nopResponseWriter) WriteHeader(int) {}

func TestSerialize(t *testing.T) {
data, err := excelsior.Serialize(func(file *excelize.File, style excelsior.Style) ([]byte, error) {
const sheetName = "not found alphabet"

excelsior.SetDefaultSheetName(file, sheetName)

headers := []any{"alphabet"}
data := nopSheetData{row: excelsior.Row{"a"}}

sheet := excelsior.NewSheet(headers, excelsior.DefaultGetStyleFn, style.Header(), data)
if err := sheet.Generate(file, sheetName); err != nil {
return nil, err
}

return excelsior.Byte(file)
})

if err != nil {
t.Errorf("failed to serialize: %v", err)
}

if len(data) == 0 {
t.Errorf("empty data")
}
}

func TestSetHeader(t *testing.T) {
header := http.Header{}
excelsior.SetHeader(header, "test")

if header.Get("Content-Disposition") != "attachment; filename=test.xlsx" {
t.Errorf("failed to set header")
}
}

func TestWriteByt(t *testing.T) {
data := []byte("test")
rw := nopResponseWriter{}
err := excelsior.WriteByte(data, "test", rw)
if err != nil {
t.Errorf("failed to write byte: %v", err)
}
}
30 changes: 15 additions & 15 deletions sheet.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ type SheetProvider interface {
// Row represents Excel row.
type Row []any

// StyleSetter is signature function for set the row by providing column number.
type StyleSetter func(colID int) int
// GetStyleByColIDFn is signature function for get the style by providing column number.
type GetStyleByColIDFn func(colID int) int

// SheetBase is the interface generated by excelsior.Sheet, reducing the boiler-plates.
type SheetBase interface {
TotalColumn() int
HeaderRow() Row
HeaderRowStyle() StyleSetter
RowStyle() StyleSetter
HeaderRowStyle() GetStyleByColIDFn
RowStyle() GetStyleByColIDFn
}

// SheetData is the interface need to be provided by the user of excelsior.
Expand All @@ -31,18 +31,18 @@ type SheetData interface {
// Sheet implements SheetProvider interface.
type Sheet struct {
SheetData
headerRow Row
rowSetter StyleSetter
headStyleID int
headerRow Row
rowGetStyleFn GetStyleByColIDFn
headStyleID int
}

// NewSheet will create new Sheet.
func NewSheet(headerRow Row, rowSetter StyleSetter, headStyleID int, data SheetData) *Sheet {
func NewSheet(headerRow Row, rowGetStyleFn GetStyleByColIDFn, headStyleID int, data SheetData) *Sheet {
return &Sheet{
SheetData: data,
headerRow: headerRow,
rowSetter: rowSetter,
headStyleID: headStyleID,
SheetData: data,
headerRow: headerRow,
rowGetStyleFn: rowGetStyleFn,
headStyleID: headStyleID,
}
}

Expand All @@ -57,15 +57,15 @@ func (s Sheet) TotalColumn() int {
}

// HeaderRowStyle returns the style rowSetter for the header row.
func (s Sheet) HeaderRowStyle() StyleSetter {
func (s Sheet) HeaderRowStyle() GetStyleByColIDFn {
return func(_ int) int {
return s.headStyleID
}
}

// RowStyle returns the style rowSetter for the remaining row.
func (s Sheet) RowStyle() StyleSetter {
return s.rowSetter
func (s Sheet) RowStyle() GetStyleByColIDFn {
return s.rowGetStyleFn
}

// Generate creates Excel sheet with provided excelize.File, and sheet name.
Expand Down
63 changes: 63 additions & 0 deletions sheet_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package excelsior_test

import (
"testing"

"github.com/kakilangit/excelsior"
)

type nopSheetData struct {
row excelsior.Row
}

func (nopSheetData) Total() int {
return 1
}

func (d nopSheetData) Row(int) excelsior.Row {
return d.row
}

func TestSheet(t *testing.T) {
header := excelsior.Row{"Name", "Age"}
getRowStyle := func(int) int { return 0 }
headStyleID := 1
data := nopSheetData{row: excelsior.Row{"John Doe", 30}}
s := excelsior.NewSheet(header, getRowStyle, headStyleID, data)

if s.TotalColumn() != 2 {
t.Errorf("TotalColumn() = %d; want 2", s.TotalColumn())
}

if s.HeaderRow()[0] != "Name" {
t.Errorf("HeaderRow()[0] = %s; want Name", s.HeaderRow()[0])
}

if s.HeaderRow()[1] != "Age" {
t.Errorf("HeaderRow()[1] = %s; want Age", s.HeaderRow()[1])
}

if s.HeaderRowStyle()(0) != 1 {
t.Errorf("HeaderRowStyle()(0) = %d; want 1", s.HeaderRowStyle()(0))
}

if s.HeaderRowStyle()(1) != 1 {
t.Errorf("HeaderRowStyle()(1) = %d; want 1", s.HeaderRowStyle()(1))
}

if s.RowStyle()(0) != 0 {
t.Errorf("RowStyle()(0) = %d; want 0", s.RowStyle()(0))
}

if s.RowStyle()(1) != 0 {
t.Errorf("RowStyle()(1) = %d; want 0", s.RowStyle()(1))
}

if s.Row(0)[0] != "John Doe" {
t.Errorf("Row(0)[0] = %s; want John Doe", s.Row(0)[0])
}

if s.Row(0)[1] != 30 {
t.Errorf("Row(0)[1] = %d; want 30", s.Row(0)[1])
}
}
4 changes: 2 additions & 2 deletions stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (
)

// SetRow sets a single row.
func SetRow(writer *excelize.StreamWriter, colID, rowID int, data []any, styleSetter StyleSetter) error {
func SetRow(writer *excelize.StreamWriter, colID, rowID int, data []any, getStyle GetStyleByColIDFn) error {
row := make([]any, len(data))
for i, value := range data {
row[i] = excelize.Cell{Value: value, StyleID: styleSetter(i)}
row[i] = excelize.Cell{Value: value, StyleID: getStyle(i)}
}

cell, err := excelize.CoordinatesToCellName(colID, rowID)
Expand Down

0 comments on commit 57664e4

Please sign in to comment.