Commit 7c7bdc8e authored by Boris Mühmer's avatar Boris Mühmer
Browse files

struct and interfaces test(s)

parent 5d108492
Loading
Loading
Loading
Loading

sai/sai.go

0 → 100644
+91 −0
Original line number Diff line number Diff line
package sai

import "fmt"

/*
 * Structs and Interfaces
 *
 * Various tests with struct and interfaces.
 */

type Baser interface {
	V() bool
	I() bool
	Data() int
}

type Base struct {
	Visible   bool
	invisible bool
}

func NewBase(v, i bool) Base {
	return Base{
		Visible:   v,
		invisible: i,
	}
}

func (s Base) V() bool {
	return s.Visible
}

func (s Base) I() bool {
	return s.invisible
}

func (s Base) Data() int {
	panic("no data here")
}

func (s Base) String() string {
	return fmt.Sprintf("Base/s:%t/i:%t/none", s.Visible, s.invisible)
}

type Implicit struct {
	Base
	Value int
}

func NewImplicit(v, i bool, d int) Implicit {
	return Implicit{
		NewBase(v, i),
		d,
	}
}

func (s Implicit) String() string {
	return fmt.Sprintf("Implicit/s:%t/i:%t/d:%d", s.Visible, s.invisible, s.Value)
}

func (s Implicit) Data() int {
	return s.Value
}

type Explicit struct {
	Root  Base
	Value int
}

func NewExplicit(v, i bool, d int) Explicit {
	return Explicit{
		Root:  NewBase(v, i),
		Value: d,
	}
}

func (s Explicit) String() string {
	return fmt.Sprintf("Explicit/s:%t/i:%t/d:%d", s.Root.Visible, s.Root.invisible, s.Value)
}

func (s Explicit) V() bool {
	return s.Root.Visible
}

func (s Explicit) I() bool {
	return s.Root.invisible
}

func (s Explicit) Data() int {
	return s.Value
}

sai/sai_test.go

0 → 100644
+13 −0
Original line number Diff line number Diff line
package sai

import "testing"

func TestStructs(t *testing.T) {
	for _, v := range []Baser{
		NewBase(true, true),
		NewImplicit(true, true, 11),
		NewExplicit(true, true, 12),
	} {
		t.Logf("- %s", v)
	}
}