Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit test #4

Merged
merged 1 commit into from
Dec 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
[![All Contributors](https://img.shields.io/badge/all_contributors-1-orange.svg?style=flat-square)](#contributors-)
<!-- ALL-CONTRIBUTORS-BADGE:END -->

The vogen is a tool to generate value objects in golang. The vogen works both as a library and as a CLI. Both will automatically generate files with Value Objects defined.
[![Go Reference](https://pkg.go.dev/badge/github.com/nao1215/vogen.svg)](https://pkg.go.dev/github.com/nao1215/vogen)
![Coverage](https://raw.githubusercontent.com/nao1215/octocovs-central-repo/main/badges/nao1215/vogen/coverage.svg)
[![reviewdog](https://github.com/nao1215/vogen/actions/workflows/reviewdog.yml/badge.svg)](https://github.com/nao1215/vogen/actions/workflows/reviewdog.yml)
[![MultiPlatformUnitTest](https://github.com/nao1215/vogen/actions/workflows/unit_test.yml/badge.svg)](https://github.com/nao1215/vogen/actions/workflows/unit_test.yml)

The vogen library is to generate value objects in golang. The vogen will automatically generate files with Value Objects defined.

In golang, the only way to implement Value Objects is to use structs. Implementing Getter and Equal() on a newly defined Value Object (structure) is a simple and tedious task. The vogen package simplifies that task.

Expand Down
2 changes: 1 addition & 1 deletion vogen.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (vo *Vogen) validate(vos ...ValueObject) error {
return ErrStructNameEmpty
}
if len(v.Fields) == 0 {
return ErrInvalidFieldName
return ErrInvalidField
}
for _, f := range v.Fields {
if f.Name == "" {
Expand Down
96 changes: 96 additions & 0 deletions vogen_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// Package vogen provides a code generator for Value Objects in Go.
// Value Objects are immutable objects that represent a value.
package vogen

import (
"errors"
"testing"
)

func TestNew(t *testing.T) {
t.Parallel()

t.Run("error occurs when the file path is not set", func(t *testing.T) {
t.Parallel()

_, err := New(WithFilePath(""))
if !errors.Is(err, ErrInvalidFilePath) {
t.Errorf("want: %v, got: %v", ErrInvalidFilePath, err)
}
})

t.Run("error occurs when the package name is not set", func(t *testing.T) {
t.Parallel()

_, err := New(WithPackageName(""))
if !errors.Is(err, ErrInvalidPackageName) {
t.Errorf("want: %v, got: %v", ErrInvalidPackageName, err)
}
})
}

func TestVogen_AppendValueObjects(t *testing.T) {
t.Parallel()

t.Run("error occurs when the StructName is not set", func(t *testing.T) {
t.Parallel()

gen, _ := New()
err := gen.AppendValueObjects(ValueObject{
StructName: "",
Fields: []Field{
{Name: "Name", Type: "string"},
},
Comments: []string{"test comment"},
})
if !errors.Is(err, ErrStructNameEmpty) {
t.Errorf("want: %v, got: %v", ErrStructNameEmpty, err)
}
})

t.Run("error occurs when the Field is not set", func(t *testing.T) {
t.Parallel()

gen, _ := New()
err := gen.AppendValueObjects(ValueObject{
StructName: "Person",
Fields: nil,
Comments: []string{"test comment"},
})
if !errors.Is(err, ErrInvalidField) {
t.Errorf("want: %v, got: %v", ErrInvalidField, err)
}
})

t.Run("error occurs when the Field 'Name' is not set", func(t *testing.T) {
t.Parallel()

gen, _ := New()
err := gen.AppendValueObjects(ValueObject{
StructName: "Person",
Fields: []Field{
{Name: "", Type: "string"},
},
Comments: []string{"test comment"},
})
if !errors.Is(err, ErrInvalidFieldName) {
t.Errorf("want: %v, got: %v", ErrInvalidFieldName, err)
}
})

t.Run("error occurs when the Field 'Type' is not set", func(t *testing.T) {
t.Parallel()

gen, _ := New()
err := gen.AppendValueObjects(ValueObject{
StructName: "Person",
Fields: []Field{
{Name: "Name", Type: ""},
},
Comments: []string{"test comment"},
})
if !errors.Is(err, ErrInvalidFieldType) {
t.Errorf("want: %v, got: %v", ErrInvalidFieldType, err)
}
})
}
Loading