Commit 11ecc9df authored by Boris Mühmer's avatar Boris Mühmer
Browse files

snapshot of current state: non-working

parent 29a9e4d0
Loading
Loading
Loading
Loading
+34 −0
Original line number Diff line number Diff line
package library

import "fmt"

// Version is used for version information.
type Version struct {
	Major    int
	Minor    int
	Revision int
}

// Versioner is an interface to obtain the version information of a component.
type Versioner interface {
	Version(r *Ruler) Version
}

// String returns the version as string.
func (v Version) String() string {
	return fmt.Sprintf("%d.%d.%d", v.Major, v.Minor, v.Revision)
}

// Avatar represents a blob.
type Avatar struct {
}

// Item represents something static.
type Item struct {
}

// Ruler governs the game mechanics.
type Ruler interface {
	Name() string
	Version() Version
}
+75 −0
Original line number Diff line number Diff line
package library

import (
	"errors"
	"strings"
)

// Name hold name information.
type Name struct {
	Title  string
	First  string
	Middle string
	Last   string
}

var (
	// ErrorNameIsEmpty indicates all fields are empty strings.
	ErrorNameIsEmpty = errors.New("name is empty")
	// ErrorNoFirstOrLastName indicates no first and last name.
	ErrorNoFirstOrLastName = errors.New("no name")
	// ErrorMiddleNameWithoutFirstOrLastName indicates a middle name without a first or last name.
	ErrorMiddleNameWithoutFirstOrLastName = errors.New("middle name without first or last name")
)

// IsValid returns nil if it is valid, or the error with problem.
func (n *Name) IsValid() (bool, error) {
	if (n.Middle != "") && ((n.First == "") || (n.Last == "")) {
		return false, ErrorMiddleNameWithoutFirstOrLastName
	}

	if (n.Title == "") && (n.First == "") && (n.Last == "") {
		return false, ErrorNameIsEmpty
	}

	if (n.First == "") && (n.Last == "") {
		return false, ErrorNoFirstOrLastName
	}
	return true, nil
}

// String returns a _formatted_ string of the name fields.
func (n *Name) String() (string, error) {
	var sb strings.Builder

	if _, err := n.IsValid(); err != nil {
		return "", err
	}

	if n.Title != "" {
		sb.WriteString(n.Title)
	}

	if n.First != "" {
		if len(sb.String()) > 0 {
			sb.WriteString(" ")
		}
		sb.WriteString(n.First)
	}

	if n.Middle != "" {
		if len(sb.String()) > 0 {
			sb.WriteString(" ")
		}
		sb.WriteString(n.Middle)
	}

	if n.Last != "" {
		if len(sb.String()) > 0 {
			sb.WriteString(" ")
		}
		sb.WriteString(n.Last)
	}

	return sb.String(), nil
}
+39 −0
Original line number Diff line number Diff line
package library

import "testing"

func TestNames(t *testing.T) {
	for _, nt := range []struct {
		n Name
		s string
		e error
	}{
		{n: Name{}, s: "", e: ErrorNameIsEmpty},
		{n: Name{Title: "Title"}, s: "", e: ErrorNoFirstOrLastName},
		{n: Name{Last: "Last"}, s: "Last"},
		{n: Name{First: "First"}, s: "First"},
		{n: Name{First: "First", Last: "Last"}, s: "First Last"},
		{n: Name{Title: "Title", First: "First"}, s: "Title First"},
		{n: Name{Title: "Title", Last: "Last"}, s: "Title Last"},
		{n: Name{Title: "Title", First: "First", Last: "Last"}, s: "Title First Last"},
		{n: Name{Middle: "Middle"}, s: "", e: ErrorMiddleNameWithoutFirstOrLastName},
		{n: Name{First: "First", Middle: "Middle"}, s: "", e: ErrorMiddleNameWithoutFirstOrLastName},
		{n: Name{Middle: "Middle", Last: "Last"}, s: "", e: ErrorMiddleNameWithoutFirstOrLastName},
		{n: Name{Title: "Title", Middle: "Middle"}, s: "", e: ErrorMiddleNameWithoutFirstOrLastName},
		{n: Name{Title: "Title", First: "First", Middle: "Middle"}, s: "", e: ErrorMiddleNameWithoutFirstOrLastName},
		{n: Name{Title: "Title", Middle: "Middle", Last: "Last"}, s: "", e: ErrorMiddleNameWithoutFirstOrLastName},
		{n: Name{Title: "Title", First: "First", Middle: "Middle", Last: "Last"}, s: "Title First Middle Last"},
	} {
		s, f := nt.n.String()
		r := nt.s
		e := nt.e

		if f != e {
			t.Errorf("name string error is \"%s\", expected \"%s\"", f, e)
		}

		if s != r {
			t.Errorf("name string value is \"%s\", expected \"%s\"", s, r)
		}
	}
}
+30 −0
Original line number Diff line number Diff line
package v000000000

import "repositories.muehmer.net/bsmrgo/wizards/pkg/wizards/library"

const (
	ruleName = "Dummy Rule Set"
)

var (
	ruleVersion = library.Version{Major: 0, Minor: 0, Revision: 0}
)

// Rules holds data for this rule set.
type Rules struct {
}

// New creates a new instance of this rule set.
func New() (string, *Rules) {
	return ruleName, &Rules{}
}

// Name is the name of this ruleset.
func (r *Rules) Name() string {
	return ruleName
}

// Version is the version of this ruleset.
func (r *Rules) Version() library.Version {
	return ruleVersion
}
+1 −0
Original line number Diff line number Diff line
package server
Loading